Merge pull request #23 from rafalsiwiec/more-detailed-up-methods

More detailed up methods
This commit is contained in:
Alexander Zeitler 2018-09-24 13:57:47 +02:00 committed by GitHub
commit b606d7c054
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 63 additions and 13 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

@ -12,13 +12,18 @@ npm install --save-dev docker-compose
`docker-compose` current supports these commands:
* `up(options)` - create and start containers - always uses the `-d` flag due to non interactive mode
* `upAll(options)` - Create and start containers - always uses the `-d` flag due to non interactive mode
* `upMany(services, options)` - Create and start containers specified in `services` - always uses the `-d` flag due to non interactive mode
* `upOne(service, options)` - Create and start container specified in `service` - always uses the `-d` flag due to non interactive mode
* `down(options)` - Stop and remove containers, networks, images, and volumes
* `kill(options)` - Kill containers
* `stop(options)` - Stop services
* `rm(options)` - Remove stopped containers - always uses the `-f` flag due to non interactive mode
* `exec(container, command, options)` - Exec `command` inside `container`, uses `-T` to properly handle stdin & stdout
* `run(container, command, options)` - Run `command` inside `container`, uses `-T` to properly handle stdin & stdout
* `buildAll(options)` - Build all images
* `buildMany(services, options)` - Build images of specified services
* `buildOne(service, options)` - Build image of specified service
All commands return a `Promise({object})` with an stdout and stderr strings
```javascript

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'));