|
| 1 | +import { remoteSandboxMock } from '@sim/testing/mocks/remote-sandbox.mock' |
| 2 | +import { |
| 3 | + remoteSandboxProviderMock, |
| 4 | + remoteSandboxProviderMockFns, |
| 5 | +} from '@sim/testing/mocks/remote-sandbox-provider.mock' |
| 6 | +import { afterEach, describe, expect, it, vi } from 'vitest' |
| 7 | + |
| 8 | +vi.mock('@/lib/execution/remote-sandbox', () => remoteSandboxMock) |
| 9 | +vi.mock('@/lib/execution/remote-sandbox/provider', () => remoteSandboxProviderMock) |
| 10 | +vi.mock('@/lib/core/config/env-capabilities.server', () => ({ |
| 11 | + getSelectedSandboxProviderId: () => 'kubernetes', |
| 12 | +})) |
| 13 | +vi.mock('@/lib/execution/remote-sandbox/kubernetes/settings', () => ({ |
| 14 | + readKubernetesSandboxSettings: () => ({ namespace: 'sim-sandboxes' }), |
| 15 | +})) |
| 16 | + |
| 17 | +import { checkSandbox } from '@/lib/self-check/sandbox' |
| 18 | + |
| 19 | +interface FakeSandbox { |
| 20 | + podAddress: string |
| 21 | + kill: ReturnType<typeof vi.fn> |
| 22 | + runCode: ReturnType<typeof vi.fn> |
| 23 | +} |
| 24 | + |
| 25 | +function fakeSandbox(address: string): FakeSandbox { |
| 26 | + return { |
| 27 | + podAddress: address, |
| 28 | + kill: vi.fn(async () => {}), |
| 29 | + runCode: vi.fn(async () => ({ |
| 30 | + text: '', |
| 31 | + stdout: '', |
| 32 | + stderr: '', |
| 33 | + error: { name: 'RuntimeError', value: 'probe crashed', traceback: '' }, |
| 34 | + })), |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +/** A provider whose creates settle only when the test says so, in call order. */ |
| 39 | +function controlledProvider() { |
| 40 | + const pending: Array<{ |
| 41 | + resolve: (sandbox: FakeSandbox) => void |
| 42 | + reject: (error: Error) => void |
| 43 | + }> = [] |
| 44 | + const create = vi.fn( |
| 45 | + () => |
| 46 | + new Promise<FakeSandbox>((resolve, reject) => { |
| 47 | + pending.push({ resolve, reject }) |
| 48 | + }) |
| 49 | + ) |
| 50 | + remoteSandboxProviderMockFns.mockResolveProvider.mockReturnValue({ id: 'kubernetes', create }) |
| 51 | + return { create, pending } |
| 52 | +} |
| 53 | + |
| 54 | +async function flush(): Promise<void> { |
| 55 | + for (let i = 0; i < 20; i++) await Promise.resolve() |
| 56 | +} |
| 57 | + |
| 58 | +afterEach(() => { |
| 59 | + remoteSandboxProviderMockFns.mockResolveProvider.mockReset() |
| 60 | +}) |
| 61 | + |
| 62 | +describe('checkSandbox', () => { |
| 63 | + it('starts the neighbor and the probe sandbox together, so two cold starts share the budget', async () => { |
| 64 | + const { create, pending } = controlledProvider() |
| 65 | + const running = checkSandbox() |
| 66 | + await flush() |
| 67 | + |
| 68 | + expect(create).toHaveBeenCalledTimes(2) |
| 69 | + |
| 70 | + const neighbor = fakeSandbox('10.0.0.11') |
| 71 | + const prober = fakeSandbox('10.0.0.12') |
| 72 | + pending[0].resolve(neighbor) |
| 73 | + pending[1].resolve(prober) |
| 74 | + const results = await running |
| 75 | + |
| 76 | + expect(prober.runCode).toHaveBeenCalledTimes(1) |
| 77 | + expect(results.find((check) => check.name === 'sandbox.execution')?.status).toBe('fail') |
| 78 | + expect(neighbor.kill).toHaveBeenCalled() |
| 79 | + expect(prober.kill).toHaveBeenCalled() |
| 80 | + }) |
| 81 | + |
| 82 | + it('kills the neighbor when the probe sandbox fails to start', async () => { |
| 83 | + const { pending } = controlledProvider() |
| 84 | + const running = checkSandbox() |
| 85 | + await flush() |
| 86 | + |
| 87 | + const neighbor = fakeSandbox('10.0.0.11') |
| 88 | + pending[0].resolve(neighbor) |
| 89 | + pending[1].reject(new Error('no capacity')) |
| 90 | + const results = await running |
| 91 | + |
| 92 | + expect(results.find((check) => check.name === 'sandbox.execution')?.status).toBe('fail') |
| 93 | + expect(neighbor.kill).toHaveBeenCalled() |
| 94 | + }) |
| 95 | + |
| 96 | + it('kills the probe sandbox when the neighbor fails to start', async () => { |
| 97 | + const { pending } = controlledProvider() |
| 98 | + const running = checkSandbox() |
| 99 | + await flush() |
| 100 | + |
| 101 | + const prober = fakeSandbox('10.0.0.12') |
| 102 | + pending[0].reject(new Error('no capacity')) |
| 103 | + pending[1].resolve(prober) |
| 104 | + const results = await running |
| 105 | + |
| 106 | + expect(results.find((check) => check.name === 'sandbox.execution')?.status).toBe('fail') |
| 107 | + expect(prober.runCode).not.toHaveBeenCalled() |
| 108 | + expect(prober.kill).toHaveBeenCalled() |
| 109 | + }) |
| 110 | +}) |
0 commit comments