apps: rework portBindings

ports is REST API input . Map of env var to the host port
portBinding is the database structure. Map of env var to host port, count, type etc

also, rename portCount -> count in various places to keep things consistent
This commit is contained in:
Girish Ramakrishnan
2024-07-16 22:21:36 +02:00
parent eb314ef507
commit aeddaa4566
12 changed files with 151 additions and 147 deletions
+22 -22
View File
@@ -21,7 +21,7 @@ describe('Apps', function () {
describe('checkForPortBindingConflict', function () {
before(async function () {
await apps.add(app.id, app.appStoreId, app.manifest, app.subdomain, app.domain, [{ hostPort: 40000, type: 'tcp', portCount: 100 }, { hostPort: 50000, type: 'udp', portCount: 1 }], app);
await apps.add(app.id, app.appStoreId, app.manifest, app.subdomain, app.domain, [{ hostPort: 40000, type: 'tcp', count: 100 }, { hostPort: 50000, type: 'udp', count: 1 }], app);
});
after(async function () {
@@ -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', count: 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', count: 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', count: 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', count: 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', count: 2 }], {});
await apps._checkForPortBindingConflict([{ hostPort: 45000, type: 'tcp', count: 1 }], {});
await apps._checkForPortBindingConflict([{ hostPort: 49995, type: 'udp', count: 2 }], {});
await apps._checkForPortBindingConflict([{ hostPort: 50001, type: 'udp', count: 1 }], {});
});
});
@@ -71,29 +71,29 @@ describe('Apps', function () {
describe('validatePortBindings', function () {
it('does not allow invalid host port', function () {
expect(apps._validatePortBindings({ PORT: -1 }, { tcpPorts: { PORT: 5000 } })).to.be.an(Error);
expect(apps._validatePortBindings({ PORT: 0 }, { tcpPorts: { PORT: 5000 } })).to.be.an(Error);
expect(apps._validatePortBindings({ PORT: 'text' }, { tcpPorts: { PORT: 5000 } })).to.be.an(Error);
expect(apps._validatePortBindings({ PORT: 65536 }, { tcpPorts: { PORT: 5000 } })).to.be.an(Error);
expect(apps._validatePortBindings({ PORT: 470 }, { tcpPorts: { PORT: 5000 } })).to.be.an(Error);
expect(apps._validatePorts({ PORT: -1 }, { tcpPorts: { PORT: 5000 } })).to.be.an(Error);
expect(apps._validatePorts({ PORT: 0 }, { tcpPorts: { PORT: 5000 } })).to.be.an(Error);
expect(apps._validatePorts({ PORT: 'text' }, { tcpPorts: { PORT: 5000 } })).to.be.an(Error);
expect(apps._validatePorts({ PORT: 65536 }, { tcpPorts: { PORT: 5000 } })).to.be.an(Error);
expect(apps._validatePorts({ PORT: 470 }, { tcpPorts: { PORT: 5000 } })).to.be.an(Error);
});
it('does not allow ports not as part of manifest', function () {
expect(apps._validatePortBindings({ PORT: 1567 }, { tcpPorts: { } })).to.be.an(Error);
expect(apps._validatePortBindings({ PORT: 1567 }, { tcpPorts: { port3: null } })).to.be.an(Error);
expect(apps._validatePorts({ PORT: 1567 }, { tcpPorts: { } })).to.be.an(Error);
expect(apps._validatePorts({ PORT: 1567 }, { tcpPorts: { port3: null } })).to.be.an(Error);
});
it('does not allow reserved ports', function () {
expect(apps._validatePortBindings({ PORT: 443 }, { tcpPorts: { PORT: 5000 } })).to.be.an(Error);
expect(apps._validatePortBindings({ PORT: 50000 }, { tcpPorts: { PORT: 5000 } })).to.be.an(Error);
expect(apps._validatePortBindings({ PORT: 51000 }, { tcpPorts: { PORT: 5000 } })).to.be.an(Error);
expect(apps._validatePortBindings({ PORT: 50100 }, { tcpPorts: { PORT: 5000 } })).to.be.an(Error);
expect(apps._validatePorts({ PORT: 443 }, { tcpPorts: { PORT: 5000 } })).to.be.an(Error);
expect(apps._validatePorts({ PORT: 50000 }, { tcpPorts: { PORT: 5000 } })).to.be.an(Error);
expect(apps._validatePorts({ PORT: 51000 }, { tcpPorts: { PORT: 5000 } })).to.be.an(Error);
expect(apps._validatePorts({ PORT: 50100 }, { tcpPorts: { PORT: 5000 } })).to.be.an(Error);
});
it('allows valid bindings', function () {
expect(apps._validatePortBindings({ PORT: 1024 }, { tcpPorts: { PORT: 5000 } })).to.be(null);
expect(apps._validatePorts({ PORT: 1024 }, { tcpPorts: { PORT: 5000 } })).to.be(null);
expect(apps._validatePortBindings({
expect(apps._validatePorts({
PORT1: 4033,
PORT2: 3242,
PORT3: 1234