From 7f721770f693d0d1ae168d2878400c690f20803a Mon Sep 17 00:00:00 2001 From: Koushik Dey Date: Fri, 26 Jun 2020 00:24:00 +0530 Subject: [PATCH] Checks in annotateNamespace to not error during failed annotation --- __tests__/run.test.ts | 29 ++++++++++++++++++++++++++--- lib/utilities/utility.js | 24 +++++++++++++++--------- src/utilities/utility.ts | 25 ++++++++++++++++--------- 3 files changed, 57 insertions(+), 21 deletions(-) diff --git a/__tests__/run.test.ts b/__tests__/run.test.ts index ca7188fe..b28cf93e 100644 --- a/__tests__/run.test.ts +++ b/__tests__/run.test.ts @@ -8,7 +8,6 @@ import * as io from '@actions/io'; import * as toolCache from '@actions/tool-cache'; import * as fileHelper from '../src/utilities/files-helper'; import { workflowAnnotations } from '../src/constants'; -import * as utility from '../src/utilities/utility'; import * as inputParam from '../src/input-parameters'; import { Kubectl, Resource } from '../src/kubectl-object-model'; @@ -21,7 +20,6 @@ const os = require("os"); const coreMock = mocked(core, true); const ioMock = mocked(io, true); -const utilityMock = mocked(utility, true); const inputParamMock = mocked(inputParam, true); const toolCacheMock = mocked(toolCache, true); @@ -43,7 +41,7 @@ const getNamespaceMock = { const resources: Resource[] = [{ type: "Deployment", name: "AppName" }]; -beforeAll(() => { +beforeEach(() => { deploymentYaml = fs.readFileSync(path.join(__dirname, 'manifests', 'deployment.yml'), 'utf8'); process.env["KUBECONFIG"] = 'kubeConfig'; @@ -274,6 +272,31 @@ test("deployment - deploy() - Annotate resources", async () => { expect(kubeCtl.annotate).toBeCalledTimes(2); }); +test("deployment - deploy() - Skip Annotate namespace", async () => { + process.env['GITHUB_REPOSITORY'] = 'test1Repo'; + const KubernetesManifestUtilityMock = mocked(KubernetesManifestUtility, true); + KubernetesManifestUtilityMock.checkManifestStability = jest.fn().mockReturnValue(""); + const KubernetesObjectUtilityMock = mocked(KubernetesObjectUtility, true); + KubernetesObjectUtilityMock.getResources = jest.fn().mockReturnValue(resources); + const fileHelperMock = mocked(fileHelper, true); + fileHelperMock.writeObjectsToFile = jest.fn().mockReturnValue(["~/Deployment_testapp_currentTimestamp"]); + const kubeCtl: jest.Mocked = new Kubectl("") as any; + kubeCtl.apply = jest.fn().mockReturnValue(""); + kubeCtl.getResource = jest.fn().mockReturnValue(getNamespaceMock); + kubeCtl.getAllPods = jest.fn().mockReturnValue(getAllPodsMock); + kubeCtl.getNewReplicaSet = jest.fn().mockReturnValue("testpod-776cbc86f9"); + kubeCtl.annotateFiles = jest.fn().mockReturnValue(""); + kubeCtl.annotate = jest.fn().mockReturnValue(""); + + const consoleOutputSpy = jest.spyOn(process.stdout, "write").mockImplementation(); + + //Invoke and assert + await expect(deployment.deploy(kubeCtl, ['manifests/deployment.yaml'], undefined)).resolves.not.toThrowError(); + expect(kubeCtl.annotateFiles).toBeCalledWith(["~/Deployment_testapp_currentTimestamp"], workflowAnnotations, true); + expect(kubeCtl.annotate).toBeCalledTimes(1); + expect(consoleOutputSpy).toHaveBeenNthCalledWith(2, `##[debug]Skipping 'annotate namespace' as namespace annotated by other workflow` + os.EOL) +}); + test("deployment - deploy() - Annotate resources failed", async () => { //Mocks inputParamMock.forceDeployment = true; diff --git a/lib/utilities/utility.js b/lib/utilities/utility.js index 80c3ce47..cb2cada0 100644 --- a/lib/utilities/utility.js +++ b/lib/utilities/utility.js @@ -30,7 +30,7 @@ function checkForErrors(execResults, warnIfError) { if (execResults.length !== 0) { let stderr = ''; execResults.forEach(result => { - if (result.stderr) { + if (result && result.stderr) { if (result.code !== 0) { stderr += result.stderr + '\n'; } @@ -72,16 +72,22 @@ function annotateChildPods(kubectl, resourceType, resourceName, allPods) { } exports.annotateChildPods = annotateChildPods; function annotateNamespace(kubectl, namespaceName) { - let annotate = true; const result = kubectl.getResource('namespace', namespaceName); - this.checkForErrors([result]); - const annotationsSet = JSON.parse(result.stdout).metadata.annotations; - if (!!annotationsSet && !!annotationsSet.runUri && annotationsSet.runUri.indexOf(process.env['GITHUB_REPOSITORY']) == -1) { - annotate = false; - core.debug(`Skipping 'annotate namespace' as namespace annotated by other workflow`); + if (result == null) { + return { code: -1, stderr: 'Failed to get resource' }; } - if (annotate) { - return kubectl.annotate('namespace', namespaceName, constants_1.workflowAnnotations, true); + else if (!!result && !!result.stderr) { + return result; + } + if (!!result && !!result.stdout) { + const annotationsSet = JSON.parse(result.stdout).metadata.annotations; + if (!!annotationsSet && !!annotationsSet.runUri && annotationsSet.runUri.indexOf(process.env['GITHUB_REPOSITORY']) == -1) { + core.debug(`Skipping 'annotate namespace' as namespace annotated by other workflow`); + return { code: 0, stdout: '' }; + } + else { + return kubectl.annotate('namespace', namespaceName, constants_1.workflowAnnotations, true); + } } } exports.annotateNamespace = annotateNamespace; diff --git a/src/utilities/utility.ts b/src/utilities/utility.ts index bf86ba91..398d4a4a 100644 --- a/src/utilities/utility.ts +++ b/src/utilities/utility.ts @@ -32,7 +32,7 @@ export function checkForErrors(execResults: IExecSyncResult[], warnIfError?: boo if (execResults.length !== 0) { let stderr = ''; execResults.forEach(result => { - if (result.stderr) { + if (result && result.stderr) { if (result.code !== 0) { stderr += result.stderr + '\n'; } else { @@ -74,16 +74,23 @@ export function annotateChildPods(kubectl: Kubectl, resourceType: string, resour } export function annotateNamespace(kubectl: Kubectl, namespaceName: string): IExecSyncResult { - let annotate = true; const result = kubectl.getResource('namespace', namespaceName); - this.checkForErrors([result]); - const annotationsSet = JSON.parse(result.stdout).metadata.annotations; - if (!!annotationsSet && !!annotationsSet.runUri && annotationsSet.runUri.indexOf(process.env['GITHUB_REPOSITORY']) == -1) { - annotate = false; - core.debug(`Skipping 'annotate namespace' as namespace annotated by other workflow`); + if (result == null) { + return { code: -1, stderr: 'Failed to get resource' }; } - if (annotate) { - return kubectl.annotate('namespace', namespaceName, workflowAnnotations, true); + else if (!!result && !!result.stderr) { + return result; + } + + if (!!result && !!result.stdout) { + const annotationsSet = JSON.parse(result.stdout).metadata.annotations; + if (!!annotationsSet && !!annotationsSet.runUri && annotationsSet.runUri.indexOf(process.env['GITHUB_REPOSITORY']) == -1) { + core.debug(`Skipping 'annotate namespace' as namespace annotated by other workflow`); + return { code: 0, stdout: '' }; + } + else { + return kubectl.annotate('namespace', namespaceName, workflowAnnotations, true); + } } }