github.com/anth0d/nomad@v0.0.0-20221214183521-ae3a0a2cad06/command/acl_policy_list_test.go (about)

     1  package command
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/hashicorp/nomad/acl"
     7  	"github.com/hashicorp/nomad/ci"
     8  	"github.com/hashicorp/nomad/command/agent"
     9  	"github.com/hashicorp/nomad/nomad/mock"
    10  	"github.com/hashicorp/nomad/nomad/structs"
    11  	"github.com/mitchellh/cli"
    12  	"github.com/shoenig/test/must"
    13  )
    14  
    15  func TestACLPolicyListCommand(t *testing.T) {
    16  	ci.Parallel(t)
    17  
    18  	config := func(c *agent.Config) {
    19  		c.ACL.Enabled = true
    20  	}
    21  
    22  	srv, _, url := testServer(t, true, config)
    23  	state := srv.Agent.Server().State()
    24  	defer stopTestAgent(srv)
    25  
    26  	// Bootstrap an initial ACL token
    27  	token := srv.RootToken
    28  	must.NotNil(t, token)
    29  
    30  	// Create a test ACLPolicy
    31  	policy := &structs.ACLPolicy{
    32  		Name:  "testPolicy",
    33  		Rules: acl.PolicyWrite,
    34  	}
    35  	policy.SetHash()
    36  	must.NoError(t, state.UpsertACLPolicies(structs.MsgTypeTestSetup, 1000, []*structs.ACLPolicy{policy}))
    37  
    38  	ui := cli.NewMockUi()
    39  	cmd := &ACLPolicyListCommand{Meta: Meta{Ui: ui, flagAddress: url}}
    40  
    41  	// Attempt to list policies without a valid management token
    42  	invalidToken := mock.ACLToken()
    43  	code := cmd.Run([]string{"-address=" + url, "-token=" + invalidToken.SecretID})
    44  	must.One(t, code)
    45  
    46  	// Apply a policy with a valid management token
    47  	code = cmd.Run([]string{"-address=" + url, "-token=" + token.SecretID})
    48  	must.Zero(t, code)
    49  
    50  	// Check the output
    51  	out := ui.OutputWriter.String()
    52  	must.StrContains(t, out, policy.Name)
    53  
    54  	// List json
    55  	must.Zero(t, cmd.Run([]string{"-address=" + url, "-token=" + token.SecretID, "-json"}))
    56  
    57  	out = ui.OutputWriter.String()
    58  	must.StrContains(t, out, "CreateIndex")
    59  	ui.OutputWriter.Reset()
    60  }