diff --git a/src/mounts.js b/src/mounts.js index 67c980e07..96ddb5f4e 100644 --- a/src/mounts.js +++ b/src/mounts.js @@ -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 } diff --git a/src/test/volumes-test.js b/src/test/volumes-test.js index f8fd895ca..fec0de57f 100644 --- a/src/test/volumes-test.js +++ b/src/test/volumes-test.js @@ -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); }); diff --git a/src/volumes.js b/src/volumes.js index fcd462921..3702f27d2 100644 --- a/src/volumes.js +++ b/src/volumes.js @@ -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' });