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

     1  package policycreate
     2  
     3  import (
     4  	"io/ioutil"
     5  	"os"
     6  	"strings"
     7  	"testing"
     8  
     9  	"github.com/hashicorp/consul/agent"
    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 TestPolicyCreateCommand_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 TestPolicyCreateCommand(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  		tokens {
    37  			master = "root"
    38  		}
    39  	}`)
    40  
    41  	a.Agent.LogWriter = logger.NewLogWriter(512)
    42  
    43  	defer a.Shutdown()
    44  	testrpc.WaitForLeader(t, a.RPC, "dc1")
    45  
    46  	ui := cli.NewMockUi()
    47  	cmd := New(ui)
    48  
    49  	rules := []byte("service \"\" { policy = \"write\" }")
    50  	err := ioutil.WriteFile(testDir+"/rules.hcl", rules, 0644)
    51  	assert.NoError(err)
    52  
    53  	args := []string{
    54  		"-http-addr=" + a.HTTPAddr(),
    55  		"-token=root",
    56  		"-name=foobar",
    57  		"-rules=@" + testDir + "/rules.hcl",
    58  	}
    59  
    60  	code := cmd.Run(args)
    61  	assert.Equal(code, 0)
    62  	assert.Empty(ui.ErrorWriter.String())
    63  }