Repository: hashicorp/nomad
File: nomad/structs/node_test.go (Go)

Code before:
		},
		{
			name: "invalid node pool all use",
			request: &NodeRegisterRequest{
				Node: &Node{
					ID:         "node-id",
					SecretID:   "node-secret-id",
					Name:       "node-name",
					NodePool:   NodePoolAll,
					NodeClass:  "node-class",
					Datacenter: "node-datacenter",
					Attributes: map[string]string{"key1": "value1"},
				},
			},
			expectedError: true,
		},
	}

	for _, tc := range testCases {
		t.Run(tc.name, func(t *testing.T) {
			actualError := tc.request.Validate()
			if tc.expectedError {
				must.Error(t, actualError)
			} else {
				must.NoError(t, actualError)
			}
		})
	}
}

func TestNodeRegisterRequest_ShouldGenerateNodeIdentity(t *testing.T) {
	ci.Parallel(t)

	// Generate a stable mock node for testing.
	mockNode := MockNode()

	testCases := []struct {
		name                     string
		inputNodeRegisterRequest *NodeRegisterRequest
		inputAuthErr             error
		inputTime                time.Time
		inputTTL                 time.Duration
		expectedOutput           bool
	}{
		{
			name:                     "expired node identity",
			inputNodeRegisterRequest: &NodeRegisterRequest{},
			inputAuthErr:             jwt.ErrExpired,
			inputTime:                time.Now(),
			inputTTL:                 10 * time.Minute,
			expectedOutput:           true,

Review comment (suggestion):
with `exepectedError` being a `bool`, this doesn't check whether the specific desired error is what's returned -- these could all be `args.Node == nil`, and the test would still pass.

I like to do either `expectError error` and make `errors.New()` as package vars to check with `must.ErrorIs()`, or short of that, `expectError string` and do `must.EqError()` or `must.ErrorContains()`

Code after:
		},
		{
			name: "invalid node pool all use",
			request: &NodeRegisterRequest{
				Node: &Node{
					ID:         "node-id",
					SecretID:   "node-secret-id",
					Name:       "node-name",
					NodePool:   NodePoolAll,
					NodeClass:  "node-class",
					Datacenter: "node-datacenter",
					Attributes: map[string]string{"key1": "value1"},
				},
			},
			errorContains: `node is not allowed to register in node pool "all"`,
		},
	}

	for _, tc := range testCases {
		t.Run(tc.name, func(t *testing.T) {
			actualError := tc.request.Validate()
			if tc.errorContains != "" {
				must.ErrorContains(t, actualError, tc.errorContains)
			} else {
				must.NoError(t, actualError)
			}
		})
	}
}

func TestNodeRegisterRequest_ShouldGenerateNodeIdentity(t *testing.T) {
	ci.Parallel(t)

	// Generate a stable mock node for testing.
	mockNode := MockNode()

	testCases := []struct {
		name                     string
		inputNodeRegisterRequest *NodeRegisterRequest
		inputAuthErr             error
		inputTime                time.Time
		inputTTL                 time.Duration
		expectedOutput           bool
	}{
		{
			name:                     "expired node identity",
			inputNodeRegisterRequest: &NodeRegisterRequest{},
			inputAuthErr:             jwt.ErrExpired,
			inputTime:                time.Now(),
			inputTTL:                 10 * time.Minute,
			expectedOutput:           true,