github.com/anuvu/nomad@v0.8.7-atom1/command/acl_policy_apply_test.go (about)

     1  package command
     2  
     3  import (
     4  	"io/ioutil"
     5  	"os"
     6  	"strings"
     7  	"testing"
     8  
     9  	"github.com/hashicorp/nomad/command/agent"
    10  	"github.com/hashicorp/nomad/nomad/mock"
    11  	"github.com/mitchellh/cli"
    12  	"github.com/stretchr/testify/assert"
    13  )
    14  
    15  func TestACLPolicyApplyCommand(t *testing.T) {
    16  	assert := assert.New(t)
    17  	t.Parallel()
    18  	config := func(c *agent.Config) {
    19  		c.ACL.Enabled = true
    20  	}
    21  
    22  	srv, _, url := testServer(t, true, config)
    23  	defer srv.Shutdown()
    24  
    25  	// Bootstrap an initial ACL token
    26  	token := srv.RootToken
    27  	assert.NotNil(token, "failed to bootstrap ACL token")
    28  
    29  	ui := new(cli.MockUi)
    30  	cmd := &ACLPolicyApplyCommand{Meta: Meta{Ui: ui, flagAddress: url}}
    31  
    32  	// Create a test policy
    33  	policy := mock.ACLPolicy()
    34  
    35  	// Get a file
    36  	f, err := ioutil.TempFile("", "nomad-test")
    37  	assert.Nil(err)
    38  	defer os.Remove(f.Name())
    39  
    40  	// Write the policy to the file
    41  	err = ioutil.WriteFile(f.Name(), []byte(policy.Rules), 0700)
    42  	assert.Nil(err)
    43  
    44  	// Attempt to apply a policy without a valid management token
    45  	os.Setenv("NOMAD_TOKEN", "foo")
    46  	code := cmd.Run([]string{"-address=" + url, "test-policy", f.Name()})
    47  	assert.Equal(1, code)
    48  
    49  	// Apply a policy with a valid management token
    50  	os.Setenv("NOMAD_TOKEN", token.SecretID)
    51  	code = cmd.Run([]string{"-address=" + url, "test-policy", f.Name()})
    52  	assert.Equal(0, code)
    53  
    54  	// Check the output
    55  	out := ui.OutputWriter.String()
    56  	if !strings.Contains(out, "Successfully wrote") {
    57  		t.Fatalf("bad: %v", out)
    58  	}
    59  }