forked from tektoncd/dashboard
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.test.js
More file actions
541 lines (475 loc) · 17 KB
/
index.test.js
File metadata and controls
541 lines (475 loc) · 17 KB
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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
/*
Copyright 2019-2026 The Tekton Authors
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.
*/
import * as API from '../api';
import * as comms from '../api/comms';
import {
fetchLogs,
fetchLogsFallback,
getByPath,
getLocale,
getLogsRetriever,
getTheme,
getViewChangeHandler,
I18N_DEV_KEY,
isValidLabel,
keyBy,
setTheme,
sortRunsByCreationTime,
sortRunsByStartTime
} from '.';
describe('getKeyByPath', () => {
it('should handle simple key lookup', () => {
const foo = '123';
expect(getByPath({ foo }, 'foo')).toEqual(foo);
});
it('should handle dot notation', () => {
const bar = '123';
expect(getByPath({ foo: { bar } }, 'foo.bar')).toEqual(bar);
});
it('should handle case where key not found', () => {
expect(getByPath({ foo: 123 }, 'bar')).toBeUndefined();
expect(getByPath({ foo: 123 }, 'foo.bar')).toBeUndefined();
});
});
describe('sortRunsByStartTime', () => {
it('should handle missing start time or status', () => {
const a = { name: 'a', status: { startTime: '0' } };
const b = { name: 'b', status: {} };
const c = { name: 'c', status: { startTime: '2' } };
const d = { name: 'd', status: { startTime: '1' } };
const e = { name: 'e', status: {} };
const f = { name: 'f', status: { startTime: '3' } };
const g = { name: 'g' };
const runs = [a, b, c, d, e, f, g];
/*
sort is stable on all modern browsers so
input order is preserved for b and e
*/
const sortedRuns = [b, e, g, f, c, d, a];
sortRunsByStartTime(runs);
expect(runs).toEqual(sortedRuns);
});
it('should leave the order unchanged if no startTimes specified', () => {
const a = { name: 'a' };
const b = { name: 'b' };
const runs = [a, b];
const sortedRuns = [a, b];
sortRunsByStartTime(runs);
expect(runs).toEqual(sortedRuns);
});
});
describe('sortRunsByCreationTime', () => {
it('should handle missing creation time or metadata', () => {
const a = { name: 'a', metadata: { creationTimestamp: '0' } };
const b = { name: 'b', metadata: {} };
const c = { name: 'c', metadata: { creationTimestamp: '2' } };
const d = { name: 'd', metadata: { creationTimestamp: '1' } };
const e = { name: 'e', metadata: {} };
const f = { name: 'f', metadata: { creationTimestamp: '3' } };
const g = { name: 'g' };
const runs = [a, b, c, d, e, f, g];
/*
sort is stable on all modern browsers so
input order is preserved for b and e
*/
const sortedRuns = [b, e, g, f, c, d, a];
sortRunsByCreationTime(runs);
expect(runs).toEqual(sortedRuns);
});
it('should leave the order unchanged if no creationTimestamps specified', () => {
const a = { name: 'a' };
const b = { name: 'b' };
const runs = [a, b];
const sortedRuns = [a, b];
sortRunsByCreationTime(runs);
expect(runs).toEqual(sortedRuns);
});
});
describe('fetchLogs', () => {
it('should return the pod logs', () => {
const namespace = 'default';
const podName = 'pipeline-run-123456';
const stepName = 'kubectl-apply';
const stepStatus = { container: 'step-kubectl-apply' };
const taskRun = {
metadata: { namespace },
status: { podName }
};
const logs = 'fake logs';
vi.spyOn(API, 'getPodLog').mockImplementation(() => logs);
const returnedLogs = fetchLogs({ stepName, stepStatus, taskRun });
expect(API.getPodLog).toHaveBeenCalledWith(
expect.objectContaining({
container: stepStatus.container,
name: podName,
namespace
})
);
returnedLogs.then(data => {
expect(data).toBe(logs);
});
});
it('should not call the API when the pod is not specified', () => {
const stepName = 'kubectl-apply';
const stepStatus = { container: 'step-kubectl-apply' };
const taskRun = { metadata: { namespace: 'default' } };
vi.spyOn(API, 'getPodLog');
fetchLogs({ stepName, stepStatus, taskRun });
expect(API.getPodLog).not.toHaveBeenCalled();
});
it('should return the streamed pod logs', () => {
const namespace = 'default';
const podName = 'pipeline-run-123456';
const stepName = 'kubectl-apply';
const stepStatus = { container: 'step-kubectl-apply' };
const taskRun = {
metadata: { namespace },
status: { podName }
};
const logs = {
getReader() {
return {
read() {
return Promise.resolve({
done: true,
value: new TextEncoder().encode('fake logs')
});
}
};
}
};
vi.spyOn(API, 'getPodLog').mockImplementation(() => logs);
const returnedLogs = fetchLogs({
stepName,
stream: true,
stepStatus,
taskRun
});
expect(API.getPodLog).toHaveBeenCalledWith(
expect.objectContaining({
container: stepStatus.container,
name: podName,
namespace,
stream: true
})
);
returnedLogs.then(data => {
expect(data).toBe(logs);
});
});
it('should not call the API when the pod is not specified', () => {
const stepName = 'kubectl-apply';
const stepStatus = { container: 'step-kubectl-apply' };
const taskRun = { metadata: { namespace: 'default' } };
vi.spyOn(API, 'getPodLog');
fetchLogs({ stepName, stream: true, stepStatus, taskRun });
expect(API.getPodLog).not.toHaveBeenCalled();
});
});
describe('fetchLogsFallback', () => {
it('should return undefined when no external log provider configured', () => {
expect(fetchLogsFallback()).toBeUndefined();
});
it('should return a function to retrieve logs from the external provider', () => {
const container = 'fake_container';
const externalLogsURL = '/fake_url';
const namespace = 'fake_namespace';
const podName = 'fake_podName';
const stepName = 'fake_stepName';
const startTime = '2000-01-02T03:04:05Z';
const completionTime = '2006-07-08T09:10:11Z';
const stepStatus = { container };
const taskRun = {
metadata: { namespace },
status: { podName, startTime, completionTime }
};
vi.spyOn(comms, 'get').mockImplementation(() => {});
const fallback = fetchLogsFallback(externalLogsURL);
fallback({ stepName, stepStatus, taskRun });
expect(comms.get).toHaveBeenCalledWith(
`http://localhost:3000${externalLogsURL}/${namespace}/${podName}/${container}?startTime=${startTime.replaceAll(
':',
'%3A'
)}&completionTime=${completionTime.replaceAll(':', '%3A')}`,
{ Accept: 'text/plain' }
);
});
it('should handle a missing startTime and completionTime', () => {
const container = 'fake_container';
const externalLogsURL = '/fake_url';
const namespace = 'fake_namespace';
const podName = 'fake_podName';
const stepName = 'fake_stepName';
const stepStatus = { container };
const taskRun = { metadata: { namespace }, status: { podName } };
vi.spyOn(comms, 'get').mockImplementation(() => {});
const fallback = fetchLogsFallback(externalLogsURL);
fallback({ stepName, stepStatus, taskRun });
expect(comms.get).toHaveBeenCalledWith(
`http://localhost:3000${externalLogsURL}/${namespace}/${podName}/${container}`,
{ Accept: 'text/plain' }
);
});
});
describe('getLogsRetriever', () => {
const namespace = 'fake_namespace';
const podName = 'fake_podName';
const stepName = 'fake_stepName';
const stepStatus = { container: stepName };
const taskRun = { metadata: { namespace }, status: { podName } };
it('should handle default logs retriever', () => {
vi.spyOn(API, 'getPodLog').mockImplementation(() => {});
const logsRetriever = getLogsRetriever({});
expect(logsRetriever).toBeDefined();
logsRetriever({ stepName, stepStatus, taskRun });
expect(API.getPodLog).toHaveBeenCalledWith({
container: stepName,
name: podName,
namespace
});
});
it('should handle default logs retriever with streaming enabled', () => {
vi.spyOn(API, 'getPodLog').mockImplementation(() => {});
const logsRetriever = getLogsRetriever({ isLogStreamingEnabled: true });
expect(logsRetriever).toBeDefined();
logsRetriever({ stepName, stepStatus, taskRun });
expect(API.getPodLog).toHaveBeenCalledWith({
container: stepName,
name: podName,
namespace,
stream: true
});
});
it('should handle default logs retriever with external fallback enabled', async () => {
const externalLogsURL = 'fake_externalLogsURL';
vi.spyOn(API, 'getPodLog').mockImplementation(() => {});
const onFallback = vi.fn();
const logsRetriever = getLogsRetriever({ externalLogsURL, onFallback });
expect(logsRetriever).toBeDefined();
await logsRetriever({ stepName, stepStatus, taskRun });
expect(API.getPodLog).toHaveBeenCalledWith({
container: stepName,
name: podName,
namespace
});
expect(onFallback).not.toHaveBeenCalled();
});
it('should handle external logs fallback', async () => {
const externalLogsURL = 'fake_externalLogsURL';
vi.spyOn(API, 'getExternalLogURL');
vi.spyOn(API, 'getPodLog').mockImplementation(() => {
throw new Error();
});
vi.spyOn(comms, 'get').mockImplementation(() => {});
const onFallback = vi.fn();
const logsRetriever = getLogsRetriever({ externalLogsURL, onFallback });
expect(logsRetriever).toBeDefined();
await logsRetriever({ stepName, stepStatus, taskRun });
expect(API.getPodLog).toHaveBeenCalledWith({
container: stepName,
name: podName,
namespace
});
expect(API.getExternalLogURL).toHaveBeenCalled();
expect(onFallback).toHaveBeenCalledWith(true);
});
});
it('getViewChangeHandler', () => {
const url = 'someURL';
const navigate = vi.fn();
const location = { pathname: url, search: '?nonViewQueryParam=someValue' };
const handleViewChange = getViewChangeHandler({ location, navigate });
const view = 'someView';
handleViewChange(view);
expect(navigate).toHaveBeenCalledWith(
`${url}?nonViewQueryParam=someValue&view=${view}`,
{ replace: undefined }
);
});
describe('getLocale', () => {
it('handles exact matches for supported locales', () => {
const locale = 'en';
expect(getLocale(locale)).toEqual(locale);
});
it('handles fallback matches for supported locales', () => {
expect(getLocale('en-US')).toEqual('en');
});
it('handles Chinese locales', () => {
localStorage.setItem(I18N_DEV_KEY, true);
const locales = {
zh: 'zh-Hans',
'zh-CN': 'zh-Hans',
'zh-Hans': 'zh-Hans',
'zh-Hant': 'zh-Hant',
'zh-HA': 'zh-Hant',
'zh-HK': 'zh-Hant',
'zh-MO': 'zh-Hant',
'zh-SG': 'zh-Hans',
'zh-TW': 'zh-Hant'
};
Object.keys(locales).forEach(locale => {
expect(getLocale(locale)).toEqual(locales[locale]);
});
localStorage.removeItem(I18N_DEV_KEY);
});
it('handles unsupported locales', () => {
expect(getLocale('zz')).toEqual('en');
});
});
describe('getTheme', () => {
afterEach(() => {
localStorage.removeItem('tkn-theme');
});
it('defaults to system if no theme persisted', () => {
localStorage.removeItem('tkn-theme');
const theme = getTheme();
expect(theme).toEqual('system');
});
it('defaults to system if an invalid theme was persisted', () => {
localStorage.setItem('tkn-theme', 'foo');
const theme = getTheme();
expect(theme).toEqual('system');
});
it('returns a valid persisted theme', () => {
localStorage.setItem('tkn-theme', 'light');
const theme = getTheme();
expect(theme).toEqual('light');
});
});
describe('keyBy', () => {
it('handles null array', () => {
expect(keyBy(null)).toEqual({});
});
it('handles empty array', () => {
expect(keyBy([])).toEqual({});
});
it('handles array', () => {
const array = [
{ name: 'a', value: '123' },
{ name: 'b', value: 'xyz' }
];
const key = 'name';
expect(keyBy(array, key)).toEqual({ a: array[0], b: array[1] });
});
it('handles nested field', () => {
const array = [
{ name: { first: 'John', last: 'Smith' } },
{ name: { first: 'Jane', last: 'Doe' } }
];
const key = 'name.first';
expect(keyBy(array, key)).toEqual({ John: array[0], Jane: array[1] });
});
});
describe('setTheme', () => {
it('defaults to system if no theme specified', () => {
localStorage.removeItem('tkn-theme');
setTheme();
expect(localStorage.getItem('tkn-theme')).toEqual('system');
});
it('defaults to system if an invalid theme is specified', () => {
localStorage.removeItem('tkn-theme');
setTheme('foo');
expect(localStorage.getItem('tkn-theme')).toEqual('system');
});
it('persists a valid theme', () => {
localStorage.removeItem('tkn-theme');
setTheme('light');
expect(localStorage.getItem('tkn-theme')).toEqual('light');
});
});
describe('isValidLabel', () => {
describe('label keys', () => {
it('should accept valid alphanumeric keys', () => {
expect(isValidLabel('key', 'app')).toBe(true);
expect(isValidLabel('key', 'env123')).toBe(true);
});
it('should accept keys with hyphens, dots, and underscores', () => {
expect(isValidLabel('key', 'app-name')).toBe(true);
expect(isValidLabel('key', 'app.name')).toBe(true);
expect(isValidLabel('key', 'my-app.v1_test')).toBe(true);
});
it('should accept keys with domain prefix', () => {
expect(isValidLabel('key', 'example.com/app')).toBe(true);
expect(isValidLabel('key', 'k8s.io/cluster-name')).toBe(true);
});
it('should reject keys starting or ending with special characters', () => {
expect(isValidLabel('key', '-app')).toBe(false);
expect(isValidLabel('key', 'app-')).toBe(false);
expect(isValidLabel('key', '.app')).toBe(false);
});
it('should reject empty keys', () => {
expect(isValidLabel('key', '')).toBe(false);
});
it('should reject keys with invalid characters', () => {
expect(isValidLabel('key', 'app@name')).toBe(false);
expect(isValidLabel('key', 'app name')).toBe(false);
});
it('should reject keys with invalid characters in domain prefix', () => {
expect(isValidLabel('key', 'app@name/app')).toBe(false);
});
it('should reject keys with name segment longer than 63 characters', () => {
expect(isValidLabel('key', 'a'.repeat(63))).toBe(true);
expect(isValidLabel('key', 'a'.repeat(64))).toBe(false);
});
it('should reject keys with multiple slashes', () => {
expect(isValidLabel('key', `a/foo`)).toBe(true);
expect(isValidLabel('key', `a/b/foo`)).toBe(false);
});
it('should reject keys with prefix segment longer than 63 characters', () => {
expect(isValidLabel('key', `${'a'.repeat(63)}/foo`)).toBe(true);
expect(isValidLabel('key', `${'a'.repeat(64)}/foo`)).toBe(false);
});
it('should reject keys with domain prefix longer than 253 characters', () => {
// 63*3 + 61 + 3 (for the dots) = 253
const commonPrefix = `${'a'.repeat(63)}.${'a'.repeat(63)}.${'a'.repeat(63)}.`;
expect(isValidLabel('key', `${commonPrefix}${'a'.repeat(61)}/foo`)).toBe(
true
);
expect(isValidLabel('key', `${commonPrefix}${'a'.repeat(62)}/foo`)).toBe(
false
);
});
});
describe('label values', () => {
it('should accept valid alphanumeric values', () => {
expect(isValidLabel('value', 'prod')).toBe(true);
expect(isValidLabel('value', 'v1.2.3-beta_1')).toBe(true);
});
it('should accept values with hyphens, dots, and underscores', () => {
expect(isValidLabel('value', 'app-name')).toBe(true);
expect(isValidLabel('value', 'app.name')).toBe(true);
expect(isValidLabel('value', 'my-app.v1_test')).toBe(true);
});
it('should accept empty values', () => {
expect(isValidLabel('value', '')).toBe(true);
});
it('should reject values starting or ending with special characters', () => {
expect(isValidLabel('value', '-prod')).toBe(false);
expect(isValidLabel('value', 'prod-')).toBe(false);
});
it('should reject values with invalid characters', () => {
expect(isValidLabel('value', 'prod@env')).toBe(false);
expect(isValidLabel('value', 'prod env')).toBe(false);
});
it('should reject values with slashes', () => {
expect(isValidLabel('value', 'prod/v1')).toBe(false);
});
it('should reject values exceeding 63 characters', () => {
expect(isValidLabel('value', 'a'.repeat(63))).toBe(true);
expect(isValidLabel('value', 'a'.repeat(64))).toBe(false);
});
});
it('should reject invalid field type', () => {
expect(isValidLabel('other', 'foo')).toBe(false);
});
});