From 348c6cfcb313dfc989d435ceaf00b3e37aea0ad3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafa=C5=82=20Siwiec?= Date: Mon, 24 Sep 2018 07:30:25 +0200 Subject: [PATCH] More detailed up methods `upAll`, `upMany` and `upOne` methods were introduced. --- index.js | 28 ++++++++++++++++++++++++++-- test/docker-compose.yml | 4 ++-- test/index.js | 37 +++++++++++++++++++++++++++++-------- 3 files changed, 57 insertions(+), 12 deletions(-) diff --git a/index.js b/index.js index f196fa8..0c003cc 100644 --- a/index.js +++ b/index.js @@ -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 }; diff --git a/test/docker-compose.yml b/test/docker-compose.yml index b56b445..471dd32 100644 --- a/test/docker-compose.yml +++ b/test/docker-compose.yml @@ -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 \ No newline at end of file + container_name: compose_test_alpine diff --git a/test/index.js b/test/index.js index b59521b..435beaa 100644 --- a/test/index.js +++ b/test/index.js @@ -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'));