From 2ff1308a6ee9e857b4ed81d74aefffd0027734cb Mon Sep 17 00:00:00 2001 From: Vsevolod Kukol Date: Fri, 11 Sep 2026 22:30:01 +0200 Subject: [PATCH] Validate E2E cleanup duration --- utils/cleanupDBs.js | 19 +++++++++++++++---- utils/cleanupDBs.test.js | 16 +++++++++++++++- 2 files changed, 30 insertions(+), 5 deletions(-) diff --git a/utils/cleanupDBs.js b/utils/cleanupDBs.js index e9c9fa7f0..1bdb1ef69 100644 --- a/utils/cleanupDBs.js +++ b/utils/cleanupDBs.js @@ -5,10 +5,21 @@ const ms = require("ms"); const subscriptionId = process.env["AZURE_SUBSCRIPTION_ID"]; const resourceGroupName = process.env["E2ETESTS_RESOURCEGROUP_NAME"]; -const cleanupMinimumAge = ms(process.env["E2E_CLEANUP_MINIMUM_AGE"] || "6h"); -if (!cleanupMinimumAge) { - throw new Error("E2E_CLEANUP_MINIMUM_AGE must be a valid duration"); +function parseCleanupMinimumAge(configuredAge) { + let parsedAge; + try { + parsedAge = ms(configuredAge === undefined ? "6h" : configuredAge); + } catch { + parsedAge = undefined; + } + + if (!Number.isFinite(parsedAge) || parsedAge <= 0) { + throw new Error("E2E_CLEANUP_MINIMUM_AGE must be a positive duration"); + } + + return parsedAge; } +const cleanupMinimumAge = parseCleanupMinimumAge(process.env["E2E_CLEANUP_MINIMUM_AGE"]); const cleanupThreshold = Date.now() - cleanupMinimumAge; function shouldDeleteResource(name, timestamp, threshold = cleanupThreshold) { @@ -150,4 +161,4 @@ if (require.main === module) { }); } -module.exports = { shouldDeleteResource }; +module.exports = { parseCleanupMinimumAge, shouldDeleteResource }; diff --git a/utils/cleanupDBs.test.js b/utils/cleanupDBs.test.js index 1d6662e48..e0dc4c4d8 100644 --- a/utils/cleanupDBs.test.js +++ b/utils/cleanupDBs.test.js @@ -1,9 +1,23 @@ const assert = require("node:assert/strict"); const test = require("node:test"); -const { shouldDeleteResource } = require("./cleanupDBs"); +const { parseCleanupMinimumAge, shouldDeleteResource } = require("./cleanupDBs"); const cleanupThreshold = Date.now(); +test("uses a six-hour cleanup age when no value is configured", () => { + assert.equal(parseCleanupMinimumAge(undefined), 6 * 60 * 60 * 1000); +}); + +test("accepts a finite positive cleanup age", () => { + assert.equal(parseCleanupMinimumAge("12h"), 12 * 60 * 60 * 1000); +}); + +test("rejects unsafe cleanup ages", () => { + for (const configuredAge of ["", "invalid", "0ms", "-1h", "Infinity"]) { + assert.throws(() => parseCleanupMinimumAge(configuredAge), /E2E_CLEANUP_MINIMUM_AGE must be a positive duration/); + } +}); + test("deletes owned test resources older than the threshold", () => { assert.equal(shouldDeleteResource("t_12345_1_dbab_1000", cleanupThreshold - 1, cleanupThreshold), true); });