18
18
19
19
import type { Cluster , CoreV1Event , KubernetesObject , ObjectCache , V1Status } from '@kubernetes/client-node' ;
20
20
import { ApiException , KubeConfig } from '@kubernetes/client-node' ;
21
- import type { Event } from '@podman-desktop/api' ;
21
+ import type { Event , Uri } from '@podman-desktop/api' ;
22
22
import { afterEach , assert , beforeEach , describe , expect , test , vi } from 'vitest' ;
23
- import { window } from '@podman-desktop/api' ;
23
+ import { kubernetes , window } from '@podman-desktop/api' ;
24
24
25
25
import type { ContextHealthState } from './context-health-checker.js' ;
26
26
import { ContextHealthChecker } from './context-health-checker.js' ;
@@ -39,6 +39,7 @@ import type {
39
39
OfflineEvent ,
40
40
ResourceInformer ,
41
41
} from '/@/types/resource-informer.js' ;
42
+ import { vol } from 'memfs' ;
42
43
43
44
const onCacheUpdatedMock = vi . fn < Event < CacheUpdatedEvent > > ( ) ;
44
45
const onOfflineMock = vi . fn < Event < OfflineEvent > > ( ) ;
@@ -278,6 +279,8 @@ const kcWithNoCurrentContext = {
278
279
] ,
279
280
} ;
280
281
282
+ vi . mock ( 'node:fs/promises' ) ;
283
+ vi . mock ( 'node:fs' ) ;
281
284
vi . mock ( './context-health-checker.js' ) ;
282
285
vi . mock ( './context-permissions-checker.js' ) ;
283
286
@@ -287,6 +290,7 @@ const originalConsoleWarn = console.warn;
287
290
const originalConsoleError = console . error ;
288
291
289
292
beforeEach ( ( ) => {
293
+ vol . reset ( ) ;
290
294
vi . clearAllMocks ( ) ;
291
295
kcWith2contexts = {
292
296
contexts : [
@@ -405,6 +409,12 @@ describe('HealthChecker is built and start is called for each context the first
405
409
406
410
expect ( permissionsStartMock ) . toHaveBeenCalledTimes ( 2 ) ;
407
411
} ) ;
412
+
413
+ test ( 'getContextsNames returns the correct contexts names' , async ( ) => {
414
+ await manager . update ( kc ) ;
415
+ const contextsNames = manager . getContextsNames ( ) ;
416
+ expect ( contextsNames ) . toEqual ( [ 'context1' , 'context2' ] ) ;
417
+ } ) ;
408
418
} ) ;
409
419
410
420
describe ( 'HealthChecker pass and PermissionsChecker resturns a value' , async ( ) => {
@@ -1398,6 +1408,28 @@ test('get currentContext', async () => {
1398
1408
expect ( currentContext ?. getKubeConfig ( ) . currentContext ) . toEqual ( 'context1' ) ;
1399
1409
} ) ;
1400
1410
1411
+ test ( 'setCurrentContext sets the current context' , async ( ) => {
1412
+ vol . fromJSON ( {
1413
+ '/path/to/config' : JSON . stringify ( kcWith2contexts ) ,
1414
+ } ) ;
1415
+ const kc = new KubeConfig ( ) ;
1416
+ kc . loadFromOptions ( kcWith2contexts ) ;
1417
+ const manager = new TestContextsManager ( ) ;
1418
+ vi . spyOn ( manager , 'startMonitoring' ) . mockImplementation ( async ( ) : Promise < void > => { } ) ;
1419
+ vi . spyOn ( manager , 'stopMonitoring' ) . mockImplementation ( ( ) : void => { } ) ;
1420
+ await manager . update ( kc ) ;
1421
+ const currentContext = manager . currentContext ;
1422
+ expect ( currentContext ?. getKubeConfig ( ) . currentContext ) . toEqual ( 'context1' ) ;
1423
+
1424
+ vi . mocked ( kubernetes . getKubeconfig ) . mockReturnValue ( {
1425
+ path : '/path/to/config' ,
1426
+ } as Uri ) ;
1427
+
1428
+ await manager . setCurrentContext ( 'context2' ) ;
1429
+ const content = vol . readFileSync ( '/path/to/config' , 'utf-8' ) ;
1430
+ expect ( content ) . toContain ( 'current-context: context2' ) ;
1431
+ } ) ;
1432
+
1401
1433
test ( 'onCurrentContextChange is fired' , async ( ) => {
1402
1434
const kc = new KubeConfig ( ) ;
1403
1435
kc . loadFromOptions ( kcWith2contexts ) ;
0 commit comments