mirror of
https://github.com/danbulant/docker-compose
synced 2026-05-22 05:48:48 +00:00
Fix tests
Always `down` containers after test Fix workdir test failing on CircleCI See: https://support.circleci.com/hc/en-us/articles/360007324514-How-can-I-mount-volumes-to-docker-containers-
This commit is contained in:
parent
1f2a8215f9
commit
5cc5ced99f
8 changed files with 5028 additions and 459 deletions
|
|
@ -1,6 +1,13 @@
|
|||
{
|
||||
"extends": "es/2015/server",
|
||||
"extends": [
|
||||
"es/2015/server",
|
||||
"plugin:jest/recommended"
|
||||
],
|
||||
"rules": {
|
||||
"no-console" :"off"
|
||||
},
|
||||
"plugins": ["jest"],
|
||||
"env": {
|
||||
"jest/globals": true
|
||||
}
|
||||
}
|
||||
|
|
|
|||
46
index.d.ts
vendored
46
index.d.ts
vendored
|
|
@ -1,49 +1,49 @@
|
|||
export declare function upAll(options: IDockerComposeOptions): Promise<IDockerComposeResult>;
|
||||
export declare function upAll(options?: IDockerComposeOptions): Promise<IDockerComposeResult>;
|
||||
|
||||
export declare function upMany(services: String[], options: IDockerComposeOptions): Promise<IDockerComposeResult>;
|
||||
export declare function upMany(services: String[], options?: IDockerComposeOptions): Promise<IDockerComposeResult>;
|
||||
|
||||
export declare function upOne(service: String, options: IDockerComposeOptions): Promise<IDockerComposeResult>;
|
||||
export declare function upOne(service: String, options?: IDockerComposeOptions): Promise<IDockerComposeResult>;
|
||||
|
||||
export declare function kill(options: IDockerComposeOptions): Promise<IDockerComposeResult>;
|
||||
export declare function kill(options?: IDockerComposeOptions): Promise<IDockerComposeResult>;
|
||||
|
||||
export declare function down(options: IDockerComposeOptions): Promise<IDockerComposeResult>;
|
||||
export declare function down(options?: IDockerComposeOptions): Promise<IDockerComposeResult>;
|
||||
|
||||
export declare function stop(options: IDockerComposeOptions): Promise<IDockerComposeResult>;
|
||||
export declare function stop(options?: IDockerComposeOptions): Promise<IDockerComposeResult>;
|
||||
|
||||
export declare function restartAll(options: IDockerComposeOptions): Promise<IDockerComposeResult>;
|
||||
export declare function restartAll(options?: IDockerComposeOptions): Promise<IDockerComposeResult>;
|
||||
|
||||
export declare function restartMany(services: String[], options: IDockerComposeOptions): Promise<IDockerComposeResult>;
|
||||
export declare function restartMany(services: String[], options?: IDockerComposeOptions): Promise<IDockerComposeResult>;
|
||||
|
||||
export declare function restartOne(service: String, options: IDockerComposeOptions): Promise<IDockerComposeResult>;
|
||||
export declare function restartOne(service: String, options?: IDockerComposeOptions): Promise<IDockerComposeResult>;
|
||||
|
||||
export declare function rm(options: IDockerComposeOptions): Promise<IDockerComposeResult>;
|
||||
export declare function rm(options?: IDockerComposeOptions): Promise<IDockerComposeResult>;
|
||||
|
||||
export declare function exec(container: String, command: String | String[], options: IDockerComposeOptions): Promise<IDockerComposeResult>;
|
||||
export declare function exec(container: String, command: String | String[], options?: IDockerComposeOptions): Promise<IDockerComposeResult>;
|
||||
|
||||
export declare function logs(services: String[], options: IDockerComposeLogOptions): Promise<IDockerComposeResult>;
|
||||
export declare function logs(services: String[], options?: IDockerComposeLogOptions): Promise<IDockerComposeResult>;
|
||||
|
||||
export declare function run(service: String, command: String | String[], options: IDockerComposeOptions): Promise<IDockerComposeResult>;
|
||||
export declare function run(service: String, command: String | String[], options?: IDockerComposeOptions): Promise<IDockerComposeResult>;
|
||||
|
||||
export declare function buildAll(options: IDockerComposeBuildOptions): Promise<IDockerComposeResult>;
|
||||
export declare function buildAll(options?: IDockerComposeBuildOptions): Promise<IDockerComposeResult>;
|
||||
|
||||
export declare function buildMany(services: String[], options: IDockerComposeBuildOptions): Promise<IDockerComposeResult>;
|
||||
export declare function buildMany(services: String[], options?: IDockerComposeBuildOptions): Promise<IDockerComposeResult>;
|
||||
|
||||
export declare function buildOne(service: String, options: IDockerComposeOptions): Promise<IDockerComposeResult>;
|
||||
export declare function buildOne(service: String, options?: IDockerComposeOptions): Promise<IDockerComposeResult>;
|
||||
|
||||
export declare function config(options: IDockerComposeOptions): Promise<IDockerComposeResult>;
|
||||
export declare function config(options?: IDockerComposeOptions): Promise<IDockerComposeResult>;
|
||||
|
||||
export declare function configServices(options: IDockerComposeOptions): Promise<IDockerComposeResult>;
|
||||
export declare function configServices(options?: IDockerComposeOptions): Promise<IDockerComposeResult>;
|
||||
|
||||
export declare function configVolumes(options: IDockerComposeOptions): Promise<IDockerComposeResult>;
|
||||
export declare function configVolumes(options?: IDockerComposeOptions): Promise<IDockerComposeResult>;
|
||||
|
||||
export declare function ps(options: IDockerComposeOptions): Promise<IDockerComposeResult>;
|
||||
export declare function ps(options?: IDockerComposeOptions): Promise<IDockerComposeResult>;
|
||||
|
||||
export declare function push(options: IDockerComposePushOptions): Promise<IDockerComposeResult>;
|
||||
export declare function push(options?: IDockerComposePushOptions): Promise<IDockerComposeResult>;
|
||||
|
||||
export declare function port(service: String, containerPort: String | Number, options: IDockerComposeOptions): Promise<IDockerComposeResult>;
|
||||
export declare function port(service: String, containerPort: String | Number, options?: IDockerComposeOptions): Promise<IDockerComposeResult>;
|
||||
|
||||
interface IDockerComposeOptions {
|
||||
cwd: string;
|
||||
cwd?: string;
|
||||
config?: string | string[];
|
||||
log?: boolean;
|
||||
composeOptions?: string[] | Array<string | string[]>;
|
||||
|
|
|
|||
11
index.js
11
index.js
|
|
@ -51,6 +51,9 @@ const composeOptionsToArgs = function (composeOptions) {
|
|||
* @param {?(string[]|Array<string|string[]>)} [options.composeOptions]
|
||||
*/
|
||||
const execCompose = (command, args, options) => new Promise((resolve, reject) => {
|
||||
if (typeof options === 'undefined') {
|
||||
options = {};
|
||||
}
|
||||
const composeOptions = options.composeOptions || [];
|
||||
const commandOptions = options.commandOptions || [];
|
||||
let composeArgs = composeOptionsToArgs(composeOptions);
|
||||
|
|
@ -82,11 +85,11 @@ const execCompose = (command, args, options) => new Promise((resolve, reject) =>
|
|||
|
||||
childProc.on('exit', exitCode => {
|
||||
result.exitCode = exitCode;
|
||||
if (exitCode !== 0) {
|
||||
return reject(new Error(result.err));
|
||||
if (exitCode === 0) {
|
||||
resolve(result);
|
||||
} else {
|
||||
reject(result);
|
||||
}
|
||||
|
||||
return resolve(result);
|
||||
});
|
||||
|
||||
if (options.log) {
|
||||
|
|
|
|||
4588
package-lock.json
generated
4588
package-lock.json
generated
File diff suppressed because it is too large
Load diff
|
|
@ -4,7 +4,7 @@
|
|||
"main": "index.js",
|
||||
"typings": "index.d.ts",
|
||||
"scripts": {
|
||||
"test": "./node_modules/.bin/tape test",
|
||||
"test": "jest test --verbose",
|
||||
"eslint": "./node_modules/.bin/eslint ."
|
||||
},
|
||||
"repository": {
|
||||
|
|
@ -59,6 +59,8 @@
|
|||
"eslint": "^5.6.0",
|
||||
"eslint-config-es": "^0.8.11",
|
||||
"eslint-plugin-extended": "^0.2.0",
|
||||
"eslint-plugin-jest": "^22.6.4",
|
||||
"jest": "^24.8.0",
|
||||
"tape": "^4.6.3",
|
||||
"tape-promise": "^2.0.1"
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
version: '2'
|
||||
|
||||
services:
|
||||
services:
|
||||
build_test_1:
|
||||
image: compose-test-build-image-1:test
|
||||
build:
|
||||
|
|
@ -20,4 +20,8 @@ services:
|
|||
image: compose-test-build-image-4:test
|
||||
build:
|
||||
context: ./
|
||||
dockerfile: build-test.Dockerfile
|
||||
dockerfile: build-test.Dockerfile
|
||||
build-nginx:
|
||||
image: nginx:1.16.0
|
||||
container_name: compose_test_nginx
|
||||
command: 'nginx -g "daemon off;"'
|
||||
|
|
|
|||
396
test/index.js
396
test/index.js
|
|
@ -1,396 +0,0 @@
|
|||
'use strict';
|
||||
|
||||
const compose = require('../index');
|
||||
const path = require('path');
|
||||
const tape = require('tape');
|
||||
const defaultTest = require('tape-promise').default;
|
||||
const test = defaultTest(tape);
|
||||
const Docker = require('dockerode');
|
||||
const docker = new Docker();
|
||||
|
||||
const isContainerRunning = async name => new Promise((resolve, reject) => {
|
||||
docker.listContainers((err, containers) => {
|
||||
if (err) {
|
||||
reject(err);
|
||||
}
|
||||
|
||||
const running = containers.find(container => container.Names.includes(name));
|
||||
|
||||
resolve(running);
|
||||
});
|
||||
});
|
||||
|
||||
const repoTags = imageInfo => imageInfo.RepoTags || [];
|
||||
|
||||
const imageExists = async name => {
|
||||
const images = await docker.listImages();
|
||||
|
||||
const foundImage = images.findIndex(imageInfo => repoTags(imageInfo).includes(name));
|
||||
|
||||
return foundImage > -1;
|
||||
};
|
||||
|
||||
const removeImagesStartingWith = async searchString => {
|
||||
const images = await docker.listImages();
|
||||
|
||||
for (const image of images) {
|
||||
for (const repoTag of repoTags(image)) {
|
||||
if (repoTag.startsWith(searchString)) {
|
||||
const dockerImage = docker.getImage(repoTag);
|
||||
|
||||
console.log(`removing image ${repoTag} ${dockerImage.id || ''}`);
|
||||
await dockerImage.remove();
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
test('ensure container gets started', async assert => {
|
||||
await compose.down({ cwd: path.join(__dirname), log: true });
|
||||
await compose.upAll({ cwd: path.join(__dirname), log: true });
|
||||
|
||||
assert.true(await isContainerRunning('/compose_test_nginx'));
|
||||
assert.end();
|
||||
});
|
||||
|
||||
test('ensure exit code is returned correctly', async assert => {
|
||||
assert.equal(0, (await compose.down({ cwd: path.join(__dirname), log: true })).exitCode);
|
||||
assert.equal(0, (await compose.upAll({ cwd: path.join(__dirname), log: true })).exitCode);
|
||||
assert.equal(1, (await compose.logs('non_existent_service', { cwd: path.join(__dirname) })).exitCode);
|
||||
});
|
||||
|
||||
test('ensure container gets started with --build option', async assert => {
|
||||
await compose.down({ cwd: path.join(__dirname), log: true, config: 'docker-compose-build.yml' });
|
||||
await compose.upAll({ cwd: path.join(__dirname), log: true, config: 'docker-compose-build.yml', commandOptions: [ '--build' ]});
|
||||
|
||||
assert.true(await isContainerRunning('/compose_test_nginx'));
|
||||
await compose.down({ cwd: path.join(__dirname), log: true, config: 'docker-compose-build.yml' });
|
||||
assert.end();
|
||||
});
|
||||
|
||||
test('ensure container gets started with --build and --timeout option', async assert => {
|
||||
await compose.down({ cwd: path.join(__dirname), log: true, config: 'docker-compose-build.yml' });
|
||||
await compose.upAll({ cwd: path.join(__dirname), log: true, config: 'docker-compose-build.yml', commandOptions: [[ '--build' ], [ '--timeout', '5' ]]});
|
||||
|
||||
assert.true(await isContainerRunning('/compose_test_nginx'));
|
||||
await compose.down({ cwd: path.join(__dirname), log: true, config: 'docker-compose-build.yml' });
|
||||
assert.end();
|
||||
});
|
||||
|
||||
test('ensure container gets started with --build and --timeout option', async assert => {
|
||||
await compose.down({ cwd: path.join(__dirname), log: true, config: 'docker-compose-build.yml' });
|
||||
await compose.upAll({ cwd: path.join(__dirname), log: true, config: 'docker-compose-build.yml', commandOptions: [ '--build', [ '--timeout', '5' ]]});
|
||||
|
||||
assert.true(await isContainerRunning('/compose_test_nginx'));
|
||||
await compose.down({ cwd: path.join(__dirname), log: true, config: 'docker-compose-build.yml' });
|
||||
assert.end();
|
||||
});
|
||||
|
||||
test('ensure container command executed with --workdir command option', async assert => {
|
||||
await compose.down({ cwd: path.join(__dirname), log: true, config: 'docker-compose-42.yml' });
|
||||
|
||||
assert.doesNotThrow(async () => {
|
||||
await compose.run('some-service', 'cat hello.txt', {
|
||||
cwd: path.join(__dirname),
|
||||
log: true,
|
||||
config: 'docker-compose-42.yml',
|
||||
composeOptions: [ '--verbose' ],
|
||||
commandOptions: [ '--workdir', '/mountedvolume/nested/dir' ]
|
||||
});
|
||||
});
|
||||
|
||||
await compose.down({ cwd: path.join(__dirname), log: true, config: 'docker-compose-42.yml' });
|
||||
assert.end();
|
||||
});
|
||||
|
||||
test('ensure only single container gets started', async assert => {
|
||||
await compose.down({ cwd: path.join(__dirname), log: true });
|
||||
await compose.upOne('alpine', { cwd: path.join(__dirname), log: true });
|
||||
|
||||
assert.true(await isContainerRunning('/compose_test_alpine'));
|
||||
assert.false(await isContainerRunning('/compose_test_nginx'));
|
||||
assert.end();
|
||||
});
|
||||
|
||||
test('ensure only multiple containers get started', async assert => {
|
||||
await compose.down({ cwd: path.join(__dirname), log: true });
|
||||
await compose.upMany([ 'alpine' ], { cwd: path.join(__dirname), log: true });
|
||||
|
||||
assert.true(await isContainerRunning('/compose_test_alpine'));
|
||||
assert.false(await isContainerRunning('/compose_test_nginx'));
|
||||
assert.end();
|
||||
});
|
||||
|
||||
test('ensure container gets down', async assert => {
|
||||
await compose.upAll({ cwd: path.join(__dirname), log: true });
|
||||
await compose.down({ cwd: path.join(__dirname), log: true });
|
||||
|
||||
assert.false(await isContainerRunning('/compose_test_nginx'));
|
||||
assert.end();
|
||||
});
|
||||
|
||||
test('ensure container gets stopped', async assert => {
|
||||
await compose.upAll({ cwd: path.join(__dirname), log: true });
|
||||
await compose.stop({ cwd: path.join(__dirname), log: true });
|
||||
|
||||
assert.false(await isContainerRunning('/compose_test_nginx'));
|
||||
assert.end();
|
||||
});
|
||||
|
||||
test('ensure container gets killed', async assert => {
|
||||
await compose.upAll({ cwd: path.join(__dirname), log: true });
|
||||
await compose.kill({ cwd: path.join(__dirname), log: true });
|
||||
|
||||
assert.false(await isContainerRunning('/compose_test_nginx'));
|
||||
assert.end();
|
||||
});
|
||||
|
||||
test('ensure custom ymls are working', async assert => {
|
||||
const config = './docker-compose-2.yml';
|
||||
const cwd = path.join(__dirname);
|
||||
const log = true;
|
||||
|
||||
await compose.upAll({ cwd, log, config });
|
||||
assert.true(await isContainerRunning('/compose_test_nginx_2'));
|
||||
|
||||
// config & [config] are the same thing, ensures that multiple configs are handled properly
|
||||
await compose.kill({ cwd, log, config: [ config ]});
|
||||
assert.false(await isContainerRunning('/compose_test_nginx_2'));
|
||||
assert.end();
|
||||
});
|
||||
|
||||
test('ensure run and exec are working', async assert => {
|
||||
const checkOSID = (out, id) => {
|
||||
// parse /etc/os-release contents
|
||||
const re = /([\w,_]+)=(.*)/g;
|
||||
let match = null;
|
||||
const os = {};
|
||||
|
||||
while ((match = re.exec(out)) !== null) { // eslint-disable-line no-cond-assign
|
||||
os[match[1]] = match[2];
|
||||
}
|
||||
|
||||
assert.equals(os.ID, id);
|
||||
};
|
||||
|
||||
const opts = { cwd: path.join(__dirname), log: false };
|
||||
|
||||
await compose.upAll(opts);
|
||||
|
||||
assert.true(await isContainerRunning('/compose_test_nginx'));
|
||||
|
||||
let std = await compose.exec('db', 'cat /etc/os-release', opts);
|
||||
|
||||
assert.false(std.err);
|
||||
checkOSID(std.out, 'debian');
|
||||
|
||||
std = await compose.run('alpine', 'cat /etc/os-release', opts);
|
||||
assert.false(std.err);
|
||||
checkOSID(std.out, 'alpine');
|
||||
|
||||
assert.end();
|
||||
});
|
||||
|
||||
test('ensure run and exec with command defined as array are working', async assert => {
|
||||
const checkOSID = (out, id) => {
|
||||
// parse /etc/os-release contents
|
||||
const re = /([\w,_]+)=(.*)/g;
|
||||
let match = null;
|
||||
const os = {};
|
||||
|
||||
while ((match = re.exec(out)) !== null) { // eslint-disable-line no-cond-assign
|
||||
os[match[1]] = match[2];
|
||||
}
|
||||
|
||||
assert.equals(os.ID, id);
|
||||
};
|
||||
|
||||
const opts = { cwd: path.join(__dirname), log: false };
|
||||
|
||||
await compose.upAll(opts);
|
||||
|
||||
assert.true(await isContainerRunning('/compose_test_nginx'));
|
||||
|
||||
let std = await compose.exec('db', [ '/bin/sh', '-c', 'cat /etc/os-release' ], opts);
|
||||
|
||||
assert.false(std.err);
|
||||
checkOSID(std.out, 'debian');
|
||||
|
||||
std = await compose.run('alpine', [ '/bin/sh', '-c', 'cat /etc/os-release' ], opts);
|
||||
assert.false(std.err);
|
||||
checkOSID(std.out, 'alpine');
|
||||
|
||||
assert.end();
|
||||
});
|
||||
|
||||
test('build single service', async assert => {
|
||||
const opts = {
|
||||
cwd: path.join(__dirname),
|
||||
log: false,
|
||||
config: 'docker-compose-build.yml'
|
||||
};
|
||||
|
||||
await removeImagesStartingWith('compose-test-build-image');
|
||||
|
||||
await compose.buildOne('build_test_1', opts);
|
||||
|
||||
assert.true(await imageExists('compose-test-build-image-1:test'));
|
||||
assert.false(await imageExists('compose-test-build-image-2:test'));
|
||||
assert.false(await imageExists('compose-test-build-image-3:test'));
|
||||
assert.false(await imageExists('compose-test-build-image-4:test'));
|
||||
|
||||
await removeImagesStartingWith('compose-test-build-image');
|
||||
|
||||
assert.end();
|
||||
});
|
||||
|
||||
test('build multiple services', async assert => {
|
||||
const opts = {
|
||||
cwd: path.join(__dirname),
|
||||
log: false,
|
||||
config: 'docker-compose-build.yml'
|
||||
};
|
||||
|
||||
await compose.buildMany([ 'build_test_2', 'build_test_3' ], opts);
|
||||
|
||||
assert.false(await imageExists('compose-test-build-image-1:test'));
|
||||
assert.true(await imageExists('compose-test-build-image-2:test'));
|
||||
assert.true(await imageExists('compose-test-build-image-3:test'));
|
||||
assert.false(await imageExists('compose-test-build-image-4:test'));
|
||||
|
||||
await removeImagesStartingWith('compose-test-build-image');
|
||||
|
||||
assert.end();
|
||||
});
|
||||
|
||||
test('build all services', async assert => {
|
||||
const opts = {
|
||||
cwd: path.join(__dirname),
|
||||
log: false,
|
||||
config: 'docker-compose-build.yml'
|
||||
};
|
||||
|
||||
await compose.buildAll(opts);
|
||||
|
||||
assert.true(await imageExists('compose-test-build-image-1:test'));
|
||||
assert.true(await imageExists('compose-test-build-image-2:test'));
|
||||
assert.true(await imageExists('compose-test-build-image-3:test'));
|
||||
assert.true(await imageExists('compose-test-build-image-4:test'));
|
||||
|
||||
await removeImagesStartingWith('compose-test-build-image');
|
||||
|
||||
assert.end();
|
||||
});
|
||||
|
||||
test('teardown', async assert => {
|
||||
docker.listContainers((err, containers) => {
|
||||
if (err) {
|
||||
throw err;
|
||||
}
|
||||
|
||||
containers.forEach(container => {
|
||||
container.Names.forEach(name => {
|
||||
if (name.startsWith('/compose_test_')) {
|
||||
console.log(`stopping ${container.Id} ${container.Names}`);
|
||||
docker.getContainer(container.Id).stop();
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
await removeImagesStartingWith('compose-test-build-image');
|
||||
|
||||
assert.end();
|
||||
});
|
||||
|
||||
test('config show data for docker-compose files', async assert => {
|
||||
const std = await compose.config({ cwd: path.join(__dirname), log: true, config: 'docker-compose-42.yml' });
|
||||
|
||||
assert.false(std.err);
|
||||
assert.true(std.out.includes('some-service'));
|
||||
assert.true(std.out.includes('test/volume:/mountedvolume:rw'));
|
||||
});
|
||||
|
||||
test('config show data for docker-compose files', async assert => {
|
||||
const std = await compose.configServices({ cwd: path.join(__dirname), log: true, config: 'docker-compose-42.yml' });
|
||||
|
||||
assert.false(std.err);
|
||||
assert.true(std.out.includes('some-service'));
|
||||
});
|
||||
|
||||
test('config show data for docker-compose files', async assert => {
|
||||
const std = await compose.configVolumes({ cwd: path.join(__dirname), log: true, config: 'docker-compose-42.yml' });
|
||||
|
||||
assert.false(std.err);
|
||||
assert.true(std.out.includes('db-data'));
|
||||
});
|
||||
|
||||
test('ps shows status data for started containers', async assert => {
|
||||
await compose.upAll({ cwd: path.join(__dirname), log: true });
|
||||
|
||||
const std = await compose.ps({ cwd: path.join(__dirname), log: true });
|
||||
|
||||
assert.false(std.err);
|
||||
assert.true(std.out.includes('compose_test_alpine'));
|
||||
assert.true(std.out.includes('compose_test_nginx'));
|
||||
|
||||
assert.end();
|
||||
});
|
||||
|
||||
test('ps does not show status data for stopped containers', async assert => {
|
||||
await compose.down({ cwd: path.join(__dirname), log: true });
|
||||
await compose.upOne('alpine', { cwd: path.join(__dirname), log: true });
|
||||
|
||||
const std = await compose.ps({ cwd: path.join(__dirname), log: true });
|
||||
|
||||
assert.false(std.err);
|
||||
assert.true(std.out.includes('compose_test_alpine'));
|
||||
assert.false(std.out.includes('compose_test_nginx'));
|
||||
|
||||
assert.end();
|
||||
});
|
||||
|
||||
test('restartAll does restart all containers', async assert => {
|
||||
await compose.upAll({ cwd: path.join(__dirname), log: true });
|
||||
await compose.restartAll({ cwd: path.join(__dirname), log: true });
|
||||
|
||||
assert.true(await isContainerRunning('/compose_test_nginx'));
|
||||
assert.end();
|
||||
});
|
||||
|
||||
test('restartMany does restart selected containers', async assert => {
|
||||
await compose.upAll({ cwd: path.join(__dirname), log: true });
|
||||
await compose.restartMany([ 'db', 'alpine' ], { cwd: path.join(__dirname), log: true });
|
||||
|
||||
assert.true(await isContainerRunning('/compose_test_nginx'));
|
||||
assert.end();
|
||||
});
|
||||
|
||||
test('restartOne does restart container', async assert => {
|
||||
await compose.upAll({ cwd: path.join(__dirname), log: true });
|
||||
await compose.restartOne('db', { cwd: path.join(__dirname), log: true });
|
||||
|
||||
assert.true(await isContainerRunning('/compose_test_nginx'));
|
||||
assert.end();
|
||||
});
|
||||
|
||||
test('logs does follow service logs', async assert => {
|
||||
await compose.upAll({ cwd: path.join(__dirname), log: true });
|
||||
await compose.logs('db', { cwd: path.join(__dirname), log: true });
|
||||
|
||||
assert.true(await isContainerRunning('/compose_test_nginx'));
|
||||
assert.end();
|
||||
});
|
||||
|
||||
test('returns the port for a started service', async assert => {
|
||||
const config = {
|
||||
cwd: path.join(__dirname),
|
||||
config: './docker-compose-2.yml'
|
||||
};
|
||||
|
||||
await compose.upAll(config);
|
||||
const port = await compose.port('db', 5432, config);
|
||||
|
||||
assert.true(port.out.match(/.*:[0-9]{1,5}/));
|
||||
assert.end();
|
||||
});
|
||||
425
test/index.test.js
Normal file
425
test/index.test.js
Normal file
|
|
@ -0,0 +1,425 @@
|
|||
'use strict';
|
||||
|
||||
const compose = require('../index');
|
||||
const path = require('path');
|
||||
const Docker = require('dockerode');
|
||||
const docker = new Docker();
|
||||
|
||||
// Docker commands, especially builds, can take some time. This makes sure that they can take the time they need.
|
||||
jest.setTimeout(25000);
|
||||
|
||||
// Set to true if you need to diagnose using output
|
||||
const logOutput = false;
|
||||
|
||||
const isContainerRunning = async name => new Promise((resolve, reject) => {
|
||||
docker.listContainers((err, containers) => {
|
||||
if (err) {
|
||||
reject(err);
|
||||
}
|
||||
|
||||
const running = containers.filter(container => container.Names.includes(name));
|
||||
|
||||
resolve(running.length > 0);
|
||||
});
|
||||
});
|
||||
|
||||
const repoTags = imageInfo => imageInfo.RepoTags || [];
|
||||
|
||||
const imageExists = async name => {
|
||||
const images = await docker.listImages();
|
||||
|
||||
const foundImage = images.findIndex(imageInfo => repoTags(imageInfo).includes(name));
|
||||
|
||||
return foundImage > -1;
|
||||
};
|
||||
|
||||
const removeImagesStartingWith = async searchString => {
|
||||
const images = await docker.listImages();
|
||||
|
||||
for (const image of images) {
|
||||
for (const repoTag of repoTags(image)) {
|
||||
if (repoTag.startsWith(searchString)) {
|
||||
const dockerImage = docker.getImage(repoTag);
|
||||
|
||||
if (logOutput) {
|
||||
process.stdout.write(`removing image ${repoTag} ${dockerImage.id || ''}`);
|
||||
}
|
||||
await dockerImage.remove();
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
test('ensure container gets started', async () => {
|
||||
await compose.down({ cwd: path.join(__dirname), log: logOutput });
|
||||
await compose.upAll({ cwd: path.join(__dirname), log: logOutput });
|
||||
|
||||
expect(await isContainerRunning('/compose_test_nginx')).toBeTruthy();
|
||||
await compose.down({ cwd: path.join(__dirname), log: logOutput });
|
||||
});
|
||||
|
||||
test('ensure exit code is returned correctly', async () => {
|
||||
let result = await compose.down({ cwd: path.join(__dirname), log: logOutput });
|
||||
|
||||
await expect(result).toMatchObject({
|
||||
exitCode: 0
|
||||
});
|
||||
|
||||
result = await compose.upAll({ cwd: path.join(__dirname), log: logOutput });
|
||||
expect(result).toMatchObject({
|
||||
exitCode: 0
|
||||
});
|
||||
try {
|
||||
await compose.logs('non_existent_service', { cwd: path.join(__dirname) });
|
||||
expect(false).toBeTruthy();
|
||||
} catch (rejectionResult) {
|
||||
expect(rejectionResult.exitCode).toBe(1);
|
||||
}
|
||||
await compose.down({ cwd: path.join(__dirname), log: logOutput });
|
||||
});
|
||||
|
||||
describe('starts containers properly with --build and --timeout options', () => {
|
||||
beforeEach(async () => {
|
||||
await compose.down({ cwd: path.join(__dirname), log: logOutput, config: 'docker-compose-build.yml' });
|
||||
});
|
||||
|
||||
afterEach(async () => {
|
||||
await compose.down({ cwd: path.join(__dirname), log: logOutput, config: 'docker-compose-build.yml' });
|
||||
});
|
||||
|
||||
test('ensure container gets started with --build option', async () => {
|
||||
await compose.upAll({
|
||||
cwd: path.join(__dirname),
|
||||
log: logOutput,
|
||||
config: 'docker-compose-build.yml',
|
||||
commandOptions: [ '--build' ]
|
||||
});
|
||||
|
||||
expect(await isContainerRunning('/compose_test_nginx')).toBeTruthy();
|
||||
});
|
||||
|
||||
test('ensure container gets started with --build and --timeout option', async () => {
|
||||
await compose.upAll({
|
||||
cwd: path.join(__dirname),
|
||||
log: logOutput,
|
||||
config: 'docker-compose-build.yml',
|
||||
commandOptions: [[ '--build' ], [ '--timeout', '5' ]]
|
||||
});
|
||||
|
||||
expect(await isContainerRunning('/compose_test_nginx')).toBeTruthy();
|
||||
});
|
||||
|
||||
test('ensure container gets started with --build and --timeout option with different command style', async () => {
|
||||
await compose.upAll({
|
||||
cwd: path.join(__dirname),
|
||||
log: logOutput,
|
||||
config: 'docker-compose-build.yml',
|
||||
commandOptions: [ '--build', [ '--timeout', '5' ]]
|
||||
});
|
||||
|
||||
expect(await isContainerRunning('/compose_test_nginx')).toBeTruthy();
|
||||
});
|
||||
});
|
||||
|
||||
test('ensure container command executed with --workdir command option', async () => {
|
||||
await compose.down({ cwd: path.join(__dirname), log: logOutput, config: 'docker-compose-42.yml' });
|
||||
const result = await compose.run('some-service', 'pwd', {
|
||||
cwd: path.join(__dirname),
|
||||
log: true,
|
||||
config: 'docker-compose-42.yml',
|
||||
composeOptions: [ '--verbose' ],
|
||||
|
||||
// Alpine has "/" as default
|
||||
commandOptions: [ '--workdir', '/home/root' ]
|
||||
});
|
||||
|
||||
expect(result.out).toBe('/home/root\n');
|
||||
|
||||
await compose.down({ cwd: path.join(__dirname), log: logOutput, config: 'docker-compose-42.yml' });
|
||||
});
|
||||
|
||||
test('ensure only single container gets started', async () => {
|
||||
await compose.down({ cwd: path.join(__dirname), log: logOutput });
|
||||
await compose.upOne('alpine', { cwd: path.join(__dirname), log: logOutput });
|
||||
|
||||
expect(await isContainerRunning('/compose_test_alpine')).toBeTruthy();
|
||||
expect(await isContainerRunning('/compose_test_nginx')).toBeFalsy();
|
||||
await compose.down({ cwd: path.join(__dirname), log: logOutput });
|
||||
});
|
||||
|
||||
test('ensure only multiple containers get started', async () => {
|
||||
await compose.down({ cwd: path.join(__dirname), log: logOutput });
|
||||
await compose.upMany([ 'alpine' ], { cwd: path.join(__dirname), log: logOutput });
|
||||
|
||||
expect(await isContainerRunning('/compose_test_alpine')).toBeTruthy();
|
||||
expect(await isContainerRunning('/compose_test_nginx')).toBeFalsy();
|
||||
await compose.down({ cwd: path.join(__dirname), log: logOutput });
|
||||
});
|
||||
|
||||
test('ensure container gets down', async () => {
|
||||
await compose.upAll({ cwd: path.join(__dirname), log: logOutput });
|
||||
expect(await isContainerRunning('/compose_test_nginx')).toBeTruthy();
|
||||
|
||||
await compose.down({ cwd: path.join(__dirname), log: logOutput });
|
||||
expect(await isContainerRunning('/compose_test_nginx')).toBeFalsy();
|
||||
});
|
||||
|
||||
test('ensure container gets stopped', async () => {
|
||||
await compose.upAll({ cwd: path.join(__dirname), log: logOutput });
|
||||
expect(await isContainerRunning('/compose_test_nginx')).toBeTruthy();
|
||||
|
||||
await compose.stop({ cwd: path.join(__dirname), log: logOutput });
|
||||
expect(await isContainerRunning('/compose_test_nginx')).toBeFalsy();
|
||||
await compose.down({ cwd: path.join(__dirname), log: logOutput });
|
||||
});
|
||||
|
||||
test('ensure container gets killed', async () => {
|
||||
await compose.upAll({ cwd: path.join(__dirname), log: logOutput });
|
||||
expect(await isContainerRunning('/compose_test_nginx')).toBeTruthy();
|
||||
|
||||
await compose.kill({ cwd: path.join(__dirname), log: logOutput });
|
||||
expect(await isContainerRunning('/compose_test_nginx')).toBeFalsy();
|
||||
|
||||
await compose.down({ cwd: path.join(__dirname), log: logOutput });
|
||||
});
|
||||
|
||||
test('ensure custom ymls are working', async () => {
|
||||
const config = './docker-compose-2.yml';
|
||||
const cwd = path.join(__dirname);
|
||||
|
||||
await compose.upAll({ cwd, log: logOutput, config });
|
||||
expect(await isContainerRunning('/compose_test_nginx_2')).toBeTruthy();
|
||||
|
||||
// config & [config] are the same thing, ensures that multiple configs are handled properly
|
||||
await compose.kill({ cwd, log: logOutput, config: [ config ]});
|
||||
expect(await isContainerRunning('/compose_test_nginx_2')).toBeFalsy();
|
||||
|
||||
await compose.down({ cwd, log: logOutput, config });
|
||||
});
|
||||
|
||||
test('ensure run and exec are working', async () => {
|
||||
const checkOSID = (out, id) => {
|
||||
// parse /etc/os-release contents
|
||||
const re = /([\w,_]+)=(.*)/g;
|
||||
let match;
|
||||
const os = {};
|
||||
|
||||
while ((match = re.exec(out)) !== null) { // eslint-disable-line no-cond-assign
|
||||
os[match[1]] = match[2];
|
||||
}
|
||||
|
||||
expect(os.ID).toBe(id);
|
||||
};
|
||||
|
||||
const opts = { cwd: path.join(__dirname), log: logOutput };
|
||||
|
||||
await compose.upAll(opts);
|
||||
|
||||
expect(await isContainerRunning('/compose_test_nginx')).toBeTruthy();
|
||||
|
||||
let std = await compose.exec('db', 'cat /etc/os-release', opts);
|
||||
|
||||
expect(std.err).toBeFalsy();
|
||||
checkOSID(std.out, 'debian');
|
||||
|
||||
std = await compose.run('alpine', 'cat /etc/os-release', opts);
|
||||
expect(std.err).toBeFalsy();
|
||||
checkOSID(std.out, 'alpine');
|
||||
|
||||
await compose.down({ cwd: path.join(__dirname), log: logOutput });
|
||||
});
|
||||
|
||||
test('ensure run and exec with command defined as array are working', async () => {
|
||||
const checkOSID = (out, id) => {
|
||||
// parse /etc/os-release contents
|
||||
const re = /([\w,_]+)=(.*)/g;
|
||||
let match;
|
||||
const os = {};
|
||||
|
||||
while ((match = re.exec(out)) !== null) { // eslint-disable-line no-cond-assign
|
||||
os[match[1]] = match[2];
|
||||
}
|
||||
|
||||
expect(os.ID).toBe(id);
|
||||
};
|
||||
|
||||
const opts = { cwd: path.join(__dirname), log: false };
|
||||
|
||||
await compose.upAll(opts);
|
||||
|
||||
expect(await isContainerRunning('/compose_test_nginx')).toBe(true);
|
||||
|
||||
let std = await compose.exec('db', [ '/bin/sh', '-c', 'cat /etc/os-release' ], opts);
|
||||
|
||||
expect(std.err).toBeFalsy();
|
||||
checkOSID(std.out, 'debian');
|
||||
|
||||
std = await compose.run('alpine', [ '/bin/sh', '-c', 'cat /etc/os-release' ], opts);
|
||||
expect(std.err).toBeFalsy();
|
||||
checkOSID(std.out, 'alpine');
|
||||
|
||||
await compose.down({ cwd: path.join(__dirname), log: logOutput });
|
||||
});
|
||||
|
||||
test('build single service', async () => {
|
||||
const opts = {
|
||||
cwd: path.join(__dirname),
|
||||
log: logOutput,
|
||||
config: 'docker-compose-build.yml'
|
||||
};
|
||||
|
||||
await removeImagesStartingWith('compose-test-build-image');
|
||||
|
||||
await compose.buildOne('build_test_1', opts);
|
||||
|
||||
expect(await imageExists('compose-test-build-image-1:test')).toBeTruthy();
|
||||
expect(await imageExists('compose-test-build-image-2:test')).toBeFalsy();
|
||||
expect(await imageExists('compose-test-build-image-3:test')).toBeFalsy();
|
||||
expect(await imageExists('compose-test-build-image-4:test')).toBeFalsy();
|
||||
|
||||
await removeImagesStartingWith('compose-test-build-image');
|
||||
});
|
||||
|
||||
test('build multiple services', async () => {
|
||||
const opts = {
|
||||
cwd: path.join(__dirname),
|
||||
log: logOutput,
|
||||
config: 'docker-compose-build.yml'
|
||||
};
|
||||
|
||||
await compose.buildMany([ 'build_test_2', 'build_test_3' ], opts);
|
||||
|
||||
expect(await imageExists('compose-test-build-image-1:test')).toBeFalsy();
|
||||
expect(await imageExists('compose-test-build-image-2:test')).toBeTruthy();
|
||||
expect(await imageExists('compose-test-build-image-3:test')).toBeTruthy();
|
||||
expect(await imageExists('compose-test-build-image-4:test')).toBeFalsy();
|
||||
|
||||
await removeImagesStartingWith('compose-test-build-image');
|
||||
});
|
||||
|
||||
test('build all services', async () => {
|
||||
const opts = {
|
||||
cwd: path.join(__dirname),
|
||||
log: logOutput,
|
||||
config: 'docker-compose-build.yml'
|
||||
};
|
||||
|
||||
await compose.buildAll(opts);
|
||||
|
||||
expect(await imageExists('compose-test-build-image-1:test')).toBeTruthy();
|
||||
expect(await imageExists('compose-test-build-image-2:test')).toBeTruthy();
|
||||
expect(await imageExists('compose-test-build-image-3:test')).toBeTruthy();
|
||||
expect(await imageExists('compose-test-build-image-4:test')).toBeTruthy();
|
||||
|
||||
await removeImagesStartingWith('compose-test-build-image');
|
||||
});
|
||||
|
||||
test('teardown', async () => {
|
||||
docker.listContainers((err, containers) => {
|
||||
if (err) {
|
||||
throw err;
|
||||
}
|
||||
|
||||
containers.forEach(container => {
|
||||
container.Names.forEach(name => {
|
||||
if (name.startsWith('/compose_test_')) {
|
||||
console.log(`stopping ${container.Id} ${container.Names}`);
|
||||
docker.getContainer(container.Id).stop();
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
await removeImagesStartingWith('compose-test-build-image');
|
||||
});
|
||||
|
||||
test('config show data for docker-compose files', async () => {
|
||||
const std = await compose.config({ cwd: path.join(__dirname), log: logOutput, config: 'docker-compose-42.yml' });
|
||||
|
||||
expect(std.err).toBeFalsy();
|
||||
expect(std.out.includes('some-service')).toBeTruthy();
|
||||
expect(std.out.includes('test/volume:/mountedvolume:rw')).toBeTruthy();
|
||||
});
|
||||
|
||||
test('config show data for docker-compose files (services)', async () => {
|
||||
const std = await compose.configServices({ cwd: path.join(__dirname), log: logOutput, config: 'docker-compose-42.yml' });
|
||||
|
||||
expect(std.err).toBeFalsy();
|
||||
expect(std.out.includes('some-service')).toBeTruthy();
|
||||
});
|
||||
|
||||
test('config show data for docker-compose files (volumes)', async () => {
|
||||
const std = await compose.configVolumes({ cwd: path.join(__dirname), log: logOutput, config: 'docker-compose-42.yml' });
|
||||
|
||||
expect(std.err).toBeFalsy();
|
||||
expect(std.out.includes('db-data')).toBeTruthy();
|
||||
});
|
||||
|
||||
test('ps shows status data for started containers', async () => {
|
||||
await compose.upAll({ cwd: path.join(__dirname), log: logOutput });
|
||||
|
||||
const std = await compose.ps({ cwd: path.join(__dirname), log: logOutput });
|
||||
|
||||
expect(std.err).toBeFalsy();
|
||||
expect(std.out.includes('compose_test_alpine')).toBeTruthy();
|
||||
expect(std.out.includes('compose_test_nginx')).toBeTruthy();
|
||||
await compose.down({ cwd: path.join(__dirname), log: logOutput });
|
||||
});
|
||||
|
||||
test('ps does not show status data for stopped containers', async () => {
|
||||
await compose.down({ cwd: path.join(__dirname), log: logOutput });
|
||||
await compose.upOne('alpine', { cwd: path.join(__dirname), log: logOutput });
|
||||
|
||||
const std = await compose.ps({ cwd: path.join(__dirname), log: logOutput });
|
||||
|
||||
expect(std.err).toBeFalsy();
|
||||
expect(std.out.includes('compose_test_alpine')).toBeTruthy();
|
||||
expect(std.out.includes('compose_test_nginx')).toBeFalsy();
|
||||
await compose.down({ cwd: path.join(__dirname), log: logOutput });
|
||||
});
|
||||
|
||||
test('restartAll does restart all containers', async () => {
|
||||
await compose.upAll({ cwd: path.join(__dirname), log: logOutput });
|
||||
await compose.restartAll({ cwd: path.join(__dirname), log: logOutput });
|
||||
|
||||
expect(await isContainerRunning('/compose_test_nginx')).toBeTruthy();
|
||||
await compose.down({ cwd: path.join(__dirname), log: logOutput });
|
||||
});
|
||||
|
||||
test('restartMany does restart selected containers', async () => {
|
||||
await compose.upAll({ cwd: path.join(__dirname), log: logOutput });
|
||||
await compose.restartMany([ 'db', 'alpine' ], { cwd: path.join(__dirname), log: logOutput });
|
||||
|
||||
expect(await isContainerRunning('/compose_test_nginx')).toBeTruthy();
|
||||
await compose.down({ cwd: path.join(__dirname), log: logOutput });
|
||||
});
|
||||
|
||||
test('restartOne does restart container', async () => {
|
||||
await compose.upAll({ cwd: path.join(__dirname), log: logOutput });
|
||||
await compose.restartOne('db', { cwd: path.join(__dirname), log: logOutput });
|
||||
|
||||
expect(await isContainerRunning('/compose_test_nginx')).toBeTruthy();
|
||||
await compose.down({ cwd: path.join(__dirname), log: logOutput });
|
||||
});
|
||||
|
||||
test('logs does follow service logs', async () => {
|
||||
await compose.upAll({ cwd: path.join(__dirname), log: logOutput });
|
||||
await compose.logs('db', { cwd: path.join(__dirname), log: logOutput });
|
||||
|
||||
expect(await isContainerRunning('/compose_test_nginx')).toBeTruthy();
|
||||
await compose.down({ cwd: path.join(__dirname), log: logOutput });
|
||||
});
|
||||
|
||||
test('returns the port for a started service', async () => {
|
||||
const config = {
|
||||
cwd: path.join(__dirname),
|
||||
config: './docker-compose-2.yml',
|
||||
log: logOutput
|
||||
};
|
||||
|
||||
await compose.upAll(config);
|
||||
const port = await compose.port('db', 5432, config);
|
||||
|
||||
expect(port.out).toMatch(/.*:[0-9]{1,5}/);
|
||||
await compose.down(config);
|
||||
});
|
||||
Loading…
Reference in a new issue