github.com/s-matyukevich/consul@v1.4.5/command/acl/agenttokens/agent_tokens_test.go (about)

     1  package agenttokens
     2  
     3  import (
     4  	"os"
     5  	"strings"
     6  	"testing"
     7  
     8  	"github.com/hashicorp/consul/agent"
     9  	"github.com/hashicorp/consul/api"
    10  	"github.com/hashicorp/consul/logger"
    11  	"github.com/hashicorp/consul/testrpc"
    12  	"github.com/hashicorp/consul/testutil"
    13  	"github.com/mitchellh/cli"
    14  	"github.com/stretchr/testify/assert"
    15  )
    16  
    17  func TestAgentTokensCommand_noTabs(t *testing.T) {
    18  	t.Parallel()
    19  
    20  	if strings.ContainsRune(New(cli.NewMockUi()).Help(), '\t') {
    21  		t.Fatal("help has tabs")
    22  	}
    23  }
    24  
    25  func TestAgentTokensCommand(t *testing.T) {
    26  	t.Parallel()
    27  	assert := assert.New(t)
    28  
    29  	testDir := testutil.TempDir(t, "acl")
    30  	defer os.RemoveAll(testDir)
    31  
    32  	a := agent.NewTestAgent(t, t.Name(), `
    33  	primary_datacenter = "dc1"
    34  	acl {
    35  		enabled = true
    36  
    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 to set
    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  	// default token
    60  	{
    61  		args := []string{
    62  			"-http-addr=" + a.HTTPAddr(),
    63  			"default",
    64  			token.SecretID,
    65  		}
    66  
    67  		code := cmd.Run(args)
    68  		assert.Equal(code, 0)
    69  		assert.Empty(ui.ErrorWriter.String())
    70  	}
    71  
    72  	// agent token
    73  	{
    74  		args := []string{
    75  			"-http-addr=" + a.HTTPAddr(),
    76  			"agent",
    77  			token.SecretID,
    78  		}
    79  
    80  		code := cmd.Run(args)
    81  		assert.Equal(code, 0)
    82  		assert.Empty(ui.ErrorWriter.String())
    83  	}
    84  
    85  	// master token
    86  	{
    87  		args := []string{
    88  			"-http-addr=" + a.HTTPAddr(),
    89  			"master",
    90  			token.SecretID,
    91  		}
    92  
    93  		code := cmd.Run(args)
    94  		assert.Equal(code, 0)
    95  		assert.Empty(ui.ErrorWriter.String())
    96  	}
    97  
    98  	// replication token
    99  	{
   100  		args := []string{
   101  			"-http-addr=" + a.HTTPAddr(),
   102  			"replication",
   103  			token.SecretID,
   104  		}
   105  
   106  		code := cmd.Run(args)
   107  		assert.Equal(code, 0)
   108  		assert.Empty(ui.ErrorWriter.String())
   109  	}
   110  }