feat: support docker swarm deployments

This commit is contained in:
Rainer Killinger
2022-05-09 13:51:14 +02:00
parent ac144095bf
commit 4bb46d8a06
5 changed files with 204 additions and 17 deletions

View File

@@ -35,6 +35,9 @@ import {
} from '../src/main';
import {resolve} from 'path';
import {mkdirSync, writeFileSync, unlinkSync, rmdirSync} from 'fs';
import proxyquire from 'proxyquire';
proxyquire.callThru().preserveCache();
process.on('unhandledRejection', async error => {
await Logger.error(error);
@@ -139,6 +142,56 @@ export class MainSpec {
Status: 'Up 3 minutes',
};
static swarmBackendContainerWithExposedPorts: ContainerInfo = {
Command: 'node ./bin/www',
Created: 1524669882,
HostConfig: {
NetworkMode: 'deployment_default',
},
Id: 'e3d3f4d18aceac2780bdb95523845d066ed25c04fc65168a5ddbd37a85671bb7',
Image: 'registry.gitlab.com/openstapps/backend/b-tu-typescript-refactor-for-new-tslint-config',
ImageID: 'sha256:ef9f0c8c4b6f99dd208948c7aae1d042590aa18e05ebeae4f586e4b4beebeac9',
Labels: {
'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.compose.version': '1.21.0',
'stapps.version': '1.0.0',
},
Mounts: [],
Names: ['/deployment_backend_1'],
NetworkSettings: {
Networks: {
ingress: {
Aliases: null,
EndpointID: 'da17549a086ff2c9f622e80de833e6f334afda52c8f07080428640c1716dcd14',
Gateway: '172.18.0.1',
GlobalIPv6Address: '',
GlobalIPv6PrefixLen: 0,
IPAMConfig: null,
IPAddress: '172.18.0.3',
IPPrefixLen: 16,
IPv6Gateway: '',
Links: null,
MacAddress: '03:41:ac:11:00:23',
NetworkID: '947ea5247cc7429e1fdebd5404fa4d15f7c05e6765f2b93ddb3bdb6aaffd1193',
},
},
},
Ports: [
{
IP: 'delete me',
PrivatePort: 3000,
PublicPort: 3000,
Type: 'tcp',
},
],
State: 'running',
Status: 'Up 3 minutes',
};
static sandbox = chai.spy.sandbox();
before() {
@@ -199,15 +252,54 @@ export class MainSpec {
}
@test
async 'upstream map calls logger error when no matching container is found'() {
const spy = MainSpec.sandbox.on(console, 'warn', () => {
async 'get gateway of backend container within docker swarm'() {
const backendContainer = MainSpec.swarmBackendContainerWithExposedPorts as any;
delete backendContainer.Ports[0].IP;
const main = proxyquire('../src/main', {
'node-port-scanner': (_host: unknown, _ports: unknown) => {
return new Promise((resolve, _reject) => {
resolve({
ports: {
open: [3000],
},
});
});
},
});
expect(await main.getGatewayOfStAppsBackend(backendContainer)).to.be.equal('172.18.0.3:3000');
}
@test
async 'fail to get gateway of backend container if unreachable'() {
const backendContainer = MainSpec.swarmBackendContainerWithExposedPorts as any;
delete backendContainer.Ports[0].IP;
delete backendContainer.Ports[0].PublicPort;
delete backendContainer.Ports[0].PrivatePort;
const spy = MainSpec.sandbox.on(console, 'error', () => {
// noop
});
expect(await generateUpstreamMap(
['0\\.8\\.\\d+'],
['1\\.1\\.\\d+'],
[MainSpec.backendContainerWithExposedPorts],
)).to.be.equal(`map $http_x_stapps_version $proxyurl {
expect(await 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 'upstream map calls logger error when no matching container is found'() {
const spy = MainSpec.sandbox.on(console, 'warn', () => {
// noop
});
expect(
await generateUpstreamMap(
['0\\.8\\.\\d+'],
['1\\.1\\.\\d+'],
[MainSpec.backendContainerWithExposedPorts],
),
).to.be.equal(`map $http_x_stapps_version $proxyurl {
default unsupported;
"~0\\.8\\.\\d+" unavailable;
"~1\\.1\\.\\d+" outdated;