1package command
2
3import (
4	"testing"
5
6	"github.com/hashicorp/nomad/command/agent"
7	"github.com/mitchellh/cli"
8	"github.com/stretchr/testify/assert"
9)
10
11func TestACLBootstrapCommand(t *testing.T) {
12	t.Parallel()
13	assert := assert.New(t)
14
15	// create a acl-enabled server without bootstrapping the token
16	config := func(c *agent.Config) {
17		c.ACL.Enabled = true
18		c.ACL.PolicyTTL = 0
19	}
20
21	srv, _, url := testServer(t, true, config)
22	defer srv.Shutdown()
23
24	assert.Nil(srv.RootToken)
25
26	ui := new(cli.MockUi)
27	cmd := &ACLBootstrapCommand{Meta: Meta{Ui: ui, flagAddress: url}}
28
29	code := cmd.Run([]string{"-address=" + url})
30	assert.Equal(0, code)
31
32	out := ui.OutputWriter.String()
33	assert.Contains(out, "Secret ID")
34}
35
36// If a bootstrap token has already been created, attempts to create more should
37// fail.
38func TestACLBootstrapCommand_ExistingBootstrapToken(t *testing.T) {
39	t.Parallel()
40	assert := assert.New(t)
41
42	config := func(c *agent.Config) {
43		c.ACL.Enabled = true
44	}
45
46	srv, _, url := testServer(t, true, config)
47	defer srv.Shutdown()
48
49	assert.NotNil(srv.RootToken)
50
51	ui := new(cli.MockUi)
52	cmd := &ACLBootstrapCommand{Meta: Meta{Ui: ui, flagAddress: url}}
53
54	code := cmd.Run([]string{"-address=" + url})
55	assert.Equal(1, code)
56
57	out := ui.OutputWriter.String()
58	assert.NotContains(out, "Secret ID")
59}
60
61// Attempting to bootstrap a token on a non-ACL enabled server should fail.
62func TestACLBootstrapCommand_NonACLServer(t *testing.T) {
63	t.Parallel()
64	assert := assert.New(t)
65
66	srv, _, url := testServer(t, true, nil)
67	defer srv.Shutdown()
68
69	ui := new(cli.MockUi)
70	cmd := &ACLBootstrapCommand{Meta: Meta{Ui: ui, flagAddress: url}}
71
72	code := cmd.Run([]string{"-address=" + url})
73	assert.Equal(1, code)
74
75	out := ui.OutputWriter.String()
76	assert.NotContains(out, "Secret ID")
77}
78