Skip to content

Commit e27eb26

Browse files
authored
Merge pull request #2288 from brendandburns/coverage
Improve coverage and testability of config.ts
2 parents 8c53df2 + 3b44c17 commit e27eb26

File tree

2 files changed

+69
-5
lines changed

2 files changed

+69
-5
lines changed

src/config.ts

Lines changed: 9 additions & 5 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,15 +409,15 @@ 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 {
418422
const envKubeconfigPathResult = child_process.spawnSync('wsl.exe', [
419423
'bash',
@@ -628,8 +632,8 @@ function dropDuplicatesAndNils(a: string[]): string[] {
628632
}
629633

630634
// Only public for testing.
631-
export function findHomeDir(): string | null {
632-
if (process.platform !== 'win32') {
635+
export function findHomeDir(platform: string = process.platform): string | null {
636+
if (platform !== 'win32') {
633637
if (process.env.HOME) {
634638
try {
635639
fs.accessSync(process.env.HOME);

src/config_test.ts

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import { deepEqual, deepStrictEqual, notStrictEqual, rejects, strictEqual, throws } from 'node:assert';
2+
import child_process from 'node:child_process';
23
import { readFileSync } from 'node:fs';
34
import https from 'node:https';
45
import { Agent, RequestOptions } from 'node:https';
56
import path, { dirname, join } from 'node:path';
67
import { fileURLToPath } from 'node:url';
8+
import { mock } from 'node:test';
79

810
import 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

Comments
 (0)