From 366f06888525aa4da9d7f1c8c4ebdc0e04fa9b09 Mon Sep 17 00:00:00 2001 From: David Gamero Date: Tue, 14 Jun 2022 12:35:29 -0400 Subject: [PATCH 1/4] don't append namespace arg for default namespace --- src/types/kubectl.test.ts | 68 +++++++++++++++++++++++++++------------ src/types/kubectl.ts | 5 +-- 2 files changed, 51 insertions(+), 22 deletions(-) diff --git a/src/types/kubectl.test.ts b/src/types/kubectl.test.ts index 172b45737..2216ac3ad 100644 --- a/src/types/kubectl.test.ts +++ b/src/types/kubectl.test.ts @@ -36,11 +36,37 @@ describe("Kubectl path", () => { }); const kubectlPath = "kubectlPath"; -const namespace = "namespace"; +const testNamespace = "testNamespace"; +const defaultNamespace = "default"; describe("Kubectl class", () => { - const kubectl = new Kubectl(kubectlPath, namespace); - describe("with a success exec return", () => { + describe("default namespace behavior", () => { + const kubectl = new Kubectl(kubectlPath, defaultNamespace); + const execReturn = { exitCode: 0, stdout: "Output", stderr: "" }; + + beforeEach(() => { + jest.spyOn(exec, "getExecOutput").mockImplementation(async () => { + return execReturn; + }); + }); + + describe("omits default namespace from commands", () => { + it("executes a command without appending --namespace arg", async () => { + // no args + const command = "command"; + expect(await kubectl.executeCommand(command)).toBe(execReturn); + expect(exec.getExecOutput).toBeCalledWith( + kubectlPath, + [command], + { silent: false } + ); + }); + }); + }); + + + describe("with a success exec return in testNamespace", () => { + const kubectl = new Kubectl(kubectlPath, testNamespace); const execReturn = { exitCode: 0, stdout: "Output", stderr: "" }; beforeEach(() => { @@ -55,7 +81,7 @@ describe("Kubectl class", () => { expect(result).toBe(execReturn); expect(exec.getExecOutput).toBeCalledWith( kubectlPath, - ["apply", "-f", configPaths, "--namespace", namespace], + ["apply", "-f", configPaths, "--namespace", testNamespace], { silent: false } ); }); @@ -71,7 +97,7 @@ describe("Kubectl class", () => { "-f", configPaths[0] + "," + configPaths[1] + "," + configPaths[2], "--namespace", - namespace, + testNamespace, ], { silent: false } ); @@ -89,7 +115,7 @@ describe("Kubectl class", () => { configPaths[0] + "," + configPaths[1] + "," + configPaths[2], "--force", "--namespace", - namespace, + testNamespace, ], { silent: false } ); @@ -102,7 +128,7 @@ describe("Kubectl class", () => { expect(result).toBe(execReturn); expect(exec.getExecOutput).toBeCalledWith( kubectlPath, - ["describe", resourceType, resourceName, "--namespace", namespace], + ["describe", resourceType, resourceName, "--namespace", testNamespace], { silent: false } ); }); @@ -114,7 +140,7 @@ describe("Kubectl class", () => { expect(result).toBe(execReturn); expect(exec.getExecOutput).toBeCalledWith( kubectlPath, - ["describe", resourceType, resourceName, "--namespace", namespace], + ["describe", resourceType, resourceName, "--namespace", testNamespace], { silent: true } ); }); @@ -138,7 +164,7 @@ describe("Kubectl class", () => { annotation, "--overwrite", "--namespace", - namespace, + testNamespace, ], { silent: false } ); @@ -158,7 +184,7 @@ describe("Kubectl class", () => { annotation, "--overwrite", "--namespace", - namespace, + testNamespace, ], { silent: false } ); @@ -178,7 +204,7 @@ describe("Kubectl class", () => { annotation, "--overwrite", "--namespace", - namespace, + testNamespace, ], { silent: false } ); @@ -198,7 +224,7 @@ describe("Kubectl class", () => { ...labels, "--overwrite", "--namespace", - namespace, + testNamespace, ], { silent: false } ); @@ -218,7 +244,7 @@ describe("Kubectl class", () => { ...labels, "--overwrite", "--namespace", - namespace, + testNamespace, ], { silent: false } ); @@ -228,7 +254,7 @@ describe("Kubectl class", () => { expect(await kubectl.getAllPods()).toBe(execReturn); expect(exec.getExecOutput).toBeCalledWith( kubectlPath, - ["get", "pods", "-o", "json", "--namespace", namespace], + ["get", "pods", "-o", "json", "--namespace", testNamespace], { silent: true } ); }); @@ -246,7 +272,7 @@ describe("Kubectl class", () => { "status", `${resourceType}/${name}`, "--namespace", - namespace, + testNamespace, ], { silent: false } ); @@ -264,7 +290,7 @@ describe("Kubectl class", () => { "-o", "json", "--namespace", - namespace, + testNamespace, ], { silent: false } ); @@ -276,7 +302,7 @@ describe("Kubectl class", () => { expect(await kubectl.executeCommand(command)).toBe(execReturn); expect(exec.getExecOutput).toBeCalledWith( kubectlPath, - [command, "--namespace", namespace], + [command, "--namespace", testNamespace], { silent: false } ); @@ -285,7 +311,7 @@ describe("Kubectl class", () => { expect(await kubectl.executeCommand(command, args)).toBe(execReturn); expect(exec.getExecOutput).toBeCalledWith( kubectlPath, - [command, args, "--namespace", namespace], + [command, args, "--namespace", testNamespace], { silent: false } ); }); @@ -295,7 +321,7 @@ describe("Kubectl class", () => { expect(await kubectl.delete(arg)).toBe(execReturn); expect(exec.getExecOutput).toBeCalledWith( kubectlPath, - ["delete", arg, "--namespace", namespace], + ["delete", arg, "--namespace", testNamespace], { silent: false } ); }); @@ -305,13 +331,15 @@ describe("Kubectl class", () => { expect(await kubectl.delete(args)).toBe(execReturn); expect(exec.getExecOutput).toBeCalledWith( kubectlPath, - ["delete", ...args, "--namespace", namespace], + ["delete", ...args, "--namespace", testNamespace], { silent: false } ); }); }); it("gets new replica sets", async () => { + const kubectl = new Kubectl(kubectlPath, testNamespace); + const newReplicaSetName = "newreplicaset"; const name = "name"; const describeReturn = { diff --git a/src/types/kubectl.ts b/src/types/kubectl.ts index 8adabf8b7..165657813 100644 --- a/src/types/kubectl.ts +++ b/src/types/kubectl.ts @@ -147,8 +147,9 @@ export class Kubectl { if (this.ignoreSSLErrors) { args.push("--insecure-skip-tls-verify"); } - args = args.concat(["--namespace", this.namespace]); - + if (this.namespace != "default") { + args = args.concat(["--namespace", this.namespace]); + } core.debug(`Kubectl run with command: ${this.kubectlPath} ${args}`); return await getExecOutput(this.kubectlPath, args, { silent }); } From ba9cb05cfbf88f0d18582a137cbb549d4cd8a1a0 Mon Sep 17 00:00:00 2001 From: David Gamero Date: Tue, 14 Jun 2022 12:39:51 -0400 Subject: [PATCH 2/4] increment jest to avoid peer dep conflict --- package.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index 73146cfa2..c917f3070 100644 --- a/package.json +++ b/package.json @@ -22,7 +22,7 @@ "@types/js-yaml": "^3.12.7", "@types/node": "^12.20.41", "jest": "^26.0.0", - "ts-jest": "^25.5.1", + "ts-jest": "^26.0.0", "typescript": "3.9.5" } -} +} \ No newline at end of file From e33c0bcbb14e5abe51080c5cce763c0fc873982b Mon Sep 17 00:00:00 2001 From: David Gamero Date: Tue, 14 Jun 2022 12:42:59 -0400 Subject: [PATCH 3/4] actually run units tests lol --- .github/workflows/unit-tests.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/unit-tests.yml b/.github/workflows/unit-tests.yml index 4ad31c7b5..56657345e 100644 --- a/.github/workflows/unit-tests.yml +++ b/.github/workflows/unit-tests.yml @@ -2,11 +2,11 @@ name: "Run unit tests." on: # rebuild any PRs and main branch changes pull_request: branches: - - master + - main - "releases/*" push: branches: - - master + - main - "releases/*" jobs: From c1beebb793c1fd0a47a129d3fa7e87bd34f2f126 Mon Sep 17 00:00:00 2001 From: David Gamero Date: Wed, 15 Jun 2022 09:57:21 -0400 Subject: [PATCH 4/4] handle empty namespace too --- src/types/kubectl.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/types/kubectl.ts b/src/types/kubectl.ts index 165657813..baae254fc 100644 --- a/src/types/kubectl.ts +++ b/src/types/kubectl.ts @@ -147,7 +147,7 @@ export class Kubectl { if (this.ignoreSSLErrors) { args.push("--insecure-skip-tls-verify"); } - if (this.namespace != "default") { + if (this.namespace && this.namespace != "default") { args = args.concat(["--namespace", this.namespace]); } core.debug(`Kubectl run with command: ${this.kubectlPath} ${args}`);