mirror of
https://github.com/wekan/wekan.git
synced 2026-11-24 18:10:06 -05:00
Retain the existing --drop restore behavior that prevents duplicate-key collisions, validate that exactly one real archive was supplied, and preserve paths containing spaces as one mongorestore argument. Add command-level positive and negative regressions with a mocked Snap environment. Thanks to ram19890 and xet7 ! Fixes #6547,
102 lines
2.8 KiB
JavaScript
102 lines
2.8 KiB
JavaScript
'use strict';
|
|
|
|
const assert = require('assert');
|
|
const fs = require('fs');
|
|
const os = require('os');
|
|
const path = require('path');
|
|
const { spawnSync } = require('child_process');
|
|
|
|
const root = path.join(__dirname, '..');
|
|
const restore = path.join(root, 'snap-src', 'bin', 'mongodb-restore');
|
|
let passed = 0;
|
|
|
|
function test(name, fn) {
|
|
fn();
|
|
passed += 1;
|
|
console.log(' ok -', name);
|
|
}
|
|
|
|
function fixture() {
|
|
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'wekan-restore-test-'));
|
|
const snap = path.join(dir, 'snap fixture');
|
|
const fakeBin = path.join(snap, 'migratemongo', 'avx');
|
|
fs.mkdirSync(path.join(snap, 'bin'), { recursive: true });
|
|
fs.mkdirSync(fakeBin, { recursive: true });
|
|
fs.writeFileSync(
|
|
path.join(snap, 'bin', 'wekan-read-settings'),
|
|
'MONGODB_BIND_IP=127.0.0.9\nMONGODB_PORT=27099\n',
|
|
);
|
|
const capture = path.join(dir, 'captured.json');
|
|
fs.writeFileSync(
|
|
path.join(fakeBin, 'mongorestore'),
|
|
'#!/bin/bash\nprintf \'%s\\n\' "$@" > "$RESTORE_CAPTURE"\n',
|
|
{ mode: 0o755 },
|
|
);
|
|
return { dir, snap, capture };
|
|
}
|
|
|
|
function run(f, args) {
|
|
return spawnSync('bash', [restore, ...args], {
|
|
encoding: 'utf8',
|
|
env: {
|
|
...process.env,
|
|
SNAP: f.snap,
|
|
SNAP_INSTANCE_NAME: 'wekan',
|
|
RESTORE_CAPTURE: f.capture,
|
|
},
|
|
});
|
|
}
|
|
|
|
test('#6547 restores by replacing existing collections with --drop', () => {
|
|
const f = fixture();
|
|
try {
|
|
const archive = path.join(f.dir, 'backup with spaces.archive');
|
|
fs.writeFileSync(archive, 'fixture');
|
|
const result = run(f, [archive]);
|
|
assert.strictEqual(result.status, 0, result.stderr);
|
|
assert.deepStrictEqual(
|
|
fs.readFileSync(f.capture, 'utf8').trim().split('\n'),
|
|
[
|
|
'--host',
|
|
'127.0.0.9',
|
|
'--port',
|
|
'27099',
|
|
'-d',
|
|
'wekan',
|
|
'--drop',
|
|
'--gzip',
|
|
`--archive=${archive}`,
|
|
],
|
|
);
|
|
} finally {
|
|
fs.rmSync(f.dir, { recursive: true, force: true });
|
|
}
|
|
});
|
|
|
|
test('negative: a missing argument fails before mongorestore', () => {
|
|
const f = fixture();
|
|
try {
|
|
const result = run(f, []);
|
|
assert.strictEqual(result.status, 2);
|
|
assert.match(result.stderr, /Usage:/);
|
|
assert.strictEqual(fs.existsSync(f.capture), false);
|
|
} finally {
|
|
fs.rmSync(f.dir, { recursive: true, force: true });
|
|
}
|
|
});
|
|
|
|
test('negative: a nonexistent archive fails before mongorestore', () => {
|
|
const f = fixture();
|
|
try {
|
|
const missing = path.join(f.dir, 'missing.archive');
|
|
const result = run(f, [missing]);
|
|
assert.strictEqual(result.status, 2);
|
|
assert.match(result.stderr, /does not exist or is not a file/);
|
|
assert.strictEqual(fs.existsSync(f.capture), false);
|
|
} finally {
|
|
fs.rmSync(f.dir, { recursive: true, force: true });
|
|
}
|
|
});
|
|
|
|
console.log(`\nsnapDatabaseRestore: all ${passed} tests passed`);
|