-
Notifications
You must be signed in to change notification settings - Fork 4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(cdk-assets): Error when building Docker Image Assets with Podman (#…
…24003) Encountering the same error as #16209 (comment). When using `podman inspect` to check whether or not an image exists, the exit code for when an image does not exist is `125`, while Docker's is `1`. This change will treat either of these exit codes as meaning that the image with the given tag does not currently exist. Closes #16209 ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
- Loading branch information
Showing
3 changed files
with
127 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
import { Docker } from '../../lib/private/docker'; | ||
import { ShellOptions, ProcessFailedError } from '../../lib/private/shell'; | ||
|
||
type ShellExecuteMock = jest.SpyInstance<ReturnType<Docker['execute']>, Parameters<Docker['execute']>>; | ||
|
||
describe('Docker', () => { | ||
describe('exists', () => { | ||
let docker: Docker; | ||
|
||
const makeShellExecuteMock = ( | ||
fn: (params: string[]) => void, | ||
): ShellExecuteMock => | ||
jest.spyOn<{ execute: Docker['execute'] }, 'execute'>(Docker.prototype as any, 'execute').mockImplementation( | ||
async (params: string[], _options?: ShellOptions) => fn(params), | ||
); | ||
|
||
afterEach(() => { | ||
jest.restoreAllMocks(); | ||
}); | ||
|
||
beforeEach(() => { | ||
docker = new Docker(); | ||
}); | ||
|
||
test('returns true when image inspect command does not throw', async () => { | ||
const spy = makeShellExecuteMock(() => undefined); | ||
|
||
const imageExists = await docker.exists('foo'); | ||
|
||
expect(imageExists).toBe(true); | ||
expect(spy.mock.calls[0][0]).toEqual(['inspect', 'foo']); | ||
}); | ||
|
||
test('throws when an arbitrary error is caught', async () => { | ||
makeShellExecuteMock(() => { | ||
throw new Error(); | ||
}); | ||
|
||
await expect(docker.exists('foo')).rejects.toThrow(); | ||
}); | ||
|
||
test('throws when the error is a shell failure but the exit code is unrecognized', async () => { | ||
makeShellExecuteMock(() => { | ||
throw new (class extends Error implements ProcessFailedError { | ||
public readonly code = 'PROCESS_FAILED' | ||
public readonly exitCode = 47 | ||
public readonly signal = null | ||
|
||
constructor() { | ||
super('foo'); | ||
} | ||
}); | ||
}); | ||
|
||
await expect(docker.exists('foo')).rejects.toThrow(); | ||
}); | ||
|
||
test('returns false when the error is a shell failure and the exit code is 1 (Docker)', async () => { | ||
makeShellExecuteMock(() => { | ||
throw new (class extends Error implements ProcessFailedError { | ||
public readonly code = 'PROCESS_FAILED' | ||
public readonly exitCode = 1 | ||
public readonly signal = null | ||
|
||
constructor() { | ||
super('foo'); | ||
} | ||
}); | ||
}); | ||
|
||
const imageExists = await docker.exists('foo'); | ||
|
||
expect(imageExists).toBe(false); | ||
}); | ||
|
||
test('returns false when the error is a shell failure and the exit code is 125 (Podman)', async () => { | ||
makeShellExecuteMock(() => { | ||
throw new (class extends Error implements ProcessFailedError { | ||
public readonly code = 'PROCESS_FAILED' | ||
public readonly exitCode = 125 | ||
public readonly signal = null | ||
|
||
constructor() { | ||
super('foo'); | ||
} | ||
}); | ||
}); | ||
|
||
const imageExists = await docker.exists('foo'); | ||
|
||
expect(imageExists).toBe(false); | ||
}); | ||
}); | ||
}); |