Files
cloudron-box/src/test/volumes-test.js
T
Girish Ramakrishnan d0a66f1701 Back to mocha!
sorry i ever left you dear mocha

node:test has two major issues:
* --bail does not work and requires strange modules and incantations.
I was able to work around this with a custom module.

* the test reporter reports _after_ the suite is run. this makes debugging
really hard. the debugs that we print all happen before the test suite summary.
poor design overall.
2026-02-19 13:24:14 +01:00

94 lines
3.7 KiB
JavaScript

import { describe, it, before, after } from 'mocha';
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);
});
}
});