fix volume test

This commit is contained in:
Girish Ramakrishnan
2021-05-17 16:23:24 -07:00
parent 124954d490
commit c7474511aa
3 changed files with 12 additions and 6 deletions
+5
View File
@@ -9,6 +9,7 @@ exports = module.exports = {
const assert = require('assert'),
BoxError = require('./boxerror.js'),
constants = require('./constants.js'),
ejs = require('ejs'),
fs = require('fs'),
path = require('path'),
@@ -48,6 +49,8 @@ function validateMountOptions(type, options) {
async function writeMountFile(volume) {
assert.strictEqual(typeof volume, 'object');
if (constants.TEST) return;
const {name, hostPath, mountType, mountOptions} = volume;
let options, what, type;
@@ -80,6 +83,8 @@ async function writeMountFile(volume) {
async function removeMountFile(hostPath) {
assert.strictEqual(typeof hostPath, 'string');
if (constants.TEST) return;
await safe(shell.promises.sudo('generateMountFile', [ RM_MOUNT_CMD, hostPath ], {})); // ignore any error
}
+5 -5
View File
@@ -35,31 +35,31 @@ describe('Volumes', function () {
after(cleanup);
it('cannot add bad name', async function () {
const [error] = await safe(volumes.add('music/is', '/tmp/music', AUDIT_SOURCE));
const [error] = await safe(volumes.add({ name: 'music/is', hostPath: '/tmp/music', mountType: 'noop', mountOptions: {} }, AUDIT_SOURCE));
if (!error) throw new Error('Expecting bad field error');
expect(error.reason).to.be(BoxError.BAD_FIELD);
});
it('cannot add bad path', async function () {
const [error] = await safe(volumes.add('music', '/tmp/music', AUDIT_SOURCE));
const [error] = await safe(volumes.add({ name: 'music', hostPath: '/tmp/music', mountType: 'noop', mountOptions: {} }, AUDIT_SOURCE));
if (!error) throw new Error('Expecting bad field error');
expect(error.reason).to.be(BoxError.BAD_FIELD);
});
let volume;
it('can add volume', async function () {
const id = await volumes.add('music', '/mnt/cloudron-test-music', AUDIT_SOURCE);
const id = await volumes.add({ name: 'music', hostPath: '/mnt/cloudron-test-music', mountType: 'noop', mountOptions: {} }, AUDIT_SOURCE);
expect(id).to.be.a('string');
volume = { id, name: 'music', hostPath: '/mnt/cloudron-test-music' };
});
it('cannot add duplicate path', async function () {
const [error] = await safe(volumes.add('music-dup', '/mnt/cloudron-test-music', AUDIT_SOURCE));
const [error] = await safe(volumes.add({ name: 'music-dup', hostPath: '/mnt/cloudron-test-music', mountType: 'noop', mountOptions: {} }, AUDIT_SOURCE));
expect(error.reason).to.be(BoxError.ALREADY_EXISTS);
});
it('cannot add duplicate name', async function () {
const [error] = await safe(volumes.add('music', '/media/cloudron-test-music', AUDIT_SOURCE));
const [error] = await safe(volumes.add({ name: 'music', hostPath: '/mnt/cloudron-test-music2', mountType: 'noop', mountOptions: {} }, AUDIT_SOURCE));
expect(error.reason).to.be(BoxError.ALREADY_EXISTS);
});
+2 -1
View File
@@ -12,6 +12,7 @@ exports = module.exports = {
const assert = require('assert'),
BoxError = require('./boxerror.js'),
collectd = require('./collectd.js'),
constants = require('./constants.js'),
database = require('./database.js'),
debug = require('debug')('box:volumes'),
ejs = require('ejs'),
@@ -59,7 +60,7 @@ function validateHostPath(hostPath, mountType) {
if (!allowedPaths.some(p => hostPath.startsWith(p))) return new BoxError(BoxError.BAD_FIELD, 'hostPath must be under /mnt, /media, /opt or /srv', { field: 'hostPath' });
if (mountType === 'noop') { // we expect user to have already mounted this
if (!constants.TEST && mountType === 'noop') { // we expect user to have already mounted this
const stat = safe.fs.lstatSync(hostPath);
if (!stat) return new BoxError(BoxError.BAD_FIELD, 'hostPath does not exist. Please create it on the server first', { field: 'hostPath' });
if (!stat.isDirectory()) return new BoxError(BoxError.BAD_FIELD, 'hostPath is not a directory', { field: 'hostPath' });