'use strict'; /* deploy/upstream-offsite: newest snapshot, verified first, copied (rsync to a local directory standing in for the far side), * never overwritten, content compared. ssh itself is not exercised (no remote host here). */ const test = require('node:test'); const assert = require('node:assert/strict'); const fs = require('fs'); const os = require('os'); const path = require('path'); const { spawnSync } = require('child_process'); const admin = require('../tools/admin.js'); const { openDb, migrate } = require('../server/db'); const SCRIPT = path.join(__dirname, '..', 'deploy', 'upstream-offsite'); const haveRsync = spawnSync('rsync', ['--version']).status === 0; const opts = { skip: !haveRsync && 'rsync not installed' }; // the real tools/admin.js does the verification, exactly like on the server (upstream-admin wrapper replaced by a tiny sh) function fixture() { const root = fs.mkdtempSync(path.join(os.tmpdir(), 'upstream-offsite-')); const p = (...n) => path.join(root, ...n); fs.mkdirSync(p('backups')); fs.mkdirSync(p('far')); fs.writeFileSync(p('admin'), '#!/bin/sh\nexec node "' + path.join(__dirname, '..', 'tools', 'admin.js') + '" "$@"\n', { mode: 0o755 }); const dbFile = p('game.db'); const db = openDb(dbFile); migrate(db); db.run("INSERT INTO worlds (name, seed, speed, map_radius, status, start_at, created_at) VALUES ('w', 1, 1, 10, 'running', 0, 0)"); db.close(); const snap = (name, ageSec) => { const code = admin.main(['backup', p('backups'), '--db', dbFile], { out() {}, err() {}, env: {}, now: () => Date.UTC(2026, 9, 11, 4, 15, 0) + (snap.n = (snap.n || 0) + 1) * 1000 }); assert.equal(code, 0); const made = fs.readdirSync(p('backups')).filter((f) => f.startsWith('upstream-')).sort().pop(); if (name) fs.renameSync(p('backups', made), p('backups', name)); const t = (Date.now() - ageSec * 1000) / 1000; fs.utimesSync(p('backups', name || made), t, t); return name || made; }; const run = (args, env) => { const r = spawnSync('sh', [SCRIPT].concat(args || []), { encoding: 'utf8', env: Object.assign({}, process.env, { BACKUP_DIR: p('backups'), UPSTREAM_ADMIN: p('admin'), OFFSITE_DEST: p('far'), OFFSITE_KNOWN_HOSTS: p('none') }, env || {}) }); return { code: r.status, out: r.stdout, err: r.stderr, all: r.stdout + r.stderr }; }; return { p, snap, run, far: () => fs.readdirSync(p('far')).sort() }; } test('--check is the ExecCondition: unset destination = exit 1, set = 0', opts, () => { const f = fixture(); assert.equal(f.run(['--check'], { OFFSITE_DEST: '' }).code, 1); assert.equal(f.run(['--check']).code, 0); const r = f.run([], { OFFSITE_DEST: '' }); assert.equal(r.code, 2); assert.match(r.err, /not configured/); }); test('copies the newest snapshot (by mtime, also against a same-second -1 name), verified, mode 0600, content identical', opts, () => { const f = fixture(); f.snap('upstream-20261010T041500Z.db', 7200); const newest = f.snap('upstream-20261011T041500Z-1.db', 60); // sorts BEFORE a plain ...Z.db by name, but is the newer one f.snap('upstream-20261011T041500Z.db', 3600); const r = f.run(); assert.equal(r.code, 0, r.all); assert.deepEqual(f.far(), [newest]); assert.deepEqual(fs.readFileSync(f.p('far', newest)), fs.readFileSync(f.p('backups', newest))); assert.equal(fs.statSync(f.p('far', newest)).mode & 0o777, 0o600); assert.match(r.out, /checksum compared/); // the copy is a real snapshot assert.equal(admin.verifySnapshot(f.p('far', newest)).integrity, 'ok'); // again: already there, still exit 0 assert.equal(f.run().code, 0); assert.deepEqual(f.far(), [newest]); }); test('a snapshot that does not verify is never copied', opts, () => { const f = fixture(); const name = f.snap('upstream-20261011T041500Z.db', 60); fs.writeFileSync(f.p('backups', name), 'not a database'); const r = f.run(); assert.equal(r.code, 1); assert.match(r.err, /does not verify/); assert.deepEqual(f.far(), []); }); test('nothing to copy, and a different file of the same name on the far side, are failures (never overwritten)', opts, () => { const f = fixture(); assert.equal(f.run().code, 1); const name = f.snap('upstream-20261011T041500Z.db', 60); fs.writeFileSync(f.p('far', name), 'someone else was here'); const r = f.run(); assert.equal(r.code, 1); assert.match(r.err, /differs/); assert.equal(fs.readFileSync(f.p('far', name), 'utf8'), 'someone else was here'); }); test('OFFSITE_METHOD=scp is refused (scp would overwrite), nothing is copied', () => { const f = fixture(); f.snap('upstream-20261011T041500Z.db', 60); const r = f.run([], { OFFSITE_METHOD: 'scp' }); assert.equal(r.code, 2); assert.match(r.err, /scp is not supported|not supported any more/); assert.deepEqual(f.far(), []); }); test('an unwritable far side fails loudly', opts, () => { const f = fixture(); f.snap('upstream-20261011T041500Z.db', 60); const r = f.run([], { OFFSITE_DEST: f.p('nope', 'deeper', 'dir') }); assert.equal(r.code, 1); assert.match(r.err, /failed/); }); test('the unit files point at the script and gate on OFFSITE_DEST', () => { const dir = path.join(__dirname, '..', 'deploy'); const svc = fs.readFileSync(path.join(dir, 'upstream-offsite.service'), 'utf8'); assert.match(svc, /^ExecCondition=\/opt\/upstream\/deploy\/upstream-offsite --check$/m); assert.match(svc, /^ExecStart=\/opt\/upstream\/deploy\/upstream-offsite$/m); assert.match(svc, /^Type=oneshot$/m); const timer = fs.readFileSync(path.join(dir, 'upstream-offsite.timer'), 'utf8'); assert.match(timer, /^Unit=upstream-offsite\.service$/m); assert.ok((fs.statSync(path.join(dir, 'upstream-offsite')).mode & 0o111) !== 0, 'executable bit (ExecStart needs it)'); assert.equal(spawnSync('sh', ['-n', SCRIPT]).status, 0); });