diff --git a/src/common/k8s-api/__tests__/kube-api.test.ts b/src/common/k8s-api/__tests__/kube-api.test.ts index 1eb0199f89..1e1bac05fe 100644 --- a/src/common/k8s-api/__tests__/kube-api.test.ts +++ b/src/common/k8s-api/__tests__/kube-api.test.ts @@ -20,12 +20,22 @@ */ import { KubeApi } from "../kube-api"; +import { KubeJsonApi } from "../kube-json-api"; import { KubeObject } from "../kube-object"; describe("KubeApi", () => { + let request: KubeJsonApi; + + beforeEach(() => { + request = new KubeJsonApi({ + serverAddress: `http://127.0.0.1:9999`, + apiBase: "/api-kube" + }); + }); + it("uses url from apiBase if apiBase contains the resource", async () => { (fetch as any).mockResponse(async (request: any) => { - if (request.url === "/api-kube/apis/networking.k8s.io/v1") { + if (request.url === "http://127.0.0.1:9999/api-kube/apis/networking.k8s.io/v1") { return { body: JSON.stringify({ resources: [{ @@ -33,7 +43,7 @@ describe("KubeApi", () => { }] as any[] }) }; - } else if (request.url === "/api-kube/apis/extensions/v1beta1") { + } else if (request.url === "http://127.0.0.1:9999/api-kube/apis/extensions/v1beta1") { // Even if the old API contains ingresses, KubeApi should prefer the apiBase url return { body: JSON.stringify({ @@ -54,6 +64,7 @@ describe("KubeApi", () => { const apiBase = "/apis/networking.k8s.io/v1/ingresses"; const fallbackApiBase = "/apis/extensions/v1beta1/ingresses"; const kubeApi = new KubeApi({ + request, objectConstructor: KubeObject, apiBase, fallbackApiBases: [fallbackApiBase], @@ -67,13 +78,13 @@ describe("KubeApi", () => { it("uses url from fallbackApiBases if apiBase lacks the resource", async () => { (fetch as any).mockResponse(async (request: any) => { - if (request.url === "/api-kube/apis/networking.k8s.io/v1") { + if (request.url === "http://127.0.0.1:9999/api-kube/apis/networking.k8s.io/v1") { return { body: JSON.stringify({ resources: [] as any[] }) }; - } else if (request.url === "/api-kube/apis/extensions/v1beta1") { + } else if (request.url === "http://127.0.0.1:9999/api-kube/apis/extensions/v1beta1") { return { body: JSON.stringify({ resources: [{ @@ -93,6 +104,7 @@ describe("KubeApi", () => { const apiBase = "apis/networking.k8s.io/v1/ingresses"; const fallbackApiBase = "/apis/extensions/v1beta1/ingresses"; const kubeApi = new KubeApi({ + request, objectConstructor: KubeObject, apiBase, fallbackApiBases: [fallbackApiBase], diff --git a/src/common/k8s-api/kube-api.ts b/src/common/k8s-api/kube-api.ts index 40b023e267..22c7dca9c8 100644 --- a/src/common/k8s-api/kube-api.ts +++ b/src/common/k8s-api/kube-api.ts @@ -23,7 +23,7 @@ import merge from "lodash/merge"; import { stringify } from "querystring"; -import { apiKubePrefix, isDevelopment, isTestEnv } from "../../common/vars"; +import { apiKubePrefix, isDevelopment } from "../../common/vars"; import logger from "../../main/logger"; import { apiManager } from "./api-manager"; import { apiKube } from "./index"; @@ -202,18 +202,9 @@ export class KubeApi { } } catch (error) { // Exception is ignored as we can try the next url - console.error(error); } } - // Avoid throwing in tests - if (isTestEnv) { - return { - apiPrefix: this.apiPrefix, - apiGroup: this.apiGroup - }; - } - throw new Error(`Can't find working API for the Kubernetes resource ${this.apiResource}`); }