mirror of
https://github.com/danbulant/docker-compose
synced 2026-05-24 12:35:32 +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> {
|
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> {
|
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> {
|
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> {
|
export const down = function (options?: IDockerComposeOptions): Promise<IDockerComposeResult> {
|
||||||
|
|
|
||||||
|
|
@ -5,7 +5,7 @@ import * as path from 'path';
|
||||||
const docker = new Docker();
|
const docker = new Docker();
|
||||||
|
|
||||||
// Docker commands, especially builds, can take some time. This makes sure that they can take the time they need.
|
// 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
|
// Set to true if you need to diagnose using output
|
||||||
const logOutput = false;
|
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 });
|
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> => {
|
test('ensure container gets killed', async (): Promise<void> => {
|
||||||
await compose.upAll({ cwd: path.join(__dirname), log: logOutput });
|
await compose.upAll({ cwd: path.join(__dirname), log: logOutput });
|
||||||
expect(await isContainerRunning('/compose_test_nginx')).toBeTruthy();
|
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 });
|
await compose.down({ cwd, log: logOutput, config });
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
test('ensure run and exec are working', async (): Promise<void> => {
|
test('ensure run and exec are working', async (): Promise<void> => {
|
||||||
const checkOSID = (out, id): void => {
|
const checkOSID = (out, id): void => {
|
||||||
// parse /etc/os-release contents
|
// parse /etc/os-release contents
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue