96 lines
3.7 KiB
JavaScript
96 lines
3.7 KiB
JavaScript
import { describe, it, before, after } from 'node:test';
|
|
/* jslint node:true */
|
|
|
|
import BoxError from '../boxerror.js';
|
|
import common from './common.js';
|
|
import assert from 'node:assert/strict';
|
|
import paths from '../paths.js';
|
|
import safe from 'safetydance';
|
|
import volumes from '../volumes.js';
|
|
|
|
|
|
describe('Volumes', function () {
|
|
const { setup, cleanup, auditSource } = common;
|
|
|
|
before(setup);
|
|
after(cleanup);
|
|
|
|
it('cannot add bad name', async function () {
|
|
const [error] = await safe(volumes.add({ name: 'music/is', hostPath: '/tmp/music', mountType: 'filesystem', mountOptions: {} }, auditSource));
|
|
if (!error) throw new Error('Expecting bad field error');
|
|
assert.equal(error.reason, BoxError.BAD_FIELD);
|
|
});
|
|
|
|
it('cannot add bad path', async function () {
|
|
const [error] = await safe(volumes.add({ name: 'music', hostPath: '/tmp/music', mountType: 'filesystem', mountOptions: {} }, auditSource));
|
|
if (!error) throw new Error('Expecting bad field error');
|
|
assert.equal(error.reason, BoxError.BAD_FIELD);
|
|
});
|
|
|
|
let volume;
|
|
it('can add volume', async function () {
|
|
const id = await volumes.add({ name: 'music', mountType: 'filesystem', mountOptions: { hostPath: '/mnt/cloudron-test-music' } }, auditSource);
|
|
assert.equal(typeof id, 'string');
|
|
volume = { id, name: 'music', hostPath: '/mnt/cloudron-test-music' };
|
|
});
|
|
|
|
it('cannot add duplicate path', async function () {
|
|
const [error] = await safe(volumes.add({ name: 'music-dup', mountType: 'filesystem', mountOptions: { hostPath: '/mnt/cloudron-test-music' } }, auditSource));
|
|
assert.equal(error.reason, BoxError.ALREADY_EXISTS);
|
|
});
|
|
|
|
it('cannot add duplicate name', async function () {
|
|
const [error] = await safe(volumes.add({ name: 'music', mountType: 'filesystem', mountOptions: { hostPath: '/mnt/cloudron-test-music2' } }, auditSource));
|
|
assert.equal(error.reason, BoxError.ALREADY_EXISTS);
|
|
});
|
|
|
|
it('can get volume', async function () {
|
|
const result = await volumes.get(volume.id);
|
|
assert.equal(result.hostPath, '/mnt/cloudron-test-music');
|
|
});
|
|
|
|
it('cannot get random volume', async function () {
|
|
const result = await volumes.get('randomvolume');
|
|
assert.equal(result, null);
|
|
});
|
|
|
|
it('can list volumes', async function () {
|
|
const result = await volumes.list();
|
|
assert.ok(Array.isArray(result));
|
|
assert.equal(result.length, 1);
|
|
assert.equal(result[0].id, volume.id);
|
|
assert.equal(result[0].hostPath, '/mnt/cloudron-test-music');
|
|
});
|
|
|
|
it('cannot del random volume', async function () {
|
|
const [error] = await safe(volumes.del({ id: 'randomvolume' }, auditSource));
|
|
assert.equal(error.reason, BoxError.NOT_FOUND);
|
|
});
|
|
|
|
it('can del volume', async function () {
|
|
await volumes.del(volume, auditSource);
|
|
});
|
|
|
|
const badPaths = [
|
|
'/opt/data/../bar', // not normalized
|
|
'opt/data/bar', // not absolute
|
|
'/', // root
|
|
'/mnt', // top level itself is reserved
|
|
'/usr/bin', // reserved
|
|
paths.VOLUMES_MOUNT_DIR, // reserved
|
|
paths.VOLUMES_MOUNT_DIR + '/', // also reserved
|
|
paths.VOLUMES_MOUNT_DIR + '/something', // also reserved
|
|
'/media/cloudron-test-music/root', // realpath won't match
|
|
'/media/cloudron-test-music/root/', // realpath won't match
|
|
'/media/cloudron-test-music/file', // need directory
|
|
'/media/cloudron-test-music/randompath', // need directory
|
|
];
|
|
|
|
for (const p of badPaths) {
|
|
it(`validateHostPath - ${p}`, function () {
|
|
const error = volumes._validateHostPath(p);
|
|
assert.equal(error.reason, BoxError.BAD_FIELD);
|
|
});
|
|
}
|
|
});
|