test case added

This commit is contained in:
tauhid621 2020-06-19 20:47:43 +05:30
parent d28a9921ec
commit 010c39c72d
12 changed files with 57 additions and 21 deletions

View File

@ -6,6 +6,8 @@ import * as deployment from '../src/utilities/strategy-helpers/deployment-helper
import * as fs from 'fs';
import * as io from '@actions/io';
import * as toolCache from '@actions/tool-cache';
import * as utility from '../src/utilities/utility';
import * as inputParam from '../src/input-parameters';
import { Kubectl, Resource } from '../src/kubectl-object-model';
@ -16,6 +18,8 @@ var path = require('path');
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);
const fileUtility = mocked(fs, true);
@ -193,4 +197,38 @@ test("deployment - deploy() - Invokes with manifestfiles", async () => {
await expect(deployment.deploy(kubeCtl, ['manifests/deployment.yaml'], undefined)).resolves.not.toThrowError();
expect(readFileSpy).toBeCalledWith("manifests/deployment.yaml");
expect(kubeCtl.getResource).toBeCalledWith("ingress", "AppName");
});
test("run() - deploy force flag on", async () => {
const kubectlVersion = 'v1.18.0'
//Mocks
coreMock.getInput = jest.fn().mockImplementation((name) => {
if (name == 'manifests') {
return 'manifests/deployment.yaml';
}
if (name == 'action') {
return 'deploy';
}
if (name == 'strategy') {
return undefined;
}
if (name == 'force') {
return 'true';
}
return kubectlVersion;
});
inputParamMock.forceDeployment = true;
coreMock.setFailed = jest.fn();
toolCacheMock.find = jest.fn().mockReturnValue('validPath');
toolCacheMock.downloadTool = jest.fn().mockReturnValue('downloadpath');
toolCacheMock.cacheFile = jest.fn().mockReturnValue('cachepath');
fileUtility.chmodSync = jest.fn();
utilityMock.checkForErrors = jest.fn();
const deploySpy = jest.spyOn(Kubectl.prototype, 'apply').mockImplementation();
//Invoke and assert
await expect(action.run()).resolves.not.toThrow();
expect(deploySpy).toBeCalledWith(expect.anything(), true);
deploySpy.mockRestore();
});

View File

@ -42,7 +42,7 @@ inputs:
required: true
default: 'deploy'
force:
description: 'Deploy when a previous deployment already exists'
description: 'Deploy when a previous deployment already exists. If true then '--force' argument is added to the apply command'.
required: false
default: false

View File

