'use strict'; // Runs deploy/pulse_worker_deploy.sh in a sandbox: a throwaway git origin, an app // directory, and fake systemctl/npm/curl on PATH. The fake systemctl "starts" the // worker by recording the VERSION it would report; a worker.js containing BROKEN // never reconnects, so the fake /healthz lists no version for it. const { execFileSync, spawnSync } = require('child_process'); const fs = require('fs'); const os = require('os'); const path = require('path'); const SCRIPT = path.join(__dirname, '..', 'deploy', 'pulse_worker_deploy.sh'); let root, app, env, commits; function sh(cwd, ...args) { return execFileSync('git', args, { cwd, stdio: ['ignore', 'pipe', 'pipe'] }).toString().trim(); } function commit(repo, name, files) { for (const [f, c] of Object.entries(files)) { fs.mkdirSync(path.dirname(path.join(repo, f)), { recursive: true }); fs.writeFileSync(path.join(repo, f), c); } sh(repo, 'add', '-A'); sh(repo, '-c', 'user.name=t', '-c', 'user.email=t@example.invalid', 'commit', '-qm', name); return sh(repo, 'rev-parse', 'HEAD'); } function deploy(sha, ts = Math.floor(Date.now() / 1000)) { const r = spawnSync('bash', [SCRIPT, sha, String(ts)], { env, encoding: 'utf8' }); return { code: r.status, out: r.stdout + r.stderr }; } const read = f => fs.readFileSync(path.join(app, f), 'utf8'); const running = () => fs.readFileSync(path.join(root, 'running'), 'utf8').trim(); const manifests = { 'package.json': '{"name":"pulse-server"}', 'package-lock.json': '{}' }; beforeAll(() => { root = fs.mkdtempSync(path.join(os.tmpdir(), 'pulse-worker-deploy-')); const work = path.join(root, 'work'); const origin = path.join(root, 'origin.git'); app = path.join(root, 'app'); const bin = path.join(root, 'bin'); [work, app, bin].forEach(d => fs.mkdirSync(d)); sh(root, 'init', '-q', '--bare', '-b', 'main', origin); sh(root, 'init', '-q', '-b', 'main', work); sh(work, 'remote', 'add', 'origin', origin); commits = {}; commits.a = commit(work, 'a', { 'worker/worker.js': 'v-a', 'server.js': 's', ...manifests }); commits.b = commit(work, 'b', { 'worker/worker.js': 'v-b' }); commits.broken = commit(work, 'broken', { 'worker/worker.js': 'v-c BROKEN' }); commits.fixed = commit(work, 'fixed', { 'worker/worker.js': 'v-d' }); sh(work, 'push', '-q', 'origin', 'main'); sh(work, 'checkout', '-q', '-b', 'side', commits.a); commits.side = commit(work, 'side', { 'worker/worker.js': 'v-side' }); sh(work, 'push', '-q', 'origin', 'side'); // The hand-installed worker before the first automated deploy: no VERSION file. fs.writeFileSync(path.join(app, 'worker.js'), 'hand-installed'); fs.writeFileSync(path.join(app, '.env'), 'WORKER_API_KEY=keep-me\n'); fs.writeFileSync(path.join(root, 'running'), ''); const fake = (name, body) => fs.writeFileSync(path.join(bin, name), `#!/bin/bash\n${body}\n`, { mode: 0o755 }); fake('npm', 'exit 0'); fake('systemctl', `if grep -q BROKEN "${app}/worker.js"; then : > "${root}/running"; else cat "${app}/VERSION" > "${root}/running" 2>/dev/null || : > "${root}/running"; fi`); fake('curl', `v=$(cat "${root}/running"); if [ -n "$v" ]; then echo "{\\"workers\\":1,\\"worker_versions\\":[\\"$v\\"]}"; else echo '{"workers":0,"worker_versions":[]}'; fi`); env = { ...process.env, PATH: `${bin}:${process.env.PATH}`, PULSE_WORKER_APP_DIR: app, PULSE_WORKER_REPO_URL: origin, PULSE_WORKER_STATE_DIR: path.join(root, 'state'), PULSE_WORKER_LOG: path.join(root, 'deploy.log'), PULSE_WORKER_LOCK: path.join(root, 'lock'), PULSE_WORKER_HEALTH_TRIES: '3', PULSE_WORKER_HEALTH_SLEEP: '0', }; }); afterAll(() => { if (process.env.KEEP) console.log(fs.readFileSync(path.join(root, 'deploy.log'), 'utf8')); fs.rmSync(root, { recursive: true, force: true }); }); // Order matters: each test builds on the state the previous one left. test('replaces a hand-installed worker with exactly the requested commit', () => { const r = deploy(commits.b); expect(r.code).toBe(0); expect(r.out).toMatch(/previous unknown/); expect(read('worker.js')).toBe('v-b'); expect(read('VERSION').trim()).toBe(commits.b); expect(read('package-lock.json')).toBe('{}'); expect(read('.env')).toBe('WORKER_API_KEY=keep-me\n'); // config is never touched expect(running()).toBe(commits.b); }); test('a replayed request is refused', () => { const ts = Math.floor(Date.now() / 1000) + 5; expect(deploy(commits.fixed, ts).code).toBe(0); const r = deploy(commits.b, ts); expect(r.code).toBe(1); expect(r.out).toMatch(/replay or out of order/); expect(read('VERSION').trim()).toBe(commits.fixed); }); test('a stale request and malformed input are refused', () => { expect(deploy(commits.b, Math.floor(Date.now() / 1000) - 3600).out).toMatch(/old \(limit 300s\)/); expect(deploy('abc', Math.floor(Date.now() / 1000) + 6).out).toMatch(/invalid sha/); expect(deploy(commits.b, 'x').out).toMatch(/invalid timestamp/); }); test('a commit that is not on main is refused', () => { const r = deploy(commits.side, Math.floor(Date.now() / 1000) + 10); expect(r.code).toBe(1); expect(r.out).toMatch(/is not on main|not found after fetch/); expect(read('worker.js')).toBe('v-d'); }); test('redeploying the running commit does nothing', () => { const r = deploy(commits.fixed, Math.floor(Date.now() / 1000) + 15); expect(r.code).toBe(0); expect(r.out).toMatch(/nothing to do/); }); test('a worker that never reconnects is rolled back to the previous files', () => { const r = deploy(commits.broken, Math.floor(Date.now() / 1000) + 20); expect(r.code).toBe(1); expect(r.out).toMatch(/did not reconnect; restoring/); expect(r.out).toMatch(/restored /); expect(read('worker.js')).toBe('v-d'); expect(read('VERSION').trim()).toBe(commits.fixed); expect(running()).toBe(commits.fixed); });