github.com/hernad/nomad@v1.6.112/command/acl_binding_rule_info_test.go (about) 1 // Copyright (c) HashiCorp, Inc. 2 // SPDX-License-Identifier: MPL-2.0 3 4 package command 5 6 import ( 7 "fmt" 8 "testing" 9 10 "github.com/hernad/nomad/ci" 11 "github.com/hernad/nomad/command/agent" 12 "github.com/hernad/nomad/helper/uuid" 13 "github.com/hernad/nomad/nomad/structs" 14 "github.com/hernad/nomad/testutil" 15 "github.com/mitchellh/cli" 16 "github.com/shoenig/test/must" 17 ) 18 19 func TestACLBindingRuleInfoCommand_Run(t *testing.T) { 20 ci.Parallel(t) 21 22 // Build a test server with ACLs enabled. 23 srv, _, url := testServer(t, false, func(c *agent.Config) { 24 c.ACL.Enabled = true 25 }) 26 defer srv.Shutdown() 27 28 // Wait for the server to start fully and ensure we have a bootstrap token. 29 testutil.WaitForLeader(t, srv.Agent.RPC) 30 rootACLToken := srv.RootToken 31 must.NotNil(t, rootACLToken) 32 33 ui := cli.NewMockUi() 34 cmd := &ACLBindingRuleInfoCommand{ 35 Meta: Meta{ 36 Ui: ui, 37 flagAddress: url, 38 }, 39 } 40 41 // Perform a lookup without specifying an ID. 42 must.Eq(t, 1, cmd.Run([]string{"-address=" + url, "-token=" + rootACLToken.SecretID})) 43 must.StrContains(t, ui.ErrorWriter.String(), "This command takes one argument: <acl_binding_rule_id>") 44 45 ui.OutputWriter.Reset() 46 ui.ErrorWriter.Reset() 47 48 // Perform a lookup specifying a random ID. 49 must.Eq(t, 1, cmd.Run([]string{"-address=" + url, "-token=" + rootACLToken.SecretID, uuid.Generate()})) 50 must.StrContains(t, ui.ErrorWriter.String(), "ACL binding rule not found") 51 52 ui.OutputWriter.Reset() 53 ui.ErrorWriter.Reset() 54 55 // Create an ACL binding rule. 56 aclBindingRule := structs.ACLBindingRule{ 57 ID: uuid.Generate(), 58 AuthMethod: "auth0", 59 BindType: "role", 60 BindName: "engineering", 61 } 62 err := srv.Agent.Server().State().UpsertACLBindingRules(10, 63 []*structs.ACLBindingRule{&aclBindingRule}, true) 64 must.NoError(t, err) 65 66 // Look up the ACL binding rule. 67 must.Eq(t, 0, cmd.Run([]string{"-address=" + url, "-token=" + rootACLToken.SecretID, aclBindingRule.ID})) 68 s := ui.OutputWriter.String() 69 must.StrContains(t, s, "auth0") 70 must.StrContains(t, s, "role") 71 must.StrContains(t, s, "engineering") 72 73 ui.OutputWriter.Reset() 74 ui.ErrorWriter.Reset() 75 76 // Test that the JSON flag works in return a string that has JSON markers. 77 must.Eq(t, 0, cmd.Run([]string{"-address=" + url, "-token=" + rootACLToken.SecretID, "-json", aclBindingRule.ID})) 78 s = ui.OutputWriter.String() 79 must.StrContains(t, s, "{") 80 must.StrContains(t, s, "}") 81 82 ui.OutputWriter.Reset() 83 ui.ErrorWriter.Reset() 84 85 // Test that we can pass in a custom go template to format the output. 86 must.Eq(t, 0, cmd.Run([]string{ 87 "-address=" + url, "-token=" + rootACLToken.SecretID, "-t", "Custom: {{ .ID }}", aclBindingRule.ID})) 88 s = ui.OutputWriter.String() 89 must.StrContains(t, s, fmt.Sprintf("Custom: %s", aclBindingRule.ID)) 90 91 ui.OutputWriter.Reset() 92 ui.ErrorWriter.Reset() 93 }