





























@@ -0,0 +1,167 @@
1+import { execFileSync, spawnSync } from "node:child_process";
2+import { chmodSync, existsSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";
3+import { tmpdir } from "node:os";
4+import path from "node:path";
5+import { afterEach, describe, expect, it } from "vitest";
6+7+const SCRIPT_PATH = path.resolve("scripts/ci-docker-pull-retry.sh");
8+const tempDirs: string[] = [];
9+10+function makeTempBin(prefix: string) {
11+const dir = mkdtempSync(path.join(tmpdir(), prefix));
12+tempDirs.push(dir);
13+return dir;
14+}
15+16+function writeExecutable(filePath: string, contents: string) {
17+writeFileSync(filePath, contents, "utf8");
18+chmodSync(filePath, 0o755);
19+}
20+21+function runPullHelper(binDir: string) {
22+return runPullHelperWithEnv(binDir, {});
23+}
24+25+function runPullHelperWithEnv(binDir: string, env: Record<string, string>) {
26+return spawnSync("/bin/bash", [SCRIPT_PATH, "registry.example/openclaw:test"], {
27+cwd: process.cwd(),
28+encoding: "utf8",
29+env: {
30+ ...process.env,
31+OPENCLAW_DOCKER_PULL_ATTEMPTS: "1",
32+OPENCLAW_DOCKER_PULL_RETRY_DELAY_SECONDS: "0",
33+OPENCLAW_DOCKER_PULL_TIMEOUT_SECONDS: "42",
34+ ...env,
35+PATH: binDir,
36+},
37+});
38+}
39+40+afterEach(() => {
41+while (tempDirs.length > 0) {
42+rmSync(tempDirs.pop()!, { force: true, recursive: true });
43+}
44+});
45+46+describe("scripts/ci-docker-pull-retry.sh", () => {
47+it("uses a kill-after grace period when timeout supports it", () => {
48+const binDir = makeTempBin("openclaw-ci-docker-pull-gnu-");
49+const timeoutArgsPath = path.join(binDir, "timeout-args.txt");
50+const dockerArgsPath = path.join(binDir, "docker-args.txt");
51+52+writeExecutable(
53+path.join(binDir, "timeout"),
54+[
55+"#!/bin/bash",
56+"set -euo pipefail",
57+'if [ "${1:-}" = "--kill-after=1s" ]; then exit 0; fi',
58+`printf "%s\\n" "$*" >${JSON.stringify(timeoutArgsPath)}`,
59+'while [ "$#" -gt 0 ] && [ "$1" != "docker" ]; do shift; done',
60+'exec "$@"',
61+"",
62+].join("\n"),
63+);
64+writeExecutable(
65+path.join(binDir, "docker"),
66+["#!/bin/sh", "set -eu", `printf "%s\\n" "$*" >${JSON.stringify(dockerArgsPath)}`, ""].join(
67+"\n",
68+),
69+);
70+71+const result = runPullHelper(binDir);
72+73+expect(result.status).toBe(0);
74+expect(execFileSync("cat", [timeoutArgsPath], { encoding: "utf8" }).trim()).toBe(
75+"--kill-after=30s 42s docker pull registry.example/openclaw:test",
76+);
77+expect(execFileSync("cat", [dockerArgsPath], { encoding: "utf8" }).trim()).toBe(
78+"pull registry.example/openclaw:test",
79+);
80+});
81+82+it("falls back to plain timeout when kill-after is unavailable", () => {
83+const binDir = makeTempBin("openclaw-ci-docker-pull-plain-");
84+const timeoutArgsPath = path.join(binDir, "timeout-args.txt");
85+const dockerArgsPath = path.join(binDir, "docker-args.txt");
86+87+writeExecutable(
88+path.join(binDir, "timeout"),
89+[
90+"#!/bin/bash",
91+"set -euo pipefail",
92+'if [ "${1:-}" = "--kill-after=1s" ]; then exit 1; fi',
93+`printf "%s\\n" "$*" >${JSON.stringify(timeoutArgsPath)}`,
94+'while [ "$#" -gt 0 ] && [ "$1" != "docker" ]; do shift; done',
95+'exec "$@"',
96+"",
97+].join("\n"),
98+);
99+writeExecutable(
100+path.join(binDir, "docker"),
101+["#!/bin/sh", "set -eu", `printf "%s\\n" "$*" >${JSON.stringify(dockerArgsPath)}`, ""].join(
102+"\n",
103+),
104+);
105+106+const result = runPullHelper(binDir);
107+108+expect(result.status).toBe(0);
109+expect(execFileSync("cat", [timeoutArgsPath], { encoding: "utf8" }).trim()).toBe(
110+"42s docker pull registry.example/openclaw:test",
111+);
112+expect(execFileSync("cat", [dockerArgsPath], { encoding: "utf8" }).trim()).toBe(
113+"pull registry.example/openclaw:test",
114+);
115+});
116+117+it("fails fast when timeout is unavailable", () => {
118+const binDir = makeTempBin("openclaw-ci-docker-pull-no-timeout-");
119+const dockerArgsPath = path.join(binDir, "docker-args.txt");
120+121+writeExecutable(
122+path.join(binDir, "docker"),
123+["#!/bin/sh", "set -eu", `printf "%s\\n" "$*" >${JSON.stringify(dockerArgsPath)}`, ""].join(
124+"\n",
125+),
126+);
127+128+const result = runPullHelper(binDir);
129+130+expect(result.status).toBe(127);
131+expect(result.stderr).toContain("timeout command not found; cannot bound Docker pull after 42s");
132+expect(existsSync(dockerArgsPath)).toBe(false);
133+});
134+135+it("returns the last pull failure status after retries are exhausted", () => {
136+const binDir = makeTempBin("openclaw-ci-docker-pull-fail-");
137+const dockerArgsPath = path.join(binDir, "docker-args.txt");
138+139+writeExecutable(
140+path.join(binDir, "timeout"),
141+[
142+"#!/bin/bash",
143+"set -euo pipefail",
144+'if [ "${1:-}" = "--kill-after=1s" ]; then exit 0; fi',
145+'while [ "$#" -gt 0 ] && [ "$1" != "docker" ]; do shift; done',
146+'exec "$@"',
147+"",
148+].join("\n"),
149+);
150+writeExecutable(
151+path.join(binDir, "docker"),
152+[
153+"#!/bin/sh",
154+"set -eu",
155+`printf "%s\\n" "$*" >>${JSON.stringify(dockerArgsPath)}`,
156+"exit 42",
157+"",
158+].join("\n"),
159+);
160+161+const result = runPullHelperWithEnv(binDir, { OPENCLAW_DOCKER_PULL_ATTEMPTS: "2" });
162+163+expect(result.status).toBe(42);
164+expect(result.stderr).toContain("Docker pull failed or timed out after 42s: status=42");
165+expect(execFileSync("wc", ["-l", dockerArgsPath], { encoding: "utf8" }).trim()).toMatch(/^2\b/u);
166+});
167+});
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。