forked from containers/podman-desktop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RunImage.spec.ts
338 lines (269 loc) · 10.3 KB
/
RunImage.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
/**********************************************************************
* 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
***********************************************************************/
/* eslint-disable @typescript-eslint/no-explicit-any */
import '@testing-library/jest-dom/vitest';
import { test, vi, type Mock, beforeAll, describe, expect } from 'vitest';
import { fireEvent, render, screen } from '@testing-library/svelte';
import { runImageInfo } from '../../stores/run-image-store';
import RunImage from '/@/lib/image/RunImage.svelte';
import type { ImageInspectInfo } from '../../../../main/src/plugin/api/image-inspect-info';
import { mockBreadcrumb } from '../../stores/breadcrumb';
import userEvent from '@testing-library/user-event';
// fake the window.events object
beforeAll(() => {
(window.events as unknown) = {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
receive: (_channel: string, func: any) => {
func();
},
};
(window as any).getImageInspect = vi.fn();
(window as any).listNetworks = vi.fn().mockResolvedValue([]);
(window as any).listContainers = vi.fn().mockResolvedValue([]);
(window as any).createAndStartContainer = vi.fn();
mockBreadcrumb();
});
async function waitRender() {
const result = render(RunImage);
//wait until dataReady is true
while (result.component.$$.ctx[29] !== true) {
await new Promise(resolve => setTimeout(resolve, 100));
}
return result;
}
async function createRunImage(entrypoint?: string | string[], cmd?: string[]) {
runImageInfo.set({
age: '',
base64RepoTag: '',
createdAt: 0,
engineId: '',
engineName: '',
humanSize: '',
id: '',
inUse: false,
name: '',
selected: false,
shortId: '',
tag: '',
});
const imageInfo: ImageInspectInfo = {
Architecture: '',
Author: '',
Comment: '',
Config: {
ArgsEscaped: false,
AttachStderr: false,
AttachStdin: false,
AttachStdout: false,
Cmd: cmd,
Domainname: '',
Entrypoint: entrypoint,
Env: [],
ExposedPorts: {},
Hostname: '',
Image: '',
Labels: {},
OnBuild: [],
OpenStdin: false,
StdinOnce: false,
Tty: false,
User: '',
Volumes: {},
WorkingDir: '',
},
Container: '',
ContainerConfig: {
ArgsEscaped: false,
AttachStderr: false,
AttachStdin: false,
AttachStdout: false,
Cmd: [],
Domainname: '',
Env: [],
ExposedPorts: {},
Hostname: '',
Image: '',
Labels: {},
OpenStdin: false,
StdinOnce: false,
Tty: false,
User: '',
Volumes: {},
WorkingDir: '',
},
Created: '',
DockerVersion: '',
GraphDriver: { Data: { DeviceId: '', DeviceName: '', DeviceSize: '' }, Name: '' },
Id: '',
Os: '',
Parent: '',
RepoDigests: [],
RepoTags: [],
RootFS: {
Type: '',
},
Size: 0,
VirtualSize: 0,
engineId: 'engineid',
engineName: 'engineName',
};
(window.getImageInspect as Mock).mockResolvedValue(imageInfo);
await waitRender();
}
describe('RunImage', () => {
test('Expect that entrypoint is displayed', async () => {
await createRunImage('entrypoint', []);
const link = screen.getByRole('link', { name: 'Basic' });
await fireEvent.click(link);
const entryPoint = screen.getByRole('textbox', { name: 'Entrypoint:' });
expect(entryPoint).toBeInTheDocument();
expect((entryPoint as HTMLInputElement).value).toBe('entrypoint');
});
test('Expect that single element array entrypoint is displayed', async () => {
await createRunImage(['entrypoint'], []);
const link = screen.getByRole('link', { name: 'Basic' });
await fireEvent.click(link);
const entryPoint = screen.getByRole('textbox', { name: 'Entrypoint:' });
expect(entryPoint).toBeInTheDocument();
expect((entryPoint as HTMLInputElement).value).toBe('entrypoint');
});
test('Expect that two elements array entrypoint is displayed', async () => {
await createRunImage(['entrypoint1', 'entrypoint2'], []);
const link = screen.getByRole('link', { name: 'Basic' });
await fireEvent.click(link);
const entryPoint = screen.getByRole('textbox', { name: 'Entrypoint:' });
expect(entryPoint).toBeInTheDocument();
expect((entryPoint as HTMLInputElement).value).toBe('entrypoint1 entrypoint2');
});
test('Expect that single element array command is displayed', async () => {
await createRunImage([], ['command']);
const link = screen.getByRole('link', { name: 'Basic' });
await fireEvent.click(link);
const command = screen.getByRole('textbox', { name: 'Command:' });
expect(command).toBeInTheDocument();
expect((command as HTMLInputElement).value).toBe('command');
});
test('Expect that two elements array command is displayed', async () => {
await createRunImage([], ['command1', 'command2']);
const link = screen.getByRole('link', { name: 'Basic' });
await fireEvent.click(link);
const entryPoint = screen.getByRole('textbox', { name: 'Command:' });
expect(entryPoint).toBeInTheDocument();
expect((entryPoint as HTMLInputElement).value).toBe('command1 command2');
});
test('Expect that entrypoint is sent to API', async () => {
await createRunImage('entrypoint', []);
const button = screen.getByRole('button', { name: 'Start Container' });
await fireEvent.click(button);
expect(window.createAndStartContainer).toHaveBeenCalledWith(
'engineid',
expect.objectContaining({ Entrypoint: ['entrypoint'] }),
);
});
test('Expect that single array entrypoint is sent to API', async () => {
await createRunImage(['entrypoint'], []);
const button = screen.getByRole('button', { name: 'Start Container' });
await fireEvent.click(button);
expect(window.createAndStartContainer).toHaveBeenCalledWith(
'engineid',
expect.objectContaining({ Entrypoint: ['entrypoint'] }),
);
});
test('Expect that single array entrypoint with space is sent to API', async () => {
await createRunImage(['entrypoint with space'], []);
const button = screen.getByRole('button', { name: 'Start Container' });
await fireEvent.click(button);
expect(window.createAndStartContainer).toHaveBeenCalledWith(
'engineid',
expect.objectContaining({ Entrypoint: ['entrypoint with space'] }),
);
});
test('Expect that two elements array entrypoint is sent to API', async () => {
await createRunImage(['entrypoint1', 'entrypoint2'], []);
const button = screen.getByRole('button', { name: 'Start Container' });
await fireEvent.click(button);
expect(window.createAndStartContainer).toHaveBeenCalledWith(
'engineid',
expect.objectContaining({ Entrypoint: ['entrypoint1', 'entrypoint2'] }),
);
});
test('Expect that image without cmd is sent to API', async () => {
await createRunImage(['entrypoint1', 'entrypoint2'], undefined);
const button = screen.getByRole('button', { name: 'Start Container' });
await fireEvent.click(button);
expect(window.createAndStartContainer).toHaveBeenCalledWith(
'engineid',
expect.objectContaining({ Entrypoint: ['entrypoint1', 'entrypoint2'] }),
);
});
test('Expect that single array command is sent to API', async () => {
await createRunImage([], ['command']);
const button = screen.getByRole('button', { name: 'Start Container' });
await fireEvent.click(button);
expect(window.createAndStartContainer).toHaveBeenCalledWith(
'engineid',
expect.objectContaining({ Cmd: ['command'] }),
);
});
test('Expect that single array command with space is sent to API', async () => {
await createRunImage([], ['command with space']);
const button = screen.getByRole('button', { name: 'Start Container' });
await fireEvent.click(button);
expect(window.createAndStartContainer).toHaveBeenCalledWith(
'engineid',
expect.objectContaining({ Cmd: ['command with space'] }),
);
});
test('Expect that two elements array command is sent to API', async () => {
await createRunImage([], ['command1', 'command2']);
const button = screen.getByRole('button', { name: 'Start Container' });
await fireEvent.click(button);
expect(window.createAndStartContainer).toHaveBeenCalledWith(
'engineid',
expect.objectContaining({ Cmd: ['command1', 'command2'] }),
);
});
test('Expect that image without entrypoint is sent to API', async () => {
await createRunImage(undefined, ['command1', 'command2']);
const button = screen.getByRole('button', { name: 'Start Container' });
await fireEvent.click(button);
expect(window.createAndStartContainer).toHaveBeenCalledWith(
'engineid',
expect.objectContaining({ Cmd: ['command1', 'command2'] }),
);
});
test('Expect to see an error if the container/host ranges have different size', async () => {
await createRunImage(undefined, ['command1', 'command2']);
const customMappingButton = screen.getByRole('button', { name: 'Add custom port mapping' });
await fireEvent.click(customMappingButton);
const hostInput = screen.getByLabelText('host port');
await userEvent.click(hostInput);
await userEvent.clear(hostInput);
await userEvent.keyboard('9000-9001');
const containerInput = screen.getByLabelText('container port');
await userEvent.click(containerInput);
await userEvent.clear(containerInput);
await userEvent.keyboard('9000-9003');
const button = screen.getByRole('button', { name: 'Start Container' });
await fireEvent.click(button);
const errorComponent = screen.getByLabelText('createError');
expect(errorComponent.textContent.trim()).toBe(
'Error: host and container port ranges (9000-9001:9000-9003) have different lengths: 2 vs 4',
);
});
});