Files
cloudron-box/src/test/backuptask-test.js

95 lines
3.7 KiB
JavaScript
Raw Normal View History

2021-07-14 19:03:12 -07:00
/* jslint node:true */
/* global it:false */
/* global describe:false */
/* global before:false */
/* global after:false */
'use strict';
const backups = require('../backups.js'),
common = require('./common.js'),
expect = require('expect.js'),
fs = require('fs'),
os = require('os'),
path = require('path'),
2023-05-14 10:53:50 +02:00
tasks = require('../tasks.js'),
timers = require('timers/promises');
2021-07-14 19:03:12 -07:00
describe('backuptask', function () {
const { setup, cleanup } = common;
2021-07-14 19:03:12 -07:00
before(setup);
after(cleanup);
describe('fullBackup', function () {
2021-07-14 19:03:12 -07:00
let backupInfo1;
const backupConfig = {
provider: 'filesystem',
backupFolder: path.join(os.tmpdir(), 'backupstask-test-filesystem'),
format: 'tgz',
};
2021-08-20 09:19:44 -07:00
before(async function () {
2021-07-14 19:03:12 -07:00
fs.rmSync(backupConfig.backupFolder, { recursive: true, force: true });
await backups.setStorage(backupConfig);
2021-07-14 19:03:12 -07:00
});
2021-09-10 12:10:10 -07:00
async function createBackup() {
const taskId = await backups.startBackupTask({ username: 'test' });
2021-07-14 19:03:12 -07:00
2021-09-10 12:10:10 -07:00
// eslint-disable-next-line no-constant-condition
while (true) {
2023-05-14 10:53:50 +02:00
await timers.setTimeout(1000);
2021-07-14 19:03:12 -07:00
2021-09-10 12:10:10 -07:00
const p = await tasks.get(taskId);
2021-07-14 19:03:12 -07:00
2021-09-10 12:10:10 -07:00
if (p.percent !== 100) continue;
if (p.error) throw new Error(`backup failed: ${p.error.message}`);
if (!p.result) throw new Error('backup has no result:' + p);
2021-07-14 19:03:12 -07:00
2021-09-10 12:10:10 -07:00
const result = await backups.getByIdentifierAndStatePaged(backups.BACKUP_IDENTIFIER_BOX, backups.BACKUP_STATE_NORMAL, 1, 1);
2021-07-14 19:03:12 -07:00
2021-09-10 12:10:10 -07:00
if (result.length !== 1) throw new Error('result is not of length 1');
2021-07-14 19:03:12 -07:00
2021-09-10 12:10:10 -07:00
// the task progress and the db entry is set in the worker. wait for 2 seconds for backup lock to get released in parent process
2023-05-14 10:53:50 +02:00
await timers.setTimeout(2000);
2021-07-14 19:03:12 -07:00
2021-09-10 12:10:10 -07:00
return result[0];
}
2021-07-14 19:03:12 -07:00
}
2021-09-10 12:10:10 -07:00
it('can backup', async function () {
2021-07-14 19:03:12 -07:00
// arch only has maria db which lacks some mysqldump options we need, this is only here to allow running the tests :-/
if (require('child_process').execSync('/usr/bin/mysqldump --version').toString().indexOf('MariaDB') !== -1) {
console.log('test skipped because of MariaDB');
2021-09-10 12:10:10 -07:00
return;
2021-07-14 19:03:12 -07:00
}
2021-09-10 12:10:10 -07:00
const result = await createBackup();
expect(fs.statSync(path.join(backupConfig.backupFolder, 'snapshot/box.tar.gz')).nlink).to.be(2); // hard linked to a rotated backup
2023-07-11 16:32:28 +05:30
expect(fs.statSync(path.join(backupConfig.backupFolder, `${result.remotePath}.tar.gz`)).nlink).to.be(2);
2021-07-14 19:03:12 -07:00
2021-09-10 12:10:10 -07:00
backupInfo1 = result;
2021-07-14 19:03:12 -07:00
});
2021-09-10 12:10:10 -07:00
it('can take another backup', async function () {
2021-07-14 19:03:12 -07:00
// arch only has maria db which lacks some mysqldump options we need, this is only here to allow running the tests :-/
if (require('child_process').execSync('/usr/bin/mysqldump --version').toString().indexOf('MariaDB') !== -1) {
console.log('test skipped because of MariaDB');
2021-09-10 12:10:10 -07:00
return;
2021-07-14 19:03:12 -07:00
}
2021-09-10 12:10:10 -07:00
const result = await createBackup();
expect(fs.statSync(path.join(backupConfig.backupFolder, 'snapshot/box.tar.gz')).nlink).to.be(2); // hard linked to a rotated backup
2023-07-11 16:32:28 +05:30
expect(fs.statSync(path.join(backupConfig.backupFolder, `${result.remotePath}.tar.gz`)).nlink).to.be(2); // hard linked to new backup
expect(fs.statSync(path.join(backupConfig.backupFolder, `${backupInfo1.remotePath}.tar.gz`)).nlink).to.be(1); // not hard linked anymore
});
it('cleanup', function () {
fs.rmSync(backupConfig.backupFolder, { recursive: true, force: true });
2021-07-14 19:03:12 -07:00
});
});
});