|
| 1 | +import { spawnSync } from "node:child_process"; |
| 2 | +import { chmodSync, mkdirSync, readFileSync, writeFileSync } from "node:fs"; |
| 3 | +import path from "node:path"; |
| 4 | +import { afterEach, describe, expect, it } from "vitest"; |
| 5 | +import { useAutoCleanupTempDirTracker } from "../helpers/temp-dir.js"; |
| 6 | + |
| 7 | +const tempDirs = useAutoCleanupTempDirTracker(afterEach); |
| 8 | + |
| 9 | +function writeExecutable(filePath: string, contents: string): void { |
| 10 | + writeFileSync(filePath, contents); |
| 11 | + chmodSync(filePath, 0o755); |
| 12 | +} |
| 13 | + |
| 14 | +function runWithStubbedKubectl( |
| 15 | + args: string[], |
| 16 | + namespace: string, |
| 17 | + options: { |
| 18 | + deleteKustomizeStatus?: number; |
| 19 | + deleteNamespaceStatus?: number; |
| 20 | + deleteSecretStatus?: number; |
| 21 | + } = {}, |
| 22 | +) { |
| 23 | + const root = tempDirs.make("openclaw-k8s-delete-"); |
| 24 | + const binDir = path.join(root, "bin"); |
| 25 | + const logPath = path.join(root, "kubectl.log"); |
| 26 | + mkdirSync(binDir); |
| 27 | + |
| 28 | + writeExecutable( |
| 29 | + path.join(binDir, "kubectl"), |
| 30 | + `#!/usr/bin/env bash |
| 31 | +set -euo pipefail |
| 32 | +printf '%s\\n' "$*" >> "$OPENCLAW_KUBECTL_LOG" |
| 33 | +if [[ "$1" == "delete" && "$2" == "-k" ]]; then |
| 34 | + exit "\${OPENCLAW_KUBECTL_DELETE_KUSTOMIZE_STATUS:-0}" |
| 35 | +fi |
| 36 | +if [[ "$1" == "delete" && "$2" == "namespace" ]]; then |
| 37 | + exit "\${OPENCLAW_KUBECTL_DELETE_NAMESPACE_STATUS:-0}" |
| 38 | +fi |
| 39 | +if [[ "$1" == "delete" && "$2" == "secret" ]]; then |
| 40 | + exit "\${OPENCLAW_KUBECTL_DELETE_SECRET_STATUS:-0}" |
| 41 | +fi |
| 42 | +if [[ "$1" == "get" && "$2" == "namespace" ]]; then |
| 43 | + exit 99 |
| 44 | +fi |
| 45 | +case "$1" in |
| 46 | + cluster-info|delete) exit 0 ;; |
| 47 | + *) exit 99 ;; |
| 48 | +esac |
| 49 | +`, |
| 50 | + ); |
| 51 | + writeExecutable(path.join(binDir, "openssl"), "#!/usr/bin/env bash\nexit 0\n"); |
| 52 | + |
| 53 | + const result = spawnSync("bash", ["scripts/k8s/deploy.sh", ...args], { |
| 54 | + cwd: process.cwd(), |
| 55 | + encoding: "utf8", |
| 56 | + env: { |
| 57 | + ...process.env, |
| 58 | + OPENCLAW_KUBECTL_DELETE_KUSTOMIZE_STATUS: String(options.deleteKustomizeStatus ?? 0), |
| 59 | + OPENCLAW_KUBECTL_DELETE_NAMESPACE_STATUS: String(options.deleteNamespaceStatus ?? 0), |
| 60 | + OPENCLAW_KUBECTL_DELETE_SECRET_STATUS: String(options.deleteSecretStatus ?? 0), |
| 61 | + OPENCLAW_KUBECTL_LOG: logPath, |
| 62 | + OPENCLAW_NAMESPACE: namespace, |
| 63 | + PATH: `${binDir}:${process.env.PATH ?? ""}`, |
| 64 | + }, |
| 65 | + }); |
| 66 | + |
| 67 | + return { |
| 68 | + calls: readFileSync(logPath, "utf8").trim().split("\n"), |
| 69 | + output: `${result.stdout}\n${result.stderr}`, |
| 70 | + result, |
| 71 | + }; |
| 72 | +} |
| 73 | + |
| 74 | +describe("scripts/k8s/deploy.sh", () => { |
| 75 | + function runDeleteResourcesWithStubbedKubectl( |
| 76 | + namespace: string, |
| 77 | + options: { |
| 78 | + deleteKustomizeStatus?: number; |
| 79 | + } = {}, |
| 80 | + ) { |
| 81 | + return runWithStubbedKubectl(["--delete-resources"], namespace, options); |
| 82 | + } |
| 83 | + |
| 84 | + it("keeps the default namespace delete mode as a full namespace teardown", () => { |
| 85 | + const { calls, output, result } = runWithStubbedKubectl(["--delete"], "openclaw"); |
| 86 | + |
| 87 | + expect(result.status, output).toBe(0); |
| 88 | + expect(output).toContain("Deleting namespace 'openclaw' and all resources"); |
| 89 | + expect(calls).toEqual(["cluster-info", "delete namespace openclaw --ignore-not-found"]); |
| 90 | + }); |
| 91 | + |
| 92 | + it("keeps a custom namespace and unrelated workloads when the legacy delete mode is used", () => { |
| 93 | + const { calls, output, result } = runWithStubbedKubectl(["--delete"], "my-namespace"); |
| 94 | + |
| 95 | + expect(result.status, output).toBe(0); |
| 96 | + expect(output).toContain("Deleting OpenClaw resources from namespace 'my-namespace'"); |
| 97 | + expect(calls).toEqual([ |
| 98 | + "cluster-info", |
| 99 | + `delete -k ${path.resolve("scripts/k8s/manifests")} -n my-namespace --ignore-not-found`, |
| 100 | + "delete secret openclaw-secrets -n my-namespace --ignore-not-found", |
| 101 | + ]); |
| 102 | + expect(calls).not.toContain("delete namespace my-namespace --ignore-not-found"); |
| 103 | + expect(calls).not.toContain("get namespace my-namespace"); |
| 104 | + }); |
| 105 | + |
| 106 | + it("deletes OpenClaw resources without deleting the namespace", () => { |
| 107 | + const { calls, output, result } = runDeleteResourcesWithStubbedKubectl("my-namespace"); |
| 108 | + |
| 109 | + expect(result.status, output).toBe(0); |
| 110 | + expect(output).toContain("Deleting OpenClaw resources from namespace 'my-namespace'"); |
| 111 | + |
| 112 | + expect(calls).toEqual([ |
| 113 | + "cluster-info", |
| 114 | + `delete -k ${path.resolve("scripts/k8s/manifests")} -n my-namespace --ignore-not-found`, |
| 115 | + "delete secret openclaw-secrets -n my-namespace --ignore-not-found", |
| 116 | + ]); |
| 117 | + expect(calls).not.toContain("delete namespace my-namespace --ignore-not-found"); |
| 118 | + expect(calls).not.toContain("get namespace my-namespace"); |
| 119 | + }); |
| 120 | + |
| 121 | + it("surfaces resource delete failures instead of reporting teardown success", () => { |
| 122 | + const { calls, output, result } = runDeleteResourcesWithStubbedKubectl("restricted-namespace", { |
| 123 | + deleteKustomizeStatus: 17, |
| 124 | + }); |
| 125 | + |
| 126 | + expect(result.status, output).toBe(17); |
| 127 | + expect(output).toContain("Deleting OpenClaw resources from namespace 'restricted-namespace'"); |
| 128 | + expect(calls).toEqual([ |
| 129 | + "cluster-info", |
| 130 | + `delete -k ${path.resolve("scripts/k8s/manifests")} -n restricted-namespace --ignore-not-found`, |
| 131 | + ]); |
| 132 | + expect(output).not.toContain("Done."); |
| 133 | + }); |
| 134 | + |
| 135 | + it("stops custom namespace teardown when deleting managed manifests fails", () => { |
| 136 | + const { calls, output, result } = runWithStubbedKubectl(["--delete"], "shared-namespace", { |
| 137 | + deleteKustomizeStatus: 17, |
| 138 | + }); |
| 139 | + |
| 140 | + expect(result.status, output).toBe(17); |
| 141 | + expect(calls).toEqual([ |
| 142 | + "cluster-info", |
| 143 | + `delete -k ${path.resolve("scripts/k8s/manifests")} -n shared-namespace --ignore-not-found`, |
| 144 | + ]); |
| 145 | + expect(output).not.toContain("Done."); |
| 146 | + }); |
| 147 | + |
| 148 | + it.each(["--delete", "--delete-resources"])( |
| 149 | + "surfaces generated Secret deletion failures for %s", |
| 150 | + (mode) => { |
| 151 | + const { calls, output, result } = runWithStubbedKubectl([mode], "shared-namespace", { |
| 152 | + deleteSecretStatus: 23, |
| 153 | + }); |
| 154 | + |
| 155 | + expect(result.status, output).toBe(23); |
| 156 | + expect(calls).toEqual([ |
| 157 | + "cluster-info", |
| 158 | + `delete -k ${path.resolve("scripts/k8s/manifests")} -n shared-namespace --ignore-not-found`, |
| 159 | + "delete secret openclaw-secrets -n shared-namespace --ignore-not-found", |
| 160 | + ]); |
| 161 | + expect(output).not.toContain("Done."); |
| 162 | + }, |
| 163 | + ); |
| 164 | + |
| 165 | + it("surfaces namespace deletion failures instead of reporting teardown success", () => { |
| 166 | + const { calls, output, result } = runWithStubbedKubectl(["--delete-namespace"], "shared", { |
| 167 | + deleteNamespaceStatus: 29, |
| 168 | + }); |
| 169 | + |
| 170 | + expect(result.status, output).toBe(29); |
| 171 | + expect(calls).toEqual(["cluster-info", "delete namespace shared --ignore-not-found"]); |
| 172 | + expect(output).not.toContain("Done."); |
| 173 | + }); |
| 174 | + |
| 175 | + it("deletes the namespace only when the explicit namespace teardown mode is selected", () => { |
| 176 | + const { calls, output, result } = runWithStubbedKubectl( |
| 177 | + ["--delete-namespace"], |
| 178 | + "shared-namespace", |
| 179 | + ); |
| 180 | + |
| 181 | + expect(result.status, output).toBe(0); |
| 182 | + expect(output).toContain("Deleting namespace 'shared-namespace' and all resources"); |
| 183 | + expect(calls).toEqual(["cluster-info", "delete namespace shared-namespace --ignore-not-found"]); |
| 184 | + }); |
| 185 | +}); |
0 commit comments