mirror of
https://github.com/danbulant/docker-compose
synced 2026-05-19 12:28:43 +00:00
feat: add ability to pass --abort-on-container-exit flag to up methods
This commit is contained in:
parent
2f573eef17
commit
e85af8c092
2 changed files with 50 additions and 4 deletions
20
src/index.ts
20
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<IDockerComposeResult> {
|
||||
return execCompose('up', [ '-d' ], options);
|
||||
const args = shouldUseDefaultNonInteractiveFlag(options) ? [ '-d' ] : [];
|
||||
return execCompose('up', args, options);
|
||||
};
|
||||
|
||||
export const upMany = function (services: string[], options?: IDockerComposeOptions): Promise<IDockerComposeResult> {
|
||||
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<IDockerComposeResult> {
|
||||
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<IDockerComposeResult> {
|
||||
|
|
|
|||
|
|
@ -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<void> => {
|
|||
await compose.down({ cwd: path.join(__dirname), log: logOutput });
|
||||
});
|
||||
|
||||
test('ensure container gets started with --abort-on-container-exit option', async (): Promise<void> => {
|
||||
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<void> => {
|
||||
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<void> => {
|
||||
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<void> => {
|
|||
await compose.down({ cwd, log: logOutput, config });
|
||||
});
|
||||
|
||||
|
||||
|
||||
test('ensure run and exec are working', async (): Promise<void> => {
|
||||
const checkOSID = (out, id): void => {
|
||||
// parse /etc/os-release contents
|
||||
|
|
|
|||
Loading…
Reference in a new issue