github.com/clly/consul@v1.4.5/command/acl/token/read/token_read_test.go (about)

     1  package tokenread
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"strings"
     7  	"testing"
     8  
     9  	"github.com/hashicorp/consul/agent"
    10  	"github.com/hashicorp/consul/api"
    11  	"github.com/hashicorp/consul/logger"
    12  	"github.com/hashicorp/consul/testrpc"
    13  	"github.com/hashicorp/consul/testutil"
    14  	"github.com/mitchellh/cli"
    15  	"github.com/stretchr/testify/assert"
    16  )
    17  
    18  func TestTokenReadCommand_noTabs(t *testing.T) {
    19  	t.Parallel()
    20  
    21  	if strings.ContainsRune(New(cli.NewMockUi()).Help(), '\t') {
    22  		t.Fatal("help has tabs")
    23  	}
    24  }
    25  
    26  func TestTokenReadCommand(t *testing.T) {
    27  	t.Parallel()
    28  	assert := assert.New(t)
    29  
    30  	testDir := testutil.TempDir(t, "acl")
    31  	defer os.RemoveAll(testDir)
    32  
    33  	a := agent.NewTestAgent(t, t.Name(), `
    34  	primary_datacenter = "dc1"
    35  	acl {
    36  		enabled = true
    37  		tokens {
    38  			master = "root"
    39  		}
    40  	}`)
    41  
    42  	a.Agent.LogWriter = logger.NewLogWriter(512)
    43  
    44  	defer a.Shutdown()
    45  	testrpc.WaitForLeader(t, a.RPC, "dc1")
    46  
    47  	ui := cli.NewMockUi()
    48  	cmd := New(ui)
    49  
    50  	// Create a token
    51  	client := a.Client()
    52  
    53  	token, _, err := client.ACL().TokenCreate(
    54  		&api.ACLToken{Description: "test"},
    55  		&api.WriteOptions{Token: "root"},
    56  	)
    57  	assert.NoError(err)
    58  
    59  	args := []string{
    60  		"-http-addr=" + a.HTTPAddr(),
    61  		"-token=root",
    62  		"-id=" + token.AccessorID,
    63  	}
    64  
    65  	code := cmd.Run(args)
    66  	assert.Equal(code, 0)
    67  	assert.Empty(ui.ErrorWriter.String())
    68  
    69  	output := ui.OutputWriter.String()
    70  	assert.Contains(output, fmt.Sprintf("test"))
    71  	assert.Contains(output, token.AccessorID)
    72  	assert.Contains(output, token.SecretID)
    73  }