Implement KubeApi.patch (#4325)

* implement KubeApi.patch

Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com>

* cleanup tests

Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com>

* keep it backward compatible

Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com>
This commit is contained in:
Jari Kolehmainen authored and GitHub committed 2021-11-12 11:15:33 +02:00
1 parent a86b306a48
commit 23d5d40d62
4 files changed
+125 -9

No files matched your search

+72 -5
View File
@@ -19,11 +19,19 @@
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
import { Pod, PodsApi } from "../endpoints/pods.api";
import type { Request } from "node-fetch";
import { forRemoteCluster, KubeApi } from "../kube-api";
import { KubeJsonApi } from "../kube-json-api";
import { KubeObject } from "../kube-object";
class TestKubeObject extends KubeObject {
static kind = "Pod";
static namespaced = true;
static apiBase = "/api/v1/pods";
}
class TestKubeApi extends KubeApi<TestKubeObject> {}
describe("forRemoteCluster", () => {
it("builds api client for KubeObject", async () => {
const api = forRemoteCluster({
@@ -33,7 +41,7 @@ describe("forRemoteCluster", () => {
user: {
token: "daa",
},
}, Pod);
}, TestKubeObject);
expect(api).toBeInstanceOf(KubeApi);
});
@@ -46,9 +54,9 @@ describe("forRemoteCluster", () => {
user: {
token: "daa",
},
}, Pod, PodsApi);
}, TestKubeObject, TestKubeApi);
expect(api).toBeInstanceOf(PodsApi);
expect(api).toBeInstanceOf(TestKubeApi);
});
it("calls right api endpoint", async () => {
@@ -59,7 +67,7 @@ describe("forRemoteCluster", () => {
user: {
token: "daa",
},
}, Pod);
}, TestKubeObject);
(fetch as any).mockResponse(async (request: any) => {
expect(request.url).toEqual("https://127.0.0.1:6443/api/v1/pods");
@@ -167,4 +175,63 @@ describe("KubeApi", () => {
expect(kubeApi.apiPrefix).toEqual("/apis");
expect(kubeApi.apiGroup).toEqual("extensions");
});
describe("patch", () => {
let api: TestKubeApi;
beforeEach(() => {
api = new TestKubeApi({
request,
objectConstructor: TestKubeObject,
});
});
it("sends strategic patch by default", async () => {
expect.hasAssertions();
(fetch as any).mockResponse(async (request: Request) => {
expect(request.method).toEqual("PATCH");
expect(request.headers.get("content-type")).toMatch("strategic-merge-patch");
expect(request.body.toString()).toEqual(JSON.stringify({ spec: { replicas: 2 }}));
return {};
});
await api.patch({ name: "test", namespace: "default" }, {
spec: { replicas: 2 },
});
});
it("allows to use merge patch", async () => {
expect.hasAssertions();
(fetch as any).mockResponse(async (request: Request) => {
expect(request.method).toEqual("PATCH");
expect(request.headers.get("content-type")).toMatch("merge-patch");
expect(request.body.toString()).toEqual(JSON.stringify({ spec: { replicas: 2 }}));
return {};
});
await api.patch({ name: "test", namespace: "default" }, {
spec: { replicas: 2 },
}, "merge");
});
it("allows to use json patch", async () => {
expect.hasAssertions();
(fetch as any).mockResponse(async (request: Request) => {
expect(request.method).toEqual("PATCH");
expect(request.headers.get("content-type")).toMatch("json-patch");
expect(request.body.toString()).toEqual(JSON.stringify([{ op: "replace", path: "/spec/replicas", value: 2 }]));
return {};
});
await api.patch({ name: "test", namespace: "default" }, [
{ op: "replace", path: "/spec/replicas", value: 2 },
], "json");
});
});
});