11import { deepEqual , deepStrictEqual , notStrictEqual , rejects , strictEqual , throws } from 'node:assert' ;
2+ import child_process from 'node:child_process' ;
23import { readFileSync } from 'node:fs' ;
34import https from 'node:https' ;
45import { Agent , RequestOptions } from 'node:https' ;
56import path , { dirname , join } from 'node:path' ;
67import { fileURLToPath } from 'node:url' ;
8+ import { mock } from 'node:test' ;
79
810import mockfs from 'mock-fs' ;
911
@@ -301,8 +303,10 @@ describe('KubeConfig', () => {
301303 const kc = new KubeConfig ( ) ;
302304 kc . loadFromFile ( kcTlsServerNameFileName ) ;
303305
306+ const requestContext = new RequestContext ( 'https://kube.example.com' , HttpMethod . GET ) ;
304307 const opts : https . RequestOptions = { } ;
305308 await kc . applyToHTTPSOptions ( opts ) ;
309+ await kc . applySecurityAuthentication ( requestContext ) ;
306310
307311 const expectedAgent = new https . Agent ( {
308312 ca : Buffer . from ( 'CADATA2' , 'utf-8' ) ,
@@ -322,6 +326,7 @@ describe('KubeConfig', () => {
322326 } ;
323327
324328 assertRequestOptionsEqual ( opts , expectedOptions ) ;
329+ strictEqual ( ( requestContext . getAgent ( ) ! as any ) . options . servername , 'kube.example2.com' ) ;
325330 } ) ;
326331 it ( 'should apply cert configs' , async ( ) => {
327332 const kc = new KubeConfig ( ) ;
@@ -1630,5 +1635,60 @@ describe('KubeConfig', () => {
16301635 strictEqual ( inputData ! . toString ( ) , data ) ;
16311636 mockfs . restore ( ) ;
16321637 } ) ;
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+ } ) ;
16331693 } ) ;
16341694} ) ;
0 commit comments