github.com/hernad/nomad@v1.6.112/command/acl_auth_method_list_test.go (about) 1 // Copyright (c) HashiCorp, Inc. 2 // SPDX-License-Identifier: MPL-2.0 3 4 package command 5 6 import ( 7 "testing" 8 9 "github.com/hernad/nomad/ci" 10 "github.com/hernad/nomad/command/agent" 11 "github.com/hernad/nomad/nomad/mock" 12 "github.com/hernad/nomad/nomad/structs" 13 "github.com/mitchellh/cli" 14 "github.com/shoenig/test/must" 15 ) 16 17 func TestACLAuthMethodListCommand(t *testing.T) { 18 ci.Parallel(t) 19 20 config := func(c *agent.Config) { 21 c.ACL.Enabled = true 22 } 23 24 srv, _, url := testServer(t, true, config) 25 state := srv.Agent.Server().State() 26 defer srv.Shutdown() 27 28 // Bootstrap an initial ACL token 29 token := srv.RootToken 30 must.NotNil(t, token) 31 32 // Create a test auth method 33 method := &structs.ACLAuthMethod{ 34 Name: "test-auth-method", 35 Config: &structs.ACLAuthMethodConfig{ 36 OIDCDiscoveryURL: "http://example.com", 37 }, 38 } 39 method.SetHash() 40 must.NoError(t, state.UpsertACLAuthMethods(1000, []*structs.ACLAuthMethod{method})) 41 42 ui := cli.NewMockUi() 43 cmd := &ACLAuthMethodListCommand{Meta: Meta{Ui: ui, flagAddress: url}} 44 45 // List with an invalid token works fine 46 invalidToken := mock.ACLToken() 47 code := cmd.Run([]string{"-address=" + url, "-token=" + invalidToken.SecretID}) 48 must.Zero(t, code) 49 50 // List with a valid management token 51 code = cmd.Run([]string{"-address=" + url, "-token=" + token.SecretID}) 52 must.Zero(t, code) 53 54 // List with no token at all 55 code = cmd.Run([]string{"-address=" + url}) 56 must.Zero(t, code) 57 58 // Check the output 59 out := ui.OutputWriter.String() 60 must.StrContains(t, out, method.Name) 61 62 // List json 63 must.Zero(t, cmd.Run([]string{"-address=" + url, "-token=" + token.SecretID, "-json"})) 64 65 out = ui.OutputWriter.String() 66 must.StrContains(t, out, "CreateIndex") 67 ui.OutputWriter.Reset() 68 }