|
| 1 | +import { afterEach, describe, expect, it } from "vitest"; |
| 2 | +import fs from "fs"; |
| 3 | +import os from "os"; |
| 4 | +import path from "path"; |
| 5 | +import { spawnSync } from "child_process"; |
| 6 | +import { fileURLToPath } from "url"; |
| 7 | + |
| 8 | +const __dirname = path.dirname(fileURLToPath(import.meta.url)); |
| 9 | +const POST_SCRIPT_PATH = path.join(__dirname, "..", "post.js"); |
| 10 | +const CLEAN_SCRIPT_PATH = path.join(__dirname, "..", "clean.sh"); |
| 11 | + |
| 12 | +const tempDirs = []; |
| 13 | + |
| 14 | +function createTempDir(prefix) { |
| 15 | + const dir = fs.mkdtempSync(path.join(os.tmpdir(), prefix)); |
| 16 | + tempDirs.push(dir); |
| 17 | + return dir; |
| 18 | +} |
| 19 | + |
| 20 | +function createFakeSudoEnvironment() { |
| 21 | + const root = createTempDir("chroot-home-cleanup-"); |
| 22 | + const fakeBin = path.join(root, "fake-bin"); |
| 23 | + fs.mkdirSync(fakeBin, { recursive: true }); |
| 24 | + |
| 25 | + const logPath = path.join(root, "sudo.log"); |
| 26 | + const fakeSudoPath = path.join(fakeBin, "sudo"); |
| 27 | + fs.writeFileSync( |
| 28 | + fakeSudoPath, |
| 29 | + `#!/usr/bin/env bash |
| 30 | +set -euo pipefail |
| 31 | +echo "$*" >> "$FAKE_SUDO_LOG" |
| 32 | +if [ "$1" = "find" ]; then |
| 33 | + if printf '%s\\n' "$*" | grep -q -- '-print'; then |
| 34 | + printf '%b' "\${FAKE_FIND_PRINT_OUTPUT:-}" |
| 35 | + exit "\${FAKE_FIND_PRINT_STATUS:-0}" |
| 36 | + fi |
| 37 | + if printf '%s\\n' "$*" | grep -q -- '-exec'; then |
| 38 | + exit "\${FAKE_FIND_EXEC_STATUS:-0}" |
| 39 | + fi |
| 40 | +fi |
| 41 | +exit 0 |
| 42 | +`, |
| 43 | + { mode: 0o755 } |
| 44 | + ); |
| 45 | + |
| 46 | + return { |
| 47 | + fakeBin, |
| 48 | + logPath, |
| 49 | + root, |
| 50 | + }; |
| 51 | +} |
| 52 | + |
| 53 | +function runPostScript(env) { |
| 54 | + return spawnSync(process.execPath, [POST_SCRIPT_PATH], { |
| 55 | + encoding: "utf8", |
| 56 | + env: { ...process.env, ...env }, |
| 57 | + }); |
| 58 | +} |
| 59 | + |
| 60 | +function runCleanScript(env) { |
| 61 | + return spawnSync("bash", [CLEAN_SCRIPT_PATH], { |
| 62 | + encoding: "utf8", |
| 63 | + env: { ...process.env, ...env }, |
| 64 | + }); |
| 65 | +} |
| 66 | + |
| 67 | +afterEach(() => { |
| 68 | + while (tempDirs.length > 0) { |
| 69 | + const dir = tempDirs.pop(); |
| 70 | + if (dir && fs.existsSync(dir)) { |
| 71 | + fs.rmSync(dir, { recursive: true, force: true }); |
| 72 | + } |
| 73 | + } |
| 74 | +}); |
| 75 | + |
| 76 | +describe("post.js chroot-home cleanup", () => { |
| 77 | + it("logs that no directories were found when find output is empty", () => { |
| 78 | + const { fakeBin, logPath } = createFakeSudoEnvironment(); |
| 79 | + const result = runPostScript({ |
| 80 | + PATH: `${fakeBin}:${process.env.PATH}`, |
| 81 | + FAKE_SUDO_LOG: logPath, |
| 82 | + FAKE_FIND_PRINT_OUTPUT: "", |
| 83 | + }); |
| 84 | + |
| 85 | + expect(result.status).toBe(0); |
| 86 | + expect(result.stdout).toContain("No /tmp/awf-*-chroot-home directories found"); |
| 87 | + expect(fs.readFileSync(logPath, "utf8")).not.toContain("-exec rm -rf -- {} +"); |
| 88 | + }); |
| 89 | + |
| 90 | + it("logs count of cleaned chroot-home directories", () => { |
| 91 | + const { fakeBin, logPath } = createFakeSudoEnvironment(); |
| 92 | + const result = runPostScript({ |
| 93 | + PATH: `${fakeBin}:${process.env.PATH}`, |
| 94 | + FAKE_SUDO_LOG: logPath, |
| 95 | + FAKE_FIND_PRINT_OUTPUT: "/tmp/awf-1-chroot-home\n/tmp/awf-2-chroot-home\n", |
| 96 | + }); |
| 97 | + |
| 98 | + expect(result.status).toBe(0); |
| 99 | + expect(result.stdout).toContain("Cleaned up 2 /tmp/awf-*-chroot-home directories"); |
| 100 | + expect(fs.readFileSync(logPath, "utf8")).toContain("-exec rm -rf -- {} +"); |
| 101 | + }); |
| 102 | +}); |
| 103 | + |
| 104 | +describe("clean.sh chroot-home cleanup", () => { |
| 105 | + it("logs when no chroot-home directories are found", () => { |
| 106 | + const { fakeBin, logPath, root } = createFakeSudoEnvironment(); |
| 107 | + const destination = path.join(root, "destination"); |
| 108 | + fs.mkdirSync(destination, { recursive: true }); |
| 109 | + |
| 110 | + const result = runCleanScript({ |
| 111 | + PATH: `${fakeBin}:${process.env.PATH}`, |
| 112 | + FAKE_SUDO_LOG: logPath, |
| 113 | + FAKE_FIND_PRINT_OUTPUT: "", |
| 114 | + INPUT_DESTINATION: destination, |
| 115 | + }); |
| 116 | + |
| 117 | + expect(result.status).toBe(0); |
| 118 | + expect(result.stdout).toContain("No /tmp/awf-*-chroot-home directories found"); |
| 119 | + expect(fs.readFileSync(logPath, "utf8")).not.toContain("-exec rm -rf -- {} +"); |
| 120 | + }); |
| 121 | + |
| 122 | + it("logs successful cleanup when chroot-home directories are found", () => { |
| 123 | + const { fakeBin, logPath, root } = createFakeSudoEnvironment(); |
| 124 | + const destination = path.join(root, "destination"); |
| 125 | + fs.mkdirSync(destination, { recursive: true }); |
| 126 | + |
| 127 | + const result = runCleanScript({ |
| 128 | + PATH: `${fakeBin}:${process.env.PATH}`, |
| 129 | + FAKE_SUDO_LOG: logPath, |
| 130 | + FAKE_FIND_PRINT_OUTPUT: "/tmp/awf-1-chroot-home\n", |
| 131 | + INPUT_DESTINATION: destination, |
| 132 | + }); |
| 133 | + |
| 134 | + expect(result.status).toBe(0); |
| 135 | + expect(result.stdout).toContain("Cleaned up /tmp/awf-*-chroot-home directories (sudo)"); |
| 136 | + expect(fs.readFileSync(logPath, "utf8")).toContain("-exec rm -rf -- {} +"); |
| 137 | + }); |
| 138 | +}); |
0 commit comments