-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathcontainer.test.ts
More file actions
281 lines (232 loc) · 11.6 KB
/
container.test.ts
File metadata and controls
281 lines (232 loc) · 11.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
import { ContainerPackager } from '../container.js';
import { afterEach, describe, expect, it, vi } from 'vitest';
const mockSpawnSync = vi.fn();
const mockExistsSync = vi.fn();
const mockResolveCodeLocation = vi.fn();
vi.mock('child_process', () => ({
spawnSync: (...args: unknown[]) => mockSpawnSync(...args),
}));
vi.mock('fs', () => ({
existsSync: (...args: unknown[]) => mockExistsSync(...args),
}));
vi.mock('../helpers', () => ({
resolveCodeLocation: (...args: unknown[]) => mockResolveCodeLocation(...args),
}));
describe('ContainerPackager', () => {
afterEach(() => vi.clearAllMocks());
const packager = new ContainerPackager();
const baseSpec = {
build: 'Container' as const,
name: 'agent',
codeLocation: './src',
entrypoint: 'main.py',
};
it('rejects with PackagingError for non-Container build type', async () => {
await expect(packager.pack({ build: 'CodeZip', name: 'a' } as any)).rejects.toThrow(
'only supports Container build type'
);
});
it('rejects when Dockerfile not found', async () => {
mockResolveCodeLocation.mockReturnValue('/resolved/src');
mockExistsSync.mockReturnValue(false);
await expect(packager.pack(baseSpec as any)).rejects.toThrow('Dockerfile not found');
});
it('resolves with empty artifact when no container runtime available', async () => {
mockResolveCodeLocation.mockReturnValue('/resolved/src');
mockExistsSync.mockReturnValue(true);
mockSpawnSync.mockImplementation((cmd: string) => {
if (cmd === 'which') {
return { status: 1 };
}
return { status: 1 };
});
const result = await packager.pack(baseSpec as any);
expect(result.artifactPath).toBe('');
expect(result.sizeBytes).toBe(0);
expect(result.stagingPath).toBe('/resolved/src');
});
it('builds and returns artifact with docker runtime', async () => {
mockResolveCodeLocation.mockReturnValue('/resolved/src');
mockExistsSync.mockReturnValue(true);
mockSpawnSync.mockImplementation((cmd: string, args: string[]) => {
if (cmd === 'which' && args[0] === 'docker') return { status: 0 };
if (cmd === 'docker' && args[0] === '--version') return { status: 0 };
if (cmd === 'docker' && args[0] === 'build') return { status: 0 };
if (cmd === 'docker' && args[0] === 'image') return { status: 0, stdout: Buffer.from('50000000') };
return { status: 1 };
});
const result = await packager.pack(baseSpec as any);
expect(result.artifactPath).toBe('docker://agentcore-package-agent');
expect(result.sizeBytes).toBe(50000000);
expect(result.stagingPath).toBe('/resolved/src');
});
it('rejects when docker build fails', async () => {
mockResolveCodeLocation.mockReturnValue('/resolved/src');
mockExistsSync.mockReturnValue(true);
mockSpawnSync.mockImplementation((cmd: string, args: string[]) => {
if (cmd === 'which' && args[0] === 'docker') return { status: 0 };
if (cmd === 'docker' && args[0] === '--version') return { status: 0 };
if (cmd === 'docker' && args[0] === 'build') return { status: 1, stderr: Buffer.from('build error occurred') };
return { status: 1 };
});
await expect(packager.pack(baseSpec as any)).rejects.toThrow('Container build failed');
});
it('rejects when image exceeds 1GB size limit', async () => {
mockResolveCodeLocation.mockReturnValue('/resolved/src');
mockExistsSync.mockReturnValue(true);
const oversized = (1024 * 1024 * 1024 + 1).toString();
mockSpawnSync.mockImplementation((cmd: string, args: string[]) => {
if (cmd === 'which' && args[0] === 'docker') return { status: 0 };
if (cmd === 'docker' && args[0] === '--version') return { status: 0 };
if (cmd === 'docker' && args[0] === 'build') return { status: 0 };
if (cmd === 'docker' && args[0] === 'image') return { status: 0, stdout: Buffer.from(oversized) };
return { status: 1 };
});
await expect(packager.pack(baseSpec as any)).rejects.toThrow('exceeds 1GB limit');
});
it('uses options.agentName over spec.name', async () => {
mockResolveCodeLocation.mockReturnValue('/resolved/src');
mockExistsSync.mockReturnValue(true);
mockSpawnSync.mockImplementation((cmd: string, args: string[]) => {
if (cmd === 'which' && args[0] === 'docker') return { status: 0 };
if (cmd === 'docker' && args[0] === '--version') return { status: 0 };
if (cmd === 'docker' && args[0] === 'build') return { status: 0 };
if (cmd === 'docker' && args[0] === 'image') return { status: 0, stdout: Buffer.from('1000') };
return { status: 1 };
});
const result = await packager.pack(baseSpec as any, { agentName: 'custom-agent' });
expect(result.artifactPath).toBe('docker://agentcore-package-custom-agent');
});
it('uses options.artifactDir as configBaseDir', async () => {
mockResolveCodeLocation.mockReturnValue('/artifact/dir/src');
mockExistsSync.mockReturnValue(true);
mockSpawnSync.mockImplementation((cmd: string) => {
if (cmd === 'which') return { status: 1 };
return { status: 1 };
});
await packager.pack(baseSpec as any, { artifactDir: '/artifact/dir' });
expect(mockResolveCodeLocation).toHaveBeenCalledWith('./src', '/artifact/dir');
});
it('uses options.projectRoot as fallback', async () => {
mockResolveCodeLocation.mockReturnValue('/project/root/src');
mockExistsSync.mockReturnValue(true);
mockSpawnSync.mockImplementation((cmd: string) => {
if (cmd === 'which') return { status: 1 };
return { status: 1 };
});
await packager.pack(baseSpec as any, { projectRoot: '/project/root' });
expect(mockResolveCodeLocation).toHaveBeenCalledWith('./src', '/project/root');
});
it('falls back to process.cwd() when no directory options', async () => {
const cwd = process.cwd();
mockResolveCodeLocation.mockReturnValue('/cwd/src');
mockExistsSync.mockReturnValue(true);
mockSpawnSync.mockImplementation((cmd: string) => {
if (cmd === 'which') return { status: 1 };
return { status: 1 };
});
await packager.pack(baseSpec as any);
expect(mockResolveCodeLocation).toHaveBeenCalledWith('./src', cwd);
});
it('detects finch runtime when docker unavailable', async () => {
mockResolveCodeLocation.mockReturnValue('/resolved/src');
mockExistsSync.mockReturnValue(true);
mockSpawnSync.mockImplementation((cmd: string, args: string[]) => {
if (cmd === 'which' && args[0] === 'docker') return { status: 1 };
if (cmd === 'which' && args[0] === 'finch') return { status: 0 };
if (cmd === 'finch' && args[0] === '--version') return { status: 0 };
if (cmd === 'finch' && args[0] === 'build') return { status: 0 };
if (cmd === 'finch' && args[0] === 'image') return { status: 0, stdout: Buffer.from('2000') };
return { status: 1 };
});
const result = await packager.pack(baseSpec as any);
expect(result.artifactPath).toBe('finch://agentcore-package-agent');
});
it('uses custom dockerfile name from spec', async () => {
mockResolveCodeLocation.mockReturnValue('/resolved/src');
mockExistsSync.mockReturnValue(true);
mockSpawnSync.mockImplementation((cmd: string, args: string[]) => {
if (cmd === 'which' && args[0] === 'docker') return { status: 0 };
if (cmd === 'docker' && args[0] === '--version') return { status: 0 };
if (cmd === 'docker' && args[0] === 'build') return { status: 0 };
if (cmd === 'docker' && args[0] === 'image') return { status: 0, stdout: Buffer.from('1000') };
return { status: 1 };
});
const specWithDockerfile = { ...baseSpec, dockerfile: 'Dockerfile.gpu' };
await packager.pack(specWithDockerfile as any);
// Verify build was called with custom dockerfile path
const buildCall = mockSpawnSync.mock.calls.find(
(c: unknown[]) => c[0] === 'docker' && (c[1] as string[])[0] === 'build'
);
expect(buildCall).toBeDefined();
const buildArgs = buildCall![1] as string[];
const fIdx = buildArgs.indexOf('-f');
expect(buildArgs[fIdx + 1]).toBe('/resolved/src/Dockerfile.gpu');
});
it('rejects when custom dockerfile not found', async () => {
mockResolveCodeLocation.mockReturnValue('/resolved/src');
mockExistsSync.mockReturnValue(false);
const specWithDockerfile = { ...baseSpec, dockerfile: 'Dockerfile.custom' };
await expect(packager.pack(specWithDockerfile as any)).rejects.toThrow('Dockerfile.custom not found');
});
it('uses buildContextPath as docker build context instead of codeLocation', async () => {
mockResolveCodeLocation.mockImplementation((path: string) => {
if (path === './src') return '/resolved/src';
if (path === './context') return '/resolved/context';
return path;
});
mockExistsSync.mockReturnValue(true);
mockSpawnSync.mockImplementation((cmd: string, args: string[]) => {
if (cmd === 'which' && args[0] === 'docker') return { status: 0 };
if (cmd === 'docker' && args[0] === '--version') return { status: 0 };
if (cmd === 'docker' && args[0] === 'build') return { status: 0 };
if (cmd === 'docker' && args[0] === 'image') return { status: 0, stdout: Buffer.from('1000') };
return { status: 1 };
});
const specWithContext = { ...baseSpec, buildContextPath: './context' };
await packager.pack(specWithContext as any);
const buildCall = mockSpawnSync.mock.calls.find(
(c: unknown[]) => c[0] === 'docker' && (c[1] as string[])[0] === 'build'
);
expect(buildCall).toBeDefined();
const buildArgs = buildCall![1] as string[];
// Last arg should be the buildContextPath, not codeLocation
expect(buildArgs[buildArgs.length - 1]).toBe('/resolved/context');
});
it('passes customDockerBuildArgs as --build-arg flags', async () => {
mockResolveCodeLocation.mockReturnValue('/resolved/src');
mockExistsSync.mockReturnValue(true);
mockSpawnSync.mockImplementation((cmd: string, args: string[]) => {
if (cmd === 'which' && args[0] === 'docker') return { status: 0 };
if (cmd === 'docker' && args[0] === '--version') return { status: 0 };
if (cmd === 'docker' && args[0] === 'build') return { status: 0 };
if (cmd === 'docker' && args[0] === 'image') return { status: 0, stdout: Buffer.from('1000') };
return { status: 1 };
});
const specWithBuildArgs = { ...baseSpec, customDockerBuildArgs: { AGENT_NAME: 'dummyagent', BUILD_ENV: 'prod' } };
await packager.pack(specWithBuildArgs as any);
const buildCall = mockSpawnSync.mock.calls.find(
(c: unknown[]) => c[0] === 'docker' && (c[1] as string[])[0] === 'build'
);
expect(buildCall).toBeDefined();
const buildArgs = buildCall![1] as string[];
expect(buildArgs).toContain('--build-arg');
expect(buildArgs).toContain('AGENT_NAME=dummyagent');
expect(buildArgs).toContain('BUILD_ENV=prod');
});
it('detects podman runtime last', async () => {
mockResolveCodeLocation.mockReturnValue('/resolved/src');
mockExistsSync.mockReturnValue(true);
mockSpawnSync.mockImplementation((cmd: string, args: string[]) => {
if (cmd === 'which' && args[0] === 'docker') return { status: 1 };
if (cmd === 'which' && args[0] === 'finch') return { status: 1 };
if (cmd === 'which' && args[0] === 'podman') return { status: 0 };
if (cmd === 'podman' && args[0] === '--version') return { status: 0 };
if (cmd === 'podman' && args[0] === 'build') return { status: 0 };
if (cmd === 'podman' && args[0] === 'image') return { status: 0, stdout: Buffer.from('3000') };
return { status: 1 };
});
const result = await packager.pack(baseSpec as any);
expect(result.artifactPath).toBe('podman://agentcore-package-agent');
});
});