Skip to content

Commit d663126

Browse files
committed
orchestrator/updates: testcases to verify reconcile is limited to configured facility
1 parent b0db5ce commit d663126

File tree

1 file changed

+149
-28
lines changed

1 file changed

+149
-28
lines changed

internal/orchestrator/updates_test.go

Lines changed: 149 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -184,41 +184,162 @@ func TestActiveConditionsToReconcile_Creates(t *testing.T) {
184184
}
185185
o.repository = repository
186186

187-
// init clean status KV
187+
type testConditionSV struct {
188+
conditionID uuid.UUID
189+
statusValue *rctypes.StatusValue
190+
facilityCode string
191+
}
192+
193+
// conditions to be created
194+
sid := uuid.New()
195+
cid := uuid.New()
196+
197+
testcases := []struct {
198+
name string
199+
// condition statusValues to be written to the status KV
200+
conditionStatusValues []*testConditionSV
201+
// conditions to be written as condition records to the active-conditions KV
202+
conditions []*rctypes.Condition
203+
wantCreates int
204+
wantUpdates int
205+
cleanActiveConditionsKV bool
206+
cleanStatusKV bool
207+
}{
208+
{
209+
// two statusValue KV records created, with no record in active-conditions KV
210+
// expect two create objects returned
211+
name: "test creates",
212+
conditionStatusValues: []*testConditionSV{
213+
{
214+
conditionID: uuid.New(),
215+
statusValue: &rctypes.StatusValue{
216+
Target: uuid.New().String(),
217+
State: string(rctypes.Active),
218+
Status: json.RawMessage(fmt.Sprintf(`{"msg":"foo", "facility": "%s"}`, o.facility)),
219+
WorkerID: registry.GetID("test1").String(),
220+
CreatedAt: time.Now().Add(-20 * time.Minute),
221+
},
222+
facilityCode: o.facility,
223+
},
224+
{
225+
conditionID: uuid.New(),
226+
statusValue: &rctypes.StatusValue{
227+
Target: uuid.New().String(),
228+
State: string(rctypes.Pending),
229+
Status: json.RawMessage(fmt.Sprintf(`{"msg":"foo", "facility": "%s"}`, o.facility)),
230+
WorkerID: registry.GetID("test2").String(),
231+
CreatedAt: time.Now().Add(-20 * time.Minute),
232+
},
233+
facilityCode: o.facility,
234+
},
235+
},
236+
wantCreates: 2,
237+
wantUpdates: 0,
238+
cleanActiveConditionsKV: true,
239+
cleanStatusKV: true,
240+
},
241+
{
242+
// two statusValue KV records created, with no record in active-conditions KV,
243+
// one statusValue is created in a different facility KV,
244+
// expect one create object returned
245+
name: "test creates - only on assigned facility",
246+
conditionStatusValues: []*testConditionSV{
247+
{
248+
conditionID: uuid.New(),
249+
statusValue: &rctypes.StatusValue{
250+
Target: uuid.New().String(),
251+
State: string(rctypes.Active),
252+
Status: json.RawMessage(fmt.Sprintf(`{"msg":"foo", "facility": "%s"}`, o.facility)),
253+
WorkerID: registry.GetID("test1").String(),
254+
CreatedAt: time.Now().Add(-20 * time.Minute),
255+
},
256+
facilityCode: o.facility,
257+
},
258+
{
259+
conditionID: uuid.New(),
260+
statusValue: &rctypes.StatusValue{
261+
Target: uuid.New().String(),
262+
State: string(rctypes.Pending),
263+
Status: json.RawMessage(fmt.Sprintf(`{"msg":"foo", "facility": "%s"}`, "rando13")),
264+
WorkerID: registry.GetID("test2").String(),
265+
CreatedAt: time.Now().Add(-20 * time.Minute),
266+
},
267+
facilityCode: "rando13",
268+
},
269+
},
270+
wantCreates: 1,
271+
wantUpdates: 0,
272+
cleanActiveConditionsKV: true,
273+
cleanStatusKV: true,
274+
},
275+
{
276+
name: "test no creates",
277+
conditionStatusValues: []*testConditionSV{
278+
{
279+
conditionID: cid,
280+
statusValue: &rctypes.StatusValue{
281+
Target: sid.String(),
282+
State: string(rctypes.Pending),
283+
Status: json.RawMessage(fmt.Sprintf(`{"msg":"foo", "facility": "%s"}`, o.facility)),
284+
WorkerID: registry.GetID("test2").String(),
285+
CreatedAt: time.Now().Add(-20 * time.Minute),
286+
},
287+
facilityCode: o.facility,
288+
},
289+
},
290+
conditions: []*rctypes.Condition{
291+
{
292+
ID: cid,
293+
Kind: rctypes.FirmwareInstall,
294+
State: rctypes.Pending,
295+
Target: sid,
296+
CreatedAt: time.Now().Add(-20 * time.Minute),
297+
},
298+
},
299+
wantCreates: 0,
300+
wantUpdates: 0,
301+
cleanActiveConditionsKV: true,
302+
cleanStatusKV: true,
303+
},
304+
}
305+
188306
fwsKV := newCleanStatusKV(t, rctypes.FirmwareInstall)
189307
_ = newCleanActiveConditionsKV(t)
190308

191-
sid1 := uuid.New()
192-
cid1 := uuid.New()
193-
sv1 := rctypes.StatusValue{
194-
Target: sid1.String(),
195-
State: string(rctypes.Active),
196-
Status: json.RawMessage(`{"msg":"foo"}`),
197-
WorkerID: registry.GetID("test1").String(),
198-
CreatedAt: time.Now().Add(-20 * time.Minute),
199-
}
309+
for idx, tc := range testcases {
310+
t.Run(tc.name, func(t *testing.T) {
311+
// clean when its not the first testcase and the testcase requires a clean.
312+
if idx != 0 && tc.cleanStatusKV {
313+
fwsKV = newCleanStatusKV(t, rctypes.FirmwareInstall)
314+
}
200315

201-
_, err = fwsKV.Put(fmt.Sprintf("%s.%s", o.facility, cid1), sv1.MustBytes())
202-
require.NoError(t, err)
316+
// clean when its not the first testcase and the testcase requires a clean.
317+
if idx != 0 && tc.cleanActiveConditionsKV {
318+
_ = newCleanActiveConditionsKV(t)
319+
}
203320

204-
sid2 := uuid.New()
205-
cid2 := uuid.New()
206-
sv2 := rctypes.StatusValue{
207-
Target: sid2.String(),
208-
State: string(rctypes.Pending),
209-
Status: json.RawMessage(`{"msg":"foo"}`),
210-
WorkerID: registry.GetID("test2").String(),
211-
CreatedAt: time.Now().Add(-20 * time.Minute),
212-
}
213-
_, err = fwsKV.Put(fmt.Sprintf("%s.%s", o.facility, cid2), sv2.MustBytes())
214-
require.NoError(t, err)
321+
for _, condSV := range tc.conditionStatusValues {
322+
key := fmt.Sprintf("%s.%s", condSV.facilityCode, condSV.conditionID)
323+
_, err = fwsKV.Put(key, condSV.statusValue.MustBytes())
324+
require.NoError(t, err)
325+
}
215326

216-
// record in pending state
217-
ctx := context.Background()
218-
creates, updates := o.activeConditionsToReconcile(ctx)
219-
assert.Len(t, creates, 2, "creates")
220-
assert.Len(t, updates, 0, "updates")
327+
ctx := context.Background()
328+
for _, cond := range tc.conditions {
329+
err := o.repository.CreateMultiple(ctx, cond.Target, cond)
330+
require.NoError(t, err)
331+
}
332+
333+
gotCreates, gotUpdates := o.activeConditionsToReconcile(ctx)
334+
assert.Len(t, gotCreates, tc.wantCreates, "creates match")
335+
assert.Len(t, gotUpdates, tc.wantUpdates, "updates match")
221336

337+
// verify the method only reconciles conditions for the facility this orchestrator configured for.
338+
for _, create := range gotCreates {
339+
assert.Contains(t, string(create.Status), o.facility)
340+
}
341+
})
342+
}
222343
}
223344

224345
func TestActiveConditionsToReconcile_Updates(t *testing.T) {

0 commit comments

Comments
 (0)