Skip to content

Commit 7f60a5d

Browse files
refactor: Verify ActionType in state_test.go
1 parent d78675e commit 7f60a5d

File tree

2 files changed

+63
-20
lines changed

2 files changed

+63
-20
lines changed

coderd/prebuilds/reconcile.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,11 @@ import (
1414
type ActionType int
1515

1616
const (
17+
// ActionTypeUndefined represents an uninitialized or invalid action type.
18+
ActionTypeUndefined ActionType = iota
19+
1720
// ActionTypeCreate indicates that new prebuilds should be created.
18-
ActionTypeCreate ActionType = iota
21+
ActionTypeCreate
1922

2023
// ActionTypeDelete indicates that existing prebuilds should be deleted.
2124
ActionTypeDelete

coderd/prebuilds/state_test.go

+59-19
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,10 @@ func TestNoPrebuilds(t *testing.T) {
8282
require.NoError(t, err)
8383

8484
validateState(t, prebuilds.ReconciliationState{ /*all zero values*/ }, *state)
85-
validateActions(t, prebuilds.ReconciliationActions{ /*all zero values*/ }, *actions)
85+
validateActions(t, prebuilds.ReconciliationActions{
86+
ActionType: prebuilds.ActionTypeCreate,
87+
Create: 0,
88+
}, *actions)
8689
}
8790

8891
// A new template version with a preset with prebuilds configured should result in a new prebuild being created.
@@ -107,7 +110,8 @@ func TestNetNew(t *testing.T) {
107110
Desired: 1,
108111
}, *state)
109112
validateActions(t, prebuilds.ReconciliationActions{
110-
Create: 1,
113+
ActionType: prebuilds.ActionTypeCreate,
114+
Create: 1,
111115
}, *actions)
112116
}
113117

