test: Improve docker start/stop script
- Simplify the loop that check availability - Increase timeout - Remove the container after stopping
This commit is contained in:
parent
fcb832bbe1
commit
a7359bffb6
@ -4,48 +4,47 @@ import got from 'got';
|
|||||||
import pRetry from 'p-retry';
|
import pRetry from 'p-retry';
|
||||||
import {mockServerClient} from 'mockserver-client';
|
import {mockServerClient} from 'mockserver-client';
|
||||||
|
|
||||||
|
const IMAGE = 'jamesdbloom/mockserver:latest';
|
||||||
const MOCK_SERVER_PORT = 1080;
|
const MOCK_SERVER_PORT = 1080;
|
||||||
const MOCK_SERVER_HOST = 'localhost';
|
const MOCK_SERVER_HOST = 'localhost';
|
||||||
const docker = new Docker();
|
const docker = new Docker();
|
||||||
let container;
|
let container;
|
||||||
|
|
||||||
async function checkStatus() {
|
|
||||||
const response = await got.put(`http://${MOCK_SERVER_HOST}:${MOCK_SERVER_PORT}/status`, {cache: false});
|
|
||||||
if (response.status === 200) {
|
|
||||||
// If the registry returns a 200 status, it's ready. Abort the retry.
|
|
||||||
throw new pRetry.AbortError();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Download the `mockserver` Docker image, create a new container and start it.
|
* Download the `mockserver` Docker image, create a new container and start it.
|
||||||
*
|
*
|
||||||
* @return {Promise} Promise that resolves when the container is started.
|
* @return {Promise} Promise that resolves when the container is started.
|
||||||
*/
|
*/
|
||||||
async function start() {
|
async function start() {
|
||||||
await getStream(await docker.pull('jamesdbloom/mockserver:latest'));
|
await getStream(await docker.pull(IMAGE));
|
||||||
|
|
||||||
container = await docker.createContainer({
|
container = await docker.createContainer({
|
||||||
Image: 'jamesdbloom/mockserver',
|
Tty: true,
|
||||||
|
Image: IMAGE,
|
||||||
PortBindings: {[`${MOCK_SERVER_PORT}/tcp`]: [{HostPort: `${MOCK_SERVER_PORT}`}]},
|
PortBindings: {[`${MOCK_SERVER_PORT}/tcp`]: [{HostPort: `${MOCK_SERVER_PORT}`}]},
|
||||||
});
|
});
|
||||||
await container.start();
|
await container.start();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// Wait for the mock server to be ready
|
// Wait for the mock server to be ready
|
||||||
await pRetry(checkStatus, {retries: 5, minTimeout: 1000, factor: 2});
|
await pRetry(() => got.put(`http://${MOCK_SERVER_HOST}:${MOCK_SERVER_PORT}/status`, {cache: false}), {
|
||||||
|
retries: 7,
|
||||||
|
minTimeout: 1000,
|
||||||
|
factor: 2,
|
||||||
|
});
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
throw new Error(`Couldn't start mock-server after 30s`);
|
throw new Error(`Couldn't start mock-server after 2 min`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Stop the `mockserver` Docker container.
|
* Stop and remote the `mockserver` Docker container.
|
||||||
*
|
*
|
||||||
* @return {Promise} Promise that resolves when the container is stopped.
|
* @return {Promise} Promise that resolves when the container is stopped.
|
||||||
*/
|
*/
|
||||||
async function stop() {
|
async function stop() {
|
||||||
return container.stop();
|
await container.stop();
|
||||||
|
await container.remove();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -3,6 +3,7 @@ import getStream from 'get-stream';
|
|||||||
import got from 'got';
|
import got from 'got';
|
||||||
import pRetry from 'p-retry';
|
import pRetry from 'p-retry';
|
||||||
|
|
||||||
|
const IMAGE = 'npmjs/npm-docker-couchdb:1.6.1';
|
||||||
const SERVER_PORT = 15986;
|
const SERVER_PORT = 15986;
|
||||||
const COUCHDB_PORT = 5984;
|
const COUCHDB_PORT = 5984;
|
||||||
const SERVER_HOST = 'localhost';
|
const SERVER_HOST = 'localhost';
|
||||||
@ -12,19 +13,17 @@ const NPM_EMAIL = 'integration@test.com';
|
|||||||
const docker = new Docker();
|
const docker = new Docker();
|
||||||
let container;
|
let container;
|
||||||
|
|
||||||
async function checkStatus() {
|
/**
|
||||||
const response = await got(`http://${SERVER_HOST}:${SERVER_PORT}/registry/_design/app`, {cache: false});
|
* Download the `npm-docker-couchdb` Docker image, create a new container and start it.
|
||||||
if (response.status === 200) {
|
*
|
||||||
// If the registry returns a 200 status, it's ready. Abort the retry.
|
* @return {Promise} Promise that resolves when the container is started.
|
||||||
throw new pRetry.AbortError();
|
*/
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async function start() {
|
async function start() {
|
||||||
await getStream(await docker.pull('npmjs/npm-docker-couchdb:1.6.1'));
|
await getStream(await docker.pull(IMAGE));
|
||||||
|
|
||||||
container = await docker.createContainer({
|
container = await docker.createContainer({
|
||||||
Image: 'npmjs/npm-docker-couchdb:1.6.1',
|
Tty: true,
|
||||||
|
Image: IMAGE,
|
||||||
PortBindings: {[`${COUCHDB_PORT}/tcp`]: [{HostPort: `${SERVER_PORT}`}]},
|
PortBindings: {[`${COUCHDB_PORT}/tcp`]: [{HostPort: `${SERVER_PORT}`}]},
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -32,9 +31,13 @@ async function start() {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
// Wait for the registry to be ready
|
// Wait for the registry to be ready
|
||||||
await pRetry(checkStatus, {retries: 5, minTimeout: 1000, factor: 2});
|
await pRetry(() => got(`http://${SERVER_HOST}:${SERVER_PORT}/registry/_design/app`, {cache: false}), {
|
||||||
|
retries: 7,
|
||||||
|
minTimeout: 1000,
|
||||||
|
factor: 2,
|
||||||
|
});
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
throw new Error(`Couldn't start npm-docker-couchdb after 30s`);
|
throw new Error(`Couldn't start npm-docker-couchdb after 2 min`);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create user
|
// Create user
|
||||||
@ -62,8 +65,14 @@ const authEnv = {
|
|||||||
NPM_EMAIL,
|
NPM_EMAIL,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Stop and remote the `npm-docker-couchdb` Docker container.
|
||||||
|
*
|
||||||
|
* @return {Promise} Promise that resolves when the container is stopped.
|
||||||
|
*/
|
||||||
async function stop() {
|
async function stop() {
|
||||||
return container.stop();
|
await container.stop();
|
||||||
|
await container.remove();
|
||||||
}
|
}
|
||||||
|
|
||||||
export default {start, stop, authEnv, url};
|
export default {start, stop, authEnv, url};
|
||||||
|
Loading…
x
Reference in New Issue
Block a user