1
0
mirror of https://github.com/lensapp/lens.git synced 2025-05-20 05:10:56 +00:00

fix tests and add try/catch

Signed-off-by: Sebastian Malton <sebastian@malton.name>
This commit is contained in:
Sebastian Malton 2021-04-27 14:17:29 -04:00
parent 847c618a2a
commit 8acf77a42c
2 changed files with 38 additions and 36 deletions

View File

@ -77,7 +77,7 @@ describe("kube helpers", () => {
}); });
}); });
describe("with invalid context object", () => { describe("with invalid context object", () => {
it("it returns an error", () => { it("returns an error", () => {
expect(String(validateKubeConfig(kc, "invalid"))).toEqual( expect(String(validateKubeConfig(kc, "invalid"))).toEqual(
expect.stringContaining("No valid context object provided in kubeconfig for context 'invalid'") expect.stringContaining("No valid context object provided in kubeconfig for context 'invalid'")
); );
@ -85,7 +85,7 @@ describe("kube helpers", () => {
}); });
describe("with invalid cluster object", () => { describe("with invalid cluster object", () => {
it("it returns an error", () => { it("returns an error", () => {
expect(String(validateKubeConfig(kc, "invalidCluster"))).toEqual( expect(String(validateKubeConfig(kc, "invalidCluster"))).toEqual(
expect.stringContaining("No valid cluster object provided in kubeconfig for context 'invalidCluster'") expect.stringContaining("No valid cluster object provided in kubeconfig for context 'invalidCluster'")
); );
@ -93,7 +93,7 @@ describe("kube helpers", () => {
}); });
describe("with invalid user object", () => { describe("with invalid user object", () => {
it("it returns an error", () => { it("returns an error", () => {
expect(String(validateKubeConfig(kc, "invalidUser"))).toEqual( expect(String(validateKubeConfig(kc, "invalidUser"))).toEqual(
expect.stringContaining("No valid user object provided in kubeconfig for context 'invalidUser'") expect.stringContaining("No valid user object provided in kubeconfig for context 'invalidUser'")
); );
@ -101,7 +101,7 @@ describe("kube helpers", () => {
}); });
describe("with invalid exec command", () => { describe("with invalid exec command", () => {
it("it returns an error", () => { it("returns an error", () => {
expect(String(validateKubeConfig(kc, "invalidExec"))).toEqual( expect(String(validateKubeConfig(kc, "invalidExec"))).toEqual(
expect.stringContaining("User Exec command \"foo\" not found on host. Please ensure binary is found in PATH or use absolute path to binary in Kubeconfig") expect.stringContaining("User Exec command \"foo\" not found on host. Please ensure binary is found in PATH or use absolute path to binary in Kubeconfig")
); );

View File

@ -210,6 +210,7 @@ export function getNodeWarningConditions(node: V1Node) {
* Note: This function returns an error instead of throwing it, returning `undefined` if the validation passes * Note: This function returns an error instead of throwing it, returning `undefined` if the validation passes
*/ */
export function validateKubeConfig(config: KubeConfig, contextName: string, validationOpts: KubeConfigValidationOpts = {}): Error | undefined { export function validateKubeConfig(config: KubeConfig, contextName: string, validationOpts: KubeConfigValidationOpts = {}): Error | undefined {
try {
// we only receive a single context, cluster & user object here so lets validate them as this // we only receive a single context, cluster & user object here so lets validate them as this
// will be called when we add a new cluster to Lens // will be called when we add a new cluster to Lens
@ -242,9 +243,10 @@ export function validateKubeConfig(config: KubeConfig, contextName: string, vali
// validate the exec struct in the user object, start with the command field // validate the exec struct in the user object, start with the command field
if (!commandExists.sync(execCommand)) { if (!commandExists.sync(execCommand)) {
logger.debug(`validateKubeConfig: exec command ${String(execCommand)} in kubeconfig ${contextName} not found`);
return new ExecValidationNotFoundError(execCommand, isAbsolute); return new ExecValidationNotFoundError(execCommand, isAbsolute);
} }
} }
} catch (error) {
return error;
}
} }