github.com/hernad/nomad@v1.6.112/command/acl_token_update_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 TestACLTokenUpdateCommand(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  	// Bootstrap an initial ACL token
    29  	token := srv.RootToken
    30  	must.NotNil(t, token)
    31  
    32  	ui := cli.NewMockUi()
    33  	cmd := &ACLTokenUpdateCommand{Meta: Meta{Ui: ui, flagAddress: url}}
    34  	state := srv.Agent.Server().State()
    35  
    36  	// Create a valid token
    37  	mockToken := mock.ACLToken()
    38  	mockToken.Policies = []string{acl.PolicyWrite}
    39  	mockToken.SetHash()
    40  	must.NoError(t, state.UpsertACLTokens(structs.MsgTypeTestSetup, 1000, []*structs.ACLToken{mockToken}))
    41  
    42  	// Request to update a new token without providing a valid management token
    43  	invalidToken := mock.ACLToken()
    44  	code := cmd.Run([]string{"--token=" + invalidToken.SecretID, "-address=" + url, "-name=bar", mockToken.AccessorID})
    45  	must.One(t, code)
    46  
    47  	// Request to update a new token with a valid management token
    48  	code = cmd.Run([]string{"--token=" + token.SecretID, "-address=" + url, "-name=bar", mockToken.AccessorID})
    49  	must.Zero(t, code)
    50  
    51  	// Check the output
    52  	out := ui.OutputWriter.String()
    53  	must.StrContains(t, out, mockToken.AccessorID)
    54  	must.StrContains(t, out, "bar")
    55  }