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

     1  package command
     2  
     3  import (
     4  	"fmt"
     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/hashicorp/nomad/nomad/structs"
    11  	"github.com/mitchellh/cli"
    12  	"github.com/shoenig/test/must"
    13  )
    14  
    15  func TestACLAuthMethodDeleteCommand(t *testing.T) {
    16  	ci.Parallel(t)
    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  	state := srv.Agent.Server().State()
    25  
    26  	// Bootstrap an initial ACL token
    27  	token := srv.RootToken
    28  	must.NotNil(t, token)
    29  
    30  	// Create a test auth method
    31  	method := &structs.ACLAuthMethod{
    32  		Name: "test-auth-method",
    33  	}
    34  	method.SetHash()
    35  	must.NoError(t, state.UpsertACLAuthMethods(1000, []*structs.ACLAuthMethod{method}))
    36  
    37  	ui := cli.NewMockUi()
    38  	cmd := &ACLAuthMethodDeleteCommand{Meta: Meta{Ui: ui, flagAddress: url}}
    39  
    40  	// Delete the method without a valid token fails
    41  	invalidToken := mock.ACLToken()
    42  	code := cmd.Run([]string{"-address=" + url, "-token=" + invalidToken.SecretID, method.Name})
    43  	must.One(t, code)
    44  
    45  	// Delete the method with a valid management token
    46  	code = cmd.Run([]string{"-address=" + url, "-token=" + token.SecretID, method.Name})
    47  	must.Zero(t, code)
    48  
    49  	// Check the output
    50  	out := ui.OutputWriter.String()
    51  	must.StrContains(t, out, fmt.Sprintf("%s successfully deleted", method.Name))
    52  }