open-nomad/command/acl_policy_apply_test.go

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

54 lines
1.2 KiB
Go
Raw Normal View History

2017-09-15 04:19:56 +00:00
package command
import (
"os"
"testing"
"github.com/hashicorp/nomad/ci"
2017-09-15 04:19:56 +00:00
"github.com/hashicorp/nomad/command/agent"
"github.com/hashicorp/nomad/nomad/mock"
"github.com/mitchellh/cli"
"github.com/shoenig/test/must"
2017-09-15 04:19:56 +00:00
)
func TestACLPolicyApplyCommand(t *testing.T) {
ci.Parallel(t)
2017-09-15 04:19:56 +00:00
config := func(c *agent.Config) {
c.ACL.Enabled = true
}
srv, _, url := testServer(t, true, config)
defer stopTestAgent(srv)
2017-09-15 04:19:56 +00:00
// Bootstrap an initial ACL token
token := srv.RootToken
must.NotNil(t, token)
2017-09-15 04:19:56 +00:00
2020-10-05 14:07:41 +00:00
ui := cli.NewMockUi()
2017-09-15 04:19:56 +00:00
cmd := &ACLPolicyApplyCommand{Meta: Meta{Ui: ui, flagAddress: url}}
// Create a test policy
policy := mock.ACLPolicy()
// Get a file
file, rm := getTempFile(t, "nomad-test")
t.Cleanup(rm)
2017-09-15 04:19:56 +00:00
// Write the policy to the file
err := os.WriteFile(file, []byte(policy.Rules), 0700)
must.NoError(t, err)
2017-09-15 04:19:56 +00:00
2017-09-15 18:12:17 +00:00
// Attempt to apply a policy without a valid management token
code := cmd.Run([]string{"-address=" + url, "-token=foo", "test-policy", file})
must.One(t, code)
2017-09-15 04:19:56 +00:00
2017-09-15 18:12:17 +00:00
// Apply a policy with a valid management token
code = cmd.Run([]string{"-address=" + url, "-token=" + token.SecretID, "test-policy", file})
must.Zero(t, code)
2017-09-15 04:19:56 +00:00
// Check the output
out := ui.OutputWriter.String()
must.StrContains(t, out, "Successfully wrote")
2017-09-15 04:19:56 +00:00
}