|
| 1 | +import { |
| 2 | + buildSyntheticMatchServerLogEntries, |
| 3 | + deriveMatchServerBootDiagnostic, |
| 4 | +} from "./bootDiagnostics"; |
| 5 | + |
| 6 | +describe("bootDiagnostics", () => { |
| 7 | + it("maps FailedScheduling to a terminal failed state", () => { |
| 8 | + const diagnostic = deriveMatchServerBootDiagnostic({ |
| 9 | + podPhase: "Pending", |
| 10 | + events: [ |
| 11 | + { |
| 12 | + type: "Warning", |
| 13 | + reason: "FailedScheduling", |
| 14 | + message: "0/2 nodes are available", |
| 15 | + lastTimestamp: "2026-04-23T12:00:00.000Z", |
| 16 | + }, |
| 17 | + ], |
| 18 | + }); |
| 19 | + |
| 20 | + expect(diagnostic).toEqual({ |
| 21 | + status: "Failed", |
| 22 | + detail: "FailedScheduling: 0/2 nodes are available", |
| 23 | + terminal: true, |
| 24 | + }); |
| 25 | + }); |
| 26 | + |
| 27 | + it("maps pulling events to PullingImage", () => { |
| 28 | + const diagnostic = deriveMatchServerBootDiagnostic({ |
| 29 | + podPhase: "Pending", |
| 30 | + containerStatuses: [ |
| 31 | + { |
| 32 | + state: { |
| 33 | + waiting: { |
| 34 | + reason: "ContainerCreating", |
| 35 | + }, |
| 36 | + }, |
| 37 | + }, |
| 38 | + ], |
| 39 | + events: [ |
| 40 | + { |
| 41 | + reason: "Pulling", |
| 42 | + message: 'Pulling image "ghcr.io/5stackgg/game-server:latest"', |
| 43 | + lastTimestamp: "2026-04-23T12:01:00.000Z", |
| 44 | + }, |
| 45 | + ], |
| 46 | + }); |
| 47 | + |
| 48 | + expect(diagnostic.status).toBe("PullingImage"); |
| 49 | + expect(diagnostic.terminal).toBe(false); |
| 50 | + expect(diagnostic.detail).toContain("Pulling"); |
| 51 | + }); |
| 52 | + |
| 53 | + it("maps ContainerCreating without pull events to Booting", () => { |
| 54 | + const diagnostic = deriveMatchServerBootDiagnostic({ |
| 55 | + podPhase: "Pending", |
| 56 | + containerStatuses: [ |
| 57 | + { |
| 58 | + state: { |
| 59 | + waiting: { |
| 60 | + reason: "ContainerCreating", |
| 61 | + }, |
| 62 | + }, |
| 63 | + }, |
| 64 | + ], |
| 65 | + }); |
| 66 | + |
| 67 | + expect(diagnostic).toEqual({ |
| 68 | + status: "Booting", |
| 69 | + detail: "ContainerCreating", |
| 70 | + terminal: false, |
| 71 | + }); |
| 72 | + }); |
| 73 | + |
| 74 | + it("maps ImagePullBackOff to a terminal failed state", () => { |
| 75 | + const diagnostic = deriveMatchServerBootDiagnostic({ |
| 76 | + podPhase: "Pending", |
| 77 | + containerStatuses: [ |
| 78 | + { |
| 79 | + state: { |
| 80 | + waiting: { |
| 81 | + reason: "ImagePullBackOff", |
| 82 | + message: "Back-off pulling image", |
| 83 | + }, |
| 84 | + }, |
| 85 | + }, |
| 86 | + ], |
| 87 | + }); |
| 88 | + |
| 89 | + expect(diagnostic).toEqual({ |
| 90 | + status: "Failed", |
| 91 | + detail: "ImagePullBackOff: Back-off pulling image", |
| 92 | + terminal: true, |
| 93 | + }); |
| 94 | + }); |
| 95 | + |
| 96 | + it("maps CrashLoopBackOff to a terminal failed state", () => { |
| 97 | + const diagnostic = deriveMatchServerBootDiagnostic({ |
| 98 | + podPhase: "Pending", |
| 99 | + containerStatuses: [ |
| 100 | + { |
| 101 | + state: { |
| 102 | + waiting: { |
| 103 | + reason: "CrashLoopBackOff", |
| 104 | + message: "Back-off restarting failed container", |
| 105 | + }, |
| 106 | + }, |
| 107 | + }, |
| 108 | + ], |
| 109 | + }); |
| 110 | + |
| 111 | + expect(diagnostic).toEqual({ |
| 112 | + status: "Failed", |
| 113 | + detail: "CrashLoopBackOff: Back-off restarting failed container", |
| 114 | + terminal: true, |
| 115 | + }); |
| 116 | + }); |
| 117 | + |
| 118 | + it("maps running pods to WaitingForPing", () => { |
| 119 | + const diagnostic = deriveMatchServerBootDiagnostic({ |
| 120 | + podPhase: "Running", |
| 121 | + events: [ |
| 122 | + { |
| 123 | + reason: "Started", |
| 124 | + message: 'Started container "game-server"', |
| 125 | + lastTimestamp: "2026-04-23T12:02:00.000Z", |
| 126 | + }, |
| 127 | + ], |
| 128 | + }); |
| 129 | + |
| 130 | + expect(diagnostic).toEqual({ |
| 131 | + status: "WaitingForPing", |
| 132 | + detail: 'Started: Started container "game-server"', |
| 133 | + terminal: false, |
| 134 | + }); |
| 135 | + }); |
| 136 | + |
| 137 | + it("builds synthetic event logs from diagnostics when no events exist", () => { |
| 138 | + const logs = buildSyntheticMatchServerLogEntries({ |
| 139 | + diagnostic: { |
| 140 | + status: "Creating", |
| 141 | + detail: "Waiting for Kubernetes to create the match server pod.", |
| 142 | + terminal: false, |
| 143 | + }, |
| 144 | + podName: "m-test", |
| 145 | + nodeName: "node-1", |
| 146 | + }); |
| 147 | + |
| 148 | + expect(logs).toHaveLength(1); |
| 149 | + expect(logs[0].pod).toBe("m-test"); |
| 150 | + expect(logs[0].container).toBe("k8s-events"); |
| 151 | + expect(logs[0].log).toBe( |
| 152 | + "Waiting for Kubernetes to create the match server pod.", |
| 153 | + ); |
| 154 | + }); |
| 155 | +}); |
0 commit comments