diff --git a/__tests__/container-service.test.ts b/__tests__/container-service.test.ts index 64bdc38..5b0782f 100644 --- a/__tests__/container-service.test.ts +++ b/__tests__/container-service.test.ts @@ -7,18 +7,36 @@ describe('ContainerService', () => { const docker = new Docker() let container: any - beforeAll(async () => { - await ImageService.pull('alpine') - container = await docker.createContainer({ - Image: 'alpine', - AttachStdout: true, - AttachStderr: true, - Cmd: ['/bin/sh', '-c', 'echo $VAR'], - Env: ['VAR=env-var'] + describe('when a container runs successfully', () => { + beforeEach(async () => { + await ImageService.pull('alpine') + container = await docker.createContainer({ + Image: 'alpine', + AttachStdout: true, + AttachStderr: true, + Cmd: ['/bin/sh', '-c', 'echo $VAR'], + Env: ['VAR=env-var'] + }) + }) + + test('it returns true', async () => { + expect(await ContainerService.run(container)).toBe(true) }) }) - test('runs containers', async () => { - await ContainerService.run(container) + describe('when a container runs unsuccessfully', () => { + beforeEach(async () => { + await ImageService.pull('alpine') + container = await docker.createContainer({ + Image: 'alpine', + AttachStdout: true, + AttachStderr: true, + Cmd: ['/bin/sh', '-c'] + }) + }) + + test('raises an exception', async () => { + await expect(ContainerService.run(container)).rejects.toThrow() + }) }) }) diff --git a/src/container-service.ts b/src/container-service.ts index 9984e93..b6bf4c1 100644 --- a/src/container-service.ts +++ b/src/container-service.ts @@ -4,6 +4,8 @@ import {pack} from 'tar-stream' import {FileFetcherInput, FileUpdaterInput, ProxyConfig} from './config-types' import {outStream, errStream} from './utils' +class ContainerRuntimeError extends Error {} + export const ContainerService = { async storeInput( name: string, @@ -29,7 +31,7 @@ export const ContainerService = { await container.putArchive(tar, {path}) }, - async run(container: Container): Promise { + async run(container: Container): Promise { try { const stream = await container.attach({ stream: true, @@ -43,7 +45,15 @@ export const ContainerService = { ) await container.start() - await container.wait() + const outcome = await container.wait() + + if (outcome.StatusCode === 0) { + return true + } else { + throw new ContainerRuntimeError( + `Failure running container ${container.id}` + ) + } } finally { await container.remove() core.info(`Cleaned up container ${container.id}`)