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

     1  package command
     2  
     3  import (
     4  	"os"
     5  	"testing"
     6  
     7  	"github.com/hashicorp/nomad/ci"
     8  	"github.com/hashicorp/nomad/command/agent"
     9  	"github.com/hashicorp/nomad/nomad/mock"
    10  	"github.com/mitchellh/cli"
    11  	"github.com/shoenig/test/must"
    12  )
    13  
    14  func TestACLPolicyApplyCommand(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  	defer stopTestAgent(srv)
    23  
    24  	// Bootstrap an initial ACL token
    25  	token := srv.RootToken
    26  	must.NotNil(t, token)
    27  
    28  	ui := cli.NewMockUi()
    29  	cmd := &ACLPolicyApplyCommand{Meta: Meta{Ui: ui, flagAddress: url}}
    30  
    31  	// Create a test policy
    32  	policy := mock.ACLPolicy()
    33  
    34  	// Get a file
    35  	file, rm := getTempFile(t, "nomad-test")
    36  	t.Cleanup(rm)
    37  
    38  	// Write the policy to the file
    39  	err := os.WriteFile(file, []byte(policy.Rules), 0700)
    40  	must.NoError(t, err)
    41  
    42  	// Attempt to apply a policy without a valid management token
    43  	code := cmd.Run([]string{"-address=" + url, "-token=foo", "test-policy", file})
    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, "test-policy", file})
    48  	must.Zero(t, code)
    49  
    50  	// Check the output
    51  	out := ui.OutputWriter.String()
    52  	must.StrContains(t, out, "Successfully wrote")
    53  }