Skip to content

Commit

Permalink
Merge pull request #2288 from brendandburns/coverage
Browse files Browse the repository at this point in the history
Improve coverage and testability of config.ts
  • Loading branch information
k8s-ci-robot authored Mar 5, 2025
2 parents 8c53df2 + 3b44c17 commit e27eb26
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 5 deletions.
14 changes: 9 additions & 5 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,11 @@ export class KubeConfig implements SecurityAuthentication {
this.contexts.push(ctx);
}

public loadFromDefault(opts?: Partial<ConfigOptions>, contextFromStartingConfig: boolean = false): void {
public loadFromDefault(
opts?: Partial<ConfigOptions>,
contextFromStartingConfig: boolean = false,
platform: string = process.platform,
): void {
if (process.env.KUBECONFIG && process.env.KUBECONFIG.length > 0) {
const files = process.env.KUBECONFIG.split(path.delimiter).filter((filename: string) => filename);
this.loadFromFile(files[0], opts);
Expand All @@ -405,15 +409,15 @@ export class KubeConfig implements SecurityAuthentication {
}
return;
}
const home = findHomeDir();
const home = findHomeDir(platform);
if (home) {
const config = path.join(home, '.kube', 'config');
if (fileExists(config)) {
this.loadFromFile(config, opts);
return;
}
}
if (process.platform === 'win32') {
if (platform === 'win32') {
try {
const envKubeconfigPathResult = child_process.spawnSync('wsl.exe', [
'bash',
Expand Down Expand Up @@ -628,8 +632,8 @@ function dropDuplicatesAndNils(a: string[]): string[] {
}

// Only public for testing.
export function findHomeDir(): string | null {
if (process.platform !== 'win32') {
export function findHomeDir(platform: string = process.platform): string | null {
if (platform !== 'win32') {
if (process.env.HOME) {
try {
fs.accessSync(process.env.HOME);
Expand Down
60 changes: 60 additions & 0 deletions src/config_test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { deepEqual, deepStrictEqual, notStrictEqual, rejects, strictEqual, throws } from 'node:assert';
import child_process from 'node:child_process';
import { readFileSync } from 'node:fs';
import https from 'node:https';
import { Agent, RequestOptions } from 'node:https';
import path, { dirname, join } from 'node:path';
import { fileURLToPath } from 'node:url';
import { mock } from 'node:test';

import mockfs from 'mock-fs';

Expand Down Expand Up @@ -301,8 +303,10 @@ describe('KubeConfig', () => {
const kc = new KubeConfig();
kc.loadFromFile(kcTlsServerNameFileName);

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

const expectedAgent = new https.Agent({
ca: Buffer.from('CADATA2', 'utf-8'),
Expand All @@ -322,6 +326,7 @@ describe('KubeConfig', () => {
};

assertRequestOptionsEqual(opts, expectedOptions);
strictEqual((requestContext.getAgent()! as any).options.servername, 'kube.example2.com');
});
it('should apply cert configs', async () => {
const kc = new KubeConfig();
Expand Down Expand Up @@ -1630,5 +1635,60 @@ describe('KubeConfig', () => {
strictEqual(inputData!.toString(), data);
mockfs.restore();
});
it('should try to load from WSL on Windows with wsl.exe not working', () => {
const kc = new KubeConfig();
const commands: { command: string; args: string[] }[] = [];
mock.method(child_process, 'spawnSync', (cmd: string, args: string[]) => {
commands.push({ command: cmd, args });
return { status: 1, stderr: 'some error' };
});
kc.loadFromDefault(undefined, false, 'win32');
strictEqual(commands.length, 2);
for (let i = 0; i < commands.length; i++) {
strictEqual(commands[i].command, 'wsl.exe');
}
});
it('should try to load from WSL on Windows with $KUBECONFIG', () => {
const kc = new KubeConfig();
const test_path = 'C:\\Users\\user\\.kube\\config';
const configData = readFileSync(kcFileName);
const commands: { command: string; args: string[] }[] = [];
const results: { status: number; stderr: string; stdout: string }[] = [
{ status: 0, stderr: '', stdout: test_path },
{ status: 0, stderr: '', stdout: configData.toString() },
];
let ix = 0;
mock.method(child_process, 'spawnSync', (cmd: string, args: string[]) => {
commands.push({ command: cmd, args });
return results[ix++];
});
kc.loadFromDefault(undefined, false, 'win32');
strictEqual(commands.length, 2);
for (let i = 0; i < commands.length; i++) {
strictEqual(commands[i].command, 'wsl.exe');
}
validateFileLoad(kc);
});
it('should try to load from WSL on Windows without $KUBECONFIG', () => {
const kc = new KubeConfig();
const configData = readFileSync(kcFileName);
const commands: { command: string; args: string[] }[] = [];
const results: { status: number; stderr: string; stdout: string }[] = [
{ status: 1, stderr: 'Some Error', stdout: '' },
{ status: 0, stderr: '', stdout: configData.toString() },
{ status: 0, stderr: '', stdout: 'C:\\wsldata\\.kube' },
];
let ix = 0;
mock.method(child_process, 'spawnSync', (cmd: string, args: string[]) => {
commands.push({ command: cmd, args });
return results[ix++];
});
kc.loadFromDefault(undefined, false, 'win32');
strictEqual(commands.length, 3);
for (let i = 0; i < commands.length; i++) {
strictEqual(commands[i].command, 'wsl.exe');
}
validateFileLoad(kc);
});
});
});

0 comments on commit e27eb26

Please sign in to comment.