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
|
import { CoreV1Api, WatchApi, AppsV1Api } from '@gitlab/cluster-client';
import { resolvers } from '~/kubernetes_dashboard/graphql/resolvers';
import k8sDashboardPodsQuery from '~/kubernetes_dashboard/graphql/queries/k8s_dashboard_pods.query.graphql';
import k8sDashboardDeploymentsQuery from '~/kubernetes_dashboard/graphql/queries/k8s_dashboard_deployments.query.graphql';
import k8sDashboardStatefulSetsQuery from '~/kubernetes_dashboard/graphql/queries/k8s_dashboard_stateful_sets.query.graphql';
import k8sDashboardReplicaSetsQuery from '~/kubernetes_dashboard/graphql/queries/k8s_dashboard_replica_sets.query.graphql';
import {
k8sPodsMock,
k8sDeploymentsMock,
k8sStatefulSetsMock,
k8sReplicaSetsMock,
} from '../mock_data';
describe('~/frontend/environments/graphql/resolvers', () => {
let mockResolvers;
const configuration = {
basePath: 'kas-proxy/',
baseOptions: {
headers: { 'GitLab-Agent-Id': '1' },
},
};
beforeEach(() => {
mockResolvers = resolvers;
});
describe('k8sPods', () => {
const client = { writeQuery: jest.fn() };
const mockWatcher = WatchApi.prototype;
const mockPodsListWatcherFn = jest.fn().mockImplementation(() => {
return Promise.resolve(mockWatcher);
});
const mockOnDataFn = jest.fn().mockImplementation((eventName, callback) => {
if (eventName === 'data') {
callback([]);
}
});
const mockPodsListFn = jest.fn().mockImplementation(() => {
return Promise.resolve({
items: k8sPodsMock,
});
});
const mockAllPodsListFn = jest.fn().mockImplementation(mockPodsListFn);
describe('when the pods data is present', () => {
beforeEach(() => {
jest
.spyOn(CoreV1Api.prototype, 'listCoreV1PodForAllNamespaces')
.mockImplementation(mockAllPodsListFn);
jest.spyOn(mockWatcher, 'subscribeToStream').mockImplementation(mockPodsListWatcherFn);
jest.spyOn(mockWatcher, 'on').mockImplementation(mockOnDataFn);
});
it('should request all pods from the cluster_client library and watch the events', async () => {
const pods = await mockResolvers.Query.k8sPods(
null,
{
configuration,
},
{ client },
);
expect(mockAllPodsListFn).toHaveBeenCalled();
expect(mockPodsListWatcherFn).toHaveBeenCalled();
expect(pods).toEqual(k8sPodsMock);
});
it('should update cache with the new data when received from the library', async () => {
await mockResolvers.Query.k8sPods(null, { configuration, namespace: '' }, { client });
expect(client.writeQuery).toHaveBeenCalledWith({
query: k8sDashboardPodsQuery,
variables: { configuration, namespace: '' },
data: { k8sPods: [] },
});
});
});
it('should not watch pods from the cluster_client library when the pods data is not present', async () => {
jest.spyOn(CoreV1Api.prototype, 'listCoreV1PodForAllNamespaces').mockImplementation(
jest.fn().mockImplementation(() => {
return Promise.resolve({
items: [],
});
}),
);
await mockResolvers.Query.k8sPods(null, { configuration }, { client });
expect(mockPodsListWatcherFn).not.toHaveBeenCalled();
});
it('should throw an error if the API call fails', async () => {
jest
.spyOn(CoreV1Api.prototype, 'listCoreV1PodForAllNamespaces')
.mockRejectedValue(new Error('API error'));
await expect(
mockResolvers.Query.k8sPods(null, { configuration }, { client }),
).rejects.toThrow('API error');
});
});
describe('k8sDeployments', () => {
const client = { writeQuery: jest.fn() };
const mockWatcher = WatchApi.prototype;
const mockDeploymentsListWatcherFn = jest.fn().mockImplementation(() => {
return Promise.resolve(mockWatcher);
});
const mockOnDataFn = jest.fn().mockImplementation((eventName, callback) => {
if (eventName === 'data') {
callback([]);
}
});
const mockDeploymentsListFn = jest.fn().mockImplementation(() => {
return Promise.resolve({
items: k8sDeploymentsMock,
});
});
const mockAllDeploymentsListFn = jest.fn().mockImplementation(mockDeploymentsListFn);
describe('when the deployments data is present', () => {
beforeEach(() => {
jest
.spyOn(AppsV1Api.prototype, 'listAppsV1DeploymentForAllNamespaces')
.mockImplementation(mockAllDeploymentsListFn);
jest
.spyOn(mockWatcher, 'subscribeToStream')
.mockImplementation(mockDeploymentsListWatcherFn);
jest.spyOn(mockWatcher, 'on').mockImplementation(mockOnDataFn);
});
it('should request all deployments from the cluster_client library and watch the events', async () => {
const deployments = await mockResolvers.Query.k8sDeployments(
null,
{
configuration,
},
{ client },
);
expect(mockAllDeploymentsListFn).toHaveBeenCalled();
expect(mockDeploymentsListWatcherFn).toHaveBeenCalled();
expect(deployments).toEqual(k8sDeploymentsMock);
});
it('should update cache with the new data when received from the library', async () => {
await mockResolvers.Query.k8sDeployments(
null,
{ configuration, namespace: '' },
{ client },
);
expect(client.writeQuery).toHaveBeenCalledWith({
query: k8sDashboardDeploymentsQuery,
variables: { configuration, namespace: '' },
data: { k8sDeployments: [] },
});
});
});
it('should not watch deployments from the cluster_client library when the deployments data is not present', async () => {
jest.spyOn(AppsV1Api.prototype, 'listAppsV1DeploymentForAllNamespaces').mockImplementation(
jest.fn().mockImplementation(() => {
return Promise.resolve({
items: [],
});
}),
);
await mockResolvers.Query.k8sDeployments(null, { configuration }, { client });
expect(mockDeploymentsListWatcherFn).not.toHaveBeenCalled();
});
it('should throw an error if the API call fails', async () => {
jest
.spyOn(AppsV1Api.prototype, 'listAppsV1DeploymentForAllNamespaces')
.mockRejectedValue(new Error('API error'));
await expect(
mockResolvers.Query.k8sDeployments(null, { configuration }, { client }),
).rejects.toThrow('API error');
});
});
describe('k8sStatefulSets', () => {
const client = { writeQuery: jest.fn() };
const mockWatcher = WatchApi.prototype;
const mockStatefulSetsListWatcherFn = jest.fn().mockImplementation(() => {
return Promise.resolve(mockWatcher);
});
const mockOnDataFn = jest.fn().mockImplementation((eventName, callback) => {
if (eventName === 'data') {
callback([]);
}
});
const mockStatefulSetsListFn = jest.fn().mockImplementation(() => {
return Promise.resolve({
items: k8sStatefulSetsMock,
});
});
const mockAllStatefulSetsListFn = jest.fn().mockImplementation(mockStatefulSetsListFn);
describe('when the StatefulSets data is present', () => {
beforeEach(() => {
jest
.spyOn(AppsV1Api.prototype, 'listAppsV1StatefulSetForAllNamespaces')
.mockImplementation(mockAllStatefulSetsListFn);
jest
.spyOn(mockWatcher, 'subscribeToStream')
.mockImplementation(mockStatefulSetsListWatcherFn);
jest.spyOn(mockWatcher, 'on').mockImplementation(mockOnDataFn);
});
it('should request all StatefulSets from the cluster_client library and watch the events', async () => {
const StatefulSets = await mockResolvers.Query.k8sStatefulSets(
null,
{
configuration,
},
{ client },
);
expect(mockAllStatefulSetsListFn).toHaveBeenCalled();
expect(mockStatefulSetsListWatcherFn).toHaveBeenCalled();
expect(StatefulSets).toEqual(k8sStatefulSetsMock);
});
it('should update cache with the new data when received from the library', async () => {
await mockResolvers.Query.k8sStatefulSets(
null,
{ configuration, namespace: '' },
{ client },
);
expect(client.writeQuery).toHaveBeenCalledWith({
query: k8sDashboardStatefulSetsQuery,
variables: { configuration, namespace: '' },
data: { k8sStatefulSets: [] },
});
});
});
it('should not watch StatefulSets from the cluster_client library when the StatefulSets data is not present', async () => {
jest.spyOn(AppsV1Api.prototype, 'listAppsV1StatefulSetForAllNamespaces').mockImplementation(
jest.fn().mockImplementation(() => {
return Promise.resolve({
items: [],
});
}),
);
await mockResolvers.Query.k8sStatefulSets(null, { configuration }, { client });
expect(mockStatefulSetsListWatcherFn).not.toHaveBeenCalled();
});
it('should throw an error if the API call fails', async () => {
jest
.spyOn(AppsV1Api.prototype, 'listAppsV1StatefulSetForAllNamespaces')
.mockRejectedValue(new Error('API error'));
await expect(
mockResolvers.Query.k8sStatefulSets(null, { configuration }, { client }),
).rejects.toThrow('API error');
});
});
describe('k8sReplicaSets', () => {
const client = { writeQuery: jest.fn() };
const mockWatcher = WatchApi.prototype;
const mockReplicaSetsListWatcherFn = jest.fn().mockImplementation(() => {
return Promise.resolve(mockWatcher);
});
const mockOnDataFn = jest.fn().mockImplementation((eventName, callback) => {
if (eventName === 'data') {
callback([]);
}
});
const mockReplicaSetsListFn = jest.fn().mockImplementation(() => {
return Promise.resolve({
items: k8sReplicaSetsMock,
});
});
const mockAllReplicaSetsListFn = jest.fn().mockImplementation(mockReplicaSetsListFn);
describe('when the ReplicaSets data is present', () => {
beforeEach(() => {
jest
.spyOn(AppsV1Api.prototype, 'listAppsV1ReplicaSetForAllNamespaces')
.mockImplementation(mockAllReplicaSetsListFn);
jest
.spyOn(mockWatcher, 'subscribeToStream')
.mockImplementation(mockReplicaSetsListWatcherFn);
jest.spyOn(mockWatcher, 'on').mockImplementation(mockOnDataFn);
});
it('should request all ReplicaSets from the cluster_client library and watch the events', async () => {
const ReplicaSets = await mockResolvers.Query.k8sReplicaSets(
null,
{
configuration,
},
{ client },
);
expect(mockAllReplicaSetsListFn).toHaveBeenCalled();
expect(mockReplicaSetsListWatcherFn).toHaveBeenCalled();
expect(ReplicaSets).toEqual(k8sReplicaSetsMock);
});
it('should update cache with the new data when received from the library', async () => {
await mockResolvers.Query.k8sReplicaSets(
null,
{ configuration, namespace: '' },
{ client },
);
expect(client.writeQuery).toHaveBeenCalledWith({
query: k8sDashboardReplicaSetsQuery,
variables: { configuration, namespace: '' },
data: { k8sReplicaSets: [] },
});
});
});
it('should not watch ReplicaSets from the cluster_client library when the ReplicaSets data is not present', async () => {
jest.spyOn(AppsV1Api.prototype, 'listAppsV1ReplicaSetForAllNamespaces').mockImplementation(
jest.fn().mockImplementation(() => {
return Promise.resolve({
items: [],
});
}),
);
await mockResolvers.Query.k8sReplicaSets(null, { configuration }, { client });
expect(mockReplicaSetsListWatcherFn).not.toHaveBeenCalled();
});
it('should throw an error if the API call fails', async () => {
jest
.spyOn(AppsV1Api.prototype, 'listAppsV1ReplicaSetForAllNamespaces')
.mockRejectedValue(new Error('API error'));
await expect(
mockResolvers.Query.k8sReplicaSets(null, { configuration }, { client }),
).rejects.toThrow('API error');
});
});
});
|