From 88894128400301fd80fb8d0e1e28ccfded52ed10 Mon Sep 17 00:00:00 2001 From: Ignatiev Mikhail Date: Sat, 16 Jun 2018 13:00:24 +0300 Subject: [PATCH] custom ymls support + tests, eslint aid for tests --- index.js | 146 ++++++++++++++------------------------ test/docker-compose-2.yml | 6 ++ test/index.js | 75 ++++++++++---------- 3 files changed, 100 insertions(+), 127 deletions(-) create mode 100644 test/docker-compose-2.yml diff --git a/index.js b/index.js index 14fd6e7..f7109c4 100644 --- a/index.js +++ b/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 }; diff --git a/test/docker-compose-2.yml b/test/docker-compose-2.yml new file mode 100644 index 0000000..6497927 --- /dev/null +++ b/test/docker-compose-2.yml @@ -0,0 +1,6 @@ +version: '2' + +services: + db: + image: alpine:3.7 + container_name: compose_test_alpine \ No newline at end of file diff --git a/test/index.js b/test/index.js index 4851c70..231fe56 100644 --- a/test/index.js +++ b/test/index.js @@ -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_')) {