github.com/adityamillind98/nomad@v0.11.8/command/acl_token_update_test.go (about)

     1  package command
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/hashicorp/nomad/acl"
     7  	"github.com/hashicorp/nomad/command/agent"
     8  	"github.com/hashicorp/nomad/nomad/mock"
     9  	"github.com/hashicorp/nomad/nomad/structs"
    10  	"github.com/mitchellh/cli"
    11  	"github.com/stretchr/testify/assert"
    12  )
    13  
    14  func TestACLTokenUpdateCommand(t *testing.T) {
    15  	assert := assert.New(t)
    16  	t.Parallel()
    17  	config := func(c *agent.Config) {
    18  		c.ACL.Enabled = true
    19  	}
    20  
    21  	srv, _, url := testServer(t, true, config)
    22  	defer srv.Shutdown()
    23  
    24  	// Bootstrap an initial ACL token
    25  	token := srv.RootToken
    26  	assert.NotNil(token, "failed to bootstrap ACL token")
    27  
    28  	ui := new(cli.MockUi)
    29  	cmd := &ACLTokenUpdateCommand{Meta: Meta{Ui: ui, flagAddress: url}}
    30  	state := srv.Agent.Server().State()
    31  
    32  	// Create a valid token
    33  	mockToken := mock.ACLToken()
    34  	mockToken.Policies = []string{acl.PolicyWrite}
    35  	mockToken.SetHash()
    36  	assert.Nil(state.UpsertACLTokens(1000, []*structs.ACLToken{mockToken}))
    37  
    38  	// Request to update a new token without providing a valid management token
    39  	invalidToken := mock.ACLToken()
    40  	code := cmd.Run([]string{"--token=" + invalidToken.SecretID, "-address=" + url, "-name=bar", mockToken.AccessorID})
    41  	assert.Equal(1, code)
    42  
    43  	// Request to update a new token with a valid management token
    44  	code = cmd.Run([]string{"--token=" + token.SecretID, "-address=" + url, "-name=bar", mockToken.AccessorID})
    45  	assert.Equal(0, code)
    46  
    47  	// Check the output
    48  	out := ui.OutputWriter.String()
    49  	assert.Contains(out, mockToken.AccessorID)
    50  	assert.Contains(out, "bar")
    51  }