diff --git a/src/apps.js b/src/apps.js index 85b395c1a..1647755f6 100644 --- a/src/apps.js +++ b/src/apps.js @@ -794,13 +794,13 @@ function accessLevel(app, user) { return canAccess(app, user) ? 'user' : null; } -async function checkForPortBindingConflict(portBindings, id = '') { +async function checkForPortBindingConflict(portBindings, options) { assert.strictEqual(typeof portBindings, 'object'); - assert.strictEqual(typeof id, 'string'); + assert.strictEqual(typeof options, 'object'); - let existingPortBindings; - if (id) existingPortBindings = await database.query('SELECT * FROM appPortBindings WHERE appId != ?', [ id ]); - else existingPortBindings = await database.query('SELECT * FROM appPortBindings', []); + const existingPortBindings = options.appId + ? await database.query('SELECT * FROM appPortBindings WHERE appId != ?', [ options.appId ]) + : await database.query('SELECT * FROM appPortBindings', []); if (existingPortBindings.length === 0) return; @@ -863,7 +863,7 @@ async function add(id, appStoreId, manifest, subdomain, domain, portBindings, da enableRedis = 'enableRedis' in data ? data.enableRedis : true, icon = data.icon || null; - await checkForPortBindingConflict(portBindings); + await checkForPortBindingConflict(portBindings, {}); const queries = []; @@ -955,7 +955,7 @@ async function updateWithConstraints(id, app, constraints) { if ('portBindings' in app) { const portBindings = app.portBindings || { }; - await checkForPortBindingConflict(portBindings, id); + await checkForPortBindingConflict(portBindings, { appId: id }); // replace entries by app id queries.push({ query: 'DELETE FROM appPortBindings WHERE appId = ?', args: [ id ] }); diff --git a/src/test/apps-test.js b/src/test/apps-test.js index 4577e96cb..03093d3a0 100644 --- a/src/test/apps-test.js +++ b/src/test/apps-test.js @@ -29,26 +29,26 @@ describe('Apps', function () { }); it('throws on exact conflict', async function () { - let [error] = await safe(apps._checkForPortBindingConflict([{ hostPort: 40000, type: 'tcp', portCount: 1 }])); + let [error] = await safe(apps._checkForPortBindingConflict([{ hostPort: 40000, type: 'tcp', portCount: 1 }], {})); expect(error.reason).to.equal(BoxError.CONFLICT); - [error] = await safe(apps._checkForPortBindingConflict([{ hostPort: 50000, type: 'udp', portCount: 1 }])); + [error] = await safe(apps._checkForPortBindingConflict([{ hostPort: 50000, type: 'udp', portCount: 1 }], {})); expect(error.reason).to.equal(BoxError.CONFLICT); }); it('throws on range conflict', async function () { - let [error] = await safe(apps._checkForPortBindingConflict([{ hostPort: 40080, type: 'tcp', portCount: 40 }])); + let [error] = await safe(apps._checkForPortBindingConflict([{ hostPort: 40080, type: 'tcp', portCount: 40 }], {})); expect(error.reason).to.equal(BoxError.CONFLICT); - [error] = await safe(apps._checkForPortBindingConflict([{ hostPort: 49995, type: 'udp', portCount: 20 }])); + [error] = await safe(apps._checkForPortBindingConflict([{ hostPort: 49995, type: 'udp', portCount: 20 }], {})); expect(error.reason).to.equal(BoxError.CONFLICT); }); it('succeeds without conflict', async function () { - await apps._checkForPortBindingConflict([{ hostPort: 39995, type: 'tcp', portCount: 2 }]); - await apps._checkForPortBindingConflict([{ hostPort: 45000, type: 'tcp', portCount: 1 }]); - await apps._checkForPortBindingConflict([{ hostPort: 49995, type: 'udp', portCount: 2 }]); - await apps._checkForPortBindingConflict([{ hostPort: 50001, type: 'udp', portCount: 1 }]); + await apps._checkForPortBindingConflict([{ hostPort: 39995, type: 'tcp', portCount: 2 }], {}); + await apps._checkForPortBindingConflict([{ hostPort: 45000, type: 'tcp', portCount: 1 }], {}); + await apps._checkForPortBindingConflict([{ hostPort: 49995, type: 'udp', portCount: 2 }], {}); + await apps._checkForPortBindingConflict([{ hostPort: 50001, type: 'udp', portCount: 1 }], {}); }); });