Checks in annotateNamespace to not error during failed annotation

This commit is contained in:
Koushik Dey 2020-06-26 00:24:00 +05:30
parent af5108bc1c
commit 7f721770f6
3 changed files with 57 additions and 21 deletions

View File

@ -8,7 +8,6 @@ import * as io from '@actions/io';
import * as toolCache from '@actions/tool-cache'; import * as toolCache from '@actions/tool-cache';
import * as fileHelper from '../src/utilities/files-helper'; import * as fileHelper from '../src/utilities/files-helper';
import { workflowAnnotations } from '../src/constants'; import { workflowAnnotations } from '../src/constants';
import * as utility from '../src/utilities/utility';
import * as inputParam from '../src/input-parameters'; import * as inputParam from '../src/input-parameters';
import { Kubectl, Resource } from '../src/kubectl-object-model'; import { Kubectl, Resource } from '../src/kubectl-object-model';
@ -21,7 +20,6 @@ const os = require("os");
const coreMock = mocked(core, true); const coreMock = mocked(core, true);
const ioMock = mocked(io, true); const ioMock = mocked(io, true);
const utilityMock = mocked(utility, true);
const inputParamMock = mocked(inputParam, true); const inputParamMock = mocked(inputParam, true);
const toolCacheMock = mocked(toolCache, true); const toolCacheMock = mocked(toolCache, true);
@ -43,7 +41,7 @@ const getNamespaceMock = {
const resources: Resource[] = [{ type: "Deployment", name: "AppName" }]; const resources: Resource[] = [{ type: "Deployment", name: "AppName" }];
beforeAll(() => { beforeEach(() => {
deploymentYaml = fs.readFileSync(path.join(__dirname, 'manifests', 'deployment.yml'), 'utf8'); deploymentYaml = fs.readFileSync(path.join(__dirname, 'manifests', 'deployment.yml'), 'utf8');
process.env["KUBECONFIG"] = 'kubeConfig'; process.env["KUBECONFIG"] = 'kubeConfig';
@ -274,6 +272,31 @@ test("deployment - deploy() - Annotate resources", async () => {
expect(kubeCtl.annotate).toBeCalledTimes(2); 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<Kubectl> = 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 () => { test("deployment - deploy() - Annotate resources failed", async () => {
//Mocks //Mocks
inputParamMock.forceDeployment = true; inputParamMock.forceDeployment = true;

View File

@ -30,7 +30,7 @@ function checkForErrors(execResults, warnIfError) {
if (execResults.length !== 0) { if (execResults.length !== 0) {
let stderr = ''; let stderr = '';
execResults.forEach(result => { execResults.forEach(result => {
if (result.stderr) { if (result && result.stderr) {
if (result.code !== 0) { if (result.code !== 0) {
stderr += result.stderr + '\n'; stderr += result.stderr + '\n';
} }
@ -72,16 +72,22 @@ function annotateChildPods(kubectl, resourceType, resourceName, allPods) {
} }
exports.annotateChildPods = annotateChildPods; exports.annotateChildPods = annotateChildPods;
function annotateNamespace(kubectl, namespaceName) { function annotateNamespace(kubectl, namespaceName) {
let annotate = true;
const result = kubectl.getResource('namespace', namespaceName); const result = kubectl.getResource('namespace', namespaceName);
this.checkForErrors([result]); if (result == null) {
const annotationsSet = JSON.parse(result.stdout).metadata.annotations; return { code: -1, stderr: 'Failed to get resource' };
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 (annotate) { else if (!!result && !!result.stderr) {
return kubectl.annotate('namespace', namespaceName, constants_1.workflowAnnotations, true); 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; exports.annotateNamespace = annotateNamespace;

View File

@ -32,7 +32,7 @@ export function checkForErrors(execResults: IExecSyncResult[], warnIfError?: boo
if (execResults.length !== 0) { if (execResults.length !== 0) {
let stderr = ''; let stderr = '';
execResults.forEach(result => { execResults.forEach(result => {
if (result.stderr) { if (result && result.stderr) {
if (result.code !== 0) { if (result.code !== 0) {
stderr += result.stderr + '\n'; stderr += result.stderr + '\n';
} else { } else {
@ -74,16 +74,23 @@ export function annotateChildPods(kubectl: Kubectl, resourceType: string, resour
} }
export function annotateNamespace(kubectl: Kubectl, namespaceName: string): IExecSyncResult { export function annotateNamespace(kubectl: Kubectl, namespaceName: string): IExecSyncResult {
let annotate = true;
const result = kubectl.getResource('namespace', namespaceName); const result = kubectl.getResource('namespace', namespaceName);
this.checkForErrors([result]); if (result == null) {
const annotationsSet = JSON.parse(result.stdout).metadata.annotations; return <IExecSyncResult>{ code: -1, stderr: 'Failed to get resource' };
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 (annotate) { else if (!!result && !!result.stderr) {
return kubectl.annotate('namespace', namespaceName, workflowAnnotations, true); 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 <IExecSyncResult>{ code: 0, stdout: '' };
}
else {
return kubectl.annotate('namespace', namespaceName, workflowAnnotations, true);
}
} }
} }