Skip to content

Commit

Permalink
omit namespace arg for default namespace (#195)
Browse files Browse the repository at this point in the history
  • Loading branch information
davidgamero authored Jun 15, 2022
1 parent 507f2d4 commit 15920eb
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 26 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/unit-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
}
68 changes: 48 additions & 20 deletions src/types/kubectl.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(() => {
Expand All @@ -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 }
);
});
Expand All @@ -71,7 +97,7 @@ describe("Kubectl class", () => {
"-f",
configPaths[0] + "," + configPaths[1] + "," + configPaths[2],
"--namespace",
namespace,
testNamespace,
],
{ silent: false }
);
Expand All @@ -89,7 +115,7 @@ describe("Kubectl class", () => {
configPaths[0] + "," + configPaths[1] + "," + configPaths[2],
"--force",
"--namespace",
namespace,
testNamespace,
],
{ silent: false }
);
Expand All @@ -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 }
);
});
Expand All @@ -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 }
);
});
Expand All @@ -138,7 +164,7 @@ describe("Kubectl class", () => {
annotation,
"--overwrite",
"--namespace",
namespace,
testNamespace,
],
{ silent: false }
);
Expand All @@ -158,7 +184,7 @@ describe("Kubectl class", () => {
annotation,
"--overwrite",
"--namespace",
namespace,
testNamespace,
],
{ silent: false }
);
Expand All @@ -178,7 +204,7 @@ describe("Kubectl class", () => {
annotation,
"--overwrite",
"--namespace",
namespace,
testNamespace,
],
{ silent: false }
);
Expand All @@ -198,7 +224,7 @@ describe("Kubectl class", () => {
...labels,
"--overwrite",
"--namespace",
namespace,
testNamespace,
],
{ silent: false }
);
Expand All @@ -218,7 +244,7 @@ describe("Kubectl class", () => {
...labels,
"--overwrite",
"--namespace",
namespace,
testNamespace,
],
{ silent: false }
);
Expand All @@ -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 }
);
});
Expand All @@ -246,7 +272,7 @@ describe("Kubectl class", () => {
"status",
`${resourceType}/${name}`,
"--namespace",
namespace,
testNamespace,
],
{ silent: false }
);
Expand All @@ -264,7 +290,7 @@ describe("Kubectl class", () => {
"-o",
"json",
"--namespace",
namespace,
testNamespace,
],
{ silent: false }
);
Expand All @@ -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 }
);

Expand All @@ -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 }
);
});
Expand All @@ -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 }
);
});
Expand All @@ -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 = {
Expand Down
5 changes: 3 additions & 2 deletions src/types/kubectl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 && 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 });
}
Expand Down

0 comments on commit 15920eb

Please sign in to comment.