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

Fix TypeError: Cannot read property 'stopServer' of undefined (#1440)

Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com>
This commit is contained in:
Jari Kolehmainen 2020-11-19 13:48:43 +02:00 committed by GitHub
parent 5aaacb21f9
commit 5589a99e7e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 34 additions and 18 deletions

View File

@ -49,6 +49,8 @@ describe("create clusters", () => {
jest.clearAllMocks() jest.clearAllMocks()
}) })
let c: Cluster
beforeEach(() => { beforeEach(() => {
const mockOpts = { const mockOpts = {
"minikube-config.yml": JSON.stringify({ "minikube-config.yml": JSON.stringify({
@ -75,6 +77,12 @@ describe("create clusters", () => {
} }
mockFs(mockOpts) mockFs(mockOpts)
jest.spyOn(Kubectl.prototype, "ensureKubectl").mockReturnValue(Promise.resolve(true)) jest.spyOn(Kubectl.prototype, "ensureKubectl").mockReturnValue(Promise.resolve(true))
c = new Cluster({
id: "foo",
contextName: "minikube",
kubeConfigPath: "minikube-config.yml",
workspace: workspaceStore.currentWorkspaceId
})
}) })
afterEach(() => { 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", () => { 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") expect(c.apiUrl).toBe("https://192.168.64.3:8443")
}) })
it("init should not throw if everything is in order", async () => { it("reconnect should not throw if contextHandler is missing", () => {
const c = new Cluster({ expect(() => c.reconnect()).not.toThrowError()
id: "foo",
contextName: "minikube",
kubeConfigPath: "minikube-config.yml",
workspace: workspaceStore.currentWorkspaceId
}) })
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 () => {
await c.init(await getFreePort()) await c.init(await getFreePort())
expect(logger.info).toBeCalledWith(expect.stringContaining("init success"), { expect(logger.info).toBeCalledWith(expect.stringContaining("init success"), {
id: "foo", id: "foo",
@ -147,7 +151,15 @@ describe("create clusters", () => {
return Promise.resolve({ gitVersion: "1.2.3" }) return Promise.resolve({ gitVersion: "1.2.3" })
}) as any) }) 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", id: "foo",
contextName: "minikube", contextName: "minikube",
kubeConfigPath: "minikube-config.yml", kubeConfigPath: "minikube-config.yml",

View File

@ -170,18 +170,22 @@ export class Cluster implements ClusterModel, ClusterState {
await this.refreshAllowedResources() await this.refreshAllowedResources()
this.isAdmin = await this.isClusterAdmin() this.isAdmin = await this.isClusterAdmin()
this.ready = true this.ready = true
this.kubeCtl = new Kubectl(this.version) this.ensureKubectl()
this.kubeCtl.ensureKubectl() // download kubectl in background, so it's not blocking dashboard
} }
this.activated = true this.activated = true
return this.pushState(); 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 @action
async reconnect() { async reconnect() {
logger.info(`[CLUSTER]: reconnect`, this.getMeta()); logger.info(`[CLUSTER]: reconnect`, this.getMeta());
this.contextHandler.stopServer(); this.contextHandler?.stopServer();
await this.contextHandler.ensureServer(); await this.contextHandler?.ensureServer();
this.disconnected = false; this.disconnected = false;
} }
@ -189,7 +193,7 @@ export class Cluster implements ClusterModel, ClusterState {
disconnect() { disconnect() {
logger.info(`[CLUSTER]: disconnect`, this.getMeta()); logger.info(`[CLUSTER]: disconnect`, this.getMeta());
this.unbindEvents(); this.unbindEvents();
this.contextHandler.stopServer(); this.contextHandler?.stopServer();
this.disconnected = true; this.disconnected = true;
this.online = false; this.online = false;
this.accessible = false; this.accessible = false;