@ -11,7 +11,7 @@ exports.deploymentStrategy = core.getInput('strategy');
exports.trafficSplitMethod = core.getInput('traffic-split-method');
exports.baselineAndCanaryReplicas = core.getInput('baseline-and-canary-replicas');
exports.args = core.getInput('arguments');
exports.forceDeployment = core.getInput('force').toLowerCase() === 'true';
exports.forceDeployment = core.getInput('force').toLowerCase() == 'true';
if (!exports.namespace) {
core.debug('Namespace was not supplied; using "default" namespace instead.');
exports.namespace = 'default';

View File

@ -11,7 +11,6 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
Object.defineProperty(exports, "__esModule", { value: true });
exports.Kubectl = void 0;
const tool_runner_1 = require("./utilities/tool-runner");
const TaskInputParameters = require("./input-parameters");
class Kubectl {
constructor(kubectlPath, namespace, ignoreSSLErrors) {
this.kubectlPath = kubectlPath;
@ -23,9 +22,9 @@ class Kubectl {
this.namespace = 'default';
}
}
apply(configurationPaths) {
apply(configurationPaths, force) {
let applyArgs = ['apply', '-f', this.createInlineArray(configurationPaths)];
if (TaskInputParameters.forceDeployment) {
if (!!force) {
console.log("force flag is on, deployment will continue even if previous deployment already exists");
applyArgs.push('--force');
}

View File

@ -67,10 +67,10 @@ function deployManifests(files, kubectl, isCanaryDeploymentStrategy) {
else {
if (canaryDeploymentHelper.isSMICanaryStrategy()) {
const updatedManifests = appendStableVersionLabelToResource(files, kubectl);
result = kubectl.apply(updatedManifests);
result = kubectl.apply(updatedManifests, TaskInputParameters.forceDeployment);
}
else {
result = kubectl.apply(files);
result = kubectl.apply(files, TaskInputParameters.forceDeployment);
}
}
utility_1.checkForErrors([result]);

View File

@ -48,7 +48,7 @@ function deployPodCanary(kubectl, filePaths) {
});
});
const manifestFiles = fileHelper.writeObjectsToFile(newObjectsList);
const result = kubectl.apply(manifestFiles);
const result = kubectl.apply(manifestFiles, TaskInputParameters.forceDeployment);
return { 'result': result, 'newFilePaths': manifestFiles };
}
exports.deployPodCanary = deployPodCanary;

View File

@ -56,7 +56,7 @@ function deploySMICanary(kubectl, filePaths) {
});
});
const manifestFiles = fileHelper.writeObjectsToFile(newObjectsList);
const result = kubectl.apply(manifestFiles);
const result = kubectl.apply(manifestFiles, TaskInputParameters.forceDeployment);
createCanaryService(kubectl, filePaths);
return { 'result': result, 'newFilePaths': manifestFiles };
}
@ -111,7 +111,7 @@ function createCanaryService(kubectl, filePaths) {
});
const manifestFiles = fileHelper.writeObjectsToFile(newObjectsList);
manifestFiles.push(...trafficObjectsList);
const result = kubectl.apply(manifestFiles);
const result = kubectl.apply(manifestFiles, TaskInputParameters.forceDeployment);
utility_1.checkForErrors([result]);
}
function redirectTrafficToCanaryDeployment(kubectl, manifestFilePaths) {
@ -144,7 +144,7 @@ function adjustTraffic(kubectl, manifestFilePaths, stableWeight, canaryWeight) {
if (trafficSplitManifests.length <= 0) {
return;
}
const result = kubectl.apply(trafficSplitManifests);
const result = kubectl.apply(trafficSplitManifests, TaskInputParameters.forceDeployment);
core.debug('serviceObjects:' + serviceObjects.join(',') + ' result:' + result);
utility_1.checkForErrors([result]);
}

View File

@ -11,7 +11,7 @@ export const deploymentStrategy: string = core.getInput('strategy');
export const trafficSplitMethod: string = core.getInput('traffic-split-method');
export const baselineAndCanaryReplicas: string = core.getInput('baseline-and-canary-replicas');
export const args: string = core.getInput('arguments');
export const forceDeployment: boolean = core.getInput('force').toLowerCase() === 'true';
export const forceDeployment: boolean = core.getInput('force').toLowerCase() == 'true';
if (!namespace) {
core.debug('Namespace was not supplied; using "default" namespace instead.');

View File

@ -1,5 +1,4 @@
import { ToolRunner, IExecOptions } from "./utilities/tool-runner";
import * as TaskInputParameters from './input-parameters';
export interface Resource {
name: string;
@ -21,10 +20,10 @@ export class Kubectl {
}
}
public apply(configurationPaths: string | string[]) {
public apply(configurationPaths: string | string[], force?: boolean) {
let applyArgs: string[] = ['apply', '-f', this.createInlineArray(configurationPaths)];
if (TaskInputParameters.forceDeployment) {
if (!!force) {
console.log("force flag is on, deployment will continue even if previous deployment already exists");
applyArgs.push('--force');
}

View File

@ -66,10 +66,10 @@ function deployManifests(files: string[], kubectl: Kubectl, isCanaryDeploymentSt
} else {
if (canaryDeploymentHelper.isSMICanaryStrategy()) {
const updatedManifests = appendStableVersionLabelToResource(files, kubectl);
result = kubectl.apply(updatedManifests);
result = kubectl.apply(updatedManifests, TaskInputParameters.forceDeployment);
}
else {
result = kubectl.apply(files);
result = kubectl.apply(files, TaskInputParameters.forceDeployment);
}
}
checkForErrors([result]);

View File

@ -52,7 +52,7 @@ export function deployPodCanary(kubectl: Kubectl, filePaths: string[]) {
});
const manifestFiles = fileHelper.writeObjectsToFile(newObjectsList);
const result = kubectl.apply(manifestFiles);
const result = kubectl.apply(manifestFiles, TaskInputParameters.forceDeployment);
return { 'result': result, 'newFilePaths': manifestFiles };
}

View File

@ -60,7 +60,7 @@ export function deploySMICanary(kubectl: Kubectl, filePaths: string[]) {
});
const manifestFiles = fileHelper.writeObjectsToFile(newObjectsList);
const result = kubectl.apply(manifestFiles);
const result = kubectl.apply(manifestFiles, TaskInputParameters.forceDeployment);
createCanaryService(kubectl, filePaths);
return { 'result': result, 'newFilePaths': manifestFiles };
}
@ -121,7 +121,7 @@ function createCanaryService(kubectl: Kubectl, filePaths: string[]) {
const manifestFiles = fileHelper.writeObjectsToFile(newObjectsList);
manifestFiles.push(...trafficObjectsList);
const result = kubectl.apply(manifestFiles);
const result = kubectl.apply(manifestFiles, TaskInputParameters.forceDeployment);
checkForErrors([result]);
}
@ -159,7 +159,7 @@ function adjustTraffic(kubectl: Kubectl, manifestFilePaths: string[], stableWeig
return;
}
const result = kubectl.apply(trafficSplitManifests);
const result = kubectl.apply(trafficSplitManifests, TaskInputParameters.forceDeployment);
core.debug('serviceObjects:' + serviceObjects.join(',') + ' result:' + result);
checkForErrors([result]);
}