|
1 | 1 | // Docker E2E Observability tests cover docker e2e observability script behavior. |
2 | | -import { readFileSync } from "node:fs"; |
3 | | -import { describe, expect, it } from "vitest"; |
| 2 | +import { spawnSync } from "node:child_process"; |
| 3 | +import { mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs"; |
| 4 | +import { tmpdir } from "node:os"; |
| 5 | +import path from "node:path"; |
| 6 | +import { afterEach, describe, expect, it } from "vitest"; |
| 7 | + |
| 8 | +const tempDirs: string[] = []; |
| 9 | + |
| 10 | +function makeTempDir(): string { |
| 11 | + const dir = mkdtempSync(path.join(tmpdir(), "openclaw-docker-e2e-observability-")); |
| 12 | + tempDirs.push(dir); |
| 13 | + return dir; |
| 14 | +} |
| 15 | + |
| 16 | +afterEach(() => { |
| 17 | + for (const dir of tempDirs.splice(0)) { |
| 18 | + rmSync(dir, { force: true, recursive: true }); |
| 19 | + } |
| 20 | +}); |
| 21 | + |
| 22 | +function successTail(scriptPath: string): string { |
| 23 | + const script = readFileSync(scriptPath, "utf8"); |
| 24 | + const index = script.lastIndexOf('if [ "$status" -ne 0 ]; then'); |
| 25 | + if (index === -1) { |
| 26 | + throw new Error(`missing status tail in ${scriptPath}`); |
| 27 | + } |
| 28 | + return script.slice(index); |
| 29 | +} |
| 30 | + |
| 31 | +function runSuccessTail(scriptPath: string) { |
| 32 | + const tempDir = makeTempDir(); |
| 33 | + const clientLog = path.join(tempDir, "client.log"); |
| 34 | + writeFileSync(clientLog, "client proof log\n", "utf8"); |
| 35 | + const harness = [ |
| 36 | + "set -euo pipefail", |
| 37 | + `CLIENT_LOG=${JSON.stringify(clientLog)}`, |
| 38 | + "status=0", |
| 39 | + "docker_e2e_print_log() {", |
| 40 | + ' printf \'LOG:%s\\n\' "$(cat "$1")"', |
| 41 | + "}", |
| 42 | + successTail(scriptPath), |
| 43 | + ].join("\n"); |
| 44 | + |
| 45 | + return spawnSync("bash", ["-c", harness], { encoding: "utf8" }); |
| 46 | +} |
4 | 47 |
|
5 | 48 | describe("Docker E2E observability", () => { |
6 | 49 | it.each(["scripts/e2e/mcp-channels-docker.sh", "scripts/e2e/cron-mcp-cleanup-docker.sh"])( |
7 | 50 | "prints successful MCP client proof logs from %s", |
8 | 51 | (scriptPath) => { |
9 | | - const script = readFileSync(scriptPath, "utf8"); |
10 | | - const successTail = script.slice(script.lastIndexOf('if [ "$status" -ne 0 ]; then')); |
| 52 | + const result = runSuccessTail(scriptPath); |
11 | 53 |
|
12 | | - expect(successTail).toContain('docker_e2e_print_log "$CLIENT_LOG"'); |
13 | | - expect(successTail.indexOf('docker_e2e_print_log "$CLIENT_LOG"')).toBeLessThan( |
14 | | - successTail.indexOf('echo "OK"'), |
15 | | - ); |
| 54 | + expect(result.status, result.stderr).toBe(0); |
| 55 | + expect(result.stdout.trim().split("\n")).toEqual(["LOG:client proof log", "OK"]); |
16 | 56 | }, |
17 | 57 | ); |
18 | 58 | }); |
0 commit comments