@@ -143,7 +147,9 @@ func TestOutdatedPrebuilds(t *testing.T) {
143147
actions, err := ps.CalculateActions(clock, backoffInterval)
144148
require.NoError(t, err)
145149
validateState(t, prebuilds.ReconciliationState{}, *state)
146-
validateActions(t, prebuilds.ReconciliationActions{DeleteIDs: []uuid.UUID{outdated.prebuildID}}, *actions)
150+
validateActions(t, prebuilds.ReconciliationActions{
151+
ActionType: prebuilds.ActionTypeDelete,
152+
DeleteIDs: []uuid.UUID{outdated.prebuildID}}, *actions)
147153

148154
// WHEN: calculating the current preset's state.
149155
ps, err = snapshot.FilterByPreset(current.presetID)
@@ -154,7 +160,10 @@ func TestOutdatedPrebuilds(t *testing.T) {
154160
actions, err = ps.CalculateActions(clock, backoffInterval)
155161
require.NoError(t, err)
156162
validateState(t, prebuilds.ReconciliationState{Desired: 1}, *state)
157-
validateActions(t, prebuilds.ReconciliationActions{Create: 1}, *actions)
163+
validateActions(t, prebuilds.ReconciliationActions{
164+
ActionType: prebuilds.ActionTypeCreate,
165+
Create: 1,
166+
}, *actions)
158167
}
159168

160169
// A new template version is created with a preset with prebuilds configured; while a prebuild is provisioning up or down,
@@ -181,7 +190,9 @@ func TestInProgressActions(t *testing.T) {
181190
inProgress: 1,
182191
checkFn: func(state prebuilds.ReconciliationState, actions prebuilds.ReconciliationActions) bool {
183192
return validateState(t, prebuilds.ReconciliationState{Desired: 1, Starting: 1}, state) &&
184-
validateActions(t, prebuilds.ReconciliationActions{}, actions)
193+
validateActions(t, prebuilds.ReconciliationActions{
194+
ActionType: prebuilds.ActionTypeCreate,
195+
}, actions)
185196
},
186197
},
187198
// With one running prebuild and one starting, no creations/deletions should occur since we're approaching the correct state.
@@ -193,7 +204,9 @@ func TestInProgressActions(t *testing.T) {
193204
inProgress: 1,
194205
checkFn: func(state prebuilds.ReconciliationState, actions prebuilds.ReconciliationActions) bool {
195206
return validateState(t, prebuilds.ReconciliationState{Actual: 1, Desired: 2, Starting: 1}, state) &&
196-
validateActions(t, prebuilds.ReconciliationActions{}, actions)
207+
validateActions(t, prebuilds.ReconciliationActions{
208+
ActionType: prebuilds.ActionTypeCreate,
209+
}, actions)
197210
},
198211
},
199212
// With one running prebuild and one starting, no creations/deletions should occur
@@ -206,7 +219,9 @@ func TestInProgressActions(t *testing.T) {
206219
inProgress: 1,
207220
checkFn: func(state prebuilds.ReconciliationState, actions prebuilds.ReconciliationActions) bool {
208221
return validateState(t, prebuilds.ReconciliationState{Actual: 2, Desired: 2, Starting: 1}, state) &&
209-
validateActions(t, prebuilds.ReconciliationActions{}, actions)
222+
validateActions(t, prebuilds.ReconciliationActions{
223+
ActionType: prebuilds.ActionTypeCreate,
224+
}, actions)
210225
},
211226
},
212227
// With one prebuild desired and one stopping, a new prebuild will be created.
@@ -218,7 +233,10 @@ func TestInProgressActions(t *testing.T) {
218233
inProgress: 1,
219234
checkFn: func(state prebuilds.ReconciliationState, actions prebuilds.ReconciliationActions) bool {
220235
return validateState(t, prebuilds.ReconciliationState{Desired: 1, Stopping: 1}, state) &&
221-
validateActions(t, prebuilds.ReconciliationActions{Create: 1}, actions)
236+
validateActions(t, prebuilds.ReconciliationActions{
237+
ActionType: prebuilds.ActionTypeCreate,
238+
Create: 1,
239+
}, actions)
222240
},
223241
},
224242
// With 3 prebuilds desired, 2 running, and 1 stopping, a new prebuild will be created.
@@ -230,7 +248,10 @@ func TestInProgressActions(t *testing.T) {
230248
inProgress: 1,
231249
checkFn: func(state prebuilds.ReconciliationState, actions prebuilds.ReconciliationActions) bool {
232250
return validateState(t, prebuilds.ReconciliationState{Actual: 2, Desired: 3, Stopping: 1}, state) &&
233-
validateActions(t, prebuilds.ReconciliationActions{Create: 1}, actions)
251+
validateActions(t, prebuilds.ReconciliationActions{
252+
ActionType: prebuilds.ActionTypeCreate,
253+
Create: 1,
254+
}, actions)
234255
},
235256
},
236257
// With 3 prebuilds desired, 3 running, and 1 stopping, no creations/deletions should occur since the desired state is already achieved.
@@ -242,7 +263,9 @@ func TestInProgressActions(t *testing.T) {
242263
inProgress: 1,
243264
checkFn: func(state prebuilds.ReconciliationState, actions prebuilds.ReconciliationActions) bool {
244265
return validateState(t, prebuilds.ReconciliationState{Actual: 3, Desired: 3, Stopping: 1}, state) &&
245-
validateActions(t, prebuilds.ReconciliationActions{}, actions)
266+
validateActions(t, prebuilds.ReconciliationActions{
267+
ActionType: prebuilds.ActionTypeCreate,
268+
}, actions)
246269
},
247270
},
248271
// With one prebuild desired and one deleting, a new prebuild will be created.
@@ -254,7 +277,10 @@ func TestInProgressActions(t *testing.T) {
254277
inProgress: 1,
255278
checkFn: func(state prebuilds.ReconciliationState, actions prebuilds.ReconciliationActions) bool {
256279
return validateState(t, prebuilds.ReconciliationState{Desired: 1, Deleting: 1}, state) &&
257-
validateActions(t, prebuilds.ReconciliationActions{Create: 1}, actions)
280+
validateActions(t, prebuilds.ReconciliationActions{
281+
ActionType: prebuilds.ActionTypeCreate,
282+
Create: 1,
283+
}, actions)
258284
},
259285
},
260286
// With 2 prebuilds desired, 1 running, and 1 deleting, a new prebuild will be created.
@@ -266,7 +292,10 @@ func TestInProgressActions(t *testing.T) {
266292
inProgress: 1,
267293
checkFn: func(state prebuilds.ReconciliationState, actions prebuilds.ReconciliationActions) bool {
268294
return validateState(t, prebuilds.ReconciliationState{Actual: 1, Desired: 2, Deleting: 1}, state) &&
269-
validateActions(t, prebuilds.ReconciliationActions{Create: 1}, actions)
295+
validateActions(t, prebuilds.ReconciliationActions{
296+
ActionType: prebuilds.ActionTypeCreate,
297+
Create: 1,
298+
}, actions)
270299
},
271300
},
272301
// With 2 prebuilds desired, 2 running, and 1 deleting, no creations/deletions should occur since the desired state is already achieved.
@@ -278,7 +307,9 @@ func TestInProgressActions(t *testing.T) {
278307
inProgress: 1,
279308
checkFn: func(state prebuilds.ReconciliationState, actions prebuilds.ReconciliationActions) bool {
280309
return validateState(t, prebuilds.ReconciliationState{Actual: 2, Desired: 2, Deleting: 1}, state) &&
281-
validateActions(t, prebuilds.ReconciliationActions{}, actions)
310+
validateActions(t, prebuilds.ReconciliationActions{
311+
ActionType: prebuilds.ActionTypeCreate,
312+
}, actions)
282313
},
283314
},
284315
// With 3 prebuilds desired, 1 running, and 2 starting, no creations should occur since the builds are in progress.
@@ -290,7 +321,7 @@ func TestInProgressActions(t *testing.T) {
290321
inProgress: 2,
291322
checkFn: func(state prebuilds.ReconciliationState, actions prebuilds.ReconciliationActions) bool {
292323
return validateState(t, prebuilds.ReconciliationState{Actual: 1, Desired: 3, Starting: 2}, state) &&
293-
validateActions(t, prebuilds.ReconciliationActions{Create: 0}, actions)
324+
validateActions(t, prebuilds.ReconciliationActions{ActionType: prebuilds.ActionTypeCreate, Create: 0}, actions)
294325
},
295326
},
296327
// With 3 prebuilds desired, 5 running, and 2 deleting, no deletions should occur since the builds are in progress.
@@ -302,8 +333,11 @@ func TestInProgressActions(t *testing.T) {
302333
inProgress: 2,
303334
checkFn: func(state prebuilds.ReconciliationState, actions prebuilds.ReconciliationActions) bool {
304335
expectedState := prebuilds.ReconciliationState{Actual: 5, Desired: 3, Deleting: 2, Extraneous: 2}
305-
expectedActions := prebuilds.ReconciliationActions{}
306-
return assert.Len(t, actions.DeleteIDs, 2, "'deleteIDs' did not match expectation") &&
336+
expectedActions := prebuilds.ReconciliationActions{
337+
ActionType: prebuilds.ActionTypeDelete,
338+
}
339+
return assert.EqualValuesf(t, expectedActions.ActionType, actions.ActionType, "'ActionType' did not match expectation") &&
340+
assert.Len(t, actions.DeleteIDs, 2, "'deleteIDs' did not match expectation") &&
307341
assert.EqualValuesf(t, expectedActions.Create, actions.Create, "'create' did not match expectation") &&
308342
assert.EqualValuesf(t, expectedState.Desired, state.Desired, "'desired' did not match expectation") &&
309343
assert.EqualValuesf(t, expectedState.Actual, state.Actual, "'actual' did not match expectation") &&
@@ -408,7 +442,8 @@ func TestExtraneous(t *testing.T) {
408442
Actual: 2, Desired: 1, Extraneous: 1, Eligible: 2,
409443
}, *state)
410444
validateActions(t, prebuilds.ReconciliationActions{
411-
DeleteIDs: []uuid.UUID{older},
445+
ActionType: prebuilds.ActionTypeDelete,
446+
DeleteIDs: []uuid.UUID{older},
412447
}, *actions)
413448
}
414449

@@ -445,7 +480,8 @@ func TestDeprecated(t *testing.T) {
445480
require.NoError(t, err)
446481
validateState(t, prebuilds.ReconciliationState{}, *state)
447482
validateActions(t, prebuilds.ReconciliationActions{
448-
DeleteIDs: []uuid.UUID{current.prebuildID},
483+
ActionType: prebuilds.ActionTypeDelete,
484+
DeleteIDs: []uuid.UUID{current.prebuildID},
449485
}, *actions)
450486
}
451487

@@ -495,6 +531,7 @@ func TestLatestBuildFailed(t *testing.T) {
495531
Actual: 0, Desired: 1,
496532
}, *state)
497533
validateActions(t, prebuilds.ReconciliationActions{
534+
ActionType: prebuilds.ActionTypeBackoff,
498535
BackoffUntil: lastBuildTime.Add(time.Duration(numFailed) * backoffInterval),
499536
}, *actions)
500537

