-
Notifications
You must be signed in to change notification settings - Fork 10
/
container-utils.spec.ts
212 lines (186 loc) · 7.64 KB
/
container-utils.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
/**********************************************************************
* Copyright (C) 2024 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 { beforeEach, expect, test, vi } from 'vitest';
import * as extensionApi from '@podman-desktop/api';
import {
getContainerEngine,
pullImage,
createAndStartContainer,
waitForContainerToExit,
removeContainerIfExists,
removeContainerAndVolumes,
deleteOldImages,
} from './container-utils';
const mocks = vi.hoisted(() => ({
logUsageMock: vi.fn(),
}));
// Mocks and utilities
vi.mock('@podman-desktop/api', async () => {
return {
containerEngine: {
pullImage: vi.fn(),
createContainer: vi.fn(),
listContainers: vi.fn(),
deleteContainer: vi.fn(),
},
provider: {
getContainerConnections: vi.fn(() => [
// Mocked container connections
{
connection: {
type: 'podman',
status: () => 'started',
},
},
]),
},
env: {
createTelemetryLogger: () => ({
logUsage: mocks.logUsageMock,
}),
},
};
});
beforeEach(() => {
vi.clearAllMocks();
});
test('getContainerEngine should return a running podman engine', async () => {
const engine = await getContainerEngine();
expect(engine).toBeDefined();
expect(engine.type).toBe('podman');
});
test('pullImage should call pullImage from containerEngine', async () => {
await pullImage('someImage');
expect(extensionApi.containerEngine.pullImage).toBeCalled();
expect(mocks.logUsageMock).toHaveBeenCalled();
});
// Test createAndStartContainer
test('createAndStartContainer should create and return container ID', async () => {
const createContainerMock = vi.fn();
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(extensionApi.containerEngine as any).createContainer = createContainerMock;
createContainerMock.mockResolvedValue({ id: '1234' });
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const containerId = await createAndStartContainer('', {} as any);
expect(containerId).toBe('1234');
});
// Test waitForContainerToExit
test('waitForContainerToExit should wait for container to exit', async () => {
const listContainersMock = vi.fn();
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(extensionApi.containerEngine as any).listContainers = listContainersMock;
listContainersMock.mockResolvedValue([{ Names: '1234', Id: '1234', State: 'exited', Status: 'Exited (0)' }]);
await expect(waitForContainerToExit('1234')).resolves.toBeUndefined();
});
test('test waitForContainerToExit should throw error if container does not exit', async () => {
const listContainersMock = vi.fn();
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(extensionApi.containerEngine as any).listContainers = listContainersMock;
listContainersMock.mockResolvedValue([]);
// Exit with a quick timeout of 1 retry
await expect(waitForContainerToExit('1234', 1)).rejects.toThrow('Container not found after maximum retries');
});
test('Check that listContainers was called 2 times with a retry count of 2 if container was not found', async () => {
const listContainersMock = vi.fn();
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(extensionApi.containerEngine as any).listContainers = listContainersMock;
listContainersMock.mockResolvedValue([]);
// Exit with a quick timeout of 2 retries
await expect(waitForContainerToExit('1234', 2)).rejects.toThrow('Container not found after maximum retries');
expect(listContainersMock).toBeCalledTimes(2);
});
// Test removeContainerIfExists
test('removeContainerIfExists should remove existing container', async () => {
const listContainersMock = vi.fn();
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(extensionApi.containerEngine as any).listContainers = listContainersMock;
listContainersMock.mockResolvedValue([{ Names: '/1234' }]);
const deleteContainerMock = vi.fn();
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(extensionApi.containerEngine as any).deleteContainer = deleteContainerMock;
await removeContainerIfExists('', '1234');
expect(deleteContainerMock).toBeCalled();
});
// Write a test that removes a container and a list of volumes that match it
// make sure we mock listVolumes() with a list of volumes that match the engineId
// has Volumes inside and containersUsage matches the container name.
// Then make sure we call deleteVolume() for each volume.
test('removeContainerAndVolumes should remove existing container and volumes associated with it', async () => {
const listContainersMock = vi.fn();
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(extensionApi.containerEngine as any).listContainers = listContainersMock;
listContainersMock.mockResolvedValue([
{ Names: '/1234', Id: '1234', State: 'exited', Status: 'Exited (0)', engineName: 'podman', engineType: 'podman' },
]);
const deleteContainerMock = vi.fn();
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(extensionApi.containerEngine as any).deleteContainer = deleteContainerMock;
const listVolumesMock = vi.fn();
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(extensionApi.containerEngine as any).listVolumes = listVolumesMock;
listVolumesMock.mockResolvedValue([
{
engineId: 'podman',
Volumes: [
{
Name: '1234',
containersUsage: [
{
id: '1234',
names: ['/1234'],
},
],
},
{
Name: '1234-volumes-2',
containersUsage: [
{
id: '1234',
names: ['/1234'],
},
],
},
],
},
]);
const deleteVolumeMock = vi.fn();
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(extensionApi.containerEngine as any).deleteVolume = deleteVolumeMock;
await removeContainerAndVolumes('podman', '1234');
expect(deleteContainerMock).toBeCalled();
expect(deleteVolumeMock).toBeCalledTimes(2);
});
// Test deleteOldImages() deletes correctly tagged images
test('deleteOldImages should remove images with other tags', async () => {
const listImagesMock = vi.fn();
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(extensionApi.containerEngine as any).listImages = listImagesMock;
listImagesMock.mockResolvedValue([
{ engineId: 'podman', Id: 'i1', RepoTags: ['test.io/name:1'] },
{ engineId: 'podman', Id: 'i2', RepoTags: ['test.io/name:2'] },
{ engineId: 'podman', Id: 'i3', RepoTags: ['test.io/name:3'] },
{ engineId: 'podman', Id: 'i4', RepoTags: ['test.io/name:4', 'keep-me'] },
]);
const deletedIds: string[] = [];
const deleteImageMock = vi.fn().mockImplementation((_engineId, id) => deletedIds.push(id));
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(extensionApi.containerEngine as any).deleteImage = deleteImageMock;
await deleteOldImages('podman', 'test.io/name:2');
expect(deleteImageMock).toHaveBeenCalledTimes(2);
expect(deletedIds).toEqual(['i1', 'i3']);
});