|
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,125 @@ 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: [{ key: "dedicated", operator: "Equal", value: "pool", effect: "NoSchedule" }], |
| 228 | + }, |
| 229 | + }); |
| 230 | + }); |
| 231 | + |
| 232 | + it("should allow either half to be omitted", () => { |
| 233 | + expect( |
| 234 | + OrgPlacementOverrides.parse(JSON.stringify({ org_123: { nodeSelector: { pool: "a" } } })) |
| 235 | + ).toEqual({ org_123: { nodeSelector: { pool: "a" } } }); |
| 236 | + |
| 237 | + expect( |
| 238 | + OrgPlacementOverrides.parse(JSON.stringify({ org_123: { tolerations: "spot:NoExecute" } })) |
| 239 | + ).toEqual({ |
| 240 | + org_123: { tolerations: [{ key: "spot", operator: "Exists", effect: "NoExecute" }] }, |
| 241 | + }); |
| 242 | + |
| 243 | + expect(OrgPlacementOverrides.parse(JSON.stringify({ org_123: {} }))).toEqual({ org_123: {} }); |
| 244 | + }); |
| 245 | + |
| 246 | + it("should reject invalid JSON at startup rather than silently skipping the override", () => { |
| 247 | + for (const invalid of ["not json", "[]", '"org_123"', "{"]) { |
| 248 | + expect(OrgPlacementOverrides.safeParse(invalid).success).toBe(false); |
| 249 | + } |
| 250 | + }); |
| 251 | + |
| 252 | + it("should treat a blank or missing value as no overrides, like the sibling settings", () => { |
| 253 | + expect(OrgPlacementOverrides.parse(undefined)).toBeUndefined(); |
| 254 | + expect(OrgPlacementOverrides.parse("")).toBeUndefined(); |
| 255 | + expect(OrgPlacementOverrides.parse(" ")).toBeUndefined(); |
| 256 | + }); |
| 257 | + |
| 258 | + it("should accept tolerations as an array of entries, matching the Helm list shape", () => { |
| 259 | + expect( |
| 260 | + OrgPlacementOverrides.parse( |
| 261 | + JSON.stringify({ |
| 262 | + org_123: { tolerations: ["dedicated=pool:NoSchedule", "spot:NoExecute"] }, |
| 263 | + }) |
| 264 | + ) |
| 265 | + ).toEqual({ |
| 266 | + org_123: { |
| 267 | + tolerations: [ |
| 268 | + { key: "dedicated", operator: "Equal", value: "pool", effect: "NoSchedule" }, |
| 269 | + { key: "spot", operator: "Exists", effect: "NoExecute" }, |
| 270 | + ], |
| 271 | + }, |
| 272 | + }); |
| 273 | + }); |
| 274 | + |
| 275 | + it("should coerce scalar node selector values to strings, as Kubernetes labels are", () => { |
| 276 | + expect( |
| 277 | + OrgPlacementOverrides.parse( |
| 278 | + JSON.stringify({ org_123: { nodeSelector: { paid: true, replicas: 3 } } }) |
| 279 | + ) |
| 280 | + ).toEqual({ org_123: { nodeSelector: { paid: "true", replicas: "3" } } }); |
| 281 | + }); |
| 282 | + |
| 283 | + it("should trim whitespace around node selector keys and values", () => { |
| 284 | + expect( |
| 285 | + OrgPlacementOverrides.parse( |
| 286 | + JSON.stringify({ org_123: { nodeSelector: { " pool ": " a " } } }) |
| 287 | + ) |
| 288 | + ).toEqual({ org_123: { nodeSelector: { pool: "a" } } }); |
| 289 | + }); |
| 290 | + |
| 291 | + it("should reject blank or padded org keys, since the lookup is exact", () => { |
| 292 | + for (const key of [" ", " org_123", "org_123 "]) { |
| 293 | + expect(OrgPlacementOverrides.safeParse(JSON.stringify({ [key]: {} })).success).toBe(false); |
| 294 | + } |
| 295 | + }); |
| 296 | + |
| 297 | + it("should reject an empty node selector value instead of pinning the org to nothing", () => { |
| 298 | + for (const value of ["", " "]) { |
| 299 | + expect( |
| 300 | + OrgPlacementOverrides.safeParse( |
| 301 | + JSON.stringify({ org_123: { nodeSelector: { pool: value } } }) |
| 302 | + ).success |
| 303 | + ).toBe(false); |
| 304 | + } |
| 305 | + }); |
| 306 | + |
| 307 | + it("should reject an unknown field, so a typo cannot silently drop an override", () => { |
| 308 | + expect( |
| 309 | + OrgPlacementOverrides.safeParse( |
| 310 | + JSON.stringify({ org_123: { toleration: "dedicated=pool:NoSchedule" } }) |
| 311 | + ).success |
| 312 | + ).toBe(false); |
| 313 | + }); |
| 314 | + |
| 315 | + it("should reject a node selector key or value Kubernetes would reject", () => { |
| 316 | + for (const invalid of [ |
| 317 | + { org_123: { nodeSelector: { "bad key": "a" } } }, |
| 318 | + { org_123: { nodeSelector: { pool: "bad value" } } }, |
| 319 | + { org_123: { nodeSelector: { "a/b/c": "a" } } }, |
| 320 | + { org_123: { nodeSelector: { pool: "v".repeat(64) } } }, |
| 321 | + ]) { |
| 322 | + expect(OrgPlacementOverrides.safeParse(JSON.stringify(invalid)).success).toBe(false); |
| 323 | + } |
| 324 | + }); |
| 325 | + |
| 326 | + it("should reject an invalid toleration inside an override", () => { |
| 327 | + expect( |
| 328 | + OrgPlacementOverrides.safeParse( |
| 329 | + JSON.stringify({ org_123: { tolerations: "dedicated=pool:Nope" } }) |
| 330 | + ).success |
| 331 | + ).toBe(false); |
| 332 | + }); |
| 333 | +}); |
0 commit comments