|
1 | 1 | // tslint:disable:typedef no-implicit-dependencies no-any |
2 | | -import {Docker} from 'node-docker-api'; |
| 2 | +import { Docker } from 'node-docker-api'; |
3 | 3 | // tslint:disable-next-line:no-submodule-imports |
4 | | -import {execSync} from 'child_process'; |
| 4 | +import { execSync } from 'child_process'; |
5 | 5 | import * as getPort from 'get-port'; |
6 | 6 |
|
7 | 7 | const testIdCounter = {}; |
8 | 8 | const testContainers = {}; |
9 | 9 | const testContainerPorts = {}; |
10 | 10 | let fetchedImage = false; |
11 | 11 |
|
12 | | -const docker : Docker = new Docker(process.env.CI === 'true' |
13 | | - ? {protocol: 'http', host: 'localhost', port: '2375'} |
14 | | - : {socketPath: '/var/run/docker.sock'}); |
| 12 | +const docker: Docker = new Docker( |
| 13 | + process.env.CI === 'true' |
| 14 | + ? { protocol: 'http', host: 'localhost', port: '2375' } |
| 15 | + : { socketPath: '/var/run/docker.sock' } |
| 16 | +); |
15 | 17 |
|
16 | | -function promisifyStream(stream) { |
| 18 | +function promisifyStream(stream: NodeJS.ReadableStream) { |
17 | 19 | return new Promise((resolve, reject) => { |
18 | | - stream.on('data', (d) => console.log(d.toString())); |
| 20 | + stream.on('data', (d: Buffer) => console.log(d.toString())); |
19 | 21 | stream.on('end', resolve); |
20 | 22 | stream.on('error', reject); |
21 | 23 | }); |
22 | 24 | } |
23 | 25 |
|
24 | | -export function stopContainersByName(...names : string[]) { |
| 26 | +export function stopContainersByName(...names: string[]) { |
25 | 27 | for (const name of names) { |
26 | | - execSync(`bash -c "docker ps | grep ${name} | awk '{print \\$1}' | xargs -I{} docker stop {}"`); |
27 | | - execSync(`bash -c "docker ps -a | grep ${name} | awk '{print \\$1}' | xargs -I{} docker rm {}"`); |
| 28 | + execSync( |
| 29 | + `bash -c "docker ps | grep ${name} | awk '{print \\$1}' | xargs -I{} docker stop {}"` |
| 30 | + ); |
| 31 | + execSync( |
| 32 | + `bash -c "docker ps -a | grep ${name} | awk '{print \\$1}' | xargs -I{} docker rm {}"` |
| 33 | + ); |
28 | 34 | } |
29 | 35 | } |
30 | 36 |
|
31 | 37 | // tslint:disable-next-line:no-any |
32 | | -export async function startContainer(testId : string, image : string, tag : string, ...ports : string[]) : Promise<any> { |
33 | | - testIdCounter[testId] = (testIdCounter[testId] || 0); |
| 38 | +export async function startContainer( |
| 39 | + testId: string, |
| 40 | + image: string, |
| 41 | + tag: string, |
| 42 | + ...ports: string[] |
| 43 | +): Promise<any> { |
| 44 | + testIdCounter[testId] = testIdCounter[testId] || 0; |
34 | 45 | testIdCounter[testId]++; |
35 | | - const containerName = ['jest-test-container', image, tag, testId, testIdCounter[testId]].join('-') |
| 46 | + const containerName = [ |
| 47 | + 'jest-test-container', |
| 48 | + image, |
| 49 | + tag, |
| 50 | + testId, |
| 51 | + testIdCounter[testId], |
| 52 | + ] |
| 53 | + .join('-') |
36 | 54 | .replace(/[^a-z0-9]+/g, '-'); |
37 | 55 | testContainers[testId] = testContainers[testId] || []; |
38 | 56 | testContainers[testId].push(containerName); |
39 | 57 | testContainerPorts[containerName] = testContainerPorts[containerName] || {}; |
40 | | - let container = await docker.container.get(containerName); |
| 58 | + let container = docker.container.get(containerName); |
41 | 59 | try { |
42 | 60 | const containerStatus = await container.status(); |
43 | 61 | console.log('Found existing container', containerStatus); |
44 | | - await stopContainersByName(testId); |
| 62 | + stopContainersByName(testId); |
45 | 63 | } catch (err) { |
46 | 64 | // ignore |
47 | 65 | } |
48 | 66 | if (!fetchedImage) { |
49 | 67 | fetchedImage = true; |
50 | | - await docker.image.create({}, {fromImage: image, tag: tag}) |
| 68 | + await docker.image |
| 69 | + .create({}, { fromImage: image, tag: tag }) |
51 | 70 | .then(promisifyStream) |
52 | | - .then(() => docker.image.get(`${image}:${tag}`) |
53 | | - .status()); |
| 71 | + .then(() => docker.image.get(`${image}:${tag}`).status()); |
54 | 72 | } |
55 | 73 | const createOpts = { |
56 | 74 | Image: `${image}:${tag}`, |
57 | 75 | name: containerName, |
58 | | - PortBindings: {} |
| 76 | + PortBindings: {}, |
59 | 77 | }; |
60 | 78 | for (const portDef of ports) { |
61 | 79 | const parts = portDef.split('/'); |
62 | 80 | const hostPort = await getPort(); |
63 | | - createOpts.PortBindings[`${parts[0]}/${parts[1] || 'tcp'}`] = [{HostPort: `${hostPort}`}]; |
| 81 | + createOpts.PortBindings[`${parts[0]}/${parts[1] || 'tcp'}`] = [ |
| 82 | + { HostPort: `${hostPort}` }, |
| 83 | + ]; |
64 | 84 | testContainerPorts[containerName][parts[0]] = hostPort; |
65 | 85 | } |
66 | 86 | container = await docker.container.create(createOpts); |
67 | 87 | await container.start(); |
68 | | - await new Promise((resolve) => setTimeout(resolve, 1000)); |
| 88 | + await new Promise<void>((resolve) => setTimeout(resolve, 1000)); |
69 | 89 | try { |
70 | | - await container.logs({ |
71 | | - stdout: true, |
72 | | - stderr: true |
73 | | - }) |
| 90 | + await container |
| 91 | + .logs({ |
| 92 | + stdout: true, |
| 93 | + stderr: true, |
| 94 | + }) |
74 | 95 | .then(promisifyStream); |
75 | 96 | } catch (err) { |
76 | 97 | // ignore |
|
0 commit comments