|
1 | 1 | import { describe, it, expect } from "vitest"; |
2 | | -import { BoolEnv, AdditionalEnvVars, NodeLabelValue, Tolerations } from "./envUtil.js"; |
| 2 | +import { |
| 3 | + BoolEnv, |
| 4 | + AdditionalEnvVars, |
| 5 | + NodeLabelValue, |
| 6 | + OrgPlacementOverrides, |
| 7 | + Tolerations, |
| 8 | +} from "./envUtil.js"; |
3 | 9 |
|
4 | 10 | describe("BoolEnv", () => { |
5 | 11 | it("should parse string 'true' as true", () => { |
@@ -203,3 +209,72 @@ describe("Tolerations", () => { |
203 | 209 | expect(Tolerations.safeParse("dedicated=runs:NoSchedule:NoExecute").success).toBe(false); |
204 | 210 | }); |
205 | 211 | }); |
| 212 | + |
| 213 | +describe("OrgPlacementOverrides", () => { |
| 214 | + it("should parse a full override with nodeSelector and tolerations", () => { |
| 215 | + expect( |
| 216 | + OrgPlacementOverrides.parse( |
| 217 | + JSON.stringify({ |
| 218 | + org_123: { |
| 219 | + nodeSelector: { "node.cluster.x-k8s.io/machinepool": "dedicated-pool" }, |
| 220 | + tolerations: "dedicated=pool:NoSchedule", |
| 221 | + }, |
| 222 | + }) |
| 223 | + ) |
| 224 | + ).toEqual({ |
| 225 | + org_123: { |
| 226 | + nodeSelector: { "node.cluster.x-k8s.io/machinepool": "dedicated-pool" }, |
| 227 | + tolerations: [ |
| 228 | + { key: "dedicated", operator: "Equal", value: "pool", effect: "NoSchedule" }, |
| 229 | + ], |
| 230 | + }, |
| 231 | + }); |
| 232 | + }); |
| 233 | + |
| 234 | + it("should allow either half to be omitted", () => { |
| 235 | + expect( |
| 236 | + OrgPlacementOverrides.parse(JSON.stringify({ org_123: { nodeSelector: { pool: "a" } } })) |
| 237 | + ).toEqual({ org_123: { nodeSelector: { pool: "a" } } }); |
| 238 | + |
| 239 | + expect( |
| 240 | + OrgPlacementOverrides.parse(JSON.stringify({ org_123: { tolerations: "spot:NoExecute" } })) |
| 241 | + ).toEqual({ |
| 242 | + org_123: { tolerations: [{ key: "spot", operator: "Exists", effect: "NoExecute" }] }, |
| 243 | + }); |
| 244 | + |
| 245 | + expect(OrgPlacementOverrides.parse(JSON.stringify({ org_123: {} }))).toEqual({ org_123: {} }); |
| 246 | + }); |
| 247 | + |
| 248 | + it("should reject invalid JSON at startup rather than silently skipping the override", () => { |
| 249 | + for (const invalid of ["not json", "[]", '"org_123"', "{"]) { |
| 250 | + expect(OrgPlacementOverrides.safeParse(invalid).success).toBe(false); |
| 251 | + } |
| 252 | + }); |
| 253 | + |
| 254 | + it("should reject an unknown field, so a typo cannot silently drop an override", () => { |
| 255 | + expect( |
| 256 | + OrgPlacementOverrides.safeParse( |
| 257 | + JSON.stringify({ org_123: { toleration: "dedicated=pool:NoSchedule" } }) |
| 258 | + ).success |
| 259 | + ).toBe(false); |
| 260 | + }); |
| 261 | + |
| 262 | + it("should reject a node selector key or value Kubernetes would reject", () => { |
| 263 | + for (const invalid of [ |
| 264 | + { org_123: { nodeSelector: { "bad key": "a" } } }, |
| 265 | + { org_123: { nodeSelector: { pool: "bad value" } } }, |
| 266 | + { org_123: { nodeSelector: { "a/b/c": "a" } } }, |
| 267 | + { org_123: { nodeSelector: { pool: "v".repeat(64) } } }, |
| 268 | + ]) { |
| 269 | + expect(OrgPlacementOverrides.safeParse(JSON.stringify(invalid)).success).toBe(false); |
| 270 | + } |
| 271 | + }); |
| 272 | + |
| 273 | + it("should reject an invalid toleration inside an override", () => { |
| 274 | + expect( |
| 275 | + OrgPlacementOverrides.safeParse( |
| 276 | + JSON.stringify({ org_123: { tolerations: "dedicated=pool:Nope" } }) |
| 277 | + ).success |
| 278 | + ).toBe(false); |
| 279 | + }); |
| 280 | +}); |
0 commit comments