2018-10-19 16:04:07 +00:00
|
|
|
package bootstrap
|
|
|
|
|
|
|
|
import (
|
2020-02-15 15:06:05 +00:00
|
|
|
"encoding/json"
|
2018-10-19 16:04:07 +00:00
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/hashicorp/consul/agent"
|
|
|
|
"github.com/hashicorp/consul/agent/structs"
|
2019-03-27 12:54:56 +00:00
|
|
|
"github.com/hashicorp/consul/sdk/testutil"
|
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"
|
2020-02-15 15:06:05 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2018-10-19 16:04:07 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestBootstrapCommand_noTabs(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
if strings.ContainsRune(New(cli.NewMockUi()).Help(), '\t') {
|
|
|
|
t.Fatal("help has tabs")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-15 15:06:05 +00:00
|
|
|
func TestBootstrapCommand_Pretty(t *testing.T) {
|
2018-10-19 16:04:07 +00:00
|
|
|
t.Parallel()
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
|
|
|
testDir := testutil.TempDir(t, "acl")
|
|
|
|
defer os.RemoveAll(testDir)
|
|
|
|
|
2019-02-14 15:59:14 +00:00
|
|
|
a := agent.NewTestAgent(t, t.Name(), `
|
2018-10-19 16:04:07 +00:00
|
|
|
primary_datacenter = "dc1"
|
|
|
|
acl {
|
|
|
|
enabled = true
|
|
|
|
}`)
|
|
|
|
|
|
|
|
defer a.Shutdown()
|
|
|
|
testrpc.WaitForLeader(t, a.RPC, "dc1")
|
|
|
|
|
|
|
|
ui := cli.NewMockUi()
|
|
|
|
cmd := New(ui)
|
|
|
|
|
|
|
|
args := []string{
|
|
|
|
"-http-addr=" + a.HTTPAddr(),
|
|
|
|
}
|
|
|
|
|
|
|
|
code := cmd.Run(args)
|
|
|
|
assert.Equal(code, 0)
|
|
|
|
assert.Empty(ui.ErrorWriter.String())
|
|
|
|
output := ui.OutputWriter.String()
|
|
|
|
assert.Contains(output, "Bootstrap Token")
|
|
|
|
assert.Contains(output, structs.ACLPolicyGlobalManagementID)
|
|
|
|
}
|
2020-02-15 15:06:05 +00:00
|
|
|
|
|
|
|
func TestBootstrapCommand_JSON(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
|
|
|
testDir := testutil.TempDir(t, "acl")
|
|
|
|
defer os.RemoveAll(testDir)
|
|
|
|
|
|
|
|
a := agent.NewTestAgent(t, t.Name(), `
|
|
|
|
primary_datacenter = "dc1"
|
|
|
|
acl {
|
|
|
|
enabled = true
|
|
|
|
}`)
|
|
|
|
|
|
|
|
defer a.Shutdown()
|
|
|
|
testrpc.WaitForLeader(t, a.RPC, "dc1")
|
|
|
|
|
|
|
|
ui := cli.NewMockUi()
|
|
|
|
cmd := New(ui)
|
|
|
|
|
|
|
|
args := []string{
|
|
|
|
"-http-addr=" + a.HTTPAddr(),
|
|
|
|
"-format=json",
|
|
|
|
}
|
|
|
|
|
|
|
|
code := cmd.Run(args)
|
|
|
|
assert.Equal(code, 0)
|
|
|
|
assert.Empty(ui.ErrorWriter.String())
|
|
|
|
output := ui.OutputWriter.String()
|
|
|
|
assert.Contains(output, "Bootstrap Token")
|
|
|
|
assert.Contains(output, structs.ACLPolicyGlobalManagementID)
|
|
|
|
|
|
|
|
var jsonOutput json.RawMessage
|
|
|
|
err := json.Unmarshal([]byte(output), &jsonOutput)
|
|
|
|
require.NoError(t, err, "token unmarshalling error")
|
|
|
|
}
|