github.com/bigcommerce/nomad@v0.9.3-bc/command/acl_policy_info_test.go (about) 1 package command 2 3 import ( 4 "os" 5 "strings" 6 "testing" 7 8 "github.com/hashicorp/nomad/acl" 9 "github.com/hashicorp/nomad/command/agent" 10 "github.com/hashicorp/nomad/nomad/mock" 11 "github.com/hashicorp/nomad/nomad/structs" 12 "github.com/mitchellh/cli" 13 "github.com/stretchr/testify/assert" 14 ) 15 16 func TestACLPolicyInfoCommand(t *testing.T) { 17 assert := assert.New(t) 18 t.Parallel() 19 config := func(c *agent.Config) { 20 c.ACL.Enabled = true 21 } 22 23 srv, _, url := testServer(t, true, config) 24 state := srv.Agent.Server().State() 25 defer srv.Shutdown() 26 27 // Bootstrap an initial ACL token 28 token := srv.RootToken 29 assert.NotNil(token, "failed to bootstrap ACL token") 30 31 // Create a test ACLPolicy 32 policy := &structs.ACLPolicy{ 33 Name: "testPolicy", 34 Rules: acl.PolicyWrite, 35 } 36 policy.SetHash() 37 assert.Nil(state.UpsertACLPolicies(1000, []*structs.ACLPolicy{policy})) 38 39 ui := new(cli.MockUi) 40 cmd := &ACLPolicyInfoCommand{Meta: Meta{Ui: ui, flagAddress: url}} 41 42 // Attempt to apply a policy without a valid management token 43 invalidToken := mock.ACLToken() 44 os.Setenv("NOMAD_TOKEN", invalidToken.SecretID) 45 code := cmd.Run([]string{"-address=" + url, policy.Name}) 46 assert.Equal(1, code) 47 48 // Apply a policy with a valid management token 49 os.Setenv("NOMAD_TOKEN", token.SecretID) 50 code = cmd.Run([]string{"-address=" + url, policy.Name}) 51 assert.Equal(0, code) 52 53 // Check the output 54 out := ui.OutputWriter.String() 55 if !strings.Contains(out, policy.Name) { 56 t.Fatalf("bad: %v", out) 57 } 58 }