kubernetes-fluent-client 3.1.1 → 3.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (60) hide show
  1. package/.husky/pre-commit +1 -0
  2. package/.lintstagedrc.json +5 -0
  3. package/dist/fluent/types.d.ts +14 -0
  4. package/dist/fluent/types.d.ts.map +1 -1
  5. package/dist/fluent/watch.d.ts.map +1 -1
  6. package/dist/fluent/watch.js +121 -71
  7. package/package.json +8 -5
  8. package/src/fluent/types.ts +16 -0
  9. package/src/fluent/watch.ts +136 -81
  10. package/dist/fetch.test.d.ts +0 -2
  11. package/dist/fetch.test.d.ts.map +0 -1
  12. package/dist/fetch.test.js +0 -97
  13. package/dist/fileSystem.test.d.ts +0 -2
  14. package/dist/fileSystem.test.d.ts.map +0 -1
  15. package/dist/fileSystem.test.js +0 -75
  16. package/dist/fluent/http2-watch.spec.d.ts +0 -2
  17. package/dist/fluent/http2-watch.spec.d.ts.map +0 -1
  18. package/dist/fluent/http2-watch.spec.js +0 -284
  19. package/dist/fluent/index.test.d.ts +0 -2
  20. package/dist/fluent/index.test.d.ts.map +0 -1
  21. package/dist/fluent/index.test.js +0 -193
  22. package/dist/fluent/utils.test.d.ts +0 -2
  23. package/dist/fluent/utils.test.d.ts.map +0 -1
  24. package/dist/fluent/utils.test.js +0 -215
  25. package/dist/fluent/watch.spec.d.ts +0 -2
  26. package/dist/fluent/watch.spec.d.ts.map +0 -1
  27. package/dist/fluent/watch.spec.js +0 -261
  28. package/dist/generate.test.d.ts +0 -2
  29. package/dist/generate.test.d.ts.map +0 -1
  30. package/dist/generate.test.js +0 -320
  31. package/dist/helpers.test.d.ts +0 -2
  32. package/dist/helpers.test.d.ts.map +0 -1
  33. package/dist/helpers.test.js +0 -37
  34. package/dist/kinds.test.d.ts +0 -2
  35. package/dist/kinds.test.d.ts.map +0 -1
  36. package/dist/kinds.test.js +0 -155
  37. package/dist/postProcessing.test.d.ts +0 -2
  38. package/dist/postProcessing.test.d.ts.map +0 -1
  39. package/dist/postProcessing.test.js +0 -550
  40. package/e2e/cli.e2e.test.ts +0 -127
  41. package/e2e/crds/policyreports.default.expected/policyreport-v1alpha1.ts +0 -332
  42. package/e2e/crds/policyreports.default.expected/policyreport-v1alpha2.ts +0 -360
  43. package/e2e/crds/policyreports.default.expected/policyreport-v1beta1.ts +0 -360
  44. package/e2e/crds/policyreports.no.post.expected/policyreport-v1alpha1.ts +0 -331
  45. package/e2e/crds/policyreports.no.post.expected/policyreport-v1alpha2.ts +0 -360
  46. package/e2e/crds/policyreports.no.post.expected/policyreport-v1beta1.ts +0 -360
  47. package/e2e/crds/test.yaml/policyreports.test.yaml +0 -1008
  48. package/e2e/crds/test.yaml/uds-podmonitors.test.yaml +0 -1245
  49. package/e2e/crds/uds-podmonitors.default.expected/podmonitor-v1.ts +0 -1333
  50. package/e2e/crds/uds-podmonitors.no.post.expected/podmonitor-v1.ts +0 -1360
  51. package/src/fetch.test.ts +0 -115
  52. package/src/fileSystem.test.ts +0 -67
  53. package/src/fluent/http2-watch.spec.ts +0 -335
  54. package/src/fluent/index.test.ts +0 -268
  55. package/src/fluent/utils.test.ts +0 -276
  56. package/src/fluent/watch.spec.ts +0 -299
  57. package/src/generate.test.ts +0 -399
  58. package/src/helpers.test.ts +0 -42
  59. package/src/kinds.test.ts +0 -166
  60. package/src/postProcessing.test.ts +0 -742
