github.com/Ilhicas/nomad@v1.0.4-0.20210304152020-e86851182bc3/command/acl_token_create_test.go (about)

     1  package command
     2  
     3  import (
     4  	"strings"
     5  	"testing"
     6  
     7  	"github.com/hashicorp/nomad/command/agent"
     8  	"github.com/mitchellh/cli"
     9  	"github.com/stretchr/testify/assert"
    10  )
    11  
    12  func TestACLTokenCreateCommand(t *testing.T) {
    13  	assert := assert.New(t)
    14  	t.Parallel()
    15  	config := func(c *agent.Config) {
    16  		c.ACL.Enabled = true
    17  	}
    18  
    19  	srv, _, url := testServer(t, true, config)
    20  	defer srv.Shutdown()
    21  
    22  	// Bootstrap an initial ACL token
    23  	token := srv.RootToken
    24  	assert.NotNil(token, "failed to bootstrap ACL token")
    25  
    26  	ui := cli.NewMockUi()
    27  	cmd := &ACLTokenCreateCommand{Meta: Meta{Ui: ui, flagAddress: url}}
    28  
    29  	// Request to create a new token without providing a valid management token
    30  	code := cmd.Run([]string{"-address=" + url, "-token=foo", "-policy=foo", "-type=client"})
    31  	assert.Equal(1, code)
    32  
    33  	// Request to create a new token with a valid management token
    34  	code = cmd.Run([]string{"-address=" + url, "-token=" + token.SecretID, "-policy=foo", "-type=client"})
    35  	assert.Equal(0, code)
    36  
    37  	// Check the output
    38  	out := ui.OutputWriter.String()
    39  	if !strings.Contains(out, "[foo]") {
    40  		t.Fatalf("bad: %v", out)
    41  	}
    42  }