Files
cloudron-box/src/test/backupsites-test.js
T

132 lines
4.8 KiB
JavaScript
Raw Normal View History

2026-02-19 13:24:14 +01:00
import { describe, it, before, after } from 'mocha';
2026-02-14 15:43:24 +01:00
import archives from '../archives.js';
import backups from '../backups.js';
2026-02-14 15:43:24 +01:00
import backupSites from '../backupsites.js';
import BoxError from '../boxerror.js';
2026-02-14 15:43:24 +01:00
import common from './common.js';
import constants from '../constants.js';
2026-02-18 22:21:54 +01:00
import assert from 'node:assert/strict';
import safe from 'safetydance';
2025-09-12 09:48:37 +02:00
describe('backups', function () {
const { setup, cleanup, auditSource, getDefaultBackupSite } = common;
before(async function () {
await setup();
});
after(cleanup);
const backupSite = {
provider: 'filesystem',
name: 'Another',
config: { backupDir: '/tmp/boxtest2' },
format: 'rsync',
retention: { keepWithinSecs: 2 * 24 * 60 * 60 },
2025-09-22 13:27:26 +02:00
schedule: '00 00 23 * * *',
2025-09-22 20:02:13 +02:00
contents: null,
enableForUpdates: true
2025-09-12 09:48:37 +02:00
};
const appBackup = {
id: null,
remotePath: 'backup-box',
encryptionVersion: 2,
packageVersion: '1.0.0',
type: backups.BACKUP_TYPE_APP,
state: backups.BACKUP_STATE_NORMAL,
identifier: 'box',
dependsOn: [ 'dep1' ],
manifest: null,
preserveSecs: 0,
label: '',
appConfig: { loc: 'loc1' },
siteId: null
};
let defaultBackupSite = null;
before(async function () {
2025-09-22 20:02:13 +02:00
defaultBackupSite = await getDefaultBackupSite();
appBackup.siteId = defaultBackupSite.id;
2025-09-12 09:48:37 +02:00
appBackup.id = await backups.add(appBackup);
await archives.add(appBackup.id, {}, auditSource);
});
it('can add another site', async function () {
const id = await backupSites.add(backupSite, auditSource);
2026-02-18 22:21:54 +01:00
assert.ok(id);
2025-09-12 09:48:37 +02:00
backupSite.id = id;
});
it('can list backup sites', async function () {
2025-09-29 14:44:42 +02:00
const result = await backupSites.list();
2026-02-18 22:21:54 +01:00
assert.equal(result.length, 2);
2025-09-12 09:48:37 +02:00
});
it('can get backup site', async function () {
2026-02-18 08:18:37 +01:00
const result = await backupSites.get(defaultBackupSite.id);
2026-02-18 22:21:54 +01:00
assert.equal(result.provider, 'filesystem');
assert.ok(result.config.backupDir); // the test sets this to some tmp location
assert.equal(result.format, 'tgz');
assert.equal(result.encryption, null);
assert.equal(result.contents, null);
assert.equal(result.enableForUpdates, true);
2025-09-12 09:48:37 +02:00
});
it('cannot get random backup site', async function () {
2026-02-18 08:18:37 +01:00
const result = await backupSites.get('random');
2026-02-18 22:21:54 +01:00
assert.equal(result, null);
2025-09-12 09:48:37 +02:00
});
it('can set backup config', async function () {
const newConfig = Object.assign({}, defaultBackupSite.config, { backupDir: '/tmp/backups' });
await backupSites.setConfig(defaultBackupSite, newConfig, auditSource);
const result = await backupSites.get(defaultBackupSite.id);
2026-02-18 22:21:54 +01:00
assert.equal(result.config.backupDir, '/tmp/backups');
2025-09-12 09:48:37 +02:00
});
it('cannot set invalid schedule', async function () {
const [error] = await safe(backupSites.setSchedule(defaultBackupSite, '', auditSource));
2026-02-18 22:21:54 +01:00
assert.equal(error.reason, BoxError.BAD_FIELD);
2025-09-12 09:48:37 +02:00
});
it('can set valid schedule', async function () {
for (const pattern of [ '00 * * * * *', constants.CRON_PATTERN_NEVER ]) {
await backupSites.setSchedule(defaultBackupSite, pattern, auditSource);
2026-02-18 08:18:37 +01:00
const result = await backupSites.get(defaultBackupSite.id);
2026-02-18 22:21:54 +01:00
assert.equal(result.schedule, pattern);
2025-09-12 09:48:37 +02:00
}
});
it('cannot set invalid retention', async function () {
const [error] = await safe(backupSites.setRetention(defaultBackupSite, { keepWhenever: 4 }, auditSource));
2026-02-18 22:21:54 +01:00
assert.equal(error.reason, BoxError.BAD_FIELD);
2025-09-12 09:48:37 +02:00
});
it('can set valid retention', async function () {
for (const retention of [ { keepWithinSecs: 1 }, { keepYearly: 3 }, { keepMonthly: 14 } ]) {
await backupSites.setRetention(defaultBackupSite, retention, auditSource);
2026-02-18 08:18:37 +01:00
const result = await backupSites.get(defaultBackupSite.id);
2026-02-18 22:21:54 +01:00
assert.deepEqual(result.retention, retention);
2025-09-12 09:48:37 +02:00
}
});
2025-09-22 20:02:13 +02:00
it('cannot disable for update', async function () {
await backupSites.setEnabledForUpdates(defaultBackupSite, false, auditSource);
2026-02-18 08:18:37 +01:00
const result = await backupSites.get(defaultBackupSite.id);
2026-02-18 22:21:54 +01:00
assert.deepEqual(result.enableForUpdates, false);
2025-09-12 09:48:37 +02:00
});
2025-09-22 20:02:13 +02:00
it('can delete all the backup sites', async function () {
2025-09-12 09:48:37 +02:00
await backupSites.del(defaultBackupSite, auditSource);
2025-09-24 14:09:06 +02:00
backupSite.integrityKeyPair = {}; // keep removePrivateFields happy
2025-09-22 20:02:13 +02:00
await backupSites.del(backupSite, auditSource);
2026-02-18 22:21:54 +01:00
assert.deepEqual(await backups.listByTypePaged(backups.BACKUP_TYPE_BOX, defaultBackupSite.id, 1, 10), []);
assert.deepEqual(await archives.list(1, 10), []);
assert.deepEqual(await backupSites.list(), []);
2025-09-12 09:48:37 +02:00
});
});