@@ -1,97 +0,0 @@
1
- "use strict";
2
- // SPDX-License-Identifier: Apache-2.0
3
- // SPDX-FileCopyrightText: 2023-Present The Kubernetes Fluent Client Authors
4
- var __importDefault = (this && this.__importDefault) || function (mod) {
5
- return (mod && mod.__esModule) ? mod : { "default": mod };
6
- };
7
- Object.defineProperty(exports, "__esModule", { value: true });
8
- const globals_1 = require("@jest/globals");
9
- const http_status_codes_1 = require("http-status-codes");
10
- const nock_1 = __importDefault(require("nock"));
11
- const fetch_1 = require("./fetch");
12
- (0, globals_1.beforeEach)(() => {
13
- (0, nock_1.default)("https://jsonplaceholder.typicode.com")
14
- .get("/todos/1")
15
- .reply(200, {
16
- userId: 1,
17
- id: 1,
18
- title: "Example title",
19
- completed: false,
20
- })
21
- .post("/todos", {
22
- title: "test todo",
23
- userId: 1,
24
- completed: false,
25
- })
26
- .reply(200, (uri, requestBody) => requestBody)
27
- .get("/todos/empty-null")
28
- .reply(200, undefined)
29
- .get("/todos/empty-string")
30
- .reply(200, "")
31
- .get("/todos/empty-object")
32
- .reply(200, {})
33
- .get("/todos/invalid")
34
- .replyWithError("Something bad happened");
35
- });
36
- (0, globals_1.test)("fetch: should return without type data", async () => {
37
- const url = "https://jsonplaceholder.typicode.com/todos/1";
38
- const { data, ok } = await (0, fetch_1.fetch)(url);
39
- (0, globals_1.expect)(ok).toBe(true);
40
- (0, globals_1.expect)(data["title"]).toBe("Example title");
41
- });
42
- (0, globals_1.test)("fetch: should return parsed JSON response as a specific type", async () => {
43
- const url = "https://jsonplaceholder.typicode.com/todos/1";
44
- const { data, ok } = await (0, fetch_1.fetch)(url);
45
- (0, globals_1.expect)(ok).toBe(true);
46
- (0, globals_1.expect)(data.id).toBe(1);
47
- (0, globals_1.expect)(typeof data.title).toBe("string");
48
- (0, globals_1.expect)(typeof data.completed).toBe("boolean");
49
- });
50
- (0, globals_1.test)("fetch: should handle additional request options", async () => {
51
- const url = "https://jsonplaceholder.typicode.com/todos";
52
- const requestOptions = {
53
- method: "POST",
54
- body: JSON.stringify({
55
- title: "test todo",
56
- userId: 1,
57
- completed: false,
58
- }),
59
- headers: {
60
- "Content-type": "application/json; charset=UTF-8",
61
- },
62
- };
63
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
64
- const { data, ok } = await (0, fetch_1.fetch)(url, requestOptions);
65
- (0, globals_1.expect)(ok).toBe(true);
66
- (0, globals_1.expect)(data["title"]).toBe("test todo");
67
- (0, globals_1.expect)(data["userId"]).toBe(1);
68
- (0, globals_1.expect)(data["completed"]).toBe(false);
69
- });
70
- (0, globals_1.test)("fetch: should handle empty (null) responses", async () => {
71
- const url = "https://jsonplaceholder.typicode.com/todos/empty-null";
72
- const resp = await (0, fetch_1.fetch)(url);
73
- (0, globals_1.expect)(resp.data).toBe("");
74
- (0, globals_1.expect)(resp.ok).toBe(true);
75
- (0, globals_1.expect)(resp.status).toBe(http_status_codes_1.StatusCodes.OK);
76
- });
77
- (0, globals_1.test)("fetch: should handle empty (string) responses", async () => {
78
- const url = "https://jsonplaceholder.typicode.com/todos/empty-string";
79
- const resp = await (0, fetch_1.fetch)(url);
80
- (0, globals_1.expect)(resp.data).toBe("");
81
- (0, globals_1.expect)(resp.ok).toBe(true);
82
- (0, globals_1.expect)(resp.status).toBe(http_status_codes_1.StatusCodes.OK);
83
- });
84
- (0, globals_1.test)("fetch: should handle empty (object) responses", async () => {
85
- const url = "https://jsonplaceholder.typicode.com/todos/empty-object";
86
- const resp = await (0, fetch_1.fetch)(url);
87
- (0, globals_1.expect)(resp.data).toEqual({});
88
- (0, globals_1.expect)(resp.ok).toBe(true);
89
- (0, globals_1.expect)(resp.status).toBe(http_status_codes_1.StatusCodes.OK);
90
- });
91
- (0, globals_1.test)("fetch: should handle failed requests without throwing an error", async () => {
92
- const url = "https://jsonplaceholder.typicode.com/todos/invalid";
93
- const resp = await (0, fetch_1.fetch)(url);
94
- (0, globals_1.expect)(resp.data).toBe(undefined);
95
- (0, globals_1.expect)(resp.ok).toBe(false);
96
- (0, globals_1.expect)(resp.status).toBe(http_status_codes_1.StatusCodes.BAD_REQUEST);
97
- });
@@ -1,2 +0,0 @@
1
- export {};
2
- //# sourceMappingURL=fileSystem.test.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"fileSystem.test.d.ts","sourceRoot":"","sources":["../src/fileSystem.test.ts"],"names":[],"mappings":""}
@@ -1,75 +0,0 @@
1
- "use strict";
2
- // SPDX-License-Identifier: Apache-2.0
3
- // SPDX-FileCopyrightText: 2023-Present The Kubernetes Fluent Client Authors
4
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
5
- if (k2 === undefined) k2 = k;
6
- var desc = Object.getOwnPropertyDescriptor(m, k);
7
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
8
- desc = { enumerable: true, get: function() { return m[k]; } };
9
- }
10
- Object.defineProperty(o, k2, desc);
11
- }) : (function(o, m, k, k2) {
12
- if (k2 === undefined) k2 = k;
13
- o[k2] = m[k];
14
- }));
15
- var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
16
- Object.defineProperty(o, "default", { enumerable: true, value: v });
17
- }) : function(o, v) {
18
- o["default"] = v;
19
- });
20
- var __importStar = (this && this.__importStar) || function (mod) {
21
- if (mod && mod.__esModule) return mod;
22
- var result = {};
23
- if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
24
- __setModuleDefault(result, mod);
25
- return result;
26
- };
27
- Object.defineProperty(exports, "__esModule", { value: true });
28
- const fs = __importStar(require("fs"));
29
- const fileSystem_1 = require("./fileSystem");
30
- const globals_1 = require("@jest/globals");
31
- // Mock the fs module
32
- globals_1.jest.mock("fs");
33
- (0, globals_1.describe)("NodeFileSystem", () => {
34
- let nodeFileSystem;
35
- (0, globals_1.beforeEach)(() => {
36
- nodeFileSystem = new fileSystem_1.NodeFileSystem();
37
- globals_1.jest.clearAllMocks(); // Clear all mocks before each test
38
- });
39
- (0, globals_1.describe)("readFile", () => {
40
- (0, globals_1.test)("should call fs.readFileSync with correct arguments", () => {
41
- const mockFilePath = "test-file.txt";
42
- const mockFileContent = "This is a test file";
43
- // Mock the fs.readFileSync method to return the mock file content
44
- fs.readFileSync.mockReturnValue(mockFileContent);
45
- const result = nodeFileSystem.readFile(mockFilePath);
46
- // Assert that fs.readFileSync was called with the correct file path and encoding
47
- (0, globals_1.expect)(fs.readFileSync).toHaveBeenCalledWith(mockFilePath, "utf8");
48
- // Assert that the returned content matches the mock file content
49
- (0, globals_1.expect)(result).toBe(mockFileContent);
50
- });
51
- });
52
- (0, globals_1.describe)("writeFile", () => {
53
- (0, globals_1.test)("should call fs.writeFileSync with correct arguments", () => {
54
- const mockFilePath = "test-file.txt";
55
- const mockFileContent = "This is a test file";
56
- // Call the writeFile method
57
- nodeFileSystem.writeFile(mockFilePath, mockFileContent);
58
- // Assert that fs.writeFileSync was called with the correct arguments
59
- (0, globals_1.expect)(fs.writeFileSync).toHaveBeenCalledWith(mockFilePath, mockFileContent, "utf8");
60
- });
61
- });
62
- (0, globals_1.describe)("readdirSync", () => {
63
- (0, globals_1.test)("should call fs.readdirSync with correct arguments and return file list", () => {
64
- const mockDirectoryPath = "test-directory";
65
- const mockFileList = ["file1.txt", "file2.txt"];
66
- // Mock the fs.readdirSync method to return the mock file list
67
- fs.readdirSync.mockReturnValue(mockFileList);
68
- const result = nodeFileSystem.readdirSync(mockDirectoryPath);
69
- // Assert that fs.readdirSync was called with the correct directory path
70
- (0, globals_1.expect)(fs.readdirSync).toHaveBeenCalledWith(mockDirectoryPath);
71
- // Assert that the returned file list matches the mock file list
72
- (0, globals_1.expect)(result).toEqual(mockFileList);
73
- });
74
- });
75
- });
@@ -1,2 +0,0 @@
1
- export {};
2
- //# sourceMappingURL=http2-watch.spec.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"http2-watch.spec.d.ts","sourceRoot":"","sources":["../../src/fluent/http2-watch.spec.ts"],"names":[],"mappings":""}
@@ -1,284 +0,0 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- /* eslint-disable @typescript-eslint/no-explicit-any */
7
- const globals_1 = require("@jest/globals");
8
- const http2_1 = __importDefault(require("http2"));
9
- const __1 = require("..");
10
- const types_1 = require("./types");
11
- const _1 = require(".");
12
- globals_1.jest.mock("http2");
13
- (0, globals_1.describe)("Watcher HTTP2", () => {
14
- let watcher;
15
- let mockClient;
16
- let mockReq;
17
- const evtMock = globals_1.jest.fn();
18
- const errMock = globals_1.jest.fn();
19
- const setupAndStartWatcher = (eventType, handler) => {
20
- watcher.events.on(eventType, handler);
21
- watcher.start().catch(errMock);
22
- };
23
- (0, globals_1.beforeEach)(() => {
24
- globals_1.jest.clearAllMocks();
25
- // http2.client
26
- mockClient = {
27
- request: globals_1.jest.fn(),
28
- close: globals_1.jest.fn(),
29
- on: globals_1.jest.fn(),
30
- destroy: globals_1.jest.fn(),
31
- };
32
- // http2.request stream
33
- mockReq = {
34
- on: globals_1.jest.fn(),
35
- end: globals_1.jest.fn(),
36
- setEncoding: globals_1.jest.fn(),
37
- };
38
- // http2.connect function to return the mocked client session
39
- http2_1.default.connect.mockReturnValue(mockClient);
40
- });
41
- (0, globals_1.afterEach)(() => {
42
- watcher.close();
43
- });
44
- (0, globals_1.it)("should watch named resources", done => {
45
- const pod = createMockPod("pod-1", "1");
46
- const response = { type: "ADDED", object: pod };
47
- watcher = (0, _1.K8s)(__1.kind.Pod).Watch(evtMock, {
48
- useHTTP2: true,
49
- });
50
- mockReq.on.mockImplementation((event, callback) => {
51
- switch (event) {
52
- case "response":
53
- callback({ ":status": 200 }, 0);
54
- break;
55
- case "data":
56
- setTimeout(() => {
57
- callback(Buffer.from(JSON.stringify(response)));
58
- }, 10);
59
- break;
60
- case "end":
61
- setTimeout(() => {
62
- callback();
63
- done();
64
- }, 20);
65
- break;
66
- case "error":
67
- errMock(new Error("HTTP2 connection error"));
68
- break;
69
- }
70
- return mockReq;
71
- });
72
- mockClient.request.mockReturnValue(mockReq);
73
- setupAndStartWatcher(__1.WatchEvent.CONNECT, () => {
74
- setupAndStartWatcher(__1.WatchEvent.DATA, (receivedPod, phase) => {
75
- (0, globals_1.expect)(receivedPod.metadata?.name).toBe("pod-1");
76
- (0, globals_1.expect)(receivedPod.metadata?.resourceVersion).toBe("1");
77
- (0, globals_1.expect)(phase).toBe(types_1.WatchPhase.Added);
78
- done();
79
- });
80
- });
81
- });
82
- (0, globals_1.it)("should handle resource version is too old", done => {
83
- const errorResponse = {
84
- type: "ERROR",
85
- object: {
86
- kind: "Status",
87
- apiVersion: "v1",
88
- metadata: {},
89
- status: "Failure",
90
- message: "too old resource version: 123 (391079)",
91
- reason: "Gone",
92
- code: 410,
93
- },
94
- };
95
- watcher = (0, _1.K8s)(__1.kind.Pod).Watch(evtMock, {
96
- useHTTP2: true,
97
- });
98
- mockReq.on.mockImplementation((event, callback) => {
99
- switch (event) {
100
- case "response":
101
- callback({ ":status": 200 }, 0);
102
- break;
103
- case "data":
104
- setTimeout(() => {
105
- callback(Buffer.from(JSON.stringify(errorResponse)));
106
- }, 10);
107
- break;
108
- case "end":
109
- setTimeout(() => {
110
- callback();
111
- done();
112
- }, 20);
113
- break;
114
- case "error":
115
- errMock(new Error("HTTP2 connection error"));
116
- break;
117
- }
118
- return mockReq;
119
- });
120
- mockClient.request.mockReturnValue(mockReq);
121
- setupAndStartWatcher(__1.WatchEvent.OLD_RESOURCE_VERSION, res => {
122
- (0, globals_1.expect)(res).toEqual("123");
123
- done();
124
- });
125
- });
126
- (0, globals_1.it)("should call the event handler for each event", done => {
127
- const pod = createMockPod("pod-0", "1");
128
- const response = { type: "ADDED", object: pod };
129
- watcher = (0, _1.K8s)(__1.kind.Pod).Watch((evt, phase) => {
130
- (0, globals_1.expect)(evt.metadata?.name).toEqual("pod-0");
131
- (0, globals_1.expect)(phase).toEqual(types_1.WatchPhase.Added);
132
- }, { useHTTP2: true });
133
- mockReq.on.mockImplementation((event, callback) => {
134
- switch (event) {
135
- case "response":
136
- callback({ ":status": 200 }, 0);
137
- break;
138
- case "data":
139
- setTimeout(() => {
140
- callback(Buffer.from(JSON.stringify(response)));
141
- }, 10);
142
- break;
143
- case "end":
144
- setTimeout(() => {
145
- callback();
146
- }, 20);
147
- break;
148
- case "error":
149
- errMock(new Error("HTTP2 connection error"));
150
- break;
151
- }
152
- return mockReq;
153
- });
154
- mockClient.request.mockReturnValue(mockReq);
155
- watcher.start().catch(errMock);
156
- done();
157
- });
158
- (0, globals_1.it)("should return the cache id", () => {
159
- watcher = (0, _1.K8s)(__1.kind.Pod).Watch(evtMock, {
160
- resyncDelaySec: 1,
161
- useHTTP2: true,
162
- });
163
- (0, globals_1.expect)(watcher.getCacheID()).toEqual("d69b75a611");
164
- });
165
- (0, globals_1.it)("should handle the CONNECT event", done => {
166
- watcher = (0, _1.K8s)(__1.kind.Pod).Watch(evtMock, {
167
- resyncDelaySec: 1,
168
- useHTTP2: true,
169
- });
170
- setupAndStartWatcher(__1.WatchEvent.CONNECT, () => { });
171
- done();
172
- });
173
- (0, globals_1.it)("should handle the DATA event", done => {
174
- watcher = (0, _1.K8s)(__1.kind.Pod).Watch(evtMock, {
175
- resyncDelaySec: 1,
176
- useHTTP2: true,
177
- });
178
- setupAndStartWatcher(__1.WatchEvent.DATA, (pod, phase) => {
179
- (0, globals_1.expect)(pod.metadata?.name).toEqual("pod-0");
180
- (0, globals_1.expect)(phase).toEqual(types_1.WatchPhase.Added);
181
- });
182
- done();
183
- });
184
- (0, globals_1.it)("should handle the NETWORK_ERROR event", done => {
185
- const errorMessage = "Something bad happened";
186
- watcher = (0, _1.K8s)(__1.kind.Pod).Watch(evtMock, {
187
- useHTTP2: true,
188
- resyncDelaySec: 1,
189
- });
190
- mockReq.on.mockImplementation((event, callback) => {
191
- if (event === "response") {
192
- callback({ ":status": 200 }, 0);
193
- }
194
- else if (event === "error") {
195
- callback(new Error(errorMessage));
196
- }
197
- return mockReq;
198
- });
199
- mockClient.request.mockReturnValue(mockReq);
200
- setupAndStartWatcher(__1.WatchEvent.NETWORK_ERROR, error => {
201
- (0, globals_1.expect)(error.message).toEqual(errorMessage);
202
- });
203
- watcher.start().catch(errMock);
204
- done();
205
- });
206
- (0, globals_1.it)("should handle the RECONNECT event on an error", done => {
207
- const errorMessage = "Something bad happened";
208
- watcher = (0, _1.K8s)(__1.kind.Pod).Watch(evtMock, {
209
- useHTTP2: true,
210
- resyncDelaySec: 0.01,
211
- });
212
- let reconnectCount = 0;
213
- mockReq.on.mockImplementation((event, callback) => {
214
- if (event === "response") {
215
- callback({ ":status": 200 }, 0);
216
- }
217
- else if (event === "error") {
218
- reconnectCount += 1;
219
- callback(new Error(errorMessage));
220
- }
221
- return mockReq;
222
- });
223
- mockClient.request.mockReturnValue(mockReq);
224
- setupAndStartWatcher(__1.WatchEvent.RECONNECT, count => {
225
- (0, globals_1.expect)(count).toEqual(reconnectCount);
226
- });
227
- watcher.start().catch(errMock);
228
- done();
229
- });
230
- (0, globals_1.it)("should perform a resync after the resync interval", done => {
231
- watcher = (0, _1.K8s)(__1.kind.Pod).Watch(evtMock, {
232
- useHTTP2: true,
233
- resyncDelaySec: 1,
234
- });
235
- watcher.start().catch(errMock);
236
- done();
237
- });
238
- (0, globals_1.it)("should handle the GIVE_UP event", done => {
239
- watcher = (0, _1.K8s)(__1.kind.Pod).Watch(evtMock, {
240
- useHTTP2: true,
241
- lastSeenLimitSeconds: 0.01,
242
- resyncDelaySec: 0.01,
243
- resyncFailureMax: 1,
244
- });
245
- setupAndStartWatcher(__1.WatchEvent.GIVE_UP, () => {
246
- (0, globals_1.expect)(errMock).toBeCalled();
247
- });
248
- watcher.start().catch(errMock);
249
- done();
250
- });
251
- });
252
- /**
253
- * Creates a mock pod object
254
- *
255
- * @param name The name of the pod
256
- * @param resourceVersion The resource version of the pod
257
- * @returns A mock pod object
258
- */
259
- function createMockPod(name, resourceVersion) {
260
- return {
261
- kind: "Pod",
262
- apiVersion: "v1",
263
- metadata: {
264
- name,
265
- resourceVersion,
266
- uid: "abc-123-xyz",
267
- },
268
- spec: {
269
- containers: [
270
- {
271
- name: "nginx",
272
- image: "nginx:1.14.2",
273
- ports: [
274
- {
275
- containerPort: 80,
276
- protocol: "TCP",
277
- },
278
- ],
279
- },
280
- ],
281
- },
282
- status: {},
283
- };
284
- }
@@ -1,2 +0,0 @@
1
- export {};
2
- //# sourceMappingURL=index.test.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.test.d.ts","sourceRoot":"","sources":["../../src/fluent/index.test.ts"],"names":[],"mappings":""}
@@ -1,193 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- const globals_1 = require("@jest/globals");
4
- const client_node_1 = require("@kubernetes/client-node");
5
- const _1 = require(".");
6
- const fetch_1 = require("../fetch");
7
- const upstream_1 = require("../upstream");
8
- const utils_1 = require("./utils");
9
- // Setup mocks
10
- globals_1.jest.mock("./utils");
11
- globals_1.jest.mock("../fetch");
12
- const generateFakePodManagedFields = (manager) => {
13
- return [
14
- {
15
- apiVersion: "v1",
16
- fieldsType: "FieldsV1",
17
- fieldsV1: {
18
- "f:metadata": {
19
- "f:labels": {
20
- "f:fake": {},
21
- },
22
- "f:spec": {
23
- "f:containers": {
24
- 'k:{"name":"fake"}': {
25
- "f:image": {},
26
- "f:name": {},
27
- "f:resources": {
28
- "f:limits": {
29
- "f:cpu": {},
30
- "f:memory": {},
31
- },
32
- "f:requests": {
33
- "f:cpu": {},
34
- "f:memory": {},
35
- },
36
- },
37
- },
38
- },
39
- },
40
- },
41
- },
42
- manager: manager,
43
- operation: "Apply",
44
- },
45
- ];
46
- };
47
- (0, globals_1.describe)("Kube", () => {
48
- const fakeResource = {
49
- metadata: {
50
- name: "fake",
51
- namespace: "default",
52
- managedFields: generateFakePodManagedFields("pepr"),
53
- },
54
- };
55
- const mockedKubeCfg = globals_1.jest.mocked(utils_1.k8sCfg);
56
- const mockedKubeExec = globals_1.jest.mocked(utils_1.k8sExec).mockResolvedValue(fakeResource);
57
- (0, globals_1.beforeEach)(() => {
58
- // Clear all instances and calls to constructor and all methods:
59
- mockedKubeExec.mockClear();
60
- });
61
- (0, globals_1.it)("should create a resource", async () => {
62
- const result = await (0, _1.K8s)(upstream_1.Pod).Create(fakeResource);
63
- (0, globals_1.expect)(result).toEqual(fakeResource);
64
- (0, globals_1.expect)(mockedKubeExec).toHaveBeenCalledWith(upstream_1.Pod, globals_1.expect.objectContaining({
65
- name: "fake",
66
- namespace: "default",
67
- }), "POST", fakeResource);
68
- });
69
- (0, globals_1.it)("should delete a resource", async () => {
70
- await (0, _1.K8s)(upstream_1.Pod).Delete(fakeResource);
71
- (0, globals_1.expect)(mockedKubeExec).toHaveBeenCalledWith(upstream_1.Pod, globals_1.expect.objectContaining({
72
- name: "fake",
73
- namespace: "default",
74
- }), "DELETE");
75
- });
76
- (0, globals_1.it)("should patch a resource", async () => {
77
- const patchOperations = [
78
- { op: "replace", path: "/metadata/name", value: "new-fake" },
79
- ];
80
- const result = await (0, _1.K8s)(upstream_1.Pod).Patch(patchOperations);
81
- (0, globals_1.expect)(result).toEqual(fakeResource);
82
- (0, globals_1.expect)(mockedKubeExec).toHaveBeenCalledWith(upstream_1.Pod, {}, "PATCH", patchOperations);
83
- });
84
- (0, globals_1.it)("should patch the status of a resource", async () => {
85
- await (0, _1.K8s)(upstream_1.Pod).PatchStatus({
86
- metadata: {
87
- name: "fake",
88
- namespace: "default",
89
- managedFields: generateFakePodManagedFields("pepr"),
90
- },
91
- spec: { priority: 3 },
92
- status: {
93
- phase: "Ready",
94
- },
95
- });
96
- (0, globals_1.expect)(utils_1.k8sExec).toBeCalledWith(upstream_1.Pod, globals_1.expect.objectContaining({
97
- name: "fake",
98
- namespace: "default",
99
- }), "PATCH_STATUS", {
100
- apiVersion: "v1",
101
- kind: "Pod",
102
- metadata: {
103
- name: "fake",
104
- namespace: "default",
105
- managedFields: generateFakePodManagedFields("pepr"),
106
- },
107
- spec: { priority: 3 },
108
- status: {
109
- phase: "Ready",
110
- },
111
- });
112
- });
113
- (0, globals_1.it)("should filter with WithField", async () => {
114
- await (0, _1.K8s)(upstream_1.Pod).WithField("metadata.name", "fake").Get();
115
- (0, globals_1.expect)(mockedKubeExec).toHaveBeenCalledWith(upstream_1.Pod, globals_1.expect.objectContaining({
116
- fields: {
117
- "metadata.name": "fake",
118
- },
119
- }), "GET");
120
- });
121
- (0, globals_1.it)("should filter with WithLabel", async () => {
122
- await (0, _1.K8s)(upstream_1.Pod).WithLabel("app", "fakeApp").Get();
123
- (0, globals_1.expect)(mockedKubeExec).toHaveBeenCalledWith(upstream_1.Pod, globals_1.expect.objectContaining({
124
- labels: {
125
- app: "fakeApp",
126
- },
127
- }), "GET");
128
- });
129
- (0, globals_1.it)("should use InNamespace", async () => {
130
- await (0, _1.K8s)(upstream_1.Pod).InNamespace("fakeNamespace").Get();
131
- (0, globals_1.expect)(mockedKubeExec).toHaveBeenCalledWith(upstream_1.Pod, globals_1.expect.objectContaining({
132
- namespace: "fakeNamespace",
133
- }), "GET");
134
- });
135
- (0, globals_1.it)("should throw an error if namespace is already specified", async () => {
136
- (0, globals_1.expect)(() => (0, _1.K8s)(upstream_1.Pod, { namespace: "default" }).InNamespace("fakeNamespace")).toThrow("Namespace already specified: default");
137
- });
138
- (0, globals_1.it)("should handle Delete when the resource doesn't exist", async () => {
139
- mockedKubeExec.mockRejectedValueOnce({ status: 404 }); // Not Found on first call
140
- await (0, globals_1.expect)((0, _1.K8s)(upstream_1.Pod).Delete("fakeResource")).resolves.toBeUndefined();
141
- });
142
- (0, globals_1.it)("should handle Get", async () => {
143
- const result = await (0, _1.K8s)(upstream_1.Pod).Get("fakeResource");
144
- (0, globals_1.expect)(result).toEqual(fakeResource);
145
- (0, globals_1.expect)(mockedKubeExec).toHaveBeenCalledWith(upstream_1.Pod, globals_1.expect.objectContaining({
146
- name: "fakeResource",
147
- }), "GET");
148
- });
149
- (0, globals_1.it)("should thrown an error if Get is called with a name and filters are already specified a name", async () => {
150
- await (0, globals_1.expect)((0, _1.K8s)(upstream_1.Pod, { name: "fake" }).Get("fakeResource")).rejects.toThrow("Name already specified: fake");
151
- });
152
- (0, globals_1.it)("should throw an error if no patch operations provided", async () => {
153
- await (0, globals_1.expect)((0, _1.K8s)(upstream_1.Pod).Patch([])).rejects.toThrow("No operations specified");
154
- });
155
- (0, globals_1.it)("should allow Apply of deep partials", async () => {
156
- const result = await (0, _1.K8s)(upstream_1.Pod).Apply({ metadata: { name: "fake" }, spec: { priority: 3 } });
157
- (0, globals_1.expect)(result).toEqual(fakeResource);
158
- });
159
- (0, globals_1.it)("should allow force apply to resolve FieldManagerConflict", async () => {
160
- const result = await (0, _1.K8s)(upstream_1.Pod).Apply({
161
- metadata: { name: "fake", managedFields: generateFakePodManagedFields("kubectl") },
162
- spec: { priority: 3 },
163
- }, { force: true });
164
- (0, globals_1.expect)(result).toEqual(fakeResource);
165
- });
166
- (0, globals_1.it)("should throw an error if a Delete failed for a reason other than Not Found", async () => {
167
- mockedKubeExec.mockRejectedValueOnce({ status: 500 }); // Internal Server Error on first call
168
- await (0, globals_1.expect)((0, _1.K8s)(upstream_1.Pod).Delete("fakeResource")).rejects.toEqual(globals_1.expect.objectContaining({ status: 500 }));
169
- });
170
- (0, globals_1.it)("should create a raw api request", async () => {
171
- mockedKubeCfg.mockReturnValue(new Promise(r => r({
172
- serverUrl: "http://localhost:8080",
173
- opts: {},
174
- })));
175
- const mockResp = {
176
- kind: "APIVersions",
177
- versions: ["v1"],
178
- serverAddressByClientCIDRs: [
179
- {
180
- serverAddress: "172.27.0.3:6443",
181
- },
182
- ],
183
- };
184
- globals_1.jest.mocked(fetch_1.fetch).mockResolvedValue({
185
- ok: true,
186
- data: mockResp,
187
- status: 200,
188
- statusText: "OK",
189
- });
190
- const result = await (0, _1.K8s)(client_node_1.V1APIGroup).Raw("/api");
191
- (0, globals_1.expect)(result).toEqual(mockResp);
192
- });
193
- });
@@ -1,2 +0,0 @@
1
- export {};
2
- //# sourceMappingURL=utils.test.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"utils.test.d.ts","sourceRoot":"","sources":["../../src/fluent/utils.test.ts"],"names":[],"mappings":""}