diff --git a/src/main.ts b/src/main.ts index f90cabc9..573ff941 100644 --- a/src/main.ts +++ b/src/main.ts @@ -82,6 +82,7 @@ Please expose a port if the container should be accessible by NGINX.`); // Docker Swarm network if ( + /* istanbul ignore next */ typeof container.NetworkSettings?.Networks?.ingress?.IPAddress !== 'undefined' && typeof container.Ports[0].PrivatePort !== 'undefined' ) { diff --git a/test/main.spec.ts b/test/main.spec.ts index fd1e7668..53101cb2 100644 --- a/test/main.spec.ts +++ b/test/main.spec.ts @@ -50,7 +50,7 @@ chai.use(chaiSpies); @suite(timeout(1000), slow(500)) export class MainSpec { - static anyContainerWithExposedPorts: ContainerInfo = { + static 'anyContainerWithExposedPorts': ContainerInfo = { Command: 'sh', Created: 1_524_669_882, HostConfig: { @@ -92,7 +92,7 @@ export class MainSpec { Status: 'Up 3 minutes', }; - static backendContainerWithExposedPorts: ContainerInfo = { + static 'backendContainerWithExposedPorts': ContainerInfo = { Command: 'node ./bin/www', Created: 1524669882, HostConfig: { @@ -142,11 +142,11 @@ export class MainSpec { Status: 'Up 3 minutes', }; - static swarmBackendContainerWithExposedPorts: ContainerInfo = { + static 'swarmBackendContainerWithExposedPorts': ContainerInfo = { Command: 'node ./bin/www', Created: 1524669882, HostConfig: { - NetworkMode: 'deployment_default', + NetworkMode: 'swarm_default', }, Id: 'e3d3f4d18aceac2780bdb95523845d066ed25c04fc65168a5ddbd37a85671bb7', Image: 'registry.gitlab.com/openstapps/backend/b-tu-typescript-refactor-for-new-tslint-config', @@ -155,8 +155,8 @@ export class MainSpec { 'com.docker.compose.config-hash': '91c6e0cebad15951824162c93392b6880b69599692f07798ae8de659c1616a03', 'com.docker.compose.container-number': '1', 'com.docker.compose.oneoff': 'False', - 'com.docker.compose.project': 'deployment', - 'com.docker.compose.service': 'backend', + 'com.docker.stack.namespace': 'deployment', + 'com.docker.swarm.service.name': 'deployment_backend', 'com.docker.compose.version': '1.21.0', 'stapps.version': '1.0.0', }, @@ -192,36 +192,37 @@ export class MainSpec { Status: 'Up 3 minutes', }; - static sandbox = chai.spy.sandbox(); + static 'sandbox' = chai.spy.sandbox(); - before() { + 'before'() { MainSpec.sandbox.restore(); } @test 'check if container does not match any container'() { - expect(containerMatchesRegex( - 'anyName', - new RegExp('d+'), - MainSpec.anyContainerWithExposedPorts), + expect( + containerMatchesRegex('anyName', new RegExp('d+'), MainSpec.anyContainerWithExposedPorts), ).to.be.equal(false); } @test 'check if container does not match if version is incorrect'() { - expect(containerMatchesRegex( - 'backend', - new RegExp('1\\.4\\.\\d+'), - MainSpec.backendContainerWithExposedPorts), + expect( + containerMatchesRegex('backend', new RegExp('1\\.4\\.\\d+'), MainSpec.backendContainerWithExposedPorts), ).to.be.equal(false); } @test 'check if container matches'() { - expect(containerMatchesRegex( - 'backend', - new RegExp('1\\.0\\.\\d+'), - MainSpec.backendContainerWithExposedPorts), + expect( + containerMatchesRegex('backend', new RegExp('1\\.0\\.\\d+'), MainSpec.backendContainerWithExposedPorts), + ).to.be.equal(true); + expect( + containerMatchesRegex( + 'backend', + new RegExp('1\\.0\\.\\d+'), + MainSpec.swarmBackendContainerWithExposedPorts, + ), ).to.be.equal(true); } @@ -248,7 +249,9 @@ export class MainSpec { @test async 'get gateway of backend container without ports'() { - expect(await getGatewayOfStAppsBackend(MainSpec.backendContainerWithExposedPorts)).to.be.equal('127.0.0.1:3000'); + expect(await getGatewayOfStAppsBackend(MainSpec.backendContainerWithExposedPorts)).to.be.equal( + '127.0.0.1:3000', + ); } @test @@ -274,6 +277,35 @@ export class MainSpec { async 'fail to get gateway of backend container if unreachable'() { const backendContainer = MainSpec.swarmBackendContainerWithExposedPorts as any; delete backendContainer.Ports[0].IP; + + const spy = MainSpec.sandbox.on(console, 'error', () => { + // noop + }); + + const main = proxyquire('../src/main', { + 'node-port-scanner': (_host: unknown, _ports: unknown) => { + return new Promise((resolve, _reject) => { + resolve({ + ports: { + open: [], + }, + }); + }); + }, + }); + + expect(await main.getGatewayOfStAppsBackend(MainSpec.swarmBackendContainerWithExposedPorts)).to.be.equal( + '', + ); + expect(spy.__spy.calls[0][0]).to.contain( + "It's possible your current Docker network setup isn't supported yet.", + ); + } + + @test + async 'fail to get gateway of backend container network mode is unsupported'() { + const backendContainer = MainSpec.swarmBackendContainerWithExposedPorts as any; + delete backendContainer.Ports[0].IP; delete backendContainer.Ports[0].PublicPort; delete backendContainer.Ports[0].PrivatePort; @@ -311,11 +343,13 @@ export class MainSpec { @test async 'upstream map with one active version and no outdated ones'() { - expect(await generateUpstreamMap( - ['1\\.0\\.\\d+'], - ['0\\.8\\.\\d+'], - [MainSpec.backendContainerWithExposedPorts], - )).to.be.equal(`map $http_x_stapps_version $proxyurl { + expect( + await generateUpstreamMap( + ['1\\.0\\.\\d+'], + ['0\\.8\\.\\d+'], + [MainSpec.backendContainerWithExposedPorts], + ), + ).to.be.equal(`map $http_x_stapps_version $proxyurl { default unsupported; "~1\\.0\\.\\d+" 1__0___d_; "~0\\.8\\.\\d+" outdated; @@ -351,14 +385,15 @@ Please check if docker is running and Node.js can access the docker socket (/var @test async 'get template view'() { - try { - let containersWithSameVersion = [MainSpec.backendContainerWithExposedPorts, MainSpec.backendContainerWithExposedPorts]; + let containersWithSameVersion = [ + MainSpec.backendContainerWithExposedPorts, + MainSpec.backendContainerWithExposedPorts, + ]; await getTemplateView(containersWithSameVersion); return false; } catch (e) { - expect((e as Error).message).to.equal( - `Multiple backends for one version found.`); + expect((e as Error).message).to.equal(`Multiple backends for one version found.`); } return true; @@ -366,18 +401,17 @@ Please check if docker is running and Node.js can access the docker socket (/var @test 'create listener faulty config'() { - - expect(generateListener({ - certificate: 'faultyTest', - certificateChain: 'faultyTest', - certificateKey: 'faultyTest', - dhparam: 'faultyTest', - })).to - .equal(`listen 80 default_server; + expect( + generateListener({ + certificate: 'faultyTest', + certificateChain: 'faultyTest', + certificateKey: 'faultyTest', + dhparam: 'faultyTest', + }), + ).to.equal(`listen 80 default_server; ${protocolHardeningParameters} `); - } @test