1
1
import { deepEqual , deepStrictEqual , notStrictEqual , rejects , strictEqual , throws } from 'node:assert' ;
2
+ import child_process from 'node:child_process' ;
2
3
import { readFileSync } from 'node:fs' ;
3
4
import https from 'node:https' ;
4
5
import { Agent , RequestOptions } from 'node:https' ;
5
6
import path , { dirname , join } from 'node:path' ;
6
7
import { fileURLToPath } from 'node:url' ;
8
+ import { mock } from 'node:test' ;
7
9
8
10
import mockfs from 'mock-fs' ;
9
11
@@ -301,8 +303,10 @@ describe('KubeConfig', () => {
301
303
const kc = new KubeConfig ( ) ;
302
304
kc . loadFromFile ( kcTlsServerNameFileName ) ;
303
305
306
+ const requestContext = new RequestContext ( 'https://kube.example.com' , HttpMethod . GET ) ;
304
307
const opts : https . RequestOptions = { } ;
305
308
await kc . applyToHTTPSOptions ( opts ) ;
309
+ await kc . applySecurityAuthentication ( requestContext ) ;
306
310
307
311
const expectedAgent = new https . Agent ( {
308
312
ca : Buffer . from ( 'CADATA2' , 'utf-8' ) ,
@@ -322,6 +326,7 @@ describe('KubeConfig', () => {
322
326
} ;
323
327
324
328
assertRequestOptionsEqual ( opts , expectedOptions ) ;
329
+ strictEqual ( ( requestContext . getAgent ( ) ! as any ) . options . servername , 'kube.example2.com' ) ;
325
330
} ) ;
326
331
it ( 'should apply cert configs' , async ( ) => {
327
332
const kc = new KubeConfig ( ) ;
@@ -1630,5 +1635,60 @@ describe('KubeConfig', () => {
1630
1635
strictEqual ( inputData ! . toString ( ) , data ) ;
1631
1636
mockfs . restore ( ) ;
1632
1637
} ) ;
1638
+ it ( 'should try to load from WSL on Windows with wsl.exe not working' , ( ) => {
1639
+ const kc = new KubeConfig ( ) ;
1640
+ const commands : { command : string ; args : string [ ] } [ ] = [ ] ;
1641
+ mock . method ( child_process , 'spawnSync' , ( cmd : string , args : string [ ] ) => {
1642
+ commands . push ( { command : cmd , args } ) ;
1643
+ return { status : 1 , stderr : 'some error' } ;
1644
+ } ) ;
1645
+ kc . loadFromDefault ( undefined , false , 'win32' ) ;
1646
+ strictEqual ( commands . length , 2 ) ;
1647
+ for ( let i = 0 ; i < commands . length ; i ++ ) {
1648
+ strictEqual ( commands [ i ] . command , 'wsl.exe' ) ;
1649
+ }
1650
+ } ) ;
1651
+ it ( 'should try to load from WSL on Windows with $KUBECONFIG' , ( ) => {
1652
+ const kc = new KubeConfig ( ) ;
1653
+ const test_path = 'C:\\Users\\user\\.kube\\config' ;
1654
+ const configData = readFileSync ( kcFileName ) ;
1655
+ const commands : { command : string ; args : string [ ] } [ ] = [ ] ;
1656
+ const results : { status : number ; stderr : string ; stdout : string } [ ] = [
1657
+ { status : 0 , stderr : '' , stdout : test_path } ,
1658
+ { status : 0 , stderr : '' , stdout : configData . toString ( ) } ,
1659
+ ] ;
1660
+ let ix = 0 ;
1661
+ mock . method ( child_process , 'spawnSync' , ( cmd : string , args : string [ ] ) => {
1662
+ commands . push ( { command : cmd , args } ) ;
1663
+ return results [ ix ++ ] ;
1664
+ } ) ;
1665
+ kc . loadFromDefault ( undefined , false , 'win32' ) ;
1666
+ strictEqual ( commands . length , 2 ) ;
1667
+ for ( let i = 0 ; i < commands . length ; i ++ ) {
1668
+ strictEqual ( commands [ i ] . command , 'wsl.exe' ) ;
1669
+ }
1670
+ validateFileLoad ( kc ) ;
1671
+ } ) ;
1672
+ it ( 'should try to load from WSL on Windows without $KUBECONFIG' , ( ) => {
1673
+ const kc = new KubeConfig ( ) ;
1674
+ const configData = readFileSync ( kcFileName ) ;
1675
+ const commands : { command : string ; args : string [ ] } [ ] = [ ] ;
1676
+ const results : { status : number ; stderr : string ; stdout : string } [ ] = [
1677
+ { status : 1 , stderr : 'Some Error' , stdout : '' } ,
1678
+ { status : 0 , stderr : '' , stdout : configData . toString ( ) } ,
1679
+ { status : 0 , stderr : '' , stdout : 'C:\\wsldata\\.kube' } ,
1680
+ ] ;
1681
+ let ix = 0 ;
1682
+ mock . method ( child_process , 'spawnSync' , ( cmd : string , args : string [ ] ) => {
1683
+ commands . push ( { command : cmd , args } ) ;
1684
+ return results [ ix ++ ] ;
1685
+ } ) ;
1686
+ kc . loadFromDefault ( undefined , false , 'win32' ) ;
1687
+ strictEqual ( commands . length , 3 ) ;
1688
+ for ( let i = 0 ; i < commands . length ; i ++ ) {
1689
+ strictEqual ( commands [ i ] . command , 'wsl.exe' ) ;
1690
+ }
1691
+ validateFileLoad ( kc ) ;
1692
+ } ) ;
1633
1693
} ) ;
1634
1694
} ) ;
0 commit comments