More detailed up methods

`upAll`, `upMany` and `upOne` methods were introduced.
This commit is contained in:
Rafał Siwiec 2018-09-24 07:30:25 +02:00
parent a936a17958
commit 348c6cfcb3
3 changed files with 57 additions and 12 deletions

View file

@ -69,10 +69,34 @@ const execCompose = (command, args, options) => new Promise((resolve, reject) =>
* @param {?(string|string[])} [options.config]
* @param {?object} [options.env]
*/
const up = function (options) {
const upAll = function (options) {
return execCompose('up', [ '-d' ], options);
};
/**
* @param {string[]} services
* @param {object} options
* @param {string} options.cwd
* @param {boolean} [options.log]
* @param {?(string|string[])} [options.config]
* @param {?object} [options.env]
*/
const upMany = function (services, options) {
return execCompose('up', [ '-d' ].concat(services), options);
};
/**
* @param {string} service
* @param {object} options
* @param {string} options.cwd
* @param {boolean} [options.log]
* @param {?(string|string[])} [options.config]
* @param {?object} [options.env]
*/
const upOne = function (service, options) {
return execCompose('up', [ '-d', service ], options);
};
/**
* @param {object} options
* @param {string} options.cwd
@ -197,4 +221,4 @@ const buildOne = function (service, options) {
return execCompose('build', [ service ], options);
};
module.exports = { up, kill, down, stop, rm, exec, run, buildAll, buildMany, buildOne };
module.exports = { upAll, upMany, upOne, kill, down, stop, rm, exec, run, buildAll, buildMany, buildOne };

View file

@ -1,9 +1,9 @@
version: '2'
services:
services:
db:
image: mongo:3.4
container_name: compose_test_mongodb
alpine:
image: alpine:3.7
container_name: compose_test_apline
container_name: compose_test_alpine

View file

@ -20,10 +20,12 @@ const isContainerRunning = async name => new Promise((resolve, reject) => {
});
});
const repoTags = imageInfo => imageInfo.RepoTags || [];
const imageExists = async name => {
const images = await docker.listImages();
const foundImage = images.findIndex(imageInfo => imageInfo.RepoTags.includes(name));
const foundImage = images.findIndex(imageInfo => repoTags(imageInfo).includes(name));
return foundImage > -1;
};
@ -32,7 +34,7 @@ const removeImagesStartingWith = async searchString => {
const images = await docker.listImages();
for (const image of images) {
for (const repoTag of image.RepoTags) {
for (const repoTag of repoTags(image)) {
if (repoTag.startsWith(searchString)) {
const dockerImage = docker.getImage(repoTag);
@ -44,14 +46,33 @@ const removeImagesStartingWith = async searchString => {
};
test('ensure container gets started', async assert => {
await compose.up({ cwd: path.join(__dirname), log: true });
await compose.down({ cwd: path.join(__dirname), log: true });
await compose.upAll({ cwd: path.join(__dirname), log: true });
assert.true(await isContainerRunning('/compose_test_mongodb'));
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_mongodb'));
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_mongodb'));
assert.end();
});
test('ensure container gets down', async assert => {
await compose.up({ cwd: path.join(__dirname), log: true });
await compose.upAll({ cwd: path.join(__dirname), log: true });
await compose.down({ cwd: path.join(__dirname), log: true });
assert.false(await isContainerRunning('/compose_test_mongodb'));
@ -59,7 +80,7 @@ test('ensure container gets down', async assert => {
});
test('ensure container gets stopped', async assert => {
await compose.up({ cwd: path.join(__dirname), log: true });
await compose.upAll({ cwd: path.join(__dirname), log: true });
await compose.stop({ cwd: path.join(__dirname), log: true });
assert.false(await isContainerRunning('/compose_test_mongodb'));
@ -67,7 +88,7 @@ test('ensure container gets stopped', async assert => {
});
test('ensure container gets killed', async assert => {
await compose.up({ cwd: path.join(__dirname), log: true });
await compose.upAll({ cwd: path.join(__dirname), log: true });
await compose.kill({ cwd: path.join(__dirname), log: true });
assert.false(await isContainerRunning('/compose_test_mongodb'));
@ -79,7 +100,7 @@ test('ensure custom ymls are working', async assert => {
const cwd = path.join(__dirname);
const log = true;
await compose.up({ cwd, log, config });
await compose.upAll({ cwd, log, config });
assert.true(await isContainerRunning('/compose_test_mongodb_2'));
// config & [config] are the same thing, ensures that multiple configs are handled properly
@ -104,7 +125,7 @@ test('ensure run and exec are working', async assert => {
const opts = { cwd: path.join(__dirname), log: false };
await compose.up(opts);
await compose.upAll(opts);
assert.true(await isContainerRunning('/compose_test_mongodb'));