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", () => {
it("it returns an error", () => {
it("returns an error", () => {
expect(String(validateKubeConfig(kc, "invalid"))).toEqual(
expect.stringContaining("No valid context object provided in kubeconfig for context 'invalid'")
);
@ -85,7 +85,7 @@ describe("kube helpers", () => {
});
describe("with invalid cluster object", () => {
it("it returns an error", () => {
it("returns an error", () => {
expect(String(validateKubeConfig(kc, "invalidCluster"))).toEqual(
expect.stringContaining("No valid cluster object provided in kubeconfig for context 'invalidCluster'")
);
@ -93,7 +93,7 @@ describe("kube helpers", () => {
});
describe("with invalid user object", () => {
it("it returns an error", () => {
it("returns an error", () => {
expect(String(validateKubeConfig(kc, "invalidUser"))).toEqual(
expect.stringContaining("No valid user object provided in kubeconfig for context 'invalidUser'")
);
@ -101,7 +101,7 @@ describe("kube helpers", () => {
});
describe("with invalid exec command", () => {
it("it returns an error", () => {
it("returns an error", () => {
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")
);

View File

@ -210,41 +210,43 @@ export function getNodeWarningConditions(node: V1Node) {
* 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 {
// 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
try {
// 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
const { validateUser = true, validateCluster = true, validateExec = true } = validationOpts;
const { validateUser = true, validateCluster = true, validateExec = true } = validationOpts;
const contextObject = config.getContextObject(contextName);
const contextObject = config.getContextObject(contextName);
// Validate the Context Object
if (!contextObject) {
return new Error(`No valid context object provided in kubeconfig for context '${contextName}'`);
}
// Validate the Cluster Object
if (validateCluster && !config.getCluster(contextObject.cluster)) {
return new Error(`No valid cluster object provided in kubeconfig for context '${contextName}'`);
}
const user = config.getUser(contextObject.user);
// Validate the User Object
if (validateUser && !user) {
return new Error(`No valid user object provided in kubeconfig for context '${contextName}'`);
}
// Validate exec command if present
if (validateExec && user?.exec) {
const execCommand = user.exec["command"];
// check if the command is absolute or not
const isAbsolute = path.isAbsolute(execCommand);
// validate the exec struct in the user object, start with the command field
if (!commandExists.sync(execCommand)) {
logger.debug(`validateKubeConfig: exec command ${String(execCommand)} in kubeconfig ${contextName} not found`);
return new ExecValidationNotFoundError(execCommand, isAbsolute);
// Validate the Context Object
if (!contextObject) {
return new Error(`No valid context object provided in kubeconfig for context '${contextName}'`);
}
// Validate the Cluster Object
if (validateCluster && !config.getCluster(contextObject.cluster)) {
return new Error(`No valid cluster object provided in kubeconfig for context '${contextName}'`);
}
const user = config.getUser(contextObject.user);
// Validate the User Object
if (validateUser && !user) {
return new Error(`No valid user object provided in kubeconfig for context '${contextName}'`);
}
// Validate exec command if present
if (validateExec && user?.exec) {
const execCommand = user.exec["command"];
// check if the command is absolute or not
const isAbsolute = path.isAbsolute(execCommand);
// validate the exec struct in the user object, start with the command field
if (!commandExists.sync(execCommand)) {
return new ExecValidationNotFoundError(execCommand, isAbsolute);
}
}
} catch (error) {
return error;
}
}