github.com/sl1pm4t/consul@v1.4.5-0.20190325224627-74c31c540f9c/command/acl/policy/list/policy_list_test.go (about)

     1  package policylist
     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 TestPolicyListCommand_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 TestPolicyListCommand(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  	var policyIDs []string
    51  
    52  	// Create a couple polices to list
    53  	client := a.Client()
    54  	for i := 0; i < 5; i++ {
    55  		name := fmt.Sprintf("test-policy-%d", i)
    56  
    57  		policy, _, err := client.ACL().PolicyCreate(
    58  			&api.ACLPolicy{Name: name},
    59  			&api.WriteOptions{Token: "root"},
    60  		)
    61  		policyIDs = append(policyIDs, policy.ID)
    62  
    63  		assert.NoError(err)
    64  	}
    65  
    66  	args := []string{
    67  		"-http-addr=" + a.HTTPAddr(),
    68  		"-token=root",
    69  	}
    70  
    71  	code := cmd.Run(args)
    72  	assert.Equal(code, 0)
    73  	assert.Empty(ui.ErrorWriter.String())
    74  	output := ui.OutputWriter.String()
    75  
    76  	for i, v := range policyIDs {
    77  		assert.Contains(output, fmt.Sprintf("test-policy-%d", i))
    78  		assert.Contains(output, v)
    79  	}
    80  }