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

137 lines
4.9 KiB
JavaScript
Raw Normal View History

2025-09-12 09:48:37 +02:00
/* jslint node:true */
import * as archives from '../archives.js';
import backups from '../backups.js';
import * as backupSites from '../backupsites.js';
import BoxError from '../boxerror.js';
import * as common from './common.js';
import constants from '../constants.js';
import expect from 'expect.js';
import safe from 'safetydance';
2025-09-12 09:48:37 +02:00
/* global it:false */
/* global describe:false */
/* global before:false */
/* global after:false */
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);
expect(id).to.be.ok();
backupSite.id = id;
});
it('can list backup sites', async function () {
2025-09-29 14:44:42 +02:00
const result = await backupSites.list();
2025-09-12 09:48:37 +02:00
expect(result.length).to.be(2);
});
it('can get backup site', async function () {
const backupSite = await backupSites.get(defaultBackupSite.id);
expect(backupSite.provider).to.be('filesystem');
expect(backupSite.config.backupDir).to.be.ok(); // the test sets this to some tmp location
expect(backupSite.format).to.be('tgz');
expect(backupSite.encryption).to.be(null);
2025-09-22 20:02:13 +02:00
expect(backupSite.contents).to.be(null);
expect(backupSite.enableForUpdates).to.be(true);
2025-09-12 09:48:37 +02:00
});
it('cannot get random backup site', async function () {
const backupSite = await backupSites.get('random');
expect(backupSite).to.be(null);
});
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);
expect(result.config.backupDir).to.be('/tmp/backups');
});
it('cannot set invalid schedule', async function () {
const [error] = await safe(backupSites.setSchedule(defaultBackupSite, '', auditSource));
expect(error.reason).to.be(BoxError.BAD_FIELD);
});
it('can set valid schedule', async function () {
for (const pattern of [ '00 * * * * *', constants.CRON_PATTERN_NEVER ]) {
await backupSites.setSchedule(defaultBackupSite, pattern, auditSource);
const backupSite = await backupSites.get(defaultBackupSite.id);
expect(backupSite.schedule).to.be(pattern);
}
});
it('cannot set invalid retention', async function () {
const [error] = await safe(backupSites.setRetention(defaultBackupSite, { keepWhenever: 4 }, auditSource));
expect(error.reason).to.be(BoxError.BAD_FIELD);
});
it('can set valid retention', async function () {
for (const retention of [ { keepWithinSecs: 1 }, { keepYearly: 3 }, { keepMonthly: 14 } ]) {
await backupSites.setRetention(defaultBackupSite, retention, auditSource);
const backupSite = await backupSites.get(defaultBackupSite.id);
expect(backupSite.retention).to.eql(retention);
}
});
2025-09-22 20:02:13 +02:00
it('cannot disable for update', async function () {
await backupSites.setEnabledForUpdates(defaultBackupSite, false, auditSource);
const backupSite = await backupSites.get(defaultBackupSite.id);
expect(backupSite.enableForUpdates).to.eql(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);
expect(await backups.listByTypePaged(backups.BACKUP_TYPE_BOX, defaultBackupSite.id, 1, 10)).to.eql([]);
2025-09-12 09:48:37 +02:00
expect(await archives.list(1, 10)).to.eql([]);
2025-09-29 14:44:42 +02:00
expect(await backupSites.list()).to.eql([]);
2025-09-12 09:48:37 +02:00
});
});