@@ -510,6 +547,7 @@ func TestLatestBuildFailed(t *testing.T) {
510547
Actual: 1, Desired: 1, Eligible: 1,
511548
}, *state)
512549
validateActions(t, prebuilds.ReconciliationActions{
550+
ActionType: prebuilds.ActionTypeCreate,
513551
BackoffUntil: time.Time{},
514552
}, *actions)
515553

@@ -526,6 +564,7 @@ func TestLatestBuildFailed(t *testing.T) {
526564
Actual: 0, Desired: 1,
527565
}, *state)
528566
validateActions(t, prebuilds.ReconciliationActions{
567+
ActionType: prebuilds.ActionTypeCreate,
529568
Create: 1, // <--- NOTE: we're now able to create a new prebuild because the interval has elapsed.
530569
BackoffUntil: lastBuildTime.Add(time.Duration(numFailed) * backoffInterval),
531570
}, *actions)
@@ -582,6 +621,7 @@ func validateState(t *testing.T, expected, actual prebuilds.ReconciliationState)
582621
// validateActions is a convenience func to make tests more readable; it exploits the fact that the default states for
583622
// prebuilds align with zero values.
584623
func validateActions(t *testing.T, expected, actual prebuilds.ReconciliationActions) bool {
585-
return assert.EqualValuesf(t, expected.DeleteIDs, actual.DeleteIDs, "'deleteIDs' did not match expectation") &&
624+
return assert.EqualValuesf(t, expected.ActionType, actual.ActionType, "'ActionType' did not match expectation") &&
625+
assert.EqualValuesf(t, expected.DeleteIDs, actual.DeleteIDs, "'deleteIDs' did not match expectation") &&
586626
assert.EqualValuesf(t, expected.Create, actual.Create, "'create' did not match expectation")
587627
}

0 commit comments

Comments
 (0)