From e24267e3d0289a32b8ec6389b561e91fb4528865 Mon Sep 17 00:00:00 2001 From: Sebastian Malton Date: Wed, 31 May 2023 15:08:49 -0400 Subject: [PATCH] chore: Support running smoke tests against non-minikube clusters Signed-off-by: Sebastian Malton --- .../__tests__/cluster-pages.tests.ts | 2 +- open-lens/integration/helpers/minikube.ts | 28 +++++++++++++++++++ open-lens/integration/helpers/utils.ts | 28 ++++++++++++++----- 3 files changed, 50 insertions(+), 8 deletions(-) diff --git a/open-lens/integration/__tests__/cluster-pages.tests.ts b/open-lens/integration/__tests__/cluster-pages.tests.ts index 30bbc79ed9..2a3c1a8584 100644 --- a/open-lens/integration/__tests__/cluster-pages.tests.ts +++ b/open-lens/integration/__tests__/cluster-pages.tests.ts @@ -27,7 +27,7 @@ describeIf(minikubeReady(TEST_NAMESPACE))("Minikube based tests", () => { ({ window, cleanup } = await utils.start()); await utils.clickWelcomeButton(window); - frame = await utils.launchMinikubeClusterFromCatalog(window); + frame = await utils.launchClusterFromCatalog(window); }, 10 * 60 * 1000); afterEach(async () => { diff --git a/open-lens/integration/helpers/minikube.ts b/open-lens/integration/helpers/minikube.ts index aa87d9e5a9..1516e57062 100644 --- a/open-lens/integration/helpers/minikube.ts +++ b/open-lens/integration/helpers/minikube.ts @@ -3,8 +3,36 @@ * Licensed under MIT License. See LICENSE in root directory for more information. */ import { spawnSync } from "child_process"; +import { clusterName, kubeConfigPath } from "./utils"; export function minikubeReady(testNamespace: string): boolean { + if (clusterName !== "minikube") { + console.log("Not running against minikube"); + + { + const { status } = spawnSync(`kubectl --kubeconfig "${kubeConfigPath}" get namespace ${testNamespace}`, { shell: true }); + + if (status === 0) { + console.warn(`Removing existing ${testNamespace} namespace`); + + const { status, stdout, stderr } = spawnSync( + `kubectl --kubeconfig "${kubeConfigPath}" delete namespace ${testNamespace}`, + { shell: true }, + ); + + if (status !== 0) { + console.warn(`Error removing ${testNamespace} namespace: ${stderr.toString()}`); + + return false; + } + + console.log(stdout.toString()); + } + } + + return true; + } + // determine if minikube is running { const { status } = spawnSync("minikube status", { shell: true }); diff --git a/open-lens/integration/helpers/utils.ts b/open-lens/integration/helpers/utils.ts index af7820136b..4ffa315131 100644 --- a/open-lens/integration/helpers/utils.ts +++ b/open-lens/integration/helpers/utils.ts @@ -3,7 +3,7 @@ * Licensed under MIT License. See LICENSE in root directory for more information. */ import { createHash } from "crypto"; -import { mkdirp, remove } from "fs-extra"; +import { mkdirp, remove, writeJson } from "fs-extra"; import * as os from "os"; import * as path from "path"; import * as uuid from "uuid"; @@ -56,6 +56,9 @@ async function getMainWindow(app: ElectronApplication, timeout = 50_000): Promis }); } +export const kubeConfigPath = process.env.LENS_INTEGRATION_TEST_KUBECONFIG || path.join(os.homedir(), ".kube", "config"); +export const clusterName = process.env.LENS_INTEGRATION_TEST_CLUSTER_NAME || "minikube"; + async function attemptStart() { const CICD = path.join(os.tmpdir(), "lens-integration-testing", uuid.v4()); @@ -63,6 +66,17 @@ async function attemptStart() { await remove(CICD).catch(noop); await mkdirp(CICD); + if (process.env.LENS_INTEGRATION_TEST_KUBECONFIG) { + await mkdirp(path.join(CICD, "OpenLens")); + await writeJson(path.join(CICD, "OpenLens", "lens-user-store.json"), { + preferences: { + syncKubeconfigEntries: [{ + filePath: kubeConfigPath, + }] + } + }); + } + const app = await electron.launch({ args: ["--integration-testing"], // this argument turns off the blocking of quit executablePath: appPaths[process.platform], @@ -109,22 +123,22 @@ export async function clickWelcomeButton(window: Page) { await window.click("[data-testid=welcome-menu-container] li a"); } -function minikubeEntityId() { - return createHash("md5").update(`${path.join(os.homedir(), ".kube", "config")}:minikube`).digest("hex"); +function entityId() { + return createHash("md5").update(`${kubeConfigPath}:${clusterName}`).digest("hex"); } /** * From the catalog, click the minikube entity and wait for it to connect, returning its frame */ -export async function launchMinikubeClusterFromCatalog(window: Page): Promise { - await window.click("div.TableCell >> text='minikube'"); +export async function launchClusterFromCatalog(window: Page): Promise { + await window.click(`div.TableCell >> text='${clusterName}'`); - const minikubeFrame = await window.waitForSelector(`#cluster-frame-${minikubeEntityId()}`); + const minikubeFrame = await window.waitForSelector(`#cluster-frame-${entityId()}`); const frame = await minikubeFrame.contentFrame(); if (!frame) { - throw new Error("No iframe for minikube found"); + throw new Error(`No iframe for "${clusterName}" found`); } await frame.waitForSelector("[data-testid=cluster-sidebar]");