github.com/hernad/nomad@v1.6.112/command/acl_binding_rule_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/helper/uuid" 12 "github.com/hernad/nomad/nomad/structs" 13 "github.com/hernad/nomad/testutil" 14 "github.com/mitchellh/cli" 15 "github.com/shoenig/test/must" 16 ) 17 18 func TestACLBindingRuleListCommand_Run(t *testing.T) { 19 ci.Parallel(t) 20 21 // Build a test server with ACLs enabled. 22 srv, _, url := testServer(t, false, func(c *agent.Config) { 23 c.ACL.Enabled = true 24 }) 25 defer srv.Shutdown() 26 27 // Wait for the server to start fully and ensure we have a bootstrap token. 28 testutil.WaitForLeader(t, srv.Agent.RPC) 29 rootACLToken := srv.RootToken 30 must.NotNil(t, rootACLToken) 31 32 ui := cli.NewMockUi() 33 cmd := &ACLBindingRuleListCommand{ 34 Meta: Meta{ 35 Ui: ui, 36 flagAddress: url, 37 }, 38 } 39 40 // Perform a list straight away without any roles held in state. 41 must.Eq(t, 0, cmd.Run([]string{"-address=" + url, "-token=" + rootACLToken.SecretID})) 42 must.StrContains(t, ui.OutputWriter.String(), "No ACL binding rules found") 43 44 ui.OutputWriter.Reset() 45 ui.ErrorWriter.Reset() 46 47 // Create an ACL binding rule. 48 aclBindingRule := structs.ACLBindingRule{ 49 ID: uuid.Generate(), 50 AuthMethod: "auth0", 51 BindType: "role", 52 BindName: "engineering", 53 } 54 err := srv.Agent.Server().State().UpsertACLBindingRules(10, 55 []*structs.ACLBindingRule{&aclBindingRule}, true) 56 must.NoError(t, err) 57 58 // Perform a listing to get the created binding rule. 59 must.Eq(t, 0, cmd.Run([]string{"-address=" + url, "-token=" + rootACLToken.SecretID})) 60 s := ui.OutputWriter.String() 61 must.StrContains(t, s, "ID") 62 must.StrContains(t, s, "Description") 63 must.StrContains(t, s, "Auth Method") 64 must.StrContains(t, s, "auth0") 65 66 ui.OutputWriter.Reset() 67 ui.ErrorWriter.Reset() 68 }