From e85af8c092a54a3fb48519bb39956324bbef4fc2 Mon Sep 17 00:00:00 2001 From: lancerutkin Date: Thu, 5 Mar 2020 20:42:02 -0500 Subject: [PATCH] feat: add ability to pass --abort-on-container-exit flag to up methods --- src/index.ts | 20 +++++++++++++++++--- test/index.test.ts | 34 +++++++++++++++++++++++++++++++++- 2 files changed, 50 insertions(+), 4 deletions(-) diff --git a/src/index.ts b/src/index.ts index 0b50630..ed8d4e6 100644 --- a/src/index.ts +++ b/src/index.ts @@ -108,16 +108,30 @@ const execCompose = (command, args, options: IDockerComposeOptions = {}): Promis } }); +/** + * Determines whether or not to use the default non-interactive flag -d for up commands + */ +const shouldUseDefaultNonInteractiveFlag = function(options: IDockerComposeOptions = {}): boolean { + const commandOptions = options.commandOptions || []; + const containsOtherNonInteractiveFlag = commandOptions.reduce((memo: boolean, item: string | string[]) => { + return memo && !item.includes('--abort-on-container-exit'); + }, true); + return containsOtherNonInteractiveFlag; +}; + export const upAll = function (options?: IDockerComposeOptions): Promise { - return execCompose('up', [ '-d' ], options); + const args = shouldUseDefaultNonInteractiveFlag(options) ? [ '-d' ] : []; + return execCompose('up', args, options); }; export const upMany = function (services: string[], options?: IDockerComposeOptions): Promise { - return execCompose('up', [ '-d' ].concat(services), options); + const args = shouldUseDefaultNonInteractiveFlag(options) ? [ '-d' ].concat(services) : services; + return execCompose('up', args, options); }; export const upOne = function (service: string, options?: IDockerComposeOptions): Promise { - return execCompose('up', [ '-d', service ], options); + const args = shouldUseDefaultNonInteractiveFlag(options) ? [ '-d', service ] : [ service ]; + return execCompose('up', args, options); }; export const down = function (options?: IDockerComposeOptions): Promise { diff --git a/test/index.test.ts b/test/index.test.ts index 01f0d2b..ebf5e71 100644 --- a/test/index.test.ts +++ b/test/index.test.ts @@ -5,7 +5,7 @@ import * as path from 'path'; const docker = new Docker(); // Docker commands, especially builds, can take some time. This makes sure that they can take the time they need. -jest.setTimeout(25000); +jest.setTimeout(50000); // Set to true if you need to diagnose using output const logOutput = false; @@ -182,6 +182,36 @@ test('ensure only single container gets stopped', async (): Promise => { await compose.down({ cwd: path.join(__dirname), log: logOutput }); }); +test('ensure container gets started with --abort-on-container-exit option', async (): Promise => { + const result = await compose.upAll({ + cwd: path.join(__dirname), + log: logOutput, + commandOptions: [ '--abort-on-container-exit' ] + }); + + expect(result).toMatchObject({ + exitCode: 0 + }); + + expect(await isContainerRunning('/compose_test_nginx')).toBeFalsy(); + expect(await isContainerRunning('/compose_test_alpine')).toBeFalsy(); + await compose.down({ cwd: path.join(__dirname), log: logOutput }); +}); + +test('ensure container gets started with --abort-on-container-exit option correctly aborts all services when a container exits', async (): Promise => { + const result = await compose.upAll({ + cwd: path.join(__dirname), + log: logOutput, + commandOptions: [ '--abort-on-container-exit' ] + }); + + expect(result.out).toMatch(/Aborting on container exit/); + + expect(await isContainerRunning('/compose_test_nginx')).toBeFalsy(); + expect(await isContainerRunning('/compose_test_alpine')).toBeFalsy(); + await compose.down({ cwd: path.join(__dirname), log: logOutput }); +}); + test('ensure container gets killed', async (): Promise => { await compose.upAll({ cwd: path.join(__dirname), log: logOutput }); expect(await isContainerRunning('/compose_test_nginx')).toBeTruthy(); @@ -206,6 +236,8 @@ test('ensure custom ymls are working', async (): Promise => { await compose.down({ cwd, log: logOutput, config }); }); + + test('ensure run and exec are working', async (): Promise => { const checkOSID = (out, id): void => { // parse /etc/os-release contents