github.com/anth0d/nomad@v0.0.0-20221214183521-ae3a0a2cad06/command/acl_token_self_test.go (about) 1 package command 2 3 import ( 4 "testing" 5 6 "github.com/hashicorp/nomad/acl" 7 "github.com/hashicorp/nomad/command/agent" 8 "github.com/hashicorp/nomad/nomad/mock" 9 "github.com/hashicorp/nomad/nomad/structs" 10 "github.com/mitchellh/cli" 11 "github.com/shoenig/test/must" 12 ) 13 14 func TestACLTokenSelfCommand_ViaEnvVar(t *testing.T) { 15 config := func(c *agent.Config) { 16 c.ACL.Enabled = true 17 } 18 19 srv, _, url := testServer(t, true, config) 20 defer stopTestAgent(srv) 21 22 state := srv.Agent.Server().State() 23 24 // Bootstrap an initial ACL token 25 token := srv.RootToken 26 must.NotNil(t, token) 27 28 ui := cli.NewMockUi() 29 cmd := &ACLTokenSelfCommand{Meta: Meta{Ui: ui, flagAddress: url}} 30 31 // Create a valid token 32 mockToken := mock.ACLToken() 33 mockToken.Policies = []string{acl.PolicyWrite} 34 mockToken.SetHash() 35 must.NoError(t, state.UpsertACLTokens(structs.MsgTypeTestSetup, 1000, []*structs.ACLToken{mockToken})) 36 37 // Attempt to fetch info on a token without providing a valid management 38 // token 39 invalidToken := mock.ACLToken() 40 t.Setenv("NOMAD_TOKEN", invalidToken.SecretID) 41 code := cmd.Run([]string{"-address=" + url}) 42 must.One(t, code) 43 44 // Fetch info on a token with a valid token 45 t.Setenv("NOMAD_TOKEN", mockToken.SecretID) 46 code = cmd.Run([]string{"-address=" + url}) 47 must.Zero(t, code) 48 49 // Check the output 50 out := ui.OutputWriter.String() 51 must.StrContains(t, out, mockToken.AccessorID) 52 }