File size: 4,981 Bytes
3e8ea5d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import assert from 'node:assert/strict';
import { mkdir, mkdtemp, readFile, rm, writeFile } from 'node:fs/promises';
import os from 'node:os';
import path from 'node:path';
import { spawnSync } from 'node:child_process';
import { fileURLToPath } from 'node:url';
import { describe, it } from 'node:test';

const repoRoot = fileURLToPath(new URL('..', import.meta.url));
const scriptPath = fileURLToPath(new URL('./test-postgres-live.mjs', import.meta.url));

describe('live PostgreSQL test launcher cleanup', () => {
    it('removes the temporary PostgreSQL container when the test command fails', async () => {
        const tempDir = await mkdtemp(path.join(os.tmpdir(), 'pg-live-launcher-'));
        const logPath = path.join(tempDir, 'docker.log');
        const binDir = path.join(tempDir, 'bin');
        await mkdir(binDir);
        await writeFile(path.join(binDir, 'node'), buildFailingNodeShim(), { mode: 0o755 });
        await writeFile(path.join(binDir, 'docker'), buildDockerShim(logPath), { mode: 0o755 });

        try {
            const result = spawnSync(process.execPath, [scriptPath], {
                cwd: repoRoot,
                encoding: 'utf8',
                env: {
                    ...process.env,
                    PATH: `${binDir}${path.delimiter}${process.env.PATH || ''}`
                }
            });

            assert.equal(result.status, 37, `stdout:\n${result.stdout}\nstderr:\n${result.stderr}`);
            const dockerLog = await readFile(logPath, 'utf8');
            assert.match(dockerLog, /^run /m);
            assert.match(dockerLog, /^exec /m);
            assert.match(dockerLog, /^port /m);
            assert.match(dockerLog, /^rm -f gpt-image-agent-test-pg-/m);
        } finally {
            await rm(tempDir, { recursive: true, force: true });
        }
    });

    it('preserves the test exit code when a PostgreSQL URL is provided', async () => {
        const tempDir = await mkdtemp(path.join(os.tmpdir(), 'pg-live-launcher-'));
        const logPath = path.join(tempDir, 'docker.log');
        const binDir = path.join(tempDir, 'bin');
        await mkdir(binDir);
        await writeFile(path.join(binDir, 'node'), buildFailingNodeShim(), { mode: 0o755 });
        await writeFile(path.join(binDir, 'docker'), buildDockerShim(logPath), { mode: 0o755 });

        try {
            const result = spawnSync(process.execPath, [scriptPath], {
                cwd: repoRoot,
                encoding: 'utf8',
                env: {
                    ...process.env,
                    AGENT_POSTGRES_TEST_DATABASE_URL: 'postgres://agent_test:agent_test@127.0.0.1:55432/agent_test',
                    PATH: `${binDir}${path.delimiter}${process.env.PATH || ''}`
                }
            });

            assert.equal(result.status, 37, `stdout:\n${result.stdout}\nstderr:\n${result.stderr}`);
            await assert.rejects(readFile(logPath, 'utf8'), { code: 'ENOENT' });
        } finally {
            await rm(tempDir, { recursive: true, force: true });
        }
    });

    it('surfaces Docker startup stderr when the temporary container cannot start', async () => {
        const tempDir = await mkdtemp(path.join(os.tmpdir(), 'pg-live-launcher-'));
        const logPath = path.join(tempDir, 'docker.log');
        const binDir = path.join(tempDir, 'bin');
        await mkdir(binDir);
        await writeFile(path.join(binDir, 'node'), buildFailingNodeShim(), { mode: 0o755 });
        await writeFile(path.join(binDir, 'docker'), buildFailingDockerRunShim(logPath), { mode: 0o755 });

        try {
            const result = spawnSync(process.execPath, [scriptPath], {
                cwd: repoRoot,
                encoding: 'utf8',
                env: {
                    ...process.env,
                    PATH: `${binDir}${path.delimiter}${process.env.PATH || ''}`
                }
            });

            assert.equal(result.status, 42, `stdout:\n${result.stdout}\nstderr:\n${result.stderr}`);
            assert.match(result.stderr, /docker daemon unavailable/);
            const dockerLog = await readFile(logPath, 'utf8');
            assert.match(dockerLog, /^run /m);
            assert.match(dockerLog, /^rm -f gpt-image-agent-test-pg-/m);
        } finally {
            await rm(tempDir, { recursive: true, force: true });
        }
    });
});

function buildFailingNodeShim() {
    return `#!/bin/sh
exit 37
`;
}

function buildDockerShim(logPath) {
    return `#!/bin/sh
printf '%s\\n' "$*" >> ${JSON.stringify(logPath)}
case "$1" in
  run)
    exit 0
    ;;
  exec)
    exit 0
    ;;
  port)
    printf '127.0.0.1:55432\\n'
    exit 0
    ;;
  rm)
    exit 0
    ;;
  *)
    exit 2
    ;;
esac
`;
}

function buildFailingDockerRunShim(logPath) {
    return `#!/bin/sh
printf '%s\\n' "$*" >> ${JSON.stringify(logPath)}
case "$1" in
  run)
    printf 'docker daemon unavailable\\n' >&2
    exit 42
    ;;
  rm)
    exit 0
    ;;
  *)
    exit 2
    ;;
esac
`;
}