github.phpd.cn/hashicorp/consul@v1.4.5/command/acl/policy/update/policy_update_test.go (about)

     1  package policyupdate
     2  
     3  import (
     4  	"io/ioutil"
     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 TestPolicyUpdateCommand_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 TestPolicyUpdateCommand(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  	rules := []byte("service \"\" { policy = \"write\" }")
    51  	err := ioutil.WriteFile(testDir+"/rules.hcl", rules, 0644)
    52  	assert.NoError(err)
    53  
    54  	// Create a policy
    55  	client := a.Client()
    56  
    57  	policy, _, err := client.ACL().PolicyCreate(
    58  		&api.ACLPolicy{Name: "test-policy"},
    59  		&api.WriteOptions{Token: "root"},
    60  	)
    61  	assert.NoError(err)
    62  
    63  	args := []string{
    64  		"-http-addr=" + a.HTTPAddr(),
    65  		"-token=root",
    66  		"-id=" + policy.ID,
    67  		"-name=new-name",
    68  		"-rules=@" + testDir + "/rules.hcl",
    69  	}
    70  
    71  	code := cmd.Run(args)
    72  	assert.Equal(code, 0)
    73  	assert.Empty(ui.ErrorWriter.String())
    74  }