-
Notifications
You must be signed in to change notification settings - Fork 268
/
ContainerList.spec.ts
334 lines (280 loc) · 10.3 KB
/
ContainerList.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
/**********************************************************************
* 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';
import { beforeAll, test, expect, vi } from 'vitest';
import { render, screen, fireEvent } from '@testing-library/svelte';
import ContainerList from './ContainerList.svelte';
import { containersInfos } from '../stores/containers';
import { get } from 'svelte/store';
import { providerInfos } from '../stores/providers';
import { tick } from 'svelte';
const listContainersMock = vi.fn();
const getProviderInfosMock = vi.fn();
const listViewsMock = vi.fn();
const deleteContainerMock = vi.fn();
const removePodMock = vi.fn();
const listPodsMock = vi.fn();
const kubernetesListPodsMock = vi.fn();
// fake the window.events object
beforeAll(() => {
const onDidUpdateProviderStatusMock = vi.fn();
(window as any).onDidUpdateProviderStatus = onDidUpdateProviderStatusMock;
onDidUpdateProviderStatusMock.mockImplementation(() => Promise.resolve());
listPodsMock.mockImplementation(() => Promise.resolve([]));
kubernetesListPodsMock.mockImplementation(() => Promise.resolve([]));
listViewsMock.mockImplementation(() => Promise.resolve([]));
(window as any).listViewsContributions = listViewsMock;
(window as any).listContainers = listContainersMock;
(window as any).listPods = listPodsMock;
(window as any).kubernetesListPods = kubernetesListPodsMock;
(window as any).getProviderInfos = getProviderInfosMock;
(window as any).removePod = removePodMock;
(window as any).deleteContainer = deleteContainerMock;
(window.events as unknown) = {
receive: (_channel: string, func: any) => {
func();
},
};
});
async function waitRender(customProperties: object): Promise<void> {
const result = render(ContainerList, { ...customProperties });
// wait that result.component.$$.ctx[2] is set
while (result.component.$$.ctx[2] === undefined) {
await new Promise(resolve => setTimeout(resolve, 100));
}
}
test('Expect no container engines being displayed', async () => {
render(ContainerList);
const noEngine = screen.getByRole('heading', { name: 'No Container Engine' });
expect(noEngine).toBeInTheDocument();
});
test('Try to delete a pod that has containers', async () => {
getProviderInfosMock.mockResolvedValue([
{
name: 'podman',
status: 'started',
internalId: 'podman-internal-id',
containerConnections: [
{
name: 'podman-machine-default',
status: 'started',
},
],
},
]);
const podId = 'pod-id';
const singleContainer = {
Id: 'sha256:1234567890123',
Image: 'sha256:123',
Names: ['foo'],
Status: 'Running',
engineId: 'podman',
engineName: 'podman',
};
// one single container and two containers part of a pod
const mockedContainers = [
singleContainer,
{
Id: 'sha256:456456456456456',
Image: 'sha256:234',
Names: ['internal-only-pod'],
RepoTags: ['veryold:image'],
Status: 'Running',
pod: {
name: 'my-pod',
id: podId,
status: 'Running',
},
engineId: 'podman',
engineName: 'podman',
},
{
Id: 'sha256:7897891234567890123',
Image: 'sha256:345',
Names: ['container-in-pod'],
Status: 'Running',
pod: {
name: 'my-pod',
id: podId,
status: 'Running',
},
engineId: 'podman',
engineName: 'podman',
},
];
listContainersMock.mockResolvedValue(mockedContainers);
window.dispatchEvent(new CustomEvent('extensions-already-started'));
window.dispatchEvent(new CustomEvent('provider-lifecycle-change'));
window.dispatchEvent(new CustomEvent('tray:update-provider'));
// wait store are populated
while (get(containersInfos).length === 0) {
await new Promise(resolve => setTimeout(resolve, 500));
}
while (get(providerInfos).length === 0) {
await new Promise(resolve => setTimeout(resolve, 500));
}
await waitRender({});
// select the checkbox
const checkbox = screen.getByRole('checkbox', { name: 'Toggle all' });
expect(checkbox).toBeInTheDocument();
// click on the checkbox
await fireEvent.click(checkbox);
// click on the delete button
const deleteButton = screen.getByRole('button', { name: 'Delete selected containers and pods' });
expect(deleteButton).toBeInTheDocument();
await fireEvent.click(deleteButton);
// expect that we call to delete the pod first (as it's a group of containers)
expect(removePodMock).toHaveBeenCalledWith('podman', podId);
// wait deleteContainerMock is called
while (deleteContainerMock.mock.calls.length === 0) {
await new Promise(resolve => setTimeout(resolve, 100));
}
// and then only the container that is not inside a pod
expect(deleteContainerMock).toBeCalledWith('podman', singleContainer.Id);
expect(deleteContainerMock).toBeCalledTimes(1);
});
test('Try to delete a container without deleting pods', async () => {
removePodMock.mockClear();
deleteContainerMock.mockClear();
listContainersMock.mockResolvedValue([]);
window.dispatchEvent(new CustomEvent('extensions-already-started'));
window.dispatchEvent(new CustomEvent('provider-lifecycle-change'));
window.dispatchEvent(new CustomEvent('tray:update-provider'));
// wait for the store to be cleared
while (get(containersInfos).length !== 0) {
await new Promise(resolve => setTimeout(resolve, 250));
}
const podId = 'pod-id2';
const singleContainer = {
Id: 'sha256:21234567890123',
Image: 'sha256:123',
Names: ['foo'],
Status: 'Running',
engineId: 'podman',
engineName: 'podman',
};
// one single container and a container as part of a pod
const mockedContainers = [
singleContainer,
{
Id: 'sha256:7897891234567890123',
Image: 'sha256:345',
Names: ['container-in-pod'],
Status: 'Running',
pod: {
name: 'my-pod2',
id: podId,
status: 'Running',
},
engineId: 'podman',
engineName: 'podman',
},
];
listContainersMock.mockResolvedValue(mockedContainers);
window.dispatchEvent(new CustomEvent('extensions-already-started'));
window.dispatchEvent(new CustomEvent('provider-lifecycle-change'));
window.dispatchEvent(new CustomEvent('tray:update-provider'));
// wait until the store is populated
while (get(containersInfos).length === 0) {
await new Promise(resolve => setTimeout(resolve, 250));
}
await waitRender({});
// select the standalone container checkbox
const containerCheckbox = screen.getAllByRole('checkbox', { name: 'Toggle container' });
expect(containerCheckbox[0]).toBeInTheDocument();
await fireEvent.click(containerCheckbox[0]);
// click on the delete button
const deleteButton = screen.getByRole('button', { name: 'Delete selected containers and pods' });
expect(deleteButton).toBeInTheDocument();
await fireEvent.click(deleteButton);
// wait until deleteContainerMock is called
while (deleteContainerMock.mock.calls.length === 0) {
await new Promise(resolve => setTimeout(resolve, 100));
}
// expect that the container has been deleted
expect(deleteContainerMock).toBeCalledWith('podman', singleContainer.Id);
// but not the other container
expect(deleteContainerMock).toHaveBeenCalledOnce();
// and not the pod
expect(removePodMock).not.toBeCalled();
});
test('Try to delete a pod without deleting container', async () => {
removePodMock.mockClear();
deleteContainerMock.mockClear();
listContainersMock.mockResolvedValue([]);
window.dispatchEvent(new CustomEvent('extensions-already-started'));
window.dispatchEvent(new CustomEvent('provider-lifecycle-change'));
window.dispatchEvent(new CustomEvent('tray:update-provider'));
// wait for the store to be cleared
while (get(containersInfos).length !== 0) {
await new Promise(resolve => setTimeout(resolve, 250));
}
const podId = 'pod-id3';
const singleContainer = {
Id: 'sha256:56789012345',
Image: 'sha256:567',
Names: ['foo'],
Status: 'Running',
engineId: 'podman',
engineName: 'podman',
};
// one single container and a container as part of a pod
const mockedContainers = [
singleContainer,
{
Id: 'sha256:7897891234567890123',
Image: 'sha256:345',
Names: ['container-in-pod'],
Status: 'Running',
pod: {
name: 'my-pod3',
id: podId,
status: 'Running',
},
engineId: 'podman',
engineName: 'podman',
},
];
listContainersMock.mockResolvedValue(mockedContainers);
window.dispatchEvent(new CustomEvent('extensions-already-started'));
window.dispatchEvent(new CustomEvent('provider-lifecycle-change'));
window.dispatchEvent(new CustomEvent('tray:update-provider'));
// wait until the store is populated
while (get(containersInfos).length === 0) {
await new Promise(resolve => setTimeout(resolve, 250));
}
await waitRender({});
// select the pod checkbox
const podCheckbox = screen.getByRole('checkbox', { name: 'Toggle pod' });
expect(podCheckbox).toBeInTheDocument();
await fireEvent.click(podCheckbox);
// click on the delete button
const deleteButton = screen.getByRole('button', { name: 'Delete selected containers and pods' });
expect(deleteButton).toBeInTheDocument();
await fireEvent.click(deleteButton);
// wait until removePodMock is called
while (removePodMock.mock.calls.length === 0) {
await new Promise(resolve => setTimeout(resolve, 100));
}
// expect that the pod has been removed
expect(removePodMock).toHaveBeenCalledWith('podman', podId);
expect(removePodMock).toHaveBeenCalledOnce();
// and the standalone container has not been deleted
expect(deleteContainerMock).not.toHaveBeenCalled();
});