github.com/anth0d/nomad@v0.0.0-20221214183521-ae3a0a2cad06/command/acl_token_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 TestACLTokenListCommand(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  	defer stopTestAgent(srv)
    24  
    25  	state := srv.Agent.Server().State()
    26  
    27  	// Bootstrap an initial ACL token
    28  	token := srv.RootToken
    29  	must.NotNil(t, token)
    30  
    31  	// Create a valid token
    32  	mockToken := mock.ACLToken()
    33  	mockToken.Policies = []string{acl.PolicyWrite}
    34  	mockToken.SetHash()
    35  	must.NoError(t, state.UpsertACLTokens(structs.MsgTypeTestSetup, 1000, []*structs.ACLToken{mockToken}))
    36  
    37  	ui := cli.NewMockUi()
    38  	cmd := &ACLTokenListCommand{Meta: Meta{Ui: ui, flagAddress: url}}
    39  
    40  	// Attempt to list tokens without a valid management token
    41  	invalidToken := mock.ACLToken()
    42  	code := cmd.Run([]string{"-address=" + url, "-token=" + invalidToken.SecretID})
    43  	must.One(t, code)
    44  
    45  	// Apply a token with a valid management token
    46  	code = cmd.Run([]string{"-address=" + url, "-token=" + token.SecretID})
    47  	must.Zero(t, code)
    48  
    49  	// Check the output
    50  	out := ui.OutputWriter.String()
    51  	must.StrContains(t, out, mockToken.Name)
    52  
    53  	// List json
    54  	must.Zero(t, cmd.Run([]string{"-address=" + url, "-token=" + token.SecretID, "-json"}))
    55  
    56  	out = ui.OutputWriter.String()
    57  	must.StrContains(t, out, "CreateIndex")
    58  	ui.OutputWriter.Reset()
    59  }