Skip to content

Commit

Permalink
ContainerService raises if the container process fails
Browse files Browse the repository at this point in the history
  • Loading branch information
Barry Gordon committed Sep 30, 2021
1 parent 3b9b586 commit 6ea99c3
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 14 deletions.
38 changes: 28 additions & 10 deletions __tests__/container-service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
})
})
})
10 changes: 9 additions & 1 deletion dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

14 changes: 12 additions & 2 deletions src/container-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -29,7 +31,7 @@ export const ContainerService = {
await container.putArchive(tar, {path})
},

async run(container: Container): Promise<void> {
async run(container: Container): Promise<boolean> {
try {
const stream = await container.attach({
stream: true,
Expand All @@ -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({v: true})
core.info(`Cleaned up container ${container.id}`)
Expand Down

0 comments on commit 6ea99c3

Please sign in to comment.