|
| 1 | +import { execFileSync, execSync } from "node:child_process"; |
| 2 | +import { chmodSync, mkdtempSync, writeFileSync } from "node:fs"; |
| 3 | +import { tmpdir } from "node:os"; |
| 4 | +import path from "node:path"; |
| 5 | +import { defaultCommand, reportDockerVersion } from "@dokploy/server"; |
| 6 | +import { describe, expect, it } from "vitest"; |
| 7 | + |
| 8 | +const resolveBin = (name: string) => |
| 9 | + execSync(`command -v ${name}`, { encoding: "utf8" }).trim(); |
| 10 | + |
| 11 | +/** |
| 12 | + * Build a sandbox PATH so `command -v docker` only sees our fake docker |
| 13 | + * binary (or nothing), regardless of what the host has installed. |
| 14 | + */ |
| 15 | +const makeSandbox = (dockerShim?: string) => { |
| 16 | + const dir = mkdtempSync(path.join(tmpdir(), "dokploy-server-setup-")); |
| 17 | + for (const tool of ["awk", "tr"]) { |
| 18 | + const shim = path.join(dir, tool); |
| 19 | + writeFileSync(shim, `#!/bin/sh\nexec ${resolveBin(tool)} "$@"\n`); |
| 20 | + chmodSync(shim, 0o755); |
| 21 | + } |
| 22 | + if (dockerShim) { |
| 23 | + const shim = path.join(dir, "docker"); |
| 24 | + writeFileSync(shim, dockerShim); |
| 25 | + chmodSync(shim, 0o755); |
| 26 | + } |
| 27 | + return dir; |
| 28 | +}; |
| 29 | + |
| 30 | +const runReport = (sandboxPath: string) => { |
| 31 | + const script = [ |
| 32 | + "DOCKER_VERSION=28.5.0", |
| 33 | + reportDockerVersion(), |
| 34 | + 'echo "$DOCKER_VERSION_REPORT"', |
| 35 | + ].join("\n"); |
| 36 | + return execFileSync(resolveBin("bash"), ["-c", script], { |
| 37 | + encoding: "utf8", |
| 38 | + env: { ...process.env, PATH: sandboxPath }, |
| 39 | + }) |
| 40 | + .trim() |
| 41 | + .split("\n") |
| 42 | + .pop(); |
| 43 | +}; |
| 44 | + |
| 45 | +describe("reportDockerVersion", () => { |
| 46 | + it("reports the engine version when docker and its daemon are available", () => { |
| 47 | + const sandbox = makeSandbox( |
| 48 | + [ |
| 49 | + "#!/bin/sh", |
| 50 | + 'if [ "$1" = "--version" ]; then', |
| 51 | + ' echo "Docker version 25.0.0, build aaaaaaa"', |
| 52 | + " exit 0", |
| 53 | + "fi", |
| 54 | + 'if [ "$1" = "version" ]; then', |
| 55 | + ' echo "29.4.3"', |
| 56 | + " exit 0", |
| 57 | + "fi", |
| 58 | + "exit 1", |
| 59 | + ].join("\n"), |
| 60 | + ); |
| 61 | + expect(runReport(sandbox)).toBe("29.4.3 (already installed)"); |
| 62 | + }); |
| 63 | + |
| 64 | + it("falls back to the client version when the daemon is unreachable", () => { |
| 65 | + const sandbox = makeSandbox( |
| 66 | + [ |
| 67 | + "#!/bin/sh", |
| 68 | + 'if [ "$1" = "--version" ]; then', |
| 69 | + ' echo "Docker version 29.4.3, build 055a478"', |
| 70 | + " exit 0", |
| 71 | + "fi", |
| 72 | + 'echo "Cannot connect to the Docker daemon" >&2', |
| 73 | + "exit 1", |
| 74 | + ].join("\n"), |
| 75 | + ); |
| 76 | + expect(runReport(sandbox)).toBe("29.4.3 (already installed)"); |
| 77 | + }); |
| 78 | + |
| 79 | + it("reports the pinned version to be installed when docker is missing", () => { |
| 80 | + expect(runReport(makeSandbox())).toBe("28.5.0 (will be installed)"); |
| 81 | + }); |
| 82 | +}); |
| 83 | + |
| 84 | +describe("defaultCommand", () => { |
| 85 | + it.each([false, true])( |
| 86 | + "prints the detected Docker version in the setup banner (isBuildServer=%s)", |
| 87 | + (isBuildServer) => { |
| 88 | + const script = defaultCommand(isBuildServer); |
| 89 | + expect(script).toContain(reportDockerVersion()); |
| 90 | + expect(script).toContain( |
| 91 | + 'echo "| Docker | $DOCKER_VERSION_REPORT"', |
| 92 | + ); |
| 93 | + expect(script).not.toContain( |
| 94 | + 'echo "| Docker | $DOCKER_VERSION"', |
| 95 | + ); |
| 96 | + }, |
| 97 | + ); |
| 98 | +}); |
0 commit comments