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

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