github.com/hernad/nomad@v1.6.112/command/acl_token_list_test.go (about)

     1  // Copyright (c) HashiCorp, Inc.
     2  // SPDX-License-Identifier: MPL-2.0
     3  
     4  package command
     5  
     6  import (
     7  	"testing"
     8  
     9  	"github.com/hernad/nomad/acl"
    10  	"github.com/hernad/nomad/ci"
    11  	"github.com/hernad/nomad/command/agent"
    12  	"github.com/hernad/nomad/nomad/mock"
    13  	"github.com/hernad/nomad/nomad/structs"
    14  	"github.com/mitchellh/cli"
    15  	"github.com/shoenig/test/must"
    16  )
    17  
    18  func TestACLTokenListCommand(t *testing.T) {
    19  	ci.Parallel(t)
    20  
    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 valid token
    35  	mockToken := mock.ACLToken()
    36  	mockToken.Policies = []string{acl.PolicyWrite}
    37  	mockToken.SetHash()
    38  	must.NoError(t, state.UpsertACLTokens(structs.MsgTypeTestSetup, 1000, []*structs.ACLToken{mockToken}))
    39  
    40  	ui := cli.NewMockUi()
    41  	cmd := &ACLTokenListCommand{Meta: Meta{Ui: ui, flagAddress: url}}
    42  
    43  	// Attempt to list tokens without a valid management token
    44  	invalidToken := mock.ACLToken()
    45  	code := cmd.Run([]string{"-address=" + url, "-token=" + invalidToken.SecretID})
    46  	must.One(t, code)
    47  
    48  	// Apply a token with a valid management token
    49  	code = cmd.Run([]string{"-address=" + url, "-token=" + token.SecretID})
    50  	must.Zero(t, code)
    51  
    52  	// Check the output
    53  	out := ui.OutputWriter.String()
    54  	must.StrContains(t, out, mockToken.Name)
    55  
    56  	// List json
    57  	must.Zero(t, cmd.Run([]string{"-address=" + url, "-token=" + token.SecretID, "-json"}))
    58  
    59  	out = ui.OutputWriter.String()
    60  	must.StrContains(t, out, "CreateIndex")
    61  	ui.OutputWriter.Reset()
    62  }