2018-10-19 16:04:07 +00:00
|
|
|
package policylist
|
|
|
|
|
|
|
|
import (
|
2020-02-15 15:06:05 +00:00
|
|
|
"encoding/json"
|
2018-10-19 16:04:07 +00:00
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/hashicorp/consul/agent"
|
|
|
|
"github.com/hashicorp/consul/api"
|
2019-04-25 16:26:33 +00:00
|
|
|
"github.com/hashicorp/consul/testrpc"
|
2018-10-19 16:04:07 +00:00
|
|
|
"github.com/mitchellh/cli"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestPolicyListCommand_noTabs(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
if strings.ContainsRune(New(cli.NewMockUi()).Help(), '\t') {
|
|
|
|
t.Fatal("help has tabs")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPolicyListCommand(t *testing.T) {
|
2020-12-07 18:42:55 +00:00
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("too slow for testing.Short")
|
|
|
|
}
|
|
|
|
|
2018-10-19 16:04:07 +00:00
|
|
|
t.Parallel()
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
2020-03-31 19:59:56 +00:00
|
|
|
a := agent.NewTestAgent(t, `
|
2018-10-19 16:04:07 +00:00
|
|
|
primary_datacenter = "dc1"
|
|
|
|
acl {
|
|
|
|
enabled = true
|
|
|
|
tokens {
|
|
|
|
master = "root"
|
|
|
|
}
|
|
|
|
}`)
|
|
|
|
|
|
|
|
defer a.Shutdown()
|
|
|
|
testrpc.WaitForLeader(t, a.RPC, "dc1")
|
|
|
|
|
|
|
|
ui := cli.NewMockUi()
|
|
|
|
cmd := New(ui)
|
|
|
|
|
|
|
|
var policyIDs []string
|
|
|
|
|
|
|
|
// Create a couple polices to list
|
|
|
|
client := a.Client()
|
|
|
|
for i := 0; i < 5; i++ {
|
|
|
|
name := fmt.Sprintf("test-policy-%d", i)
|
|
|
|
|
|
|
|
policy, _, err := client.ACL().PolicyCreate(
|
|
|
|
&api.ACLPolicy{Name: name},
|
|
|
|
&api.WriteOptions{Token: "root"},
|
|
|
|
)
|
|
|
|
policyIDs = append(policyIDs, policy.ID)
|
|
|
|
|
|
|
|
assert.NoError(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{
|
|
|
|
"-http-addr=" + a.HTTPAddr(),
|
|
|
|
"-token=root",
|
|
|
|
}
|
|
|
|
|
|
|
|
code := cmd.Run(args)
|
|
|
|
assert.Equal(code, 0)
|
|
|
|
assert.Empty(ui.ErrorWriter.String())
|
|
|
|
output := ui.OutputWriter.String()
|
|
|
|
|
|
|
|
for i, v := range policyIDs {
|
|
|
|
assert.Contains(output, fmt.Sprintf("test-policy-%d", i))
|
|
|
|
assert.Contains(output, v)
|
|
|
|
}
|
|
|
|
}
|
2020-02-15 15:06:05 +00:00
|
|
|
|
|
|
|
func TestPolicyListCommand_JSON(t *testing.T) {
|
2020-12-07 18:42:55 +00:00
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("too slow for testing.Short")
|
|
|
|
}
|
|
|
|
|
2020-02-15 15:06:05 +00:00
|
|
|
t.Parallel()
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
2020-03-31 19:59:56 +00:00
|
|
|
a := agent.NewTestAgent(t, `
|
2020-02-15 15:06:05 +00:00
|
|
|
primary_datacenter = "dc1"
|
|
|
|
acl {
|
|
|
|
enabled = true
|
|
|
|
tokens {
|
|
|
|
master = "root"
|
|
|
|
}
|
|
|
|
}`)
|
|
|
|
|
|
|
|
defer a.Shutdown()
|
|
|
|
testrpc.WaitForLeader(t, a.RPC, "dc1")
|
|
|
|
|
|
|
|
ui := cli.NewMockUi()
|
|
|
|
cmd := New(ui)
|
|
|
|
|
|
|
|
var policyIDs []string
|
|
|
|
|
|
|
|
// Create a couple polices to list
|
|
|
|
client := a.Client()
|
|
|
|
for i := 0; i < 5; i++ {
|
|
|
|
name := fmt.Sprintf("test-policy-%d", i)
|
|
|
|
|
|
|
|
policy, _, err := client.ACL().PolicyCreate(
|
|
|
|
&api.ACLPolicy{Name: name},
|
|
|
|
&api.WriteOptions{Token: "root"},
|
|
|
|
)
|
|
|
|
policyIDs = append(policyIDs, policy.ID)
|
|
|
|
|
|
|
|
assert.NoError(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{
|
|
|
|
"-http-addr=" + a.HTTPAddr(),
|
|
|
|
"-token=root",
|
|
|
|
"-format=json",
|
|
|
|
}
|
|
|
|
|
|
|
|
code := cmd.Run(args)
|
|
|
|
assert.Equal(code, 0)
|
|
|
|
assert.Empty(ui.ErrorWriter.String())
|
|
|
|
output := ui.OutputWriter.String()
|
|
|
|
|
|
|
|
for i, v := range policyIDs {
|
|
|
|
assert.Contains(output, fmt.Sprintf("test-policy-%d", i))
|
|
|
|
assert.Contains(output, v)
|
|
|
|
}
|
|
|
|
|
|
|
|
var jsonOutput json.RawMessage
|
|
|
|
err := json.Unmarshal([]byte(output), &jsonOutput)
|
|
|
|
assert.NoError(err)
|
|
|
|
}
|