Repository: hashicorp/nomad
File: nomad/job_endpoint_test.go (Go)

Code before:

	const requestedCount = 6
	job := mock.Job()
	job.TaskGroups[0].Count = requestedCount

	// register the job
	err := state.UpsertJob(structs.MsgTypeTestSetup, 1000, nil, job)
	require.Nil(err)

	var resp structs.JobRegisterResponse
	scale := &structs.JobScaleRequest{
		JobID: job.ID,
		Target: map[string]string{
			structs.ScalingTargetGroup: job.TaskGroups[0].Name,
		},
		Count:          pointer.Of(int64(requestedCount)),
		Message:        "count too high",
		PolicyOverride: false,
		WriteRequest: structs.WriteRequest{
			Region:    "global",
			Namespace: job.Namespace,
		},
	}
	err = msgpackrpc.CallWithCodec(codec, "Job.Scale", scale, &resp)
	require.Error(err)
	require.Contains(err.Error(), "total count was greater than configured job_max_count: 6 > 4")
}

func TestJobEndpoint_Scale_NoEval(t *testing.T) {
	ci.Parallel(t)
	require := require.New(t)

	s1, cleanupS1 := TestServer(t, nil)
	defer cleanupS1()
	codec := rpcClient(t, s1)
	testutil.WaitForLeader(t, s1.RPC)
	state := s1.fsm.State()

	job := mock.Job()
	groupName := job.TaskGroups[0].Name
	originalCount := job.TaskGroups[0].Count
	var resp structs.JobRegisterResponse
	err := msgpackrpc.CallWithCodec(codec, "Job.Register", &structs.JobRegisterRequest{
		Job: job,
		WriteRequest: structs.WriteRequest{
			Region:    "global",
			Namespace: job.Namespace,
		},
	}, &resp)
	jobCreateIndex := resp.Index
	require.NoError(err)

Review comment (performance):
We're (slowly) migrating all tests in this repo to use `shoenig/test` rather than `testify`:

```suggestion
	must.ErrorContains(t, err, "total count was greater than configured job_max_count: 6 > 4")
```

(typical of all the uses of `require` in the PR)

Code after:
	require.Error(err)
	require.Contains(err.Error(), "not found")

	// register the job
	err = state.UpsertJob(structs.MsgTypeTestSetup, 1000, nil, job)
	require.Nil(err)

	scale.Count = pointer.Of(int64(10))
	scale.Message = "error message"
	scale.Error = true
	err = msgpackrpc.CallWithCodec(codec, "Job.Scale", scale, &resp)
	require.Error(err)
	require.Contains(err.Error(), "should not contain count if error is true")
}

func TestJobEndpoint_Scale_TaskGroupOutOfBounds(t *testing.T) {
	ci.Parallel(t)
	require := require.New(t)

	s1, cleanupS1 := TestServer(t, nil)
	defer cleanupS1()
	codec := rpcClient(t, s1)
	testutil.WaitForLeader(t, s1.RPC)
	state := s1.fsm.State()

	job, pol := mock.JobWithScalingPolicy()
	pol.Min = 3
	pol.Max = 10
	job.TaskGroups[0].Count = 5

	// register the job
	err := state.UpsertJob(structs.MsgTypeTestSetup, 1000, nil, job)
	require.Nil(err)

	var resp structs.JobRegisterResponse
	scale := &structs.JobScaleRequest{
		JobID: job.ID,
		Target: map[string]string{
			structs.ScalingTargetGroup: job.TaskGroups[0].Name,
		},
		Count:          pointer.Of(pol.Max + 1),
		Message:        "out of bounds",
		PolicyOverride: false,
		WriteRequest: structs.WriteRequest{
			Region:    "global",
			Namespace: job.Namespace,
		},
	}
	err = msgpackrpc.CallWithCodec(codec, "Job.Scale", scale, &resp)
	require.Error(err)
	require.Contains(err.Error(), "group count was greater than scaling policy maximum: 11 > 10")