|
| 1 | +import { Docker } from '../../lib/private/docker'; |
| 2 | +import { ShellOptions, ProcessFailedError } from '../../lib/private/shell'; |
| 3 | + |
| 4 | +type ShellExecuteMock = jest.SpyInstance<ReturnType<Docker['execute']>, Parameters<Docker['execute']>>; |
| 5 | + |
| 6 | +describe('Docker', () => { |
| 7 | + describe('exists', () => { |
| 8 | + let docker: Docker; |
| 9 | + |
| 10 | + const makeShellExecuteMock = ( |
| 11 | + fn: (params: string[]) => void, |
| 12 | + ): ShellExecuteMock => |
| 13 | + jest.spyOn<{ execute: Docker['execute'] }, 'execute'>(Docker.prototype as any, 'execute').mockImplementation( |
| 14 | + async (params: string[], _options?: ShellOptions) => fn(params), |
| 15 | + ); |
| 16 | + |
| 17 | + afterEach(() => { |
| 18 | + jest.restoreAllMocks(); |
| 19 | + }); |
| 20 | + |
| 21 | + beforeEach(() => { |
| 22 | + docker = new Docker(); |
| 23 | + }); |
| 24 | + |
| 25 | + test('returns true when image inspect command does not throw', async () => { |
| 26 | + const spy = makeShellExecuteMock(() => undefined); |
| 27 | + |
| 28 | + const imageExists = await docker.exists('foo'); |
| 29 | + |
| 30 | + expect(imageExists).toBe(true); |
| 31 | + expect(spy.mock.calls[0][0]).toEqual(['inspect', 'foo']); |
| 32 | + }); |
| 33 | + |
| 34 | + test('throws when an arbitrary error is caught', async () => { |
| 35 | + makeShellExecuteMock(() => { |
| 36 | + throw new Error(); |
| 37 | + }); |
| 38 | + |
| 39 | + await expect(docker.exists('foo')).rejects.toThrow(); |
| 40 | + }); |
| 41 | + |
| 42 | + test('throws when the error is a shell failure but the exit code is unrecognized', async () => { |
| 43 | + makeShellExecuteMock(() => { |
| 44 | + throw new (class extends Error implements ProcessFailedError { |
| 45 | + public readonly code = 'PROCESS_FAILED' |
| 46 | + public readonly exitCode = 47 |
| 47 | + public readonly signal = null |
| 48 | + |
| 49 | + constructor() { |
| 50 | + super('foo'); |
| 51 | + } |
| 52 | + }); |
| 53 | + }); |
| 54 | + |
| 55 | + await expect(docker.exists('foo')).rejects.toThrow(); |
| 56 | + }); |
| 57 | + |
| 58 | + test('returns false when the error is a shell failure and the exit code is 1 (Docker)', async () => { |
| 59 | + makeShellExecuteMock(() => { |
| 60 | + throw new (class extends Error implements ProcessFailedError { |
| 61 | + public readonly code = 'PROCESS_FAILED' |
| 62 | + public readonly exitCode = 1 |
| 63 | + public readonly signal = null |
| 64 | + |
| 65 | + constructor() { |
| 66 | + super('foo'); |
| 67 | + } |
| 68 | + }); |
| 69 | + }); |
| 70 | + |
| 71 | + const imageExists = await docker.exists('foo'); |
| 72 | + |
| 73 | + expect(imageExists).toBe(false); |
| 74 | + }); |
| 75 | + |
| 76 | + test('returns false when the error is a shell failure and the exit code is 125 (Podman)', async () => { |
| 77 | + makeShellExecuteMock(() => { |
| 78 | + throw new (class extends Error implements ProcessFailedError { |
| 79 | + public readonly code = 'PROCESS_FAILED' |
| 80 | + public readonly exitCode = 125 |
| 81 | + public readonly signal = null |
| 82 | + |
| 83 | + constructor() { |
| 84 | + super('foo'); |
| 85 | + } |
| 86 | + }); |
| 87 | + }); |
| 88 | + |
| 89 | + const imageExists = await docker.exists('foo'); |
| 90 | + |
| 91 | + expect(imageExists).toBe(false); |
| 92 | + }); |
| 93 | + }); |
| 94 | +}); |
0 commit comments