127 lines
5.1 KiB
JavaScript
127 lines
5.1 KiB
JavaScript
'use strict';
|
|||
|
|
|
||
|
|
// Runs deploy/pulse_deploy.sh in a sandbox: a throwaway git origin and checkout,
|
||
|
|
// with fake systemctl/npm/curl on PATH. "Production" health is simulated by the
|
||
|
|
// fake curl, which reports whatever commit the fake systemctl last started; a
|
||
|
|
// commit containing a BROKEN file reports db:false (an unhealthy build).
|
||
|
|
|
||
|
|
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_deploy.sh');
|
||
|
|
let root, app, bin, 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.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 head = () => sh(app, 'rev-parse', 'HEAD');
|
||
|
|
const running = () => fs.readFileSync(path.join(root, 'running'), 'utf8').trim();
|
||
|
|
|
||
|
|
beforeAll(() => {
|
||
|
|
root = fs.mkdtempSync(path.join(os.tmpdir(), 'pulse-deploy-'));
|
||
|
|
const work = path.join(root, 'work');
|
||
|
|
const origin = path.join(root, 'origin.git');
|
||
|
|
app = path.join(root, 'app');
|
||
|
|
bin = path.join(root, 'bin');
|
||
|
|
fs.mkdirSync(work); fs.mkdirSync(bin);
|
||
|
|
|
||
|
|
sh(root, 'init', '-q', '--bare', '-b', 'main', origin);
|
||
|
|
sh(root, 'init', '-q', '-b', 'main', work);
|
||
|
|
commits = {};
|
||
|
|
commits.a = commit(work, 'a', { 'server.js': 'a' });
|
||
|
|
sh(work, 'remote', 'add', 'origin', origin);
|
||
|
|
sh(work, 'push', '-q', 'origin', 'main');
|
||
|
|
sh(root, 'clone', '-q', origin, app);
|
||
|
|
fs.writeFileSync(path.join(app, '.env'), 'SECRET=keep-me\n'); // untracked, must survive
|
||
|
|
|
||
|
|
commits.b = commit(work, 'b', { 'server.js': 'b' });
|
||
|
|
commits.broken = commit(work, 'broken', { 'server.js': 'c', BROKEN: '1' });
|
||
|
|
commits.fixed = commit(work, 'fixed', { 'server.js': 'd' });
|
||
|
|
fs.unlinkSync(path.join(work, 'BROKEN'));
|
||
|
|
commits.fixed = commit(work, 'fixed2', { 'server.js': 'e' });
|
||
|
|
sh(work, 'push', '-q', 'origin', 'main');
|
||
|
|
sh(work, 'checkout', '-q', '-b', 'side', commits.a);
|
||
|
|
commits.side = commit(work, 'side', { 'server.js': 'side' });
|
||
|
|
sh(work, 'push', '-q', 'origin', 'side');
|
||
|
|
|
||
|
|
const fake = (name, body) => fs.writeFileSync(path.join(bin, name), `#!/bin/bash\n${body}\n`, { mode: 0o755 });
|
||
|
|
fake('npm', 'exit 0');
|
||
|
|
fake('systemctl', `git -C "${app}" rev-parse HEAD > "${root}/running"`);
|
||
|
|
fake('curl', `s=$(cat "${root}/running" 2>/dev/null) || exit 7
|
||
|
|
if [ -e "${app}/BROKEN" ]; then db=false; else db=true; fi
|
||
|
|
echo "{\\"status\\":\\"ok\\",\\"sha\\":\\"$s\\",\\"db\\":$db}"`);
|
||
|
|
fs.writeFileSync(path.join(root, 'running'), commits.a);
|
||
|
|
|
||
|
|
env = {
|
||
|
|
...process.env,
|
||
|
|
PATH: `${bin}:${process.env.PATH}`,
|
||
|
|
PULSE_DEPLOY_APP_ROOT: app,
|
||
|
|
PULSE_DEPLOY_STATE_DIR: path.join(root, 'state'),
|
||
|
|
PULSE_DEPLOY_LOG: path.join(root, 'deploy.log'),
|
||
|
|
PULSE_DEPLOY_LOCK: path.join(root, 'lock'),
|
||
|
|
PULSE_DEPLOY_HEALTH_TRIES: '3',
|
||
|
|
PULSE_DEPLOY_HEALTH_SLEEP: '0'
|
||
|
|
};
|
||
|
|
});
|
||
|
|
|
||
|
|
afterAll(() => fs.rmSync(root, { recursive: true, force: true }));
|
||
|
|
|
||
|
|
// Order matters: each test builds on the state the previous one left.
|
||
|
|
test('deploys exactly the requested commit, not just the tip of main', () => {
|
||
|
|
const r = deploy(commits.b);
|
||
|
|
expect(r.code).toBe(0);
|
||
|
|
expect(head()).toBe(commits.b);
|
||
|
|
expect(running()).toBe(commits.b);
|
||
|
|
expect(fs.readFileSync(path.join(app, '.env'), 'utf8')).toBe('SECRET=keep-me\n');
|
||
|
|
});
|
||
|
|
|
||
|
|
test('a replayed request (same or older timestamp) 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(head()).toBe(commits.fixed);
|
||
|
|
});
|
||
|
|
|
||
|
|
test('a stale request is refused', () => {
|
||
|
|
const r = deploy(commits.b, Math.floor(Date.now() / 1000) - 3600);
|
||
|
|
expect(r.code).toBe(1);
|
||
|
|
expect(r.out).toMatch(/old \(limit 300s\)/);
|
||
|
|
});
|
||
|
|
|
||
|
|
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);
|
||
|
|
// Only main is fetched, so a commit from another branch is unknown or not an ancestor.
|
||
|
|
expect(r.out).toMatch(/is not on main|not found after fetch/);
|
||
|
|
expect(head()).toBe(commits.fixed);
|
||
|
|
});
|
||
|
|
|
||
|
|
test('malformed input is refused', () => {
|
||
|
|
expect(deploy('abc', Math.floor(Date.now() / 1000) + 11).out).toMatch(/invalid sha/);
|
||
|
|
expect(deploy(commits.b, 'x').out).toMatch(/invalid timestamp/);
|
||
|
|
});
|
||
|
|
|
||
|
|
test('an unhealthy build is rolled back to the previous commit', () => {
|
||
|
|
const r = deploy(commits.broken, Math.floor(Date.now() / 1000) + 20);
|
||
|
|
expect(r.code).toBe(1);
|
||
|
|
expect(r.out).toMatch(/rolling back/);
|
||
|
|
expect(r.out).toMatch(/rolled back to .*healthy/);
|
||
|
|
expect(head()).toBe(commits.fixed);
|
||
|
|
expect(running()).toBe(commits.fixed);
|
||
|
|
});
|