|
| 1 | +const mockExistsSync = vi.hoisted(() => vi.fn()); |
| 2 | +const mockProbeSocketApiVersion = vi.hoisted(() => vi.fn()); |
| 3 | +const mockDisableSocketRedirects = vi.hoisted(() => vi.fn()); |
| 4 | +const mockInspect = vi.hoisted(() => vi.fn()); |
| 5 | +const mockGetContainer = vi.hoisted(() => vi.fn(() => ({ inspect: mockInspect }))); |
| 6 | +const mockDockerode = vi.hoisted(() => |
| 7 | + vi.fn(function MockDockerode() { |
| 8 | + return { |
| 9 | + getContainer: mockGetContainer, |
| 10 | + }; |
| 11 | + }), |
| 12 | +); |
| 13 | + |
| 14 | +vi.mock('node:fs', () => ({ |
| 15 | + default: { |
| 16 | + existsSync: (...args: unknown[]) => mockExistsSync(...args), |
| 17 | + }, |
| 18 | +})); |
| 19 | + |
| 20 | +vi.mock('dockerode', () => ({ |
| 21 | + default: mockDockerode, |
| 22 | +})); |
| 23 | + |
| 24 | +vi.mock('../watchers/providers/docker/socket-version-probe.js', () => ({ |
| 25 | + probeSocketApiVersion: (...args: unknown[]) => mockProbeSocketApiVersion(...args), |
| 26 | +})); |
| 27 | + |
| 28 | +vi.mock('../watchers/providers/docker/disable-socket-redirects.js', () => ({ |
| 29 | + disableSocketRedirects: (...args: unknown[]) => mockDisableSocketRedirects(...args), |
| 30 | +})); |
| 31 | + |
| 32 | +describe('curl healthcheck compatibility', () => { |
| 33 | + const originalHostname = process.env.HOSTNAME; |
| 34 | + |
| 35 | + beforeEach(() => { |
| 36 | + vi.clearAllMocks(); |
| 37 | + process.env.HOSTNAME = 'drydock-self'; |
| 38 | + mockExistsSync.mockReturnValue(true); |
| 39 | + mockProbeSocketApiVersion.mockResolvedValue('1.44'); |
| 40 | + mockInspect.mockResolvedValue({ |
| 41 | + Config: { |
| 42 | + Healthcheck: { |
| 43 | + Test: ['CMD-SHELL', 'curl --fail http://localhost:3000/health || exit 1'], |
| 44 | + }, |
| 45 | + }, |
| 46 | + }); |
| 47 | + }); |
| 48 | + |
| 49 | + afterAll(() => { |
| 50 | + if (originalHostname === undefined) { |
| 51 | + delete process.env.HOSTNAME; |
| 52 | + } else { |
| 53 | + process.env.HOSTNAME = originalHostname; |
| 54 | + } |
| 55 | + }); |
| 56 | + |
| 57 | + test('detects a custom curl healthcheck override on the current container', async () => { |
| 58 | + const { getCurlHealthcheckOverrideCompatibility } = await import('./curl-healthcheck.js'); |
| 59 | + |
| 60 | + const result = await getCurlHealthcheckOverrideCompatibility(); |
| 61 | + |
| 62 | + expect(result).toEqual({ |
| 63 | + detected: true, |
| 64 | + commandPreview: 'CMD-SHELL curl --fail http://localhost:3000/health || exit 1', |
| 65 | + }); |
| 66 | + expect(mockDockerode).toHaveBeenCalledWith({ |
| 67 | + socketPath: '/var/run/docker.sock', |
| 68 | + version: 'v1.44', |
| 69 | + }); |
| 70 | + expect(mockGetContainer).toHaveBeenCalledWith(expect.any(String)); |
| 71 | + expect(mockDisableSocketRedirects).toHaveBeenCalled(); |
| 72 | + }); |
| 73 | + |
| 74 | + test('returns not detected when hostname is not a valid container identifier', async () => { |
| 75 | + const { getCurlHealthcheckOverrideCompatibility } = await import('./curl-healthcheck.js'); |
| 76 | + const originalHostname = process.env.HOSTNAME; |
| 77 | + process.env.HOSTNAME = 'pod/name'; |
| 78 | + |
| 79 | + try { |
| 80 | + await expect(getCurlHealthcheckOverrideCompatibility()).resolves.toEqual({ |
| 81 | + detected: false, |
| 82 | + }); |
| 83 | + expect(mockDockerode).not.toHaveBeenCalled(); |
| 84 | + } finally { |
| 85 | + if (originalHostname === undefined) { |
| 86 | + delete process.env.HOSTNAME; |
| 87 | + } else { |
| 88 | + process.env.HOSTNAME = originalHostname; |
| 89 | + } |
| 90 | + } |
| 91 | + }); |
| 92 | + |
| 93 | + test('returns not detected when the healthcheck does not use curl', async () => { |
| 94 | + mockInspect.mockResolvedValue({ |
| 95 | + Config: { |
| 96 | + Healthcheck: { |
| 97 | + Test: ['CMD', '/bin/healthcheck', '3000'], |
| 98 | + }, |
| 99 | + }, |
| 100 | + }); |
| 101 | + const { getCurlHealthcheckOverrideCompatibility } = await import('./curl-healthcheck.js'); |
| 102 | + |
| 103 | + await expect(getCurlHealthcheckOverrideCompatibility()).resolves.toEqual({ |
| 104 | + detected: false, |
| 105 | + }); |
| 106 | + }); |
| 107 | +}); |
0 commit comments