1package command
2
3import (
4	"os"
5	"strings"
6	"testing"
7
8	"github.com/hashicorp/nomad/command/agent"
9	"github.com/mitchellh/cli"
10	"github.com/stretchr/testify/assert"
11)
12
13func TestACLTokenCreateCommand(t *testing.T) {
14	assert := assert.New(t)
15	t.Parallel()
16	config := func(c *agent.Config) {
17		c.ACL.Enabled = true
18	}
19
20	srv, _, url := testServer(t, true, config)
21	defer srv.Shutdown()
22
23	// Bootstrap an initial ACL token
24	token := srv.RootToken
25	assert.NotNil(token, "failed to bootstrap ACL token")
26
27	ui := new(cli.MockUi)
28	cmd := &ACLTokenCreateCommand{Meta: Meta{Ui: ui, flagAddress: url}}
29
30	// Request to create a new token without providing a valid management token
31	os.Setenv("NOMAD_TOKEN", "foo")
32	code := cmd.Run([]string{"-address=" + url, "-policy=foo", "-type=client"})
33	assert.Equal(1, code)
34
35	// Request to create a new token with a valid management token
36	os.Setenv("NOMAD_TOKEN", token.SecretID)
37	code = cmd.Run([]string{"-address=" + url, "-policy=foo", "-type=client"})
38	assert.Equal(0, code)
39
40	// Check the output
41	out := ui.OutputWriter.String()
42	if !strings.Contains(out, "[foo]") {
43		t.Fatalf("bad: %v", out)
44	}
45}
46