github.com/iqoqo/nomad@v0.11.3-0.20200911112621-d7021c74d101/command/acl_token_list_test.go (about) 1 package command 2 3 import ( 4 "strings" 5 "testing" 6 7 "github.com/hashicorp/nomad/acl" 8 "github.com/hashicorp/nomad/command/agent" 9 "github.com/hashicorp/nomad/nomad/mock" 10 "github.com/hashicorp/nomad/nomad/structs" 11 "github.com/mitchellh/cli" 12 "github.com/stretchr/testify/assert" 13 ) 14 15 func TestACLTokenListCommand(t *testing.T) { 16 assert := assert.New(t) 17 t.Parallel() 18 config := func(c *agent.Config) { 19 c.ACL.Enabled = true 20 } 21 22 srv, _, url := testServer(t, true, config) 23 state := srv.Agent.Server().State() 24 defer srv.Shutdown() 25 26 // Bootstrap an initial ACL token 27 token := srv.RootToken 28 assert.NotNil(token, "failed to bootstrap ACL token") 29 30 // Create a valid token 31 mockToken := mock.ACLToken() 32 mockToken.Policies = []string{acl.PolicyWrite} 33 mockToken.SetHash() 34 assert.Nil(state.UpsertACLTokens(1000, []*structs.ACLToken{mockToken})) 35 36 ui := new(cli.MockUi) 37 cmd := &ACLTokenListCommand{Meta: Meta{Ui: ui, flagAddress: url}} 38 39 // Attempt to list tokens without a valid management token 40 invalidToken := mock.ACLToken() 41 code := cmd.Run([]string{"-address=" + url, "-token=" + invalidToken.SecretID}) 42 assert.Equal(1, code) 43 44 // Apply a token with a valid management token 45 code = cmd.Run([]string{"-address=" + url, "-token=" + token.SecretID}) 46 assert.Equal(0, code) 47 48 // Check the output 49 out := ui.OutputWriter.String() 50 if !strings.Contains(out, mockToken.Name) { 51 t.Fatalf("bad: %v", out) 52 } 53 54 // List json 55 if code := cmd.Run([]string{"-address=" + url, "-token=" + token.SecretID, "-json"}); code != 0 { 56 t.Fatalf("expected exit 0, got: %d; %v", code, ui.ErrorWriter.String()) 57 } 58 out = ui.OutputWriter.String() 59 if !strings.Contains(out, "CreateIndex") { 60 t.Fatalf("expected json output, got: %s", out) 61 } 62 ui.OutputWriter.Reset() 63 }