Repository: hashicorp/nomad
File: command/acl_policy_self_test.go (Go)

Code before:
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: BUSL-1.1

package command

import (
	"testing"

	"github.com/hashicorp/cli"
	"github.com/hashicorp/nomad/command/agent"
	"github.com/hashicorp/nomad/nomad/mock"
	"github.com/hashicorp/nomad/nomad/structs"
	"github.com/shoenig/test/must"
)

func TestACLPolicySelfCommand_ViaEnvVar(t *testing.T) {
	const policyName = "nw"

	setupServer := func(t *testing.T) (*agent.TestAgent, string, *structs.ACLToken) {
		config := func(c *agent.Config) {
			c.ACL.Enabled = true
		}
		srv, _, url := testServer(t, true, config)
		t.Cleanup(srv.Shutdown)
		return srv, url, srv.RootToken
	}

	createPolicy := func(t *testing.T, srv *agent.TestAgent, token *structs.ACLToken, job *structs.Job) {
		args := structs.ACLPolicyUpsertRequest{
			Policies: []*structs.ACLPolicy{
				{
					Name:        policyName,
					Description: "test job can write to nodes",
					Rules:       `node { policy = "write" }`,
					JobACL: &structs.JobACL{
						Namespace: job.Namespace,
						JobID:     job.ID,
					},
				},
			},
			WriteRequest: structs.WriteRequest{
				Region:    job.Region,
				AuthToken: token.SecretID,
				Namespace: job.Namespace,
			},
		}
		reply := structs.GenericResponse{}
		must.NoError(t, srv.RPC("ACL.UpsertPolicies", &args, &reply))
	}

	runCommand := func(t *testing.T, url, token string) string {

Review comment (performance):
Setting up the server and waiting for it to be live is relatively expensive and the subtests for this test don't need to be hermetic (it might be better if they're not, in fact, because this is basically an integration test and not a unit test). I'd do all the setup in the main test and then only set the token and run the command in the subtests.

Code after:
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: BUSL-1.1

package command

import (
	"testing"

	"github.com/hashicorp/cli"
	"github.com/hashicorp/nomad/command/agent"
	"github.com/hashicorp/nomad/nomad/mock"
	"github.com/hashicorp/nomad/nomad/structs"
	"github.com/shoenig/test/must"
)

func TestACLPolicySelfCommand_ViaEnvVar(t *testing.T) {
	const policyName = "nw"

	config := func(c *agent.Config) {
		c.ACL.Enabled = true
	}
	srv, _, url := testServer(t, true, config)
	t.Cleanup(srv.Shutdown)

	createPolicy := func(t *testing.T, srv *agent.TestAgent, token *structs.ACLToken, job *structs.Job) {
		args := structs.ACLPolicyUpsertRequest{
			Policies: []*structs.ACLPolicy{
				{
					Name:        policyName,
					Description: "test job can write to nodes",
					Rules:       `node { policy = "write" }`,
					JobACL: &structs.JobACL{
						Namespace: job.Namespace,
						JobID:     job.ID,
					},
				},
			},
			WriteRequest: structs.WriteRequest{
				Region:    job.Region,
				AuthToken: token.SecretID,
				Namespace: job.Namespace,
			},
		}
		reply := structs.GenericResponse{}
		must.NoError(t, srv.RPC("ACL.UpsertPolicies", &args, &reply))
	}

	runCommand := func(t *testing.T, url, token string) string {
		ui := cli.NewMockUi()
		cmd := &ACLPolicySelfCommand{Meta: Meta{Ui: ui, flagAddress: url}}
		t.Setenv("NOMAD_TOKEN", token)