Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: skip connection check in case there is no port exposed + map old port format to new format #154

Merged
merged 3 commits into from
Mar 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions packages/dockest/src/@types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
7 changes: 6 additions & 1 deletion packages/dockest/src/run/waitForServices/checkConnection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void>

Expand Down Expand Up @@ -84,6 +85,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(
Expand All @@ -92,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.
Expand Down
3 changes: 2 additions & 1 deletion packages/dockest/src/test-helper/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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}`)
}
Expand Down
3 changes: 2 additions & 1 deletion packages/dockest/src/utils/createDefaultReadinessChecks.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { execaWrapper } from './execaWrapper'
import { selectPortMapping } from './selectPortMapping'
import { DefaultReadinessChecks, Runner } from '../@types'

export const createDefaultReadinessChecks = ({
Expand All @@ -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 })
Expand Down
29 changes: 29 additions & 0 deletions packages/dockest/src/utils/selectPortMapping.spec.ts
Original file line number Diff line number Diff line change
@@ -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)
})
})
7 changes: 7 additions & 0 deletions packages/dockest/src/utils/selectPortMapping.ts
Original file line number Diff line number Diff line change
@@ -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) }
}
n1ru4l marked this conversation as resolved.
Show resolved Hide resolved