mirror of
https://github.com/danbulant/docker-compose
synced 2026-05-19 20:38:35 +00:00
custom ymls support + tests, eslint aid for tests
This commit is contained in:
parent
3c87d3a8df
commit
8889412840
3 changed files with 100 additions and 127 deletions
146
index.js
146
index.js
|
|
@ -19,133 +19,97 @@ const logStandards = function (standards) {
|
|||
};
|
||||
|
||||
/**
|
||||
* Converts supplied yml files to cli arguments
|
||||
* https://docs.docker.com/compose/reference/overview/#use--f-to-specify-name-and-path-of-one-or-more-compose-files
|
||||
* @param {?(string|string[])} config
|
||||
*/
|
||||
const configToArgs = config => {
|
||||
if (typeof config === 'undefined') {
|
||||
return '';
|
||||
} else if (typeof config === 'string') {
|
||||
return `-f ${config}`;
|
||||
} else if (config instanceof Array) {
|
||||
return config.map(configToArgs).join(' ');
|
||||
}
|
||||
throw new Error(`Invalid argument supplied: ${config}`);
|
||||
};
|
||||
|
||||
/**
|
||||
* Executes docker-compose command with common options
|
||||
* @param {string} command
|
||||
* @param {object} options
|
||||
* @param {boolean} [options.log]
|
||||
* @param {string} options.cwd
|
||||
* @param {?(string|string[])} options.config
|
||||
*/
|
||||
const execCompose = (command, options) => new Promise((resolve, reject) => {
|
||||
const cmd = `docker-compose ${configToArgs(options.config)} ${command}`;
|
||||
const cwd = options.cwd;
|
||||
|
||||
exec(cmd, { cwd }).then(
|
||||
standards => {
|
||||
if (options.log) {
|
||||
logStandards(standards);
|
||||
}
|
||||
|
||||
resolve();
|
||||
},
|
||||
error => {
|
||||
logger.error(error.message);
|
||||
|
||||
return reject(error);
|
||||
}
|
||||
);
|
||||
});
|
||||
|
||||
/**
|
||||
* @param {object} options
|
||||
* @param {boolean} [options.log]
|
||||
* @param {string} options.cwd
|
||||
* @param {?(string|string[])} options.config
|
||||
*/
|
||||
const up = function (options) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const cwd = options.cwd;
|
||||
|
||||
exec('docker-compose up -d', { cwd }).then(
|
||||
standards => {
|
||||
if (options.log) {
|
||||
logStandards(standards);
|
||||
}
|
||||
|
||||
return resolve();
|
||||
},
|
||||
err => {
|
||||
logger.error(err.message);
|
||||
|
||||
return reject(err);
|
||||
}
|
||||
);
|
||||
});
|
||||
return execCompose('up -d', options);
|
||||
};
|
||||
|
||||
/**
|
||||
* @param {object} options
|
||||
* @param {boolean} [options.log]
|
||||
* @param {string} options.cwd
|
||||
* @param {?(string|string[])} options.config
|
||||
*/
|
||||
const down = function (options) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const cwd = options.cwd;
|
||||
|
||||
exec('docker-compose down', { cwd }).then(
|
||||
standards => {
|
||||
if (options.log) {
|
||||
logStandards(standards);
|
||||
}
|
||||
|
||||
return resolve();
|
||||
},
|
||||
err => {
|
||||
logger.error(err.message);
|
||||
|
||||
return reject(err);
|
||||
}
|
||||
);
|
||||
});
|
||||
return execCompose('down', options);
|
||||
};
|
||||
|
||||
/**
|
||||
* @param {object} options
|
||||
* @param {boolean} [options.log]
|
||||
* @param {cwd} options.cwd
|
||||
* @param {string} options.cwd
|
||||
* @param {?(string|string[])} options.config
|
||||
*/
|
||||
const stop = function (options) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const cwd = options.cwd;
|
||||
|
||||
exec('docker-compose stop', { cwd }).then(
|
||||
standards => {
|
||||
if (options.log) {
|
||||
logStandards(standards);
|
||||
}
|
||||
|
||||
return resolve();
|
||||
},
|
||||
err => {
|
||||
logger.error(err.message);
|
||||
|
||||
return reject(err);
|
||||
}
|
||||
);
|
||||
});
|
||||
return execCompose('stop', options);
|
||||
};
|
||||
|
||||
/**
|
||||
* @param {object} options
|
||||
* @param {boolean} [options.log]
|
||||
* @param {string} options.cwd
|
||||
* @param {?(string|string[])} options.config
|
||||
*/
|
||||
const kill = function (options) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const cwd = options.cwd;
|
||||
|
||||
exec('docker-compose kill', { cwd }).then(
|
||||
standards => {
|
||||
if (options.log) {
|
||||
logStandards(standards);
|
||||
}
|
||||
|
||||
return resolve();
|
||||
},
|
||||
err => {
|
||||
logger.error(err.message);
|
||||
|
||||
return reject(err);
|
||||
}
|
||||
);
|
||||
});
|
||||
return execCompose('kill', options);
|
||||
};
|
||||
|
||||
/**
|
||||
* @param {object} options
|
||||
* @param {boolean} [options.log]
|
||||
* @param {string} options.cwd
|
||||
* @param {?(string|string[])} options.config
|
||||
*/
|
||||
const rm = function (options) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const cwd = options.cwd;
|
||||
|
||||
exec('docker-compose rm -f', { cwd }).then(
|
||||
standards => {
|
||||
if (options.log) {
|
||||
logStandards(standards);
|
||||
}
|
||||
|
||||
return resolve();
|
||||
},
|
||||
err => {
|
||||
logger.error(err.message);
|
||||
|
||||
return reject(err);
|
||||
}
|
||||
);
|
||||
});
|
||||
return execCompose('rm -f', options);
|
||||
};
|
||||
|
||||
module.exports = { up, kill, down, stop, rm };
|
||||
|
|
|
|||
6
test/docker-compose-2.yml
Normal file
6
test/docker-compose-2.yml
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
version: '2'
|
||||
|
||||
services:
|
||||
db:
|
||||
image: alpine:3.7
|
||||
container_name: compose_test_alpine
|
||||
|
|
@ -3,71 +3,74 @@
|
|||
const compose = require('../index');
|
||||
const path = require('path');
|
||||
const tape = require('tape');
|
||||
const _test = require('tape-promise').default;
|
||||
const test = _test(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);
|
||||
});
|
||||
});
|
||||
|
||||
test('ensure container gets started', async assert => {
|
||||
await compose.up({ cwd: path.join(__dirname), log: true });
|
||||
|
||||
docker.listContainers((err, containers) => {
|
||||
const containerIsRunning = containers.find(container =>
|
||||
container.Names.includes('/compose_test_mongodb')
|
||||
);
|
||||
|
||||
assert.true(containerIsRunning);
|
||||
assert.end();
|
||||
});
|
||||
assert.true(await isContainerRunning('/compose_test_mongodb'));
|
||||
assert.end();
|
||||
});
|
||||
|
||||
test('ensure container gets down', async assert => {
|
||||
await compose.up({ cwd: path.join(__dirname), log: true });
|
||||
await compose.down({ cwd: path.join(__dirname), log: true });
|
||||
|
||||
docker.listContainers((err, containers) => {
|
||||
// eslint-disable-line
|
||||
const containerIsRunning = containers.find(container =>
|
||||
container.Names.includes('/compose_test_mongodb')
|
||||
);
|
||||
|
||||
assert.false(containerIsRunning);
|
||||
assert.end();
|
||||
});
|
||||
assert.false(await isContainerRunning('/compose_test_mongodb'));
|
||||
assert.end();
|
||||
});
|
||||
|
||||
test('ensure container gets stopped', async assert => {
|
||||
await compose.up({ cwd: path.join(__dirname), log: true });
|
||||
await compose.stop({ cwd: path.join(__dirname), log: true });
|
||||
|
||||
docker.listContainers((err, containers) => {
|
||||
// eslint-disable-line
|
||||
const containerIsRunning = containers.find(container =>
|
||||
container.Names.includes('/compose_test_mongodb')
|
||||
);
|
||||
|
||||
assert.false(containerIsRunning);
|
||||
assert.end();
|
||||
});
|
||||
assert.false(await isContainerRunning('/compose_test_mongodb'));
|
||||
assert.end();
|
||||
});
|
||||
|
||||
test('ensure container gets killed', async assert => {
|
||||
await compose.up({ cwd: path.join(__dirname), log: true });
|
||||
await compose.kill({ cwd: path.join(__dirname), log: true });
|
||||
|
||||
docker.listContainers((err, containers) => {
|
||||
// eslint-disable-line
|
||||
const containerIsRunning = containers.find(container =>
|
||||
container.Names.includes('/compose_test_mongodb')
|
||||
);
|
||||
assert.false(await isContainerRunning('/compose_test_mongodb'));
|
||||
assert.end();
|
||||
});
|
||||
|
||||
assert.false(containerIsRunning);
|
||||
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.up({ cwd, log, config });
|
||||
assert.true(await isContainerRunning('/compose_test_alpine'));
|
||||
|
||||
// config & [config] is the same thing, ensures that multiple configs are handled properly
|
||||
await compose.kill({ cwd, log, config: [ config ]});
|
||||
assert.false(await isContainerRunning('/compose_test_alpine'));
|
||||
assert.end();
|
||||
});
|
||||
|
||||
test('teardown', assert => {
|
||||
docker.listContainers((err, containers) => {
|
||||
if (err) {
|
||||
throw err;
|
||||
}
|
||||
|
||||
containers.forEach(container => {
|
||||
container.Names.forEach(name => {
|
||||
if (name.startsWith('/compose_test_')) {
|
||||
|
|
|
|||
Loading…
Reference in a new issue