Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions packages/cli/src/serve/sandbox.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@ describe('start_sandbox', () => {
await vi.waitFor(() => expect(spawnMock).toHaveBeenCalledTimes(2));
const args = spawnMock.mock.calls[1]?.[1] as string[];
const options = spawnMock.mock.calls[1]?.[2];
expect(args[args.indexOf('--hostname') + 1]).toBe(
args[args.indexOf('--name') + 1],
);
const envFlagIndex = args.indexOf(PRIVATE_ACP_CAPABILITY_ENV);
expect(args.slice(envFlagIndex - 1, envFlagIndex + 1)).toEqual([
'--env',
Expand All @@ -108,6 +111,46 @@ describe('start_sandbox', () => {
await expect(result).resolves.toBe(0);
});

it('lets the runtime choose a hostname for image-ID containers', async () => {
vi.stubEnv('SANDBOX_SET_UID_GID', 'false');
vi.stubEnv('QWEN_CODE_WARNINGS_FILE', '');
vi.spyOn(fs, 'existsSync').mockReturnValue(true);
vi.spyOn(fs, 'realpathSync').mockImplementation((filePath) =>
String(filePath),
);
execSyncMock.mockReturnValue(Buffer.from(''));

const imageCheck = Object.assign(new EventEmitter(), {
stdout: new EventEmitter(),
});
const child = new EventEmitter();
spawnMock
.mockImplementationOnce(() => {
queueMicrotask(() => {
imageCheck.stdout.emit('data', Buffer.from('image-id'));
imageCheck.emit('close', 0);
});
return imageCheck;
})
.mockReturnValueOnce(child);

const result = start_sandbox(
{ command: 'docker', image: `sha256:${'a'.repeat(64)}` },
[],
undefined,
[process.execPath, '/path/to/cli.js', '--acp'],
);

await vi.waitFor(() => expect(spawnMock).toHaveBeenCalledTimes(2));
const args = spawnMock.mock.calls[1]?.[1] as string[];
const containerName = args[args.indexOf('--name') + 1];
expect(containerName.length).toBeGreaterThan(64);
expect(args).not.toContain('--hostname');

child.emit('close', 0);
await expect(result).resolves.toBe(0);
});

it('omits the warnings file environment variable when it is unset', async () => {
vi.stubEnv('SANDBOX_SET_UID_GID', 'false');
vi.stubEnv('QWEN_CODE_WARNINGS_FILE', '');
Expand Down
5 changes: 4 additions & 1 deletion packages/cli/src/serve/sandbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -654,7 +654,10 @@ export async function start_sandbox(
containerName = `${imageName}-${randomBytes(4).toString('hex')}`;
writeStderrLine(`ContainerName (regular): ${containerName}`);
}
args.push('--name', containerName, '--hostname', containerName);
args.push('--name', containerName);
if (containerName.length <= 64) {
args.push('--hostname', containerName);
}

// copy QWEN_CODE_TEST_VAR for integration tests
if (process.env['QWEN_CODE_TEST_VAR']) {
Expand Down
Loading