github.com/hernad/nomad@v1.6.112/command/acl_policy_delete_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/acl"
    11  	"github.com/hernad/nomad/ci"
    12  	"github.com/hernad/nomad/command/agent"
    13  	"github.com/hernad/nomad/nomad/mock"
    14  	"github.com/hernad/nomad/nomad/structs"
    15  	"github.com/mitchellh/cli"
    16  	"github.com/shoenig/test/must"
    17  )
    18  
    19  func TestACLPolicyDeleteCommand(t *testing.T) {
    20  	ci.Parallel(t)
    21  	config := func(c *agent.Config) {
    22  		c.ACL.Enabled = true
    23  	}
    24  
    25  	srv, _, url := testServer(t, true, config)
    26  	defer srv.Shutdown()
    27  
    28  	state := srv.Agent.Server().State()
    29  
    30  	// Bootstrap an initial ACL token
    31  	token := srv.RootToken
    32  	must.NotNil(t, token)
    33  
    34  	// Create a test ACLPolicy
    35  	policy := &structs.ACLPolicy{
    36  		Name:  "testPolicy",
    37  		Rules: acl.PolicyWrite,
    38  	}
    39  	policy.SetHash()
    40  	must.NoError(t, state.UpsertACLPolicies(structs.MsgTypeTestSetup, 1000, []*structs.ACLPolicy{policy}))
    41  
    42  	ui := cli.NewMockUi()
    43  	cmd := &ACLPolicyDeleteCommand{Meta: Meta{Ui: ui, flagAddress: url}}
    44  
    45  	// Delete the policy without a valid token fails
    46  	invalidToken := mock.ACLToken()
    47  	code := cmd.Run([]string{"-address=" + url, "-token=" + invalidToken.SecretID, policy.Name})
    48  	must.One(t, code)
    49  
    50  	// Delete the policy with a valid management token
    51  	code = cmd.Run([]string{"-address=" + url, "-token=" + token.SecretID, policy.Name})
    52  	must.Zero(t, code)
    53  
    54  	// Check the output
    55  	out := ui.OutputWriter.String()
    56  	must.StrContains(t, out, fmt.Sprintf("Successfully deleted %s policy", policy.Name))
    57  }