Repository navigation
Expand file tree
/
Copy pathmachines.test.ts
More file actions
1278 lines (1200 loc) · 46.7 KB
/
Copy pathmachines.test.ts
File metadata and controls
1278 lines (1200 loc) · 46.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import { describe, expect, test } from "vitest";
import { conditions } from "../../test/support/conditions.ts";
import {
address,
bootEvent,
checkpoint,
effectsOf,
emptyLab,
hasFailed,
hasHalted,
hasStopped,
IMAGES,
isReady,
isRestored,
isSaved,
labWith,
linkEvent,
machine,
named,
power,
remove,
restoreEvent,
run,
type SpawnOptions,
spawnEvent,
spawnRequest,
switchNamed,
WIDE,
withSwitches,
} from "../../test/support/model.ts";
import { port0 } from "../../test/support/ports.ts";
import { nameOf } from "../net/index.ts";
import { identity } from "./identity.ts";
import { memoryGiven, nextHues, planBoot, planSpawn } from "./machines.ts";
import { addressText } from "./selectors.ts";
import { ACCEPTS, hostsFor, isPlanError, lowestFreePorts, machineList, nameBook, takenPorts } from "./topology.ts";
import {
BOOT_TIMEOUT_MS,
type BridgePriority,
type CommandEvent,
type LabEvent,
type LabState,
type MachineName,
type MachineSpec,
type NicSpec,
type PathCost,
type Phase,
type RuntimeEvent,
type VlanId,
} from "./types.ts";
import { initialState, update } from "./update.ts";
import { planLink } from "./wiring.ts";
import { restoreWorkspace, saveWorkspace } from "./workspace.ts";
const ALPINE = spawnRequest();
const UNPLUGGED = spawnRequest({ net: "none" });
const alpineAt = (text: string, net: "switch0" | "none" = "switch0"): MachineSpec => ({
image: "alpine",
memMiB: 256,
disks: { root: "4", extra: [] },
gateway: true,
nics: [{ net, address: address(text) }],
});
const reloaded = (s: LabState): LabState => restoreWorkspace(initialState(IMAGES, WIDE), saveWorkspace(s)).state;
/** Both are booting, and node1 has a checkpoint in its set 1. */
const CHECKPOINTED = reloaded(run(labWith(2), checkpoint("node1", "before"), isSaved("node1", 1)));
/** A lab whose node1 is in `phase` on boot attempt `attempt`, beside a ready node2. */
function labIn(phase: Phase, attempt: 1 | 2): LabState {
let s = run(CHECKPOINTED, isReady("node2"));
if (phase === "restoring") {
s = run(s, isReady("node1"));
s = run(s, restoreEvent(s, "node1", 1));
// One that a reload cut short goes on on the first attempt.
return attempt === 2 ? s : run(reloaded(s), isReady("node2"));
}
if (attempt === 2) {
s = run(s, hasFailed("node1", 1));
s = run(s, bootEvent(s, "node1", 2000));
}
if (phase === "booting") return s;
if (phase === "failed") return run(s, hasFailed("node1", attempt));
s = run(s, isReady("node1", attempt));
if (phase === "ready") return s;
if (phase === "removing") return run(s, remove("node1"));
s = run(s, power("off", "node1"));
return phase === "stopping" ? s : run(s, hasStopped("node1", attempt));
}
type Column = (RuntimeEvent | CommandEvent)["type"] | "tick past the boot timeout";
/**
* `ignored`: the same state object and no effects.
* `untouched`: the machine's lifecycle is the same object, whatever else moved.
* A phase: the machine is in it afterwards, by this event's doing.
*/
type Outcome = "ignored" | "untouched" | Phase | "gone";
const TABLE: Readonly<Record<Phase, Readonly<Record<Column, Outcome>>>> = {
booting: {
"vm/stage": "booting",
"vm/ready": "ready",
"vm/failed": "failed",
"vm/halted": "stopping",
"vm/nic-lost": "ignored",
"vm/stopped": "ignored",
"checkpoint/progress": "ignored",
"checkpoint/saved": "ignored",
"checkpoint/unsaved": "ignored",
"checkpoint/restored": "ignored",
"session/opened": "ignored",
"session/ended": "ignored",
"session/shell": "untouched",
tick: "untouched",
"tick past the boot timeout": "failed",
"cmd/spawn": "untouched",
"cmd/remove": "removing",
"cmd/switch-add": "untouched",
"cmd/switch-remove": "ignored",
"cmd/boot": "ignored",
"cmd/power": "ignored",
"cmd/checkpoint": "ignored",
"cmd/restore": "ignored",
"cmd/forget": "untouched",
"cmd/link": "ignored",
"cmd/plug": "ignored",
"cmd/cable": "untouched",
"cmd/uncable": "ignored",
"cmd/cable-link": "ignored",
"cmd/vlan": "ignored",
"cmd/conditions": "booting",
"switch/storm": "untouched",
"cmd/stp": "untouched",
"cmd/priority": "untouched",
"cmd/cost": "untouched",
"switch/tree": "ignored",
"cmd/shell": "ignored",
"cmd/focus": "untouched",
"cmd/capture": "untouched",
"cmd/logged": "untouched",
},
ready: {
"vm/stage": "ignored",
"vm/ready": "ignored",
"vm/failed": "failed",
"vm/halted": "stopping",
"vm/nic-lost": "ready",
"vm/stopped": "ignored",
"checkpoint/progress": "ignored",
"checkpoint/saved": "ignored",
"checkpoint/unsaved": "ignored",
"checkpoint/restored": "ignored",
"session/opened": "ignored",
"session/ended": "ignored",
"session/shell": "untouched",
tick: "untouched",
"tick past the boot timeout": "untouched",
"cmd/spawn": "untouched",
"cmd/remove": "removing",
"cmd/switch-add": "untouched",
"cmd/switch-remove": "ignored",
"cmd/boot": "ignored",
"cmd/power": "stopping",
"cmd/checkpoint": "ready",
"cmd/restore": "restoring",
"cmd/forget": "untouched",
"cmd/link": "ready",
"cmd/plug": "ready",
"cmd/cable": "untouched",
"cmd/uncable": "ignored",
"cmd/cable-link": "ignored",
"cmd/vlan": "ignored",
"cmd/conditions": "ready",
"switch/storm": "untouched",
"cmd/stp": "untouched",
"cmd/priority": "untouched",
"cmd/cost": "untouched",
"switch/tree": "ignored",
"cmd/shell": "ready",
"cmd/focus": "untouched",
"cmd/capture": "untouched",
"cmd/logged": "untouched",
},
stopping: {
"vm/stage": "ignored",
"vm/ready": "ignored",
"vm/failed": "failed",
"vm/halted": "ignored",
"vm/nic-lost": "ignored",
"vm/stopped": "off",
"checkpoint/progress": "ignored",
"checkpoint/saved": "ignored",
"checkpoint/unsaved": "ignored",
"checkpoint/restored": "ignored",
"session/opened": "ignored",
"session/ended": "ignored",
"session/shell": "untouched",
tick: "untouched",
"tick past the boot timeout": "untouched",
"cmd/spawn": "untouched",
"cmd/remove": "removing",
"cmd/switch-add": "untouched",
"cmd/switch-remove": "ignored",
"cmd/boot": "ignored",
"cmd/power": "ignored",
"cmd/checkpoint": "ignored",
"cmd/restore": "ignored",
"cmd/forget": "untouched",
"cmd/link": "ignored",
"cmd/plug": "ignored",
"cmd/cable": "untouched",
"cmd/uncable": "ignored",
"cmd/cable-link": "ignored",
"cmd/vlan": "ignored",
"cmd/conditions": "stopping",
"switch/storm": "untouched",
"cmd/stp": "untouched",
"cmd/priority": "untouched",
"cmd/cost": "untouched",
"switch/tree": "ignored",
"cmd/shell": "ignored",
"cmd/focus": "untouched",
"cmd/capture": "untouched",
"cmd/logged": "untouched",
},
off: {
"vm/stage": "ignored",
"vm/ready": "ignored",
"vm/failed": "ignored",
"vm/halted": "ignored",
"vm/nic-lost": "ignored",
"vm/stopped": "ignored",
"checkpoint/progress": "ignored",
"checkpoint/saved": "ignored",
"checkpoint/unsaved": "ignored",
"checkpoint/restored": "ignored",
"session/opened": "ignored",
"session/ended": "ignored",
"session/shell": "untouched",
tick: "untouched",
"tick past the boot timeout": "untouched",
"cmd/spawn": "untouched",
"cmd/remove": "removing",
"cmd/switch-add": "untouched",
"cmd/switch-remove": "ignored",
"cmd/boot": "booting",
"cmd/power": "ignored",
"cmd/checkpoint": "off",
"cmd/restore": "restoring",
"cmd/forget": "untouched",
"cmd/link": "off",
"cmd/plug": "off",
"cmd/cable": "untouched",
"cmd/uncable": "ignored",
"cmd/cable-link": "ignored",
"cmd/vlan": "ignored",
"cmd/conditions": "off",
"switch/storm": "untouched",
"cmd/stp": "untouched",
"cmd/priority": "untouched",
"cmd/cost": "untouched",
"switch/tree": "ignored",
"cmd/shell": "ignored",
"cmd/focus": "untouched",
"cmd/capture": "untouched",
"cmd/logged": "untouched",
},
restoring: {
"vm/stage": "ignored",
"vm/ready": "ignored",
"vm/failed": "failed",
"vm/halted": "ignored",
"vm/nic-lost": "ignored",
"vm/stopped": "ignored",
"checkpoint/progress": "restoring",
"checkpoint/saved": "ignored",
"checkpoint/unsaved": "ignored",
"checkpoint/restored": "booting",
"session/opened": "ignored",
"session/ended": "ignored",
"session/shell": "untouched",
tick: "untouched",
"tick past the boot timeout": "untouched",
"cmd/spawn": "untouched",
"cmd/remove": "removing",
"cmd/switch-add": "untouched",
"cmd/switch-remove": "ignored",
"cmd/boot": "ignored",
"cmd/power": "ignored",
"cmd/checkpoint": "ignored",
"cmd/restore": "ignored",
"cmd/forget": "ignored",
"cmd/link": "ignored",
"cmd/plug": "ignored",
"cmd/cable": "untouched",
"cmd/uncable": "ignored",
"cmd/cable-link": "ignored",
"cmd/vlan": "ignored",
"cmd/conditions": "restoring",
"switch/storm": "untouched",
"cmd/stp": "untouched",
"cmd/priority": "untouched",
"cmd/cost": "untouched",
"switch/tree": "ignored",
"cmd/shell": "ignored",
"cmd/focus": "untouched",
"cmd/capture": "untouched",
"cmd/logged": "untouched",
},
failed: {
"vm/stage": "ignored",
"vm/ready": "ignored",
"vm/failed": "ignored",
"vm/halted": "ignored",
"vm/nic-lost": "ignored",
"vm/stopped": "ignored",
"checkpoint/progress": "ignored",
"checkpoint/saved": "ignored",
"checkpoint/unsaved": "ignored",
"checkpoint/restored": "ignored",
"session/opened": "ignored",
"session/ended": "ignored",
"session/shell": "untouched",
tick: "untouched",
"tick past the boot timeout": "untouched",
"cmd/spawn": "untouched",
"cmd/remove": "removing",
"cmd/switch-add": "untouched",
"cmd/switch-remove": "ignored",
"cmd/boot": "booting",
"cmd/power": "ignored",
"cmd/checkpoint": "ignored",
"cmd/restore": "restoring",
"cmd/forget": "untouched",
"cmd/link": "ignored",
"cmd/plug": "ignored",
"cmd/cable": "untouched",
"cmd/uncable": "ignored",
"cmd/cable-link": "ignored",
"cmd/vlan": "ignored",
"cmd/conditions": "ignored",
"switch/storm": "untouched",
"cmd/stp": "untouched",
"cmd/priority": "untouched",
"cmd/cost": "untouched",
"switch/tree": "ignored",
"cmd/shell": "ignored",
"cmd/focus": "untouched",
"cmd/capture": "untouched",
"cmd/logged": "untouched",
},
removing: {
"vm/stage": "ignored",
"vm/ready": "ignored",
"vm/failed": "ignored",
"vm/halted": "ignored",
"vm/nic-lost": "ignored",
"vm/stopped": "gone",
"checkpoint/progress": "ignored",
"checkpoint/saved": "ignored",
"checkpoint/unsaved": "ignored",
"checkpoint/restored": "ignored",
"session/opened": "ignored",
"session/ended": "ignored",
"session/shell": "untouched",
tick: "untouched",
"tick past the boot timeout": "untouched",
"cmd/spawn": "untouched",
"cmd/remove": "ignored",
"cmd/switch-add": "untouched",
"cmd/switch-remove": "ignored",
"cmd/boot": "ignored",
"cmd/power": "ignored",
"cmd/checkpoint": "ignored",
"cmd/restore": "ignored",
"cmd/forget": "ignored",
"cmd/link": "ignored",
"cmd/plug": "ignored",
"cmd/cable": "untouched",
"cmd/uncable": "ignored",
"cmd/cable-link": "ignored",
"cmd/vlan": "ignored",
"cmd/conditions": "ignored",
"switch/storm": "untouched",
"cmd/stp": "untouched",
"cmd/priority": "untouched",
"cmd/cost": "untouched",
"switch/tree": "ignored",
"cmd/shell": "ignored",
"cmd/focus": "untouched",
"cmd/capture": "untouched",
"cmd/logged": "untouched",
},
};
/** The last set the machine was given the number of. */
const newest = (s: LabState, name: MachineName): number => (s.machines[name]?.sets.next ?? 0) - 1;
/** One concrete event per column, aimed at `name` on boot attempt `attempt`. */
function eventFor(column: Column, s: LabState, name: MachineName, attempt: number): LabEvent {
switch (column) {
case "vm/stage":
return { type: "vm/stage", machine: name, attempt, stage: "starting the kernel" };
case "vm/ready":
return isReady(name, attempt);
case "vm/failed":
return hasFailed(name, attempt);
case "vm/halted":
return hasHalted(name, "reboot", attempt);
case "vm/nic-lost":
return { type: "vm/nic-lost", machine: name, attempt, nic: 0, reason: "eth0 stopped: signal 9." };
case "vm/stopped":
return hasStopped(name, attempt);
case "checkpoint/progress":
return { type: "checkpoint/progress", machine: name, attempt, set: newest(s, name), percent: 40 };
case "checkpoint/saved":
return isSaved(name, 2, attempt);
case "checkpoint/unsaved":
return { type: "checkpoint/unsaved", machine: name, attempt, set: 2, reason: "Browser storage is full." };
case "checkpoint/restored":
return isRestored(name, newest(s, name), attempt);
case "cmd/checkpoint":
return checkpoint(name, "after");
case "cmd/restore":
return { type: "cmd/restore", plan: { machine: name, set: 1, ports: [port0(1)] } };
case "cmd/forget":
return { type: "cmd/forget", machine: name, set: 1 };
case "session/opened":
return { type: "session/opened", session: `${name}:console` };
case "session/ended":
return { type: "session/ended", session: `${name}:console` };
case "session/shell":
return { type: "session/shell", session: `${name}:console`, shell: "running" };
case "tick":
return { type: "tick", now: s.now + 100 };
case "tick past the boot timeout":
return { type: "tick", now: s.now + BOOT_TIMEOUT_MS };
case "cmd/spawn":
return spawnEvent(s, 1, { net: "none" });
case "cmd/remove":
return remove(name);
case "cmd/switch-add":
return { type: "cmd/switch-add", plan: { name: switchNamed("lan") } };
case "cmd/switch-remove":
return { type: "cmd/switch-remove", switch: "switch0", cause: "bar" };
case "cmd/boot":
return bootEvent(s, name);
case "cmd/power":
return power("off", name);
case "cmd/link":
return {
type: "cmd/link",
changes: [{ machine: name, nic: 0, up: false, port: port0(1), newPort: false }],
cause: "bar",
};
case "cmd/plug":
return { type: "cmd/plug", plan: { machine: name, nic: 0, to: port0(5) }, cause: "bar" };
case "cmd/cable":
return { type: "cmd/cable", plan: { ends: [port0(7), port0(8)] }, cause: "bar" };
case "cmd/uncable":
return { type: "cmd/uncable", port: port0(8), cause: "bar" };
case "cmd/cable-link":
return { type: "cmd/cable-link", changes: [{ port: port0(8), up: false }], cause: "bar" };
case "cmd/vlan":
return { type: "cmd/vlan", port: port0(8), vlan: { mode: "access", vlan: 10 as VlanId } };
case "cmd/conditions":
return { type: "cmd/conditions", changes: [{ port: port0(1), conditions: conditions({ delay: 100 }) }] };
case "switch/storm":
return { type: "switch/storm", switch: "switch0", storm: { since: s.now, rate: 100, dropped: 0 } };
case "cmd/stp":
return { type: "cmd/stp", switches: ["switch0"], on: true };
case "cmd/priority":
return { type: "cmd/priority", switches: ["switch0"], priority: 4096 as BridgePriority };
case "cmd/cost":
return { type: "cmd/cost", port: { switch: "switch0", port: 1 }, cost: 2000 as PathCost };
case "switch/tree": {
// Of a switch that takes no part, so it is not kept.
const bridge = { priority: 32768, address: "02:00:00:00:00:01" };
return { type: "switch/tree", switch: "switch0", tree: { bridge, root: bridge, cost: 0, ports: [] } };
}
case "cmd/shell":
return { type: "cmd/shell", machine: name };
case "cmd/focus":
return { type: "cmd/focus", target: name, mode: "plain" };
case "cmd/capture":
return { type: "cmd/capture", filter: "arp" };
case "cmd/logged":
return { type: "cmd/logged", line: "ls", source: "bar", at: s.now, outcome: "ok", window: null };
}
}
const PHASES: readonly Phase[] = ["booting", "ready", "stopping", "off", "restoring", "failed", "removing"];
const ATTEMPTS = [1, 2] as const;
const COLUMNS = Object.keys(TABLE.booting).filter((key): key is Column => key in TABLE.booting);
const VM_EVENTS = [
"vm/stage",
"vm/ready",
"vm/failed",
"vm/halted",
"vm/nic-lost",
"vm/stopped",
"checkpoint/progress",
"checkpoint/saved",
"checkpoint/unsaved",
"checkpoint/restored",
] as const;
/** Every event that names a machine or one of its sessions. */
const ADDRESSED = COLUMNS.filter(
(column) =>
![
"tick",
"tick past the boot timeout",
"cmd/spawn",
"cmd/switch-add",
"cmd/cable",
"cmd/uncable",
"cmd/cable-link",
"cmd/vlan",
"cmd/conditions",
"switch/storm",
"cmd/stp",
"cmd/priority",
"cmd/cost",
"switch/tree",
"cmd/capture",
"cmd/logged",
].includes(column),
);
describe("the lifecycle table", () => {
const cells = PHASES.flatMap((phase) =>
ATTEMPTS.flatMap((attempt) => COLUMNS.map((column) => ({ phase, attempt, column, outcome: TABLE[phase][column] }))),
);
test.each(cells)("$phase (attempt $attempt) × $column → $outcome", ({ phase, attempt, column, outcome }) => {
const before = labIn(phase, attempt);
expect(machine(before, "node1").life).toMatchObject({ phase, attempt });
const step = update(before, eventFor(column, before, "node1", attempt));
const after = step.state.machines.node1;
if (outcome === "ignored") {
expect(step.state).toBe(before);
expect(step.effects).toEqual([]);
} else if (outcome === "untouched") {
expect(after?.life).toBe(machine(before, "node1").life);
} else if (outcome === "gone") {
expect(after).toBeUndefined();
expect(step.state.rail).not.toContain("node1");
} else {
expect(step.state).not.toBe(before);
expect(after?.life.phase).toBe(outcome);
}
expect(step.state.machines.node2).toBe(before.machines.node2);
});
const stale = PHASES.flatMap((phase) =>
ATTEMPTS.flatMap((attempt) =>
VM_EVENTS.flatMap((column) => [
{ phase, attempt, column, stamped: attempt - 1 },
{ phase, attempt, column, stamped: attempt + 1 },
]),
),
);
test.each(stale)("$phase (attempt $attempt) ignores $column stamped with attempt $stamped", (cell) => {
const before = labIn(cell.phase, cell.attempt);
const step = update(before, eventFor(cell.column, before, "node1", cell.stamped));
expect(step.state).toBe(before);
expect(step.effects).toEqual([]);
});
test.each(ADDRESSED)("%s for a machine that does not exist is ignored", (column) => {
const before = labWith(2);
const step = update(before, eventFor(column, before, "node99", 1));
expect(step.state).toBe(before);
expect(step.effects).toEqual([]);
});
test("what each phase accepts", () => {
expect(ACCEPTS).toEqual({
booting: { commands: false, boot: false, cables: false, remove: true, restore: false },
ready: { commands: true, boot: false, cables: true, remove: true, restore: true },
stopping: { commands: false, boot: false, cables: false, remove: true, restore: false },
off: { commands: false, boot: true, cables: true, remove: true, restore: true },
restoring: { commands: false, boot: false, cables: false, remove: true, restore: false },
failed: { commands: false, boot: true, cables: false, remove: true, restore: true },
removing: { commands: false, boot: false, cables: false, remove: false, restore: false },
});
});
test("a failed machine holds no port, and a removing one keeps its port until it is gone", () => {
let s = labWith(2);
expect([...takenPorts(s, "switch0")]).toEqual([1, 2]);
s = run(s, hasFailed("node1"));
expect([...takenPorts(s, "switch0")]).toEqual([2]);
s = run(s, remove("node2"));
expect(machine(s, "node2").life).toEqual({ phase: "removing", attempt: 1, ports: [port0(2)] });
expect([...takenPorts(s, "switch0")]).toEqual([2]);
s = run(s, hasStopped("node2"));
expect([...takenPorts(s, "switch0")]).toEqual([]);
});
test("a late message from the worker a boot again replaced is ignored", () => {
let s = labIn("failed", 1);
s = run(s, bootEvent(s, "node1"));
expect(machine(s, "node1").life).toMatchObject({ phase: "booting", attempt: 2 });
expect(update(s, isReady("node1", 1)).state).toBe(s);
expect(update(s, hasStopped("node1", 1)).state).toBe(s);
expect(update(s, hasFailed("node1", 1)).state).toBe(s);
});
});
describe("names", () => {
test("the number starts at 1 and only goes up", () => {
let s = emptyLab();
s = run(s, spawnEvent(s, 2));
expect(s.rail).toEqual(["node1", "node2"]);
s = run(s, spawnEvent(s, 1));
expect(s.rail).toEqual(["node1", "node2", "node3"]);
});
test("a removed name is not reused", () => {
let s = labWith(2);
s = run(s, remove("node2"), hasStopped("node2"));
s = run(s, spawnEvent(s, 1));
expect(s.rail).toEqual(["node1", "node3"]);
expect(machine(s, "node3")).toMatchObject(identity(3));
});
});
describe("hues", () => {
test("each machine takes the first hue not in use, in order", () => {
const s = labWith(3);
expect(s.rail.map((name) => machine(s, name).hue)).toEqual([1, 2, 3]);
expect(nextHues(s, 2)).toEqual([4, 5]);
});
test("a hue is held through failure and removal, and freed when the machine is gone", () => {
let s = labWith(3);
s = run(s, hasFailed("node1"), remove("node2"));
expect(nextHues(s, 1)).toEqual([4]);
s = run(s, hasStopped("node2"));
expect(nextHues(s, 2)).toEqual([2, 4]);
s = run(s, spawnEvent(s, 2));
expect(machine(s, "node4").hue).toBe(2);
expect(machine(s, "node5").hue).toBe(4);
});
test("boot again keeps the hue", () => {
let s = labWith(3);
s = run(s, hasFailed("node2"));
s = run(s, bootEvent(s, "node2"));
expect(machine(s, "node2").hue).toBe(2);
});
test("past eight machines, each takes the hue that the fewest have, so none is on more than two", () => {
let s = labWith(8);
expect(nextHues(s, 3)).toEqual([1, 2, 3]);
s = run(s, spawnEvent(s, 8, { net: "none" }));
const hues = () => s.rail.map((name) => machine(s, name).hue);
expect(hues()).toEqual([1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8]);
s = run(s, remove("node3", "node11", "node6"), hasStopped("node3"), hasStopped("node11"), hasStopped("node6"));
expect(nextHues(s, 3)).toEqual([3, 3, 6]);
s = run(s, spawnEvent(s, 3, { net: "none" }));
expect(hues().slice(-3)).toEqual([3, 3, 6]);
});
});
describe("the memory the lab has given", () => {
test("is what each machine that runs was given: one that is off, has failed or is being removed holds none", () => {
const given = (s: LabState) => memoryGiven(machineList(s));
let s = emptyLab();
expect(given(s)).toBe(0);
s = run(s, spawnEvent(s, 1, { memMiB: 512 }));
s = run(s, spawnEvent(s, 3, { net: "none" }));
expect(given(s)).toBe(1280);
s = run(s, ...s.rail.map((name) => isReady(name)));
expect(given(s)).toBe(1280);
s = run(s, power("off", "node1"));
expect(given(s)).toBe(1280);
s = run(s, hasStopped("node1"));
expect(given(s)).toBe(768);
s = run(s, hasFailed("node2"));
expect(given(s)).toBe(512);
s = run(s, remove("node3"));
expect(given(s)).toBe(256);
s = run(s, bootEvent(s, "node1"));
expect(given(s)).toBe(768);
});
});
describe("ports", () => {
test("a machine takes the lowest free port at spawn", () => {
const s = labWith(3);
expect(s.rail.map((name) => machine(s, name).life)).toMatchObject([
{ plugs: [{ kind: "port", port: port0(1), link: "up" }] },
{ plugs: [{ kind: "port", port: port0(2), link: "up" }] },
{ plugs: [{ kind: "port", port: port0(3), link: "up" }] },
]);
expect(lowestFreePorts(s, "switch0", 8)).toEqual([4, 5, 6, 7, 8]);
expect(lowestFreePorts(s, "switch0", 2)).toEqual([4, 5]);
});
test("a machine keeps its port while cut", () => {
let s = labWith(3);
s = run(s, linkEvent(s, false, "node2"));
expect(machine(s, "node2").life).toMatchObject({
phase: "ready",
plugs: [{ kind: "port", port: port0(2), link: "cut" }],
});
expect([...takenPorts(s, "switch0")]).toEqual([1, 2, 3]);
s = run(s, spawnEvent(s, 1));
expect(machine(s, "node4").life).toMatchObject({ plugs: [{ port: port0(4) }] });
s = run(s, linkEvent(s, true, "node2"));
expect(machine(s, "node2").life).toMatchObject({ plugs: [{ kind: "port", port: port0(2), link: "up" }] });
});
test("boot again takes the lowest free port, and keeps the name, hue, image and memory", () => {
let s = emptyLab();
s = run(s, spawnEvent(s, 3, { memMiB: 512, image: "debian" }), isReady("node1"), isReady("node3"));
s = run(s, hasFailed("node2"));
expect(machine(s, "node2").life).toEqual({ phase: "failed", attempt: 1, reason: "Kernel panic." });
s = run(s, spawnEvent(s, 1));
expect(machine(s, "node4").life).toMatchObject({ plugs: [{ port: port0(2) }] });
s = run(s, bootEvent(s, "node2", 9000));
expect(machine(s, "node2")).toMatchObject({
...identity(2),
hue: 2,
spec: { image: "debian", memMiB: 512, nics: [{ net: "switch0" }] },
life: { phase: "booting", attempt: 2, since: 9000, plugs: [{ kind: "port", port: port0(4), link: "up" }] },
});
});
test("a boot-again planned for a port someone has since taken is ignored", () => {
let s = labWith(2);
s = run(s, hasFailed("node1"));
const planned = bootEvent(s, "node1");
s = run(s, spawnEvent(s, 1));
expect(machine(s, "node3").life).toMatchObject({ plugs: [{ port: port0(1) }] });
expect(update(s, planned).state).toBe(s);
});
test("a machine spawned with --no-net has no port, and stays without one when it boots again", () => {
let s = emptyLab();
s = run(s, spawnEvent(s, 1, { net: "none" }));
expect(machine(s, "node1").life).toMatchObject({ plugs: [{ kind: "none" }] });
expect([...takenPorts(s, "switch0")]).toEqual([]);
s = run(s, hasFailed("node1"));
expect(planBoot(s, ["node1"])).toEqual([{ machine: "node1", ports: [null] }]);
s = run(s, bootEvent(s, "node1"));
expect(machine(s, "node1").life).toMatchObject({ phase: "booting", plugs: [{ kind: "none" }] });
});
test("link up gives an unplugged machine the lowest free port", () => {
let s = emptyLab();
s = run(s, spawnEvent(s, 1));
s = run(s, spawnEvent(s, 1, { net: "none" }));
s = run(s, spawnEvent(s, 1));
s = run(s, isReady("node1"), isReady("node2"), isReady("node3"));
expect(machine(s, "node3").life).toMatchObject({ plugs: [{ port: port0(2) }] });
expect(planLink(s, ["node2"], true)).toEqual([
{ machine: "node2", nic: 0, up: true, port: port0(3), newPort: true },
]);
s = run(s, remove("node1"), hasStopped("node1"));
expect(planLink(s, ["node2"], true)).toEqual([
{ machine: "node2", nic: 0, up: true, port: port0(1), newPort: true },
]);
s = run(s, linkEvent(s, true, "node2"));
expect(machine(s, "node2").life).toMatchObject({ plugs: [{ kind: "port", port: port0(1), link: "up" }] });
s = run(s, hasFailed("node2"), remove("node3"), hasStopped("node3"));
expect(planBoot(s, ["node2"])).toEqual([{ machine: "node2", ports: [port0(1)] }]);
});
});
describe("planSpawn", () => {
test("plans names, addresses, hues and ports before anything boots", () => {
const s = labWith(3);
expect(planSpawn(s, 2, ALPINE)).toEqual({
machines: [
{ identity: identity(4), hue: 4, ports: [port0(4)], spec: alpineAt("10.88.0.14/24") },
{ identity: identity(5), hue: 5, ports: [port0(5)], spec: alpineAt("10.88.0.15/24") },
],
request: ALPINE,
});
expect(planSpawn(s, 1, UNPLUGGED)).toEqual({
machines: [
{
identity: identity(4),
hue: 4,
ports: [null],
spec: { ...alpineAt("10.88.0.14/24", "none"), gateway: false },
},
],
request: UNPLUGGED,
});
});
test("asking for more machines than there is room for says how much room there is", () => {
let s = labWith(6);
s = run(s, spawnEvent(s, 8, { net: "none" }));
const short = { kind: "no-room", free: 2, wanted: 3, limit: "machines" };
expect(planSpawn(s, 3, ALPINE)).toEqual(short);
expect(planSpawn(s, 3, UNPLUGGED)).toEqual(short);
expect(planSpawn(s, 2, ALPINE)).toMatchObject({ machines: [{ ports: [port0(7)] }, { ports: [port0(8)] }] });
});
test("a lab holds sixteen machines, counting ones still being removed", () => {
let s = labWith(8);
s = run(s, spawnEvent(s, 8, { net: "none" }));
const full = { kind: "no-room", free: 0, wanted: 1, limit: "machines" };
expect(planSpawn(s, 1, UNPLUGGED)).toEqual(full);
s = run(s, remove("node3"));
expect(planSpawn(s, 1, UNPLUGGED)).toEqual(full);
s = run(s, hasStopped("node3"));
expect(planSpawn(s, 1, ALPINE)).toEqual({
machines: [{ identity: identity(17), hue: 3, ports: [port0(3)], spec: alpineAt("10.88.0.27/24") }],
request: ALPINE,
});
});
test("an image the lab does not have is an error", () => {
expect(planSpawn(emptyLab(), 1, { ...ALPINE, image: "plan9" })).toEqual({ kind: "unknown-image", image: "plan9" });
});
test("names are never reused, so a lab runs out of MACs after node244", () => {
let s = emptyLab();
for (let round = 0; round < 30; round++) {
s = run(s, spawnEvent(s, 8, { net: "none" }));
s = run(s, remove(...s.rail), ...s.rail.map((name) => hasStopped(name)));
}
expect(s.nextN).toBe(241);
expect(planSpawn(s, 5, ALPINE)).toEqual({ kind: "no-room", free: 4, wanted: 5, limit: "numbers" });
s = run(s, spawnEvent(s, 4));
expect(s.rail).toEqual(["node241", "node242", "node243", "node244"]);
expect(addressText(machine(s, "node244"))).toBe("10.88.0.254");
expect(planSpawn(s, 1, ALPINE)).toEqual({ kind: "no-room", free: 0, wanted: 1, limit: "numbers" });
});
test("a full lab that has given out its last number says that, since removing one would not help", () => {
let s = emptyLab();
for (let round = 0; round < 28; round++) {
s = run(s, spawnEvent(s, 8, { net: "none" }));
s = run(s, remove(...s.rail), ...s.rail.map((name) => hasStopped(name)));
}
s = run(s, spawnEvent(s, 4, { net: "none" }));
s = run(s, remove(...s.rail), ...s.rail.map((name) => hasStopped(name)));
s = run(s, spawnEvent(s, 16, { net: "none" }));
expect(s.rail).toHaveLength(16);
expect(s.rail.at(-1)).toBe("node244");
expect(planSpawn(s, 1, ALPINE)).toEqual({ kind: "no-room", free: 0, wanted: 1, limit: "numbers" });
});
test("isPlanError tells a plan from an error", () => {
expect(isPlanError(planSpawn(labWith(8), 1, ALPINE))).toBe(true);
expect(isPlanError(planSpawn(emptyLab(), 1, ALPINE))).toBe(false);
expect(isPlanError(planLink(labWith(1), ["node1"], false))).toBe(false);
expect(isPlanError(planBoot(labWith(1), ["node1"]))).toBe(false);
});
});
describe("planLink", () => {
test("plans only the cables that would change", () => {
let s = labWith(3);
s = run(s, linkEvent(s, false, "node2"));
expect(planLink(s, ["node1", "node2", "node3"], false)).toEqual([
{ machine: "node1", nic: 0, up: false, port: port0(1), newPort: false },
{ machine: "node3", nic: 0, up: false, port: port0(3), newPort: false },
]);
expect(planLink(s, ["node1", "node2", "node3"], true)).toEqual([
{ machine: "node2", nic: 0, up: true, port: port0(2), newPort: false },
]);
});
test("skips machines that are not ready or not there, and repeats", () => {
let s = labWith(1);
s = run(s, spawnEvent(s, 1));
expect(planLink(s, ["node1", "node1", "node2", "node7"], false)).toEqual([
{ machine: "node1", nic: 0, up: false, port: port0(1), newPort: false },
]);
});
test("unplugged machines take the lowest free ports in the order named", () => {
let s = emptyLab();
s = run(s, spawnEvent(s, 3, { net: "none" }));
s = run(s, ...s.rail.map((name) => isReady(name)));
expect(planLink(s, ["node3", "node1"], true)).toEqual([
{ machine: "node3", nic: 0, up: true, port: port0(1), newPort: true },
{ machine: "node1", nic: 0, up: true, port: port0(2), newPort: true },
]);
expect(planLink(s, ["node3"], false)).toEqual([]);
});
});
describe("planBoot", () => {
test("is only for a machine that is off or failed", () => {
const s = labWith(1);
expect(planBoot(s, ["node1"])).toEqual([]);
expect(planBoot(s, ["node9"])).toEqual([]);
});
test("plans the lowest free port", () => {
let s = labWith(3);
s = run(s, hasFailed("node1"), hasFailed("node3"));
expect(planBoot(s, ["node3"])).toEqual([{ machine: "node3", ports: [port0(1)] }]);
});
test("plans several at once: off machines hold their ports, and failed ones share out the rest", () => {
let s = labWith(4);
s = run(s, power("off", "node2"), hasStopped("node2"), hasFailed("node1"), hasFailed("node4"));
expect(planBoot(s, ["node4", "node3", "node2", "node1"])).toEqual([
{ machine: "node4", ports: [port0(1)] },
{ machine: "node2", ports: [port0(2)] },
{ machine: "node1", ports: [port0(4)] },
]);
});
});
describe("hostsFor", () => {
test("lists every machine in the lab, in spawn order", () => {
let s = labWith(2);
s = run(s, spawnEvent(s, 1));
expect(hostsFor(s)).toEqual([
{ ip: "10.88.0.11", name: "node1" },
{ ip: "10.88.0.12", name: "node2" },
{ ip: "10.88.0.13", name: "node3" },
]);
s = run(s, remove("node1"), hasStopped("node1"));
expect(hostsFor(s).map((h) => h.name)).toEqual(["node2", "node3"]);
});
});
describe("addresses", () => {
const plan = (s: LabState, options: SpawnOptions, count = 1) => planSpawn(s, count, spawnRequest(options));
const addresses = (s: LabState, name: MachineName) => machine(s, name).spec.nics.map((nic) => nic.address);
test("with no choice, eth0 has 10.88.0.(10+n) and the others none", () => {
const s = run(emptyLab(), spawnEvent(emptyLab(), 2, { net: ["switch0", "switch0"] }));
expect(addresses(s, "node1")).toEqual([address("10.88.0.11/24"), null]);
expect(addresses(s, "node2")).toEqual([address("10.88.0.12/24"), null]);
});
test("chosen ones are the machine's, and stay when it boots again", () => {
let s = emptyLab();
s = run(s, spawnEvent(s, 1, { net: ["switch0", "switch0"], addr: [null, "10.1.0.1/16"], gateway: false }));
expect(machine(s, "node1").spec).toMatchObject({
gateway: false,
nics: [{ address: null }, { address: address("10.1.0.1/16") }],
});
s = run(s, hasFailed("node1"));
const again = effectsOf(s, bootEvent(s, "node1")).find((e) => e.type === "vm/boot");
expect(again).toMatchObject({ config: { spec: machine(s, "node1").spec } });
});
test("a machine goes by its first one, and one with none is not in the host table", () => {
let s = emptyLab();
s = run(s, spawnEvent(s, 1, { net: ["switch0", "switch0"], addr: [null, "10.2.0.1/24"] }));
s = run(s, spawnEvent(s, 1, { addr: [null] }));
s = run(s, spawnEvent(s, 1, { net: ["switch0", "switch0"], addr: ["10.3.0.1/24", "10.2.0.3/24"] }));
expect(hostsFor(s)).toEqual([
{ ip: "10.2.0.1", name: "node1" },
{ ip: "10.3.0.1", name: "node3" },
]);
expect(s.rail.map((name) => addressText(machine(s, name)))).toEqual(["10.2.0.1", "no address", "10.3.0.1"]);
});
test("one another machine has is refused, until that machine is gone", () => {
let s = run(
emptyLab(),
spawnEvent(emptyLab(), 1, { net: ["switch0", "switch0"], addr: ["10.1.0.1/24", "10.2.0.1/24"] }),
);
const taken = { kind: "address-taken", address: address("10.2.0.1/16"), by: { machine: "node1", nic: 1 } };
expect(plan(s, { addr: ["10.2.0.1/16"] })).toEqual(taken);
s = run(s, hasFailed("node1"));
expect(plan(s, { addr: ["10.2.0.1/16"] })).toEqual(taken);
s = run(s, remove("node1"));
expect(plan(s, { addr: ["10.2.0.1/16"] })).toEqual(taken);
s = run(s, hasStopped("node1"));
expect(isPlanError(plan(s, { addr: ["10.2.0.1/16"] }))).toBe(false);
});
test("one the lab would give a later machine is refused to that machine", () => {
const s = run(emptyLab(), spawnEvent(emptyLab(), 1, { addr: ["10.88.0.13/24"] }));
expect(isPlanError(plan(s, {}, 1))).toBe(false);
expect(plan(s, {}, 2)).toMatchObject({ kind: "address-taken", by: { machine: "node1" } });
});
test.each([
["the same", "10.1.0.1/24", "10.1.0.1/24"],
["two in one", "10.1.0.1/24", "10.1.0.2/24"],
["a wide one around a narrow one", "10.1.0.1/16", "10.1.9.1/24"],
["a narrow one inside a wide one", "10.1.9.1/24", "10.1.0.1/16"],
])("a machine cannot have NICs in overlapping subnets: %s", (_what, eth0, eth2) => {
const request = { net: ["switch0", "switch0", "switch0"] as const, addr: [eth0, null, eth2] };
expect(plan(emptyLab(), request)).toEqual({ kind: "address-overlap", nics: [0, 2] });
});
test("neighbouring subnets do not overlap", () => {