From 010c39c72d02ea0a83639735c7408b3f10bc1abb Mon Sep 17 00:00:00 2001 From: tauhid621 Date: Fri, 19 Jun 2020 20:47:43 +0530 Subject: [PATCH] test case added --- __tests__/run.test.ts | 38 +++++++++++++++++++ action.yml | 2 +- lib/input-parameters.js | 2 +- lib/kubectl-object-model.js | 5 +-- .../strategy-helpers/deployment-helper.js | 4 +- .../pod-canary-deployment-helper.js | 2 +- .../smi-canary-deployment-helper.js | 6 +-- src/input-parameters.ts | 2 +- src/kubectl-object-model.ts | 5 +-- .../strategy-helpers/deployment-helper.ts | 4 +- .../pod-canary-deployment-helper.ts | 2 +- .../smi-canary-deployment-helper.ts | 6 +-- 12 files changed, 57 insertions(+), 21 deletions(-) diff --git a/__tests__/run.test.ts b/__tests__/run.test.ts index 339501d2..1ef25b68 100644 --- a/__tests__/run.test.ts +++ b/__tests__/run.test.ts @@ -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(); }); \ No newline at end of file diff --git a/action.yml b/action.yml index 1d53d10e..d00a8038 100644 --- a/action.yml +++ b/action.yml @@ -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 diff --git a/lib/input-parameters.js b/lib/input-parameters.js index 93040606..21744518 100644 --- a/lib/input-parameters.js +++ b/lib/input-parameters.js @@ -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'; diff --git a/lib/kubectl-object-model.js b/lib/kubectl-object-model.js index 425426b9..5c19c509 100644 --- a/lib/kubectl-object-model.js +++ b/lib/kubectl-object-model.js @@ -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'); } diff --git a/lib/utilities/strategy-helpers/deployment-helper.js b/lib/utilities/strategy-helpers/deployment-helper.js index a2e242cd..aec778fd 100644 --- a/lib/utilities/strategy-helpers/deployment-helper.js +++ b/lib/utilities/strategy-helpers/deployment-helper.js @@ -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]); diff --git a/lib/utilities/strategy-helpers/pod-canary-deployment-helper.js b/lib/utilities/strategy-helpers/pod-canary-deployment-helper.js index 4e0ae883..79235e55 100644 --- a/lib/utilities/strategy-helpers/pod-canary-deployment-helper.js +++ b/lib/utilities/strategy-helpers/pod-canary-deployment-helper.js @@ -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; diff --git a/lib/utilities/strategy-helpers/smi-canary-deployment-helper.js b/lib/utilities/strategy-helpers/smi-canary-deployment-helper.js index fd90abcb..18882d66 100644 --- a/lib/utilities/strategy-helpers/smi-canary-deployment-helper.js +++ b/lib/utilities/strategy-helpers/smi-canary-deployment-helper.js @@ -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]); } diff --git a/src/input-parameters.ts b/src/input-parameters.ts index ad0b4a62..0bec64ab 100644 --- a/src/input-parameters.ts +++ b/src/input-parameters.ts @@ -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.'); diff --git a/src/kubectl-object-model.ts b/src/kubectl-object-model.ts index 8b224f10..3293161e 100644 --- a/src/kubectl-object-model.ts +++ b/src/kubectl-object-model.ts @@ -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'); } diff --git a/src/utilities/strategy-helpers/deployment-helper.ts b/src/utilities/strategy-helpers/deployment-helper.ts index 238c4a4b..1148dbb2 100644 --- a/src/utilities/strategy-helpers/deployment-helper.ts +++ b/src/utilities/strategy-helpers/deployment-helper.ts @@ -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]); diff --git a/src/utilities/strategy-helpers/pod-canary-deployment-helper.ts b/src/utilities/strategy-helpers/pod-canary-deployment-helper.ts index 4c4ca4df..f6b6dcf0 100644 --- a/src/utilities/strategy-helpers/pod-canary-deployment-helper.ts +++ b/src/utilities/strategy-helpers/pod-canary-deployment-helper.ts @@ -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 }; } diff --git a/src/utilities/strategy-helpers/smi-canary-deployment-helper.ts b/src/utilities/strategy-helpers/smi-canary-deployment-helper.ts index d06e3899..5239b583 100644 --- a/src/utilities/strategy-helpers/smi-canary-deployment-helper.ts +++ b/src/utilities/strategy-helpers/smi-canary-deployment-helper.ts @@ -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]); }