From eef7bec80d01240c826bf860c0101922acca1802 Mon Sep 17 00:00:00 2001 From: Laurin Quast Date: Wed, 11 Mar 2020 10:25:52 +0100 Subject: [PATCH 1/3] Update checkConnection.ts --- packages/dockest/src/run/waitForServices/checkConnection.ts | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/packages/dockest/src/run/waitForServices/checkConnection.ts b/packages/dockest/src/run/waitForServices/checkConnection.ts index 43cab245..6d686399 100644 --- a/packages/dockest/src/run/waitForServices/checkConnection.ts +++ b/packages/dockest/src/run/waitForServices/checkConnection.ts @@ -84,6 +84,10 @@ export const createCheckConnection = ({ }) => { const host = runnerHost || 'localhost' const portKey = isBridgeNetworkMode ? 'target' : 'published' + if (!ports || ports.length === 0) { + runner.logger.debug(`${LOG_PREFIX} Skip connection check as there are no ports exposed.`) + return + } return race( dockerEventStream$.pipe( From aba12c84aca335b36568f44894fd9eae81c43f12 Mon Sep 17 00:00:00 2001 From: Laurin Quast Date: Wed, 11 Mar 2020 13:17:32 +0100 Subject: [PATCH 2/3] fix: automatically transform legacy port mappings to the new format --- packages/dockest/src/@types.ts | 17 +++++++++++------ .../src/run/waitForServices/checkConnection.ts | 3 ++- packages/dockest/src/test-helper/index.ts | 3 ++- .../src/utils/createDefaultReadinessChecks.ts | 3 ++- packages/dockest/src/utils/selectPortMapping.ts | 7 +++++++ 5 files changed, 24 insertions(+), 9 deletions(-) create mode 100644 packages/dockest/src/utils/selectPortMapping.ts diff --git a/packages/dockest/src/@types.ts b/packages/dockest/src/@types.ts index d419b3de..94652af6 100644 --- a/packages/dockest/src/@types.ts +++ b/packages/dockest/src/@types.ts @@ -45,14 +45,19 @@ export interface RunnersObj { [key: string]: Runner } +export type DockerComposePortStringFormat = string +export type DockerComposePortObjectFormat = { + /** The publicly exposed port */ + published: number + /** The port inside the container */ + target: number +} + +export type DockerComposePortFormat = DockerComposePortStringFormat | DockerComposePortObjectFormat + export interface DockerComposeFileService { /** Expose ports */ - ports: { - /** The publicly exposed port */ - published: number - /** The port inside the container */ - target: number - }[] + ports: DockerComposePortFormat[] [key: string]: any } diff --git a/packages/dockest/src/run/waitForServices/checkConnection.ts b/packages/dockest/src/run/waitForServices/checkConnection.ts index 6d686399..1c46d19b 100644 --- a/packages/dockest/src/run/waitForServices/checkConnection.ts +++ b/packages/dockest/src/run/waitForServices/checkConnection.ts @@ -3,6 +3,7 @@ import { race, of, from } from 'rxjs' import { concatMap, delay, ignoreElements, map, mergeMap, retryWhen, skipWhile, tap, takeWhile } from 'rxjs/operators' import { DockestError } from '../../Errors' import { Runner } from '../../@types' +import { selectPortMapping } from '../../utils/selectPortMapping' export type AcquireConnectionFunctionType = ({ host, port }: { host: string; port: number }) => Promise @@ -96,7 +97,7 @@ export const createCheckConnection = ({ throw new DockestError('Container unexpectedly died.', { event }) }), ), - of(...ports).pipe( + of(...ports.map(selectPortMapping)).pipe( // concatMap -> run checks for each port in sequence concatMap(({ [portKey]: port }) => checkPortConnection({ runner, host, port, acquireConnection })), // we do not care about the single elements, we only want this stream to complete without errors. diff --git a/packages/dockest/src/test-helper/index.ts b/packages/dockest/src/test-helper/index.ts index 9fe0b88c..b316f183 100644 --- a/packages/dockest/src/test-helper/index.ts +++ b/packages/dockest/src/test-helper/index.ts @@ -2,6 +2,7 @@ import isDocker from 'is-docker' // eslint-disable-line import/default import { DockerComposeFile } from '../@types' import { DOCKEST_ATTACH_TO_PROCESS, DOCKEST_HOST_ADDRESS, DEFAULT_HOST_NAME } from '../constants' import { DockestError } from '../Errors' +import { selectPortMapping } from '../utils/selectPortMapping' const isInsideDockerContainer = isDocker() const dockestConfig = process.env[DOCKEST_ATTACH_TO_PROCESS] @@ -26,7 +27,7 @@ export const getServiceAddress = (serviceName: string, targetPort: number | stri throw new DockestError(`Service "${serviceName}" does not exist`) } - const portBinding = service.ports.find(portBinding => portBinding.target === targetPort) + const portBinding = service.ports.map(selectPortMapping).find(portBinding => portBinding.target === targetPort) if (!portBinding) { throw new DockestError(`Service "${serviceName}" has no target port ${portBinding}`) } diff --git a/packages/dockest/src/utils/createDefaultReadinessChecks.ts b/packages/dockest/src/utils/createDefaultReadinessChecks.ts index bab62069..9b32b275 100644 --- a/packages/dockest/src/utils/createDefaultReadinessChecks.ts +++ b/packages/dockest/src/utils/createDefaultReadinessChecks.ts @@ -1,4 +1,5 @@ import { execaWrapper } from './execaWrapper' +import { selectPortMapping } from './selectPortMapping' import { DefaultReadinessChecks, Runner } from '../@types' export const createDefaultReadinessChecks = ({ @@ -25,7 +26,7 @@ export const createDefaultReadinessChecks = ({ redis: async () => { const command = `docker exec ${containerId} redis-cli \ -h localhost \ - -p ${ports[0].target} \ + -p ${selectPortMapping(ports[0]).target} \ PING` await execaWrapper(command, { runner }) diff --git a/packages/dockest/src/utils/selectPortMapping.ts b/packages/dockest/src/utils/selectPortMapping.ts new file mode 100644 index 00000000..d171d55d --- /dev/null +++ b/packages/dockest/src/utils/selectPortMapping.ts @@ -0,0 +1,7 @@ +import { DockerComposePortFormat } from '../@types' + +export const selectPortMapping = (input: DockerComposePortFormat) => { + if (typeof input !== 'string') return input + const [published, target] = input.split(':') + return { published: parseInt(published, 10), target: parseInt(target, 10) } +} From 61641c1ac9bb7c3373c02c68d15ab4ae0a08960d Mon Sep 17 00:00:00 2001 From: n1ru4l Date: Thu, 12 Mar 2020 08:37:23 +0100 Subject: [PATCH 3/3] add unit tests for selectPortMapping. --- .../src/utils/selectPortMapping.spec.ts | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 packages/dockest/src/utils/selectPortMapping.spec.ts diff --git a/packages/dockest/src/utils/selectPortMapping.spec.ts b/packages/dockest/src/utils/selectPortMapping.spec.ts new file mode 100644 index 00000000..2892b312 --- /dev/null +++ b/packages/dockest/src/utils/selectPortMapping.spec.ts @@ -0,0 +1,29 @@ +import { selectPortMapping } from './selectPortMapping' + +describe('selectPortMapping', () => { + it.each([ + ['1234:1111', { target: 1111, published: 1234 }], + ['2222:1111', { target: 1111, published: 2222 }], + ['12:20', { target: 20, published: 12 }], + ['90:1000', { target: 1000, published: 90 }], + ])('can parse the string format', (input, expected) => { + expect(selectPortMapping(input)).toEqual(expected) + }) + + it.each([ + [ + { target: 1234, published: 1111 }, + { target: 1234, published: 1111 }, + ], + [ + { target: 1111, published: 1234 }, + { target: 1111, published: 1234 }, + ], + [ + { target: 3333, published: 3333 }, + { target: 3333, published: 3333 }, + ], + ])('passes through the object format', (input, expected) => { + expect(selectPortMapping(input)).toEqual(expected) + }) +})