Skip to content

Commit 30460bc

Browse files
committed
improve coverage and testability of config.ts
1 parent f9b0d97 commit 30460bc

File tree

2 files changed

+79
-9
lines changed

2 files changed

+79
-9
lines changed

src/config.ts

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,11 @@ export class KubeConfig implements SecurityAuthentication {
394394
this.contexts.push(ctx);
395395
}
396396

397-
public loadFromDefault(opts?: Partial<ConfigOptions>, contextFromStartingConfig: boolean = false): void {
397+
public loadFromDefault(
398+
opts?: Partial<ConfigOptions>,
399+
contextFromStartingConfig: boolean = false,
400+
platform: string = process.platform,
401+
): void {
398402
if (process.env.KUBECONFIG && process.env.KUBECONFIG.length > 0) {
399403
const files = process.env.KUBECONFIG.split(path.delimiter).filter((filename: string) => filename);
400404
this.loadFromFile(files[0], opts);
@@ -405,23 +409,23 @@ export class KubeConfig implements SecurityAuthentication {
405409
}
406410
return;
407411
}
408-
const home = findHomeDir();
412+
const home = findHomeDir(platform);
409413
if (home) {
410414
const config = path.join(home, '.kube', 'config');
411415
if (fileExists(config)) {
412416
this.loadFromFile(config, opts);
413417
return;
414418
}
415419
}
416-
if (process.platform === 'win32') {
420+
if (platform === 'win32') {
417421
try {
418-
const envKubeconfigPathResult = child_process.spawnSync('wsl.exe', [
422+
const envKubeconfigPathResult = this.spawnSync('wsl.exe', [
419423
'bash',
420424
'-c',
421425
'printenv KUBECONFIG',
422426
]);
423427
if (envKubeconfigPathResult.status === 0 && envKubeconfigPathResult.stdout.length > 0) {
424-
const result = child_process.spawnSync('wsl.exe', [
428+
const result = this.spawnSync('wsl.exe', [
425429
'cat',
426430
envKubeconfigPathResult.stdout.toString('utf8'),
427431
]);
@@ -434,10 +438,10 @@ export class KubeConfig implements SecurityAuthentication {
434438
// Falling back to default kubeconfig
435439
}
436440
try {
437-
const configResult = child_process.spawnSync('wsl.exe', ['cat', '~/.kube/config']);
441+
const configResult = this.spawnSync('wsl.exe', ['cat', '~/.kube/config']);
438442
if (configResult.status === 0) {
439443
this.loadFromString(configResult.stdout.toString('utf8'), opts);
440-
const result = child_process.spawnSync('wsl.exe', ['wslpath', '-w', '~/.kube']);
444+
const result = this.spawnSync('wsl.exe', ['wslpath', '-w', '~/.kube']);
441445
if (result.status === 0) {
442446
this.makePathsAbsolute(result.stdout.toString('utf8'));
443447
}
@@ -593,6 +597,13 @@ export class KubeConfig implements SecurityAuthentication {
593597
this.applyHTTPSOptions(opts);
594598
await this.applyAuthorizationHeader(opts);
595599
}
600+
601+
private spawnSync(
602+
command: string,
603+
args: string[],
604+
): { status: number | null; stdout: Buffer; stderr: Buffer } {
605+
return child_process.spawnSync(command, args);
606+
}
596607
}
597608

598609
export type ApiConstructor<T extends ApiType> = new (config: Configuration) => T;
@@ -628,8 +639,8 @@ function dropDuplicatesAndNils(a: string[]): string[] {
628639
}
629640

630641
// Only public for testing.
631-
export function findHomeDir(): string | null {
632-
if (process.platform !== 'win32') {
642+
export function findHomeDir(platform: string = process.platform): string | null {
643+
if (platform !== 'win32') {
633644
if (process.env.HOME) {
634645
try {
635646
fs.accessSync(process.env.HOME);

src/config_test.ts

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,8 +301,10 @@ describe('KubeConfig', () => {
301301
const kc = new KubeConfig();
302302
kc.loadFromFile(kcTlsServerNameFileName);
303303

304+
const requestContext = new RequestContext('https://kube.example.com', HttpMethod.GET);
304305
const opts: https.RequestOptions = {};
305306
await kc.applyToHTTPSOptions(opts);
307+
await kc.applySecurityAuthentication(requestContext);
306308

307309
const expectedAgent = new https.Agent({
308310
ca: Buffer.from('CADATA2', 'utf-8'),
@@ -322,6 +324,8 @@ describe('KubeConfig', () => {
322324
};
323325

324326
assertRequestOptionsEqual(opts, expectedOptions);
327+
console.log(requestContext.getAgent());
328+
strictEqual((requestContext.getAgent()! as any).options.servername, 'kube.example2.com');
325329
});
326330
it('should apply cert configs', async () => {
327331
const kc = new KubeConfig();
@@ -1630,5 +1634,60 @@ describe('KubeConfig', () => {
16301634
strictEqual(inputData!.toString(), data);
16311635
mockfs.restore();
16321636
});
1637+
it('should try to load from WSL on Windows with wsl.exe not working', () => {
1638+
const kc = new KubeConfig();
1639+
const commands: { command: string; args: string[] }[] = [];
1640+
(kc as any).spawnSync = (cmd: string, args: string[]) => {
1641+
commands.push({ command: cmd, args });
1642+
return { status: 1, stderr: 'some error' };
1643+
};
1644+
kc.loadFromDefault(undefined, false, 'win32');
1645+
strictEqual(commands.length, 2);
1646+
for (let i = 0; i < commands.length; i++) {
1647+
strictEqual(commands[i].command, 'wsl.exe');
1648+
}
1649+
});
1650+
it('should try to load from WSL on Windows with $KUBECONFIG', () => {
1651+
const kc = new KubeConfig();
1652+
const test_path = 'C:\\Users\\user\\.kube\\config';
1653+
const configData = readFileSync(kcFileName);
1654+
const commands: { command: string; args: string[] }[] = [];
1655+
const results: { status: number; stderr: string; stdout: string }[] = [
1656+
{ status: 0, stderr: '', stdout: test_path },
1657+
{ status: 0, stderr: '', stdout: configData.toString() },
1658+
];
1659+
let ix = 0;
1660+
(kc as any).spawnSync = (cmd: string, args: string[]) => {
1661+
commands.push({ command: cmd, args });
1662+
return results[ix++];
1663+
};
1664+
kc.loadFromDefault(undefined, false, 'win32');
1665+
strictEqual(commands.length, 2);
1666+
for (let i = 0; i < commands.length; i++) {
1667+
strictEqual(commands[i].command, 'wsl.exe');
1668+
}
1669+
validateFileLoad(kc);
1670+
});
1671+
it('should try to load from WSL on Windows without $KUBECONFIG', () => {
1672+
const kc = new KubeConfig();
1673+
const configData = readFileSync(kcFileName);
1674+
const commands: { command: string; args: string[] }[] = [];
1675+
const results: { status: number; stderr: string; stdout: string }[] = [
1676+
{ status: 1, stderr: 'Some Error', stdout: '' },
1677+
{ status: 0, stderr: '', stdout: configData.toString() },
1678+
{ status: 0, stderr: '', stdout: 'C:\\wsldata\\.kube' },
1679+
];
1680+
let ix = 0;
1681+
(kc as any).spawnSync = (cmd: string, args: string[]) => {
1682+
commands.push({ command: cmd, args });
1683+
return results[ix++];
1684+
};
1685+
kc.loadFromDefault(undefined, false, 'win32');
1686+
strictEqual(commands.length, 3);
1687+
for (let i = 0; i < commands.length; i++) {
1688+
strictEqual(commands[i].command, 'wsl.exe');
1689+
}
1690+
validateFileLoad(kc);
1691+
});
16331692
});
16341693
});

0 commit comments

Comments
 (0)