diff --git a/src/main/__test__/cluster.test.ts b/src/main/__test__/cluster.test.ts index 52e984b195..1fb92ed4bc 100644 --- a/src/main/__test__/cluster.test.ts +++ b/src/main/__test__/cluster.test.ts @@ -49,6 +49,8 @@ describe("create clusters", () => { jest.clearAllMocks() }) + let c: Cluster + beforeEach(() => { const mockOpts = { "minikube-config.yml": JSON.stringify({ @@ -75,6 +77,12 @@ describe("create clusters", () => { } mockFs(mockOpts) jest.spyOn(Kubectl.prototype, "ensureKubectl").mockReturnValue(Promise.resolve(true)) + c = new Cluster({ + id: "foo", + contextName: "minikube", + kubeConfigPath: "minikube-config.yml", + workspace: workspaceStore.currentWorkspaceId + }) }) afterEach(() => { @@ -82,22 +90,18 @@ describe("create clusters", () => { }) it("should be able to create a cluster from a cluster model and apiURL should be decoded", () => { - const c = new Cluster({ - id: "foo", - contextName: "minikube", - kubeConfigPath: "minikube-config.yml", - workspace: workspaceStore.currentWorkspaceId - }) expect(c.apiUrl).toBe("https://192.168.64.3:8443") }) + it("reconnect should not throw if contextHandler is missing", () => { + expect(() => c.reconnect()).not.toThrowError() + }) + + it("disconnect should not throw if contextHandler is missing", () => { + expect(() => c.disconnect()).not.toThrowError() + }) + it("init should not throw if everything is in order", async () => { - const c = new Cluster({ - id: "foo", - contextName: "minikube", - kubeConfigPath: "minikube-config.yml", - workspace: workspaceStore.currentWorkspaceId - }) await c.init(await getFreePort()) expect(logger.info).toBeCalledWith(expect.stringContaining("init success"), { id: "foo", @@ -147,7 +151,15 @@ describe("create clusters", () => { return Promise.resolve({ gitVersion: "1.2.3" }) }) as any) - const c = new Cluster({ + const c = new class extends Cluster { + // only way to mock protected methods, without these we leak promises + protected bindEvents() { + return + } + protected async ensureKubectl() { + return Promise.resolve(true) + } + }({ id: "foo", contextName: "minikube", kubeConfigPath: "minikube-config.yml", diff --git a/src/main/cluster.ts b/src/main/cluster.ts index ecb6e6db9c..a2b71a865b 100644 --- a/src/main/cluster.ts +++ b/src/main/cluster.ts @@ -170,18 +170,22 @@ export class Cluster implements ClusterModel, ClusterState { await this.refreshAllowedResources() this.isAdmin = await this.isClusterAdmin() this.ready = true - this.kubeCtl = new Kubectl(this.version) - this.kubeCtl.ensureKubectl() // download kubectl in background, so it's not blocking dashboard + this.ensureKubectl() } this.activated = true return this.pushState(); } + protected async ensureKubectl() { + this.kubeCtl = new Kubectl(this.version) + return this.kubeCtl.ensureKubectl() // download kubectl in background, so it's not blocking dashboard + } + @action async reconnect() { logger.info(`[CLUSTER]: reconnect`, this.getMeta()); - this.contextHandler.stopServer(); - await this.contextHandler.ensureServer(); + this.contextHandler?.stopServer(); + await this.contextHandler?.ensureServer(); this.disconnected = false; } @@ -189,7 +193,7 @@ export class Cluster implements ClusterModel, ClusterState { disconnect() { logger.info(`[CLUSTER]: disconnect`, this.getMeta()); this.unbindEvents(); - this.contextHandler.stopServer(); + this.contextHandler?.stopServer(); this.disconnected = true; this.online = false; this.accessible = false;