test: expand test coverage

This commit is contained in:
Rainer Killinger
2022-05-10 12:58:22 +02:00
parent 4bb46d8a06
commit 39e710e685
2 changed files with 74 additions and 39 deletions

View File

@@ -82,6 +82,7 @@ Please expose a port if the container should be accessible by NGINX.`);
// Docker Swarm network // Docker Swarm network
if ( if (
/* istanbul ignore next */
typeof container.NetworkSettings?.Networks?.ingress?.IPAddress !== 'undefined' && typeof container.NetworkSettings?.Networks?.ingress?.IPAddress !== 'undefined' &&
typeof container.Ports[0].PrivatePort !== 'undefined' typeof container.Ports[0].PrivatePort !== 'undefined'
) { ) {

View File

@@ -50,7 +50,7 @@ chai.use(chaiSpies);
@suite(timeout(1000), slow(500)) @suite(timeout(1000), slow(500))
export class MainSpec { export class MainSpec {
static anyContainerWithExposedPorts: ContainerInfo = { static 'anyContainerWithExposedPorts': ContainerInfo = {
Command: 'sh', Command: 'sh',
Created: 1_524_669_882, Created: 1_524_669_882,
HostConfig: { HostConfig: {
@@ -92,7 +92,7 @@ export class MainSpec {
Status: 'Up 3 minutes', Status: 'Up 3 minutes',
}; };
static backendContainerWithExposedPorts: ContainerInfo = { static 'backendContainerWithExposedPorts': ContainerInfo = {
Command: 'node ./bin/www', Command: 'node ./bin/www',
Created: 1524669882, Created: 1524669882,
HostConfig: { HostConfig: {
@@ -142,11 +142,11 @@ export class MainSpec {
Status: 'Up 3 minutes', Status: 'Up 3 minutes',
}; };
static swarmBackendContainerWithExposedPorts: ContainerInfo = { static 'swarmBackendContainerWithExposedPorts': ContainerInfo = {
Command: 'node ./bin/www', Command: 'node ./bin/www',
Created: 1524669882, Created: 1524669882,
HostConfig: { HostConfig: {
NetworkMode: 'deployment_default', NetworkMode: 'swarm_default',
}, },
Id: 'e3d3f4d18aceac2780bdb95523845d066ed25c04fc65168a5ddbd37a85671bb7', Id: 'e3d3f4d18aceac2780bdb95523845d066ed25c04fc65168a5ddbd37a85671bb7',
Image: 'registry.gitlab.com/openstapps/backend/b-tu-typescript-refactor-for-new-tslint-config', 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.config-hash': '91c6e0cebad15951824162c93392b6880b69599692f07798ae8de659c1616a03',
'com.docker.compose.container-number': '1', 'com.docker.compose.container-number': '1',
'com.docker.compose.oneoff': 'False', 'com.docker.compose.oneoff': 'False',
'com.docker.compose.project': 'deployment', 'com.docker.stack.namespace': 'deployment',
'com.docker.compose.service': 'backend', 'com.docker.swarm.service.name': 'deployment_backend',
'com.docker.compose.version': '1.21.0', 'com.docker.compose.version': '1.21.0',
'stapps.version': '1.0.0', 'stapps.version': '1.0.0',
}, },
@@ -192,36 +192,37 @@ export class MainSpec {
Status: 'Up 3 minutes', Status: 'Up 3 minutes',
}; };
static sandbox = chai.spy.sandbox(); static 'sandbox' = chai.spy.sandbox();
before() { 'before'() {
MainSpec.sandbox.restore(); MainSpec.sandbox.restore();
} }
@test @test
'check if container does not match any container'() { 'check if container does not match any container'() {
expect(containerMatchesRegex( expect(
'anyName', containerMatchesRegex('anyName', new RegExp('d+'), MainSpec.anyContainerWithExposedPorts),
new RegExp('d+'),
MainSpec.anyContainerWithExposedPorts),
).to.be.equal(false); ).to.be.equal(false);
} }
@test @test
'check if container does not match if version is incorrect'() { 'check if container does not match if version is incorrect'() {
expect(containerMatchesRegex( expect(
'backend', containerMatchesRegex('backend', new RegExp('1\\.4\\.\\d+'), MainSpec.backendContainerWithExposedPorts),
new RegExp('1\\.4\\.\\d+'),
MainSpec.backendContainerWithExposedPorts),
).to.be.equal(false); ).to.be.equal(false);
} }
@test @test
'check if container matches'() { 'check if container matches'() {
expect(containerMatchesRegex( expect(
'backend', containerMatchesRegex('backend', new RegExp('1\\.0\\.\\d+'), MainSpec.backendContainerWithExposedPorts),
new RegExp('1\\.0\\.\\d+'), ).to.be.equal(true);
MainSpec.backendContainerWithExposedPorts), expect(
containerMatchesRegex(
'backend',
new RegExp('1\\.0\\.\\d+'),
MainSpec.swarmBackendContainerWithExposedPorts,
),
).to.be.equal(true); ).to.be.equal(true);
} }
@@ -248,7 +249,9 @@ export class MainSpec {
@test @test
async 'get gateway of backend container without ports'() { 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 @test
@@ -274,6 +277,35 @@ export class MainSpec {
async 'fail to get gateway of backend container if unreachable'() { async 'fail to get gateway of backend container if unreachable'() {
const backendContainer = MainSpec.swarmBackendContainerWithExposedPorts as any; const backendContainer = MainSpec.swarmBackendContainerWithExposedPorts as any;
delete backendContainer.Ports[0].IP; 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].PublicPort;
delete backendContainer.Ports[0].PrivatePort; delete backendContainer.Ports[0].PrivatePort;
@@ -311,11 +343,13 @@ export class MainSpec {
@test @test
async 'upstream map with one active version and no outdated ones'() { async 'upstream map with one active version and no outdated ones'() {
expect(await generateUpstreamMap( expect(
['1\\.0\\.\\d+'], await generateUpstreamMap(
['0\\.8\\.\\d+'], ['1\\.0\\.\\d+'],
[MainSpec.backendContainerWithExposedPorts], ['0\\.8\\.\\d+'],
)).to.be.equal(`map $http_x_stapps_version $proxyurl { [MainSpec.backendContainerWithExposedPorts],
),
).to.be.equal(`map $http_x_stapps_version $proxyurl {
default unsupported; default unsupported;
"~1\\.0\\.\\d+" 1__0___d_; "~1\\.0\\.\\d+" 1__0___d_;
"~0\\.8\\.\\d+" outdated; "~0\\.8\\.\\d+" outdated;
@@ -351,14 +385,15 @@ Please check if docker is running and Node.js can access the docker socket (/var
@test @test
async 'get template view'() { async 'get template view'() {
try { try {
let containersWithSameVersion = [MainSpec.backendContainerWithExposedPorts, MainSpec.backendContainerWithExposedPorts]; let containersWithSameVersion = [
MainSpec.backendContainerWithExposedPorts,
MainSpec.backendContainerWithExposedPorts,
];
await getTemplateView(containersWithSameVersion); await getTemplateView(containersWithSameVersion);
return false; return false;
} catch (e) { } catch (e) {
expect((e as Error).message).to.equal( expect((e as Error).message).to.equal(`Multiple backends for one version found.`);
`Multiple backends for one version found.`);
} }
return true; return true;
@@ -366,18 +401,17 @@ Please check if docker is running and Node.js can access the docker socket (/var
@test @test
'create listener faulty config'() { 'create listener faulty config'() {
expect(
expect(generateListener({ generateListener({
certificate: 'faultyTest', certificate: 'faultyTest',
certificateChain: 'faultyTest', certificateChain: 'faultyTest',
certificateKey: 'faultyTest', certificateKey: 'faultyTest',
dhparam: 'faultyTest', dhparam: 'faultyTest',
})).to }),
.equal(`listen 80 default_server; ).to.equal(`listen 80 default_server;
${protocolHardeningParameters} ${protocolHardeningParameters}
`); `);
} }
@test @test