From 2faf35c553b8d07fd194882ba008e2a42dfe7bff Mon Sep 17 00:00:00 2001 From: Chelsea Holland Komlo Date: Tue, 24 Oct 2017 13:37:03 -0400 Subject: [PATCH 1/2] add command test for acl bootstrap --- command/acl_bootstrap_test.go | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/command/acl_bootstrap_test.go b/command/acl_bootstrap_test.go index 26b991f8c..e66be1dae 100644 --- a/command/acl_bootstrap_test.go +++ b/command/acl_bootstrap_test.go @@ -3,10 +3,32 @@ package command import ( "testing" + "github.com/hashicorp/nomad/command/agent" "github.com/mitchellh/cli" + "github.com/stretchr/testify/assert" ) func TestACLBootstrapCommand_Implements(t *testing.T) { t.Parallel() - var _ cli.Command = &ACLBootstrapCommand{} + assert := assert.New(t) + + // create a acl-enabled server without bootstrapping the token + config := func(c *agent.Config) { + c.ACL.Enabled = true + c.ACL.PolicyTTL = 0 + } + + srv, _, url := testServer(t, true, config) + defer srv.Shutdown() + + assert.Nil(srv.RootToken) + + ui := new(cli.MockUi) + cmd := &ACLBootstrapCommand{Meta: Meta{Ui: ui, flagAddress: url}} + + code := cmd.Run([]string{"-address=" + url}) + assert.Equal(0, code) + + out := ui.OutputWriter.String() + assert.Contains(out, "Secret ID") } From c95fdc7d754e43f3c65b6b38916437e68313f0e0 Mon Sep 17 00:00:00 2001 From: Chelsea Holland Komlo Date: Wed, 25 Oct 2017 14:08:15 -0400 Subject: [PATCH 2/2] add failure test cases --- command/acl_bootstrap_test.go | 45 ++++++++++++++++++++++++++++++++++- 1 file changed, 44 insertions(+), 1 deletion(-) diff --git a/command/acl_bootstrap_test.go b/command/acl_bootstrap_test.go index e66be1dae..936fd6938 100644 --- a/command/acl_bootstrap_test.go +++ b/command/acl_bootstrap_test.go @@ -8,7 +8,7 @@ import ( "github.com/stretchr/testify/assert" ) -func TestACLBootstrapCommand_Implements(t *testing.T) { +func TestACLBootstrapCommand(t *testing.T) { t.Parallel() assert := assert.New(t) @@ -32,3 +32,46 @@ func TestACLBootstrapCommand_Implements(t *testing.T) { out := ui.OutputWriter.String() assert.Contains(out, "Secret ID") } + +// If a bootsrap token has already been created, attempts to create more should +// fail. +func TestACLBootstrapCommand_ExistingBootstrapToken(t *testing.T) { + t.Parallel() + assert := assert.New(t) + + config := func(c *agent.Config) { + c.ACL.Enabled = true + } + + srv, _, url := testServer(t, true, config) + defer srv.Shutdown() + + assert.NotNil(srv.RootToken) + + ui := new(cli.MockUi) + cmd := &ACLBootstrapCommand{Meta: Meta{Ui: ui, flagAddress: url}} + + code := cmd.Run([]string{"-address=" + url}) + assert.Equal(1, code) + + out := ui.OutputWriter.String() + assert.NotContains(out, "Secret ID") +} + +// Attempting to bootstrap a token on a non-ACL enabled server should fail. +func TestACLBootstrapCommand_NonACLServer(t *testing.T) { + t.Parallel() + assert := assert.New(t) + + srv, _, url := testServer(t, true, nil) + defer srv.Shutdown() + + ui := new(cli.MockUi) + cmd := &ACLBootstrapCommand{Meta: Meta{Ui: ui, flagAddress: url}} + + code := cmd.Run([]string{"-address=" + url}) + assert.Equal(1, code) + + out := ui.OutputWriter.String() + assert.NotContains(out, "Secret ID") +}