forked from containers/podman-desktop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
exec.spec.ts
379 lines (311 loc) · 11.6 KB
/
exec.spec.ts
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
/**********************************************************************
* Copyright (C) 2023 Red Hat, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
***********************************************************************/
import { expect, describe, test, vi, beforeEach, afterEach } from 'vitest';
import { getInstallationPath, macosExtraPath, exec } from './exec.js';
import * as util from '../../util.js';
import type { ChildProcessWithoutNullStreams } from 'child_process';
import { spawn } from 'child_process';
import type { Readable } from 'node:stream';
import type { Proxy } from '../proxy.js';
/* eslint-disable @typescript-eslint/no-explicit-any */
describe('exec', () => {
const proxy: Proxy = {
isEnabled: vi.fn().mockReturnValue(false),
} as unknown as Proxy;
test('should run the command and resolve with the result', async () => {
const command = 'echo';
const args = ['Hello, World!'];
vi.mock('child_process', () => {
return {
spawn: vi.fn(),
};
});
const on: any = vi.fn().mockImplementationOnce((event: string, cb: (arg0: string) => string) => {
if (event === 'data') {
cb('Hello, World!');
}
}) as unknown as Readable;
const spawnMock = vi.mocked(spawn).mockReturnValue({
stdout: { on, setEncoding: vi.fn() },
stderr: { on, setEncoding: vi.fn() },
on: vi.fn().mockImplementation((event: string, cb: (arg0: number) => void) => {
if (event === 'exit') {
cb(0);
}
}),
} as any);
const { stdout } = await exec(command, proxy, args);
expect(spawnMock).toHaveBeenCalledWith(command, args, { env: expect.any(Object) });
expect(stdout).toBeDefined();
expect(stdout).toContain('Hello, World!');
});
test('should run the command with custom cwd and resolve with the result', async () => {
const command = 'echo';
const args = ['Hello, World!'];
const cwd = '/tmp';
vi.mock('child_process', () => {
return {
spawn: vi.fn(),
};
});
const on: any = vi.fn().mockImplementationOnce((event: string, cb: (arg0: string) => string) => {
if (event === 'data') {
cb('Hello, World!');
}
}) as unknown as Readable;
const spawnMock = vi.mocked(spawn).mockReturnValue({
stdout: { on, setEncoding: vi.fn() },
stderr: { on, setEncoding: vi.fn() },
on: vi.fn().mockImplementation((event: string, cb: (arg0: number) => void) => {
if (event === 'exit') {
cb(0);
}
}),
} as any);
const { stdout } = await exec(command, proxy, args, { cwd });
// caller should contains the cwd provided
expect(spawnMock).toHaveBeenCalledWith(command, args, expect.objectContaining({ cwd: cwd }));
expect(stdout).toBeDefined();
expect(stdout).toContain('Hello, World!');
});
test('should reject with an error when the command execution fails', async () => {
const command = 'nonexistent-command';
vi.mock('child_process', () => {
return {
spawn: vi.fn(),
};
});
const on: any = vi.fn().mockImplementationOnce((event: string, cb: (arg0: string) => string) => {
if (event === 'data') {
cb('');
}
}) as unknown as Readable;
vi.mocked(spawn).mockReturnValue({
stdout: { on, setEncoding: vi.fn() },
stderr: { on, setEncoding: vi.fn() },
on: vi.fn().mockImplementation((event: string, cb: (arg0: number) => void) => {
if (event === 'exit') {
cb(1);
}
}),
} as any);
await expect(exec(command, proxy)).rejects.toThrowError(/Command execution failed with exit code 1/);
});
test('should reject with an error when the execution is cancelled', async () => {
const command = 'sleep';
const args = ['1'];
const cancellationToken = {
isCancellationRequested: true,
onCancellationRequested: vi.fn(),
};
const options = {
token: cancellationToken,
logger: { log: vi.fn(), warn: vi.fn(), error: vi.fn() },
};
vi.mock('child_process', () => {
return {
spawn: vi.fn(),
};
});
const childProcessMock: unknown = {
killed: false,
stdout: { on: vi.fn(), setEncoding: vi.fn() },
stderr: { on: vi.fn(), setEncoding: vi.fn() },
on: vi.fn().mockImplementation((event: string, cb: (arg0: number) => void) => {
if (event === 'exit') {
cb(0);
}
}),
kill: vi.fn(),
};
vi.mocked(spawn).mockReturnValue(childProcessMock as ChildProcessWithoutNullStreams);
vi.mocked(cancellationToken.onCancellationRequested).mockImplementationOnce((handler: () => void) => {
handler();
});
await expect(exec(command, proxy, args, options)).rejects.toThrowError(/Execution cancelled/);
expect((childProcessMock as any).kill).toHaveBeenCalled();
expect(options.logger.error).toHaveBeenCalledWith('Execution cancelled');
});
test('should run the command and set HTTP_PROXY', async () => {
const command = 'echo';
const args = ['Hello, World!'];
vi.mock('child_process', () => {
return {
spawn: vi.fn(),
};
});
const on: any = vi.fn().mockImplementationOnce((event: string, cb: (arg0: string) => string) => {
if (event === 'data') {
cb('Hello, World!');
}
}) as unknown as Readable;
const spawnMock = vi.mocked(spawn).mockReturnValue({
stdout: { on, setEncoding: vi.fn() },
stderr: { on, setEncoding: vi.fn() },
on: vi.fn().mockImplementation((event: string, cb: (arg0: number) => void) => {
if (event === 'exit') {
cb(0);
}
}),
} as any);
const httpProxy = {
isEnabled: vi.fn().mockReturnValue(true),
proxy: {
httpProxy: '127.0.0.1:8888',
},
} as unknown as Proxy;
const { stdout } = await exec(command, httpProxy, args);
expect(spawnMock).toHaveBeenCalledWith(command, args, {
env: expect.objectContaining({ HTTP_PROXY: 'http://127.0.0.1:8888' }),
});
expect(stdout).toBeDefined();
expect(stdout).toContain('Hello, World!');
});
test('should run the command and set HTTPS_PROXY', async () => {
const command = 'echo';
const args = ['Hello, World!'];
vi.mock('child_process', () => {
return {
spawn: vi.fn(),
};
});
const on: any = vi.fn().mockImplementationOnce((event: string, cb: (arg0: string) => string) => {
if (event === 'data') {
cb('Hello, World!');
}
}) as unknown as Readable;
const spawnMock = vi.mocked(spawn).mockReturnValue({
stdout: { on, setEncoding: vi.fn() },
stderr: { on, setEncoding: vi.fn() },
on: vi.fn().mockImplementation((event: string, cb: (arg0: number) => void) => {
if (event === 'exit') {
cb(0);
}
}),
} as any);
const httpsProxy = {
isEnabled: vi.fn().mockReturnValue(true),
proxy: {
httpsProxy: '127.0.0.1:8888',
},
} as unknown as Proxy;
const { stdout } = await exec(command, httpsProxy, args);
expect(spawnMock).toHaveBeenCalledWith(command, args, {
env: expect.objectContaining({ HTTPS_PROXY: 'http://127.0.0.1:8888' }),
});
expect(stdout).toBeDefined();
expect(stdout).toContain('Hello, World!');
});
test('should run the command and set NO_PROXY', async () => {
const command = 'echo';
const args = ['Hello, World!'];
vi.mock('child_process', () => {
return {
spawn: vi.fn(),
};
});
const on: any = vi.fn().mockImplementationOnce((event: string, cb: (arg0: string) => string) => {
if (event === 'data') {
cb('Hello, World!');
}
}) as unknown as Readable;
const spawnMock = vi.mocked(spawn).mockReturnValue({
stdout: { on, setEncoding: vi.fn() },
stderr: { on, setEncoding: vi.fn() },
on: vi.fn().mockImplementation((event: string, cb: (arg0: number) => void) => {
if (event === 'exit') {
cb(0);
}
}),
} as any);
const noProxy = {
isEnabled: vi.fn().mockReturnValue(true),
proxy: {
noProxy: '127.0.0.1',
},
} as unknown as Proxy;
const { stdout } = await exec(command, noProxy, args);
expect(spawnMock).toHaveBeenCalledWith(command, args, { env: expect.objectContaining({ NO_PROXY: '127.0.0.1' }) });
expect(stdout).toBeDefined();
expect(stdout).toContain('Hello, World!');
});
});
vi.mock('./util', () => {
return {
isWindows: vi.fn(),
isMac: vi.fn(),
};
});
describe('getInstallationPath', () => {
let originalPath: string | undefined;
beforeEach(() => {
originalPath = process.env.PATH;
});
afterEach(() => {
process.env.PATH = originalPath;
});
test('should return the installation path for Windows', () => {
vi.spyOn(util, 'isWindows').mockImplementation(() => true);
vi.spyOn(util, 'isMac').mockImplementation(() => false);
process.env.PATH = '';
const path = getInstallationPath();
expect(path).toBe('c:\\Program Files\\RedHat\\Podman;');
});
test('should return the installation path for Windows with pre-filled PATH', () => {
vi.spyOn(util, 'isWindows').mockImplementation(() => true);
vi.spyOn(util, 'isMac').mockImplementation(() => false);
process.env.PATH = 'c:\\Local';
const path = getInstallationPath();
expect(path).toBe('c:\\Program Files\\RedHat\\Podman;c:\\Local');
});
test('should return the installation path for Windows with defined param', () => {
vi.spyOn(util, 'isWindows').mockImplementation(() => true);
vi.spyOn(util, 'isMac').mockImplementation(() => false);
process.env.PATH = 'c:\\Local';
const path = getInstallationPath('c:\\Directory');
expect(path).toBe('c:\\Program Files\\RedHat\\Podman;c:\\Directory');
});
test('should return the installation path for macOS', () => {
vi.spyOn(util, 'isWindows').mockImplementation(() => false);
vi.spyOn(util, 'isMac').mockImplementation(() => true);
process.env.PATH = '/usr/bin';
const path = getInstallationPath();
expect(path).toBe(`/usr/bin:${macosExtraPath}`);
});
test('should return the installation path for macOS with defined param', () => {
vi.spyOn(util, 'isWindows').mockImplementation(() => false);
vi.spyOn(util, 'isMac').mockImplementation(() => true);
process.env.PATH = '/usr/bin';
const path = getInstallationPath('/usr/other');
expect(path).toBe(`/usr/other:${macosExtraPath}`);
});
test('should return the installation path for other platforms', () => {
vi.spyOn(util, 'isWindows').mockImplementation(() => false);
vi.spyOn(util, 'isMac').mockImplementation(() => false);
process.env.PATH = '/usr/bin'; // Example PATH for other platforms
const path = getInstallationPath();
expect(path).toBe('/usr/bin');
});
test('should return the installation path for other platforms with defined param', () => {
vi.spyOn(util, 'isWindows').mockImplementation(() => false);
vi.spyOn(util, 'isMac').mockImplementation(() => false);
process.env.PATH = '/usr/bin'; // Example PATH for other platforms
const path = getInstallationPath('/usr/other');
expect(path).toBe('/usr/other');
});
});