github.com/kjdelisle/consul@v1.4.5/command/acl/policy/delete/policy_delete_test.go (about)

     1  package policydelete
     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 TestPolicyDeleteCommand_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 TestPolicyDeleteCommand(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 policy
    51  	client := a.Client()
    52  
    53  	policy, _, err := client.ACL().PolicyCreate(
    54  		&api.ACLPolicy{Name: "test-policy"},
    55  		&api.WriteOptions{Token: "root"},
    56  	)
    57  	assert.NoError(err)
    58  
    59  	args := []string{
    60  		"-http-addr=" + a.HTTPAddr(),
    61  		"-token=root",
    62  		"-id=" + policy.ID,
    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("deleted successfully"))
    71  	assert.Contains(output, policy.ID)
    72  
    73  	_, _, err = client.ACL().PolicyRead(
    74  		policy.ID,
    75  		&api.QueryOptions{Token: "root"},
    76  	)
    77  	assert.EqualError(err, "Unexpected response code: 403 (ACL not found)")
    78  }