Migrate to esbuild/Vitest and upgrade @actions/* to ESM-only versions (#492)

* Migrate build toolchain from ncc/Jest to esbuild/Vitest

Replace the legacy ncc/Jest/Babel build stack with a modern ESM toolchain:

Build:
- Replace @vercel/ncc with esbuild (--platform=node --target=node20 --format=esm)
- Add createRequire banner for CJS interop in ESM bundle
- Add "type": "module" to package.json
- Add tsc --noEmit typecheck script (esbuild strips types without checking)
- Add typecheck to husky pre-commit hook

Dependencies:
- Bump @actions/core@3, exec@3, io@3, tool-cache@4 (ESM-only)
- Replace jest/ts-jest/@babel/* with vitest@4

Tests:
- Convert 29 test files: jest.fn()→vi.fn(), jest.mock()→vi.mock(), jest.spyOn()→vi.spyOn()
- Fix vitest 4 compat: mockImplementation requires args, mock call tracking, await .rejects

CI:
- Update build step from ncc build → npm run build
- Update composite action to use npm run build

* Switch tsconfig to NodeNext module resolution

Change module/moduleResolution from ES2022/bundler to NodeNext/NodeNext
and target from ES2022 to ES2020.

- Add .js extensions to all relative imports across 59 source/test files
  (required by NodeNext module resolution)
- Add vitest/globals to tsconfig types array for global test API declarations
This commit is contained in:
David Gamero 2026-02-24 14:57:56 -05:00 committed by GitHub
parent 84e2095bf0
commit 01cfe404ef
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
67 changed files with 1967 additions and 10133 deletions

View File

@ -15,18 +15,14 @@ runs:
rm -rf node_modules/ rm -rf node_modules/
npm install npm install
- name: Install ncc - name: Build
shell: bash shell: bash
run: npm i -g @vercel/ncc run: npm run build
- name: Install conntrack - name: Install conntrack
shell: bash shell: bash
run: sudo apt-get install -y conntrack run: sudo apt-get install -y conntrack
- name: Build
shell: bash
run: ncc build src/run.ts -o lib
- uses: Azure/setup-kubectl@776406bce94f63e41d621b960d78ee25c8b76ede # v4.0.1 - uses: Azure/setup-kubectl@776406bce94f63e41d621b960d78ee25c8b76ede # v4.0.1
name: Install Kubectl name: Install Kubectl

View File

@ -25,10 +25,10 @@ jobs:
run: | run: |
rm -rf node_modules/ rm -rf node_modules/
npm install npm install
- name: Install ncc
run: npm i -g @vercel/ncc
- name: Build - name: Build
run: ncc build src/run.ts -o lib run: npm run build
- name: Azure login - name: Azure login
uses: azure/login@v2.3.0 uses: azure/login@v2.3.0
with: with:

View File

@ -1,3 +1,9 @@
npm run typecheck || {
echo ""
echo "❌ Type check failed."
echo "💡 Run 'npm run typecheck' to see errors."
exit 1
}
npm test npm test
npm run format-check || { npm run format-check || {
echo "" echo ""

View File

@ -1,20 +0,0 @@
module.exports = {
moduleFileExtensions: ['js', 'ts'],
testEnvironment: 'node',
testMatch: ['**/*.test.ts'],
transform: {
'\\.[jt]sx?$': 'babel-jest'
},
transformIgnorePatterns: [
'node_modules/(?!' +
[
'@octokit',
'universal-user-agent',
'before-after-hook',
'minimist'
].join('|') +
')'
],
verbose: true,
testTimeout: 9000
}

10743
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -3,38 +3,34 @@
"version": "5.0.4", "version": "5.0.4",
"author": "Deepak Sattiraju", "author": "Deepak Sattiraju",
"license": "MIT", "license": "MIT",
"type": "module",
"scripts": { "scripts": {
"prebuild": "npm i @vercel/ncc", "build": "esbuild src/run.ts --bundle --platform=node --target=node20 --format=esm --outfile=lib/index.js --banner:js=\"import { createRequire } from 'module';const require = createRequire(import.meta.url);\"",
"build": "ncc build src/run.ts -o lib", "typecheck": "tsc --noEmit",
"test": "jest", "test": "vitest run",
"coverage": "jest --coverage=true", "coverage": "vitest run --coverage",
"format": "prettier --write .", "format": "prettier --write .",
"format-check": "prettier --check .", "format-check": "prettier --check .",
"prepare": "husky" "prepare": "husky"
}, },
"dependencies": { "dependencies": {
"@actions/core": "^2.0.2", "@actions/core": "^3.0.0",
"@actions/exec": "^2.0.0", "@actions/exec": "^3.0.0",
"@actions/io": "^2.0.0", "@actions/io": "^3.0.2",
"@actions/tool-cache": "3.0.0", "@actions/tool-cache": "4.0.0",
"@babel/preset-env": "^7.28.6",
"@babel/preset-typescript": "^7.28.5",
"@octokit/core": "^7.0.6", "@octokit/core": "^7.0.6",
"@octokit/plugin-retry": "^8.0.3", "@octokit/plugin-retry": "^8.0.3",
"@types/minipass": "^3.3.5",
"husky": "^9.1.7",
"js-yaml": "4.1.1", "js-yaml": "4.1.1",
"minimist": "^1.2.8" "minimist": "^1.2.8"
}, },
"devDependencies": { "devDependencies": {
"@types/jest": "^30.0.0",
"@types/js-yaml": "^4.0.9", "@types/js-yaml": "^4.0.9",
"@types/minimist": "^1.2.5", "@types/minimist": "^1.2.5",
"@types/node": "^25.0.9", "@types/node": "^25.2.3",
"@vercel/ncc": "^0.38.4", "esbuild": "^0.27",
"jest": "^30.2.0", "husky": "^9.1.7",
"prettier": "^3.8.0", "prettier": "^3.8.1",
"ts-jest": "^29.4.6", "typescript": "5.9.3",
"typescript": "5.9.3" "vitest": "^4"
} }
} }

View File

@ -1,19 +1,19 @@
import * as core from '@actions/core' import * as core from '@actions/core'
import * as models from '../types/kubernetesTypes' import * as models from '../types/kubernetesTypes.js'
import * as KubernetesConstants from '../types/kubernetesTypes' import * as KubernetesConstants from '../types/kubernetesTypes.js'
import {Kubectl, Resource} from '../types/kubectl' import {Kubectl, Resource} from '../types/kubectl.js'
import { import {
getResources, getResources,
updateManifestFiles updateManifestFiles
} from '../utilities/manifestUpdateUtils' } from '../utilities/manifestUpdateUtils.js'
import { import {
annotateAndLabelResources, annotateAndLabelResources,
checkManifestStability, checkManifestStability,
deployManifests deployManifests
} from '../strategyHelpers/deploymentHelper' } from '../strategyHelpers/deploymentHelper.js'
import {DeploymentStrategy} from '../types/deploymentStrategy' import {DeploymentStrategy} from '../types/deploymentStrategy.js'
import {parseTrafficSplitMethod} from '../types/trafficSplitMethod' import {parseTrafficSplitMethod} from '../types/trafficSplitMethod.js'
import {ClusterType} from '../inputUtils' import {ClusterType} from '../inputUtils.js'
export const ResourceTypeManagedCluster = export const ResourceTypeManagedCluster =
'Microsoft.ContainerService/managedClusters' 'Microsoft.ContainerService/managedClusters'
export const ResourceTypeFleet = 'Microsoft.ContainerService/fleets' export const ResourceTypeFleet = 'Microsoft.ContainerService/fleets'

View File

@ -1,44 +1,44 @@
import * as core from '@actions/core' import * as core from '@actions/core'
import * as canaryDeploymentHelper from '../strategyHelpers/canary/canaryHelper' import * as canaryDeploymentHelper from '../strategyHelpers/canary/canaryHelper.js'
import * as SMICanaryDeploymentHelper from '../strategyHelpers/canary/smiCanaryHelper' import * as SMICanaryDeploymentHelper from '../strategyHelpers/canary/smiCanaryHelper.js'
import * as PodCanaryHelper from '../strategyHelpers/canary/podCanaryHelper' import * as PodCanaryHelper from '../strategyHelpers/canary/podCanaryHelper.js'
import { import {
getResources, getResources,
updateManifestFiles updateManifestFiles
} from '../utilities/manifestUpdateUtils' } from '../utilities/manifestUpdateUtils.js'
import {annotateAndLabelResources} from '../strategyHelpers/deploymentHelper' import {annotateAndLabelResources} from '../strategyHelpers/deploymentHelper.js'
import * as models from '../types/kubernetesTypes' import * as models from '../types/kubernetesTypes.js'
import * as KubernetesManifestUtility from '../utilities/manifestStabilityUtils' import * as KubernetesManifestUtility from '../utilities/manifestStabilityUtils.js'
import { import {
deleteGreenObjects, deleteGreenObjects,
getManifestObjects, getManifestObjects,
NONE_LABEL_VALUE NONE_LABEL_VALUE
} from '../strategyHelpers/blueGreen/blueGreenHelper' } from '../strategyHelpers/blueGreen/blueGreenHelper.js'
import {BlueGreenManifests} from '../types/blueGreenTypes' import {BlueGreenManifests} from '../types/blueGreenTypes.js'
import {DeployResult} from '../types/deployResult' import {DeployResult} from '../types/deployResult.js'
import { import {
promoteBlueGreenIngress, promoteBlueGreenIngress,
promoteBlueGreenService, promoteBlueGreenService,
promoteBlueGreenSMI promoteBlueGreenSMI
} from '../strategyHelpers/blueGreen/promote' } from '../strategyHelpers/blueGreen/promote.js'
import { import {
routeBlueGreenService, routeBlueGreenService,
routeBlueGreenIngressUnchanged, routeBlueGreenIngressUnchanged,
routeBlueGreenSMI routeBlueGreenSMI
} from '../strategyHelpers/blueGreen/route' } from '../strategyHelpers/blueGreen/route.js'
import {cleanupSMI} from '../strategyHelpers/blueGreen/smiBlueGreenHelper' import {cleanupSMI} from '../strategyHelpers/blueGreen/smiBlueGreenHelper.js'
import {Kubectl, Resource} from '../types/kubectl' import {Kubectl, Resource} from '../types/kubectl.js'
import {DeploymentStrategy} from '../types/deploymentStrategy' import {DeploymentStrategy} from '../types/deploymentStrategy.js'
import { import {
parseTrafficSplitMethod, parseTrafficSplitMethod,
TrafficSplitMethod TrafficSplitMethod
} from '../types/trafficSplitMethod' } from '../types/trafficSplitMethod.js'
import {parseRouteStrategy, RouteStrategy} from '../types/routeStrategy' import {parseRouteStrategy, RouteStrategy} from '../types/routeStrategy.js'
import {ClusterType} from '../inputUtils' import {ClusterType} from '../inputUtils.js'
export async function promote( export async function promote(
kubectl: Kubectl, kubectl: Kubectl,

View File

@ -1,20 +1,20 @@
import * as core from '@actions/core' import * as core from '@actions/core'
import * as canaryDeploymentHelper from '../strategyHelpers/canary/canaryHelper' import * as canaryDeploymentHelper from '../strategyHelpers/canary/canaryHelper.js'
import * as SMICanaryDeploymentHelper from '../strategyHelpers/canary/smiCanaryHelper' import * as SMICanaryDeploymentHelper from '../strategyHelpers/canary/smiCanaryHelper.js'
import {Kubectl} from '../types/kubectl' import {Kubectl} from '../types/kubectl.js'
import {BlueGreenManifests} from '../types/blueGreenTypes' import {BlueGreenManifests} from '../types/blueGreenTypes.js'
import { import {
rejectBlueGreenIngress, rejectBlueGreenIngress,
rejectBlueGreenService, rejectBlueGreenService,
rejectBlueGreenSMI rejectBlueGreenSMI
} from '../strategyHelpers/blueGreen/reject' } from '../strategyHelpers/blueGreen/reject.js'
import {getManifestObjects} from '../strategyHelpers/blueGreen/blueGreenHelper' import {getManifestObjects} from '../strategyHelpers/blueGreen/blueGreenHelper.js'
import {DeploymentStrategy} from '../types/deploymentStrategy' import {DeploymentStrategy} from '../types/deploymentStrategy.js'
import { import {
parseTrafficSplitMethod, parseTrafficSplitMethod,
TrafficSplitMethod TrafficSplitMethod
} from '../types/trafficSplitMethod' } from '../types/trafficSplitMethod.js'
import {parseRouteStrategy, RouteStrategy} from '../types/routeStrategy' import {parseRouteStrategy, RouteStrategy} from '../types/routeStrategy.js'
export async function reject( export async function reject(
kubectl: Kubectl, kubectl: Kubectl,

View File

@ -1,5 +1,8 @@
import {parseResourceTypeInput} from './inputUtils' import {parseResourceTypeInput} from './inputUtils.js'
import {ResourceTypeFleet, ResourceTypeManagedCluster} from './actions/deploy' import {
ResourceTypeFleet,
ResourceTypeManagedCluster
} from './actions/deploy.js'
describe('InputUtils', () => { describe('InputUtils', () => {
describe('parseResourceTypeInput', () => { describe('parseResourceTypeInput', () => {

View File

@ -1,6 +1,9 @@
import * as core from '@actions/core' import * as core from '@actions/core'
import {parseAnnotations} from './types/annotations' import {parseAnnotations} from './types/annotations.js'
import {ResourceTypeFleet, ResourceTypeManagedCluster} from './actions/deploy' import {
ResourceTypeFleet,
ResourceTypeManagedCluster
} from './actions/deploy.js'
export const inputAnnotations = parseAnnotations( export const inputAnnotations = parseAnnotations(
core.getInput('annotations', {required: false}) core.getInput('annotations', {required: false})

View File

@ -1,19 +1,19 @@
import * as core from '@actions/core' import * as core from '@actions/core'
import {getKubectlPath, Kubectl} from './types/kubectl' import {getKubectlPath, Kubectl} from './types/kubectl.js'
import { import {
deploy, deploy,
ResourceTypeFleet, ResourceTypeFleet,
ResourceTypeManagedCluster ResourceTypeManagedCluster
} from './actions/deploy' } from './actions/deploy.js'
import {ClusterType} from './inputUtils' import {ClusterType} from './inputUtils.js'
import {promote} from './actions/promote' import {promote} from './actions/promote.js'
import {reject} from './actions/reject' import {reject} from './actions/reject.js'
import {Action, parseAction} from './types/action' import {Action, parseAction} from './types/action.js'
import {parseDeploymentStrategy} from './types/deploymentStrategy' import {parseDeploymentStrategy} from './types/deploymentStrategy.js'
import {getFilesFromDirectoriesAndURLs} from './utilities/fileUtils' import {getFilesFromDirectoriesAndURLs} from './utilities/fileUtils.js'
import {PrivateKubectl} from './types/privatekubectl' import {PrivateKubectl} from './types/privatekubectl.js'
import {parseResourceTypeInput} from './inputUtils' import {parseResourceTypeInput} from './inputUtils.js'
import {parseDuration} from './utilities/durationUtils' import {parseDuration} from './utilities/durationUtils.js'
export async function run() { export async function run() {
// verify kubeconfig is set // verify kubeconfig is set

View File

@ -1,3 +1,5 @@
import {vi} from 'vitest'
import type {MockInstance} from 'vitest'
import { import {
deployWithLabel, deployWithLabel,
deleteGreenObjects, deleteGreenObjects,
@ -8,16 +10,16 @@ import {
getNewBlueGreenObject, getNewBlueGreenObject,
GREEN_LABEL_VALUE, GREEN_LABEL_VALUE,
isServiceRouted isServiceRouted
} from './blueGreenHelper' } from './blueGreenHelper.js'
import {BlueGreenDeployment} from '../../types/blueGreenTypes' import {BlueGreenDeployment} from '../../types/blueGreenTypes.js'
import * as bgHelper from './blueGreenHelper' import * as bgHelper from './blueGreenHelper.js'
import {Kubectl} from '../../types/kubectl' import {Kubectl} from '../../types/kubectl.js'
import * as fileHelper from '../../utilities/fileUtils' import * as fileHelper from '../../utilities/fileUtils.js'
import {K8sObject} from '../../types/k8sObject' import {K8sObject} from '../../types/k8sObject.js'
import * as manifestUpdateUtils from '../../utilities/manifestUpdateUtils' import * as manifestUpdateUtils from '../../utilities/manifestUpdateUtils.js'
import {ExecOutput} from '@actions/exec' import {ExecOutput} from '@actions/exec'
jest.mock('../../types/kubectl') vi.mock('../../types/kubectl')
const kubectl = new Kubectl('') const kubectl = new Kubectl('')
const TEST_TIMEOUT = '60s' const TEST_TIMEOUT = '60s'
@ -37,17 +39,17 @@ const MOCK_EXEC_OUTPUT = {
describe('bluegreenhelper functions', () => { describe('bluegreenhelper functions', () => {
let testObjects let testObjects
beforeEach(() => { beforeEach(() => {
//@ts-ignore vi.restoreAllMocks()
Kubectl.mockClear() vi.mocked(Kubectl).mockClear()
testObjects = getManifestObjects(['test/unit/manifests/test-ingress.yml']) testObjects = getManifestObjects(['test/unit/manifests/test-ingress.yml'])
jest vi.spyOn(fileHelper, 'writeObjectsToFile').mockImplementationOnce(() => [
.spyOn(fileHelper, 'writeObjectsToFile') ''
.mockImplementationOnce(() => ['']) ])
}) })
test('correctly deletes services and workloads according to label', async () => { test('correctly deletes services and workloads according to label', async () => {
jest.spyOn(bgHelper, 'deleteObjects').mockReturnValue({} as Promise<void>) vi.spyOn(bgHelper, 'deleteObjects').mockReturnValue({} as Promise<void>)
const value = await deleteGreenObjects( const value = await deleteGreenObjects(
kubectl, kubectl,
@ -65,21 +67,16 @@ describe('bluegreenhelper functions', () => {
}) })
test('handles timeout when deleting objects', async () => { test('handles timeout when deleting objects', async () => {
// Mock deleteObjects to prevent actual execution const deleteMock = vi.fn().mockResolvedValue(MOCK_EXEC_OUTPUT)
const deleteSpy = jest kubectl.delete = deleteMock
.spyOn(kubectl, 'delete')
.mockResolvedValue(MOCK_EXEC_OUTPUT)
await bgHelper.deleteObjects( const deleteList = EXPECTED_GREEN_OBJECTS
kubectl,
EXPECTED_GREEN_OBJECTS,
TEST_TIMEOUT
)
// Verify kubectl.delete is called with timeout for each object in deleteList await bgHelper.deleteObjects(kubectl, deleteList, TEST_TIMEOUT)
expect(deleteSpy).toHaveBeenCalledTimes(EXPECTED_GREEN_OBJECTS.length)
EXPECTED_GREEN_OBJECTS.forEach(({name, kind}) => { expect(deleteMock).toHaveBeenCalledTimes(deleteList.length)
expect(deleteSpy).toHaveBeenCalledWith( deleteList.forEach(({name, kind}) => {
expect(deleteMock).toHaveBeenCalledWith(
[kind, name], [kind, name],
undefined, undefined,
TEST_TIMEOUT TEST_TIMEOUT
@ -135,7 +132,7 @@ describe('bluegreenhelper functions', () => {
}) })
test('correctly makes labeled workloads', async () => { test('correctly makes labeled workloads', async () => {
const kubectlApplySpy = jest.spyOn(kubectl, 'apply').mockResolvedValue({ const kubectlApplySpy = vi.spyOn(kubectl, 'apply').mockResolvedValue({
stdout: 'deployment.apps/nginx-deployment created', stdout: 'deployment.apps/nginx-deployment created',
stderr: '', stderr: '',
exitCode: 0 exitCode: 0
@ -179,9 +176,9 @@ describe('bluegreenhelper functions', () => {
exitCode: 0 exitCode: 0
} }
jest vi.spyOn(kubectl, 'getResource').mockImplementation(() =>
.spyOn(kubectl, 'getResource') Promise.resolve(mockExecOutput)
.mockImplementation(() => Promise.resolve(mockExecOutput)) )
const fetched = await fetchResource( const fetched = await fetchResource(
kubectl, kubectl,
'nginx-deployment', 'nginx-deployment',
@ -209,12 +206,12 @@ describe('bluegreenhelper functions', () => {
] ]
for (const testCase of errorTestCases) { for (const testCase of errorTestCases) {
const spy = jest.spyOn(kubectl, 'getResource') const spy = vi.spyOn(kubectl, 'getResource')
if (testCase.mockOutput) { if (testCase.mockOutput) {
spy.mockImplementation(() => Promise.resolve(testCase.mockOutput)) spy.mockImplementation(() => Promise.resolve(testCase.mockOutput))
} else { } else {
spy.mockImplementation() spy.mockResolvedValue(null)
} }
const fetched = await fetchResource( const fetched = await fetchResource(
@ -235,12 +232,13 @@ describe('bluegreenhelper functions', () => {
stderr: '' stderr: ''
} as ExecOutput } as ExecOutput
jest.spyOn(kubectl, 'getResource').mockResolvedValue(mockExecOutput) vi.spyOn(kubectl, 'getResource').mockResolvedValue(mockExecOutput)
jest vi.spyOn(
.spyOn(manifestUpdateUtils, 'UnsetClusterSpecificDetails') manifestUpdateUtils,
.mockImplementation(() => { 'UnsetClusterSpecificDetails'
throw new Error('test error') ).mockImplementation(() => {
}) throw new Error('test error')
})
expect( expect(
await fetchResource(kubectl, 'nginx-deployment', 'Deployment') await fetchResource(kubectl, 'nginx-deployment', 'Deployment')
@ -267,7 +265,7 @@ describe('bluegreenhelper functions', () => {
describe('deployObjects', () => { describe('deployObjects', () => {
let mockObjects: any[] let mockObjects: any[]
let kubectlApplySpy: jest.SpyInstance let kubectlApplySpy: MockInstance
const mockSuccessResult: ExecOutput = { const mockSuccessResult: ExecOutput = {
stdout: 'deployment.apps/nginx-deployment created', stdout: 'deployment.apps/nginx-deployment created',
@ -285,11 +283,11 @@ describe('bluegreenhelper functions', () => {
// //@ts-ignore // //@ts-ignore
// Kubectl.mockClear() // Kubectl.mockClear()
mockObjects = [testObjects.deploymentEntityList[0]] mockObjects = [testObjects.deploymentEntityList[0]]
kubectlApplySpy = jest.spyOn(kubectl, 'apply') kubectlApplySpy = vi.spyOn(kubectl, 'apply')
}) })
afterEach(() => { afterEach(() => {
jest.clearAllMocks() vi.clearAllMocks()
}) })
it('should return execution result and manifest files when kubectl apply succeeds', async () => { it('should return execution result and manifest files when kubectl apply succeeds', async () => {

View File

@ -2,27 +2,27 @@ import * as core from '@actions/core'
import * as fs from 'fs' import * as fs from 'fs'
import * as yaml from 'js-yaml' import * as yaml from 'js-yaml'
import {DeployResult} from '../../types/deployResult' import {DeployResult} from '../../types/deployResult.js'
import {K8sObject, K8sDeleteObject} from '../../types/k8sObject' import {K8sObject, K8sDeleteObject} from '../../types/k8sObject.js'
import {Kubectl} from '../../types/kubectl' import {Kubectl} from '../../types/kubectl.js'
import { import {
isDeploymentEntity, isDeploymentEntity,
isIngressEntity, isIngressEntity,
isServiceEntity, isServiceEntity,
KubernetesWorkload KubernetesWorkload
} from '../../types/kubernetesTypes' } from '../../types/kubernetesTypes.js'
import { import {
BlueGreenDeployment, BlueGreenDeployment,
BlueGreenManifests BlueGreenManifests
} from '../../types/blueGreenTypes' } from '../../types/blueGreenTypes.js'
import * as fileHelper from '../../utilities/fileUtils' import * as fileHelper from '../../utilities/fileUtils.js'
import {updateSpecLabels} from '../../utilities/manifestSpecLabelUtils' import {updateSpecLabels} from '../../utilities/manifestSpecLabelUtils.js'
import {checkForErrors} from '../../utilities/kubectlUtils' import {checkForErrors} from '../../utilities/kubectlUtils.js'
import { import {
UnsetClusterSpecificDetails, UnsetClusterSpecificDetails,
updateObjectLabels, updateObjectLabels,
updateSelectorLabels updateSelectorLabels
} from '../../utilities/manifestUpdateUtils' } from '../../utilities/manifestUpdateUtils.js'
export const GREEN_LABEL_VALUE = 'green' export const GREEN_LABEL_VALUE = 'green'
export const NONE_LABEL_VALUE = 'None' export const NONE_LABEL_VALUE = 'None'

View File

@ -1,21 +1,23 @@
import {BlueGreenDeployment} from '../../types/blueGreenTypes' import {vi} from 'vitest'
import type {MockInstance} from 'vitest'
import {BlueGreenDeployment} from '../../types/blueGreenTypes.js'
import { import {
deployBlueGreen, deployBlueGreen,
deployBlueGreenIngress, deployBlueGreenIngress,
deployBlueGreenService, deployBlueGreenService,
deployBlueGreenSMI deployBlueGreenSMI
} from './deploy' } from './deploy.js'
import * as routeTester from './route' import * as routeTester from './route.js'
import {Kubectl} from '../../types/kubectl' import {Kubectl} from '../../types/kubectl.js'
import {RouteStrategy} from '../../types/routeStrategy' import {RouteStrategy} from '../../types/routeStrategy.js'
import * as TSutils from '../../utilities/trafficSplitUtils' import * as TSutils from '../../utilities/trafficSplitUtils.js'
import * as bgHelper from './blueGreenHelper' import * as bgHelper from './blueGreenHelper.js'
import * as smiHelper from './smiBlueGreenHelper' import * as smiHelper from './smiBlueGreenHelper.js'
import {ExecOutput} from '@actions/exec' import {ExecOutput} from '@actions/exec'
const ingressFilepath = ['test/unit/manifests/test-ingress-new.yml'] const ingressFilepath = ['test/unit/manifests/test-ingress-new.yml']
jest.mock('../../types/kubectl') vi.mock('../../types/kubectl')
// Shared variables and mock objects used across all test suites // Shared variables and mock objects used across all test suites
const mockDeployResult = { const mockDeployResult = {
@ -30,7 +32,7 @@ const mockBgDeployment: BlueGreenDeployment = {
describe('deploy tests', () => { describe('deploy tests', () => {
let kubectl: Kubectl let kubectl: Kubectl
let kubectlApplySpy: jest.SpyInstance let kubectlApplySpy: MockInstance
const mockSuccessResult: ExecOutput = { const mockSuccessResult: ExecOutput = {
stdout: 'deployment.apps/nginx-deployment created', stdout: 'deployment.apps/nginx-deployment created',
@ -45,21 +47,20 @@ describe('deploy tests', () => {
} }
beforeEach(() => { beforeEach(() => {
//@ts-ignore vi.mocked(Kubectl).mockClear()
Kubectl.mockClear()
kubectl = new Kubectl('') kubectl = new Kubectl('')
kubectlApplySpy = jest.spyOn(kubectl, 'apply') kubectlApplySpy = vi.spyOn(kubectl, 'apply')
}) })
test('correctly determines deploy type and acts accordingly', async () => { test('correctly determines deploy type and acts accordingly', async () => {
kubectlApplySpy.mockResolvedValue(mockSuccessResult) kubectlApplySpy.mockResolvedValue(mockSuccessResult)
jest vi.spyOn(routeTester, 'routeBlueGreenForDeploy').mockImplementation(() =>
.spyOn(routeTester, 'routeBlueGreenForDeploy') Promise.resolve(mockBgDeployment)
.mockImplementation(() => Promise.resolve(mockBgDeployment)) )
jest vi.spyOn(TSutils, 'getTrafficSplitAPIVersion').mockImplementation(() =>
.spyOn(TSutils, 'getTrafficSplitAPIVersion') Promise.resolve('v1alpha3')
.mockImplementation(() => Promise.resolve('v1alpha3')) )
const ingressResult = await deployBlueGreen( const ingressResult = await deployBlueGreen(
kubectl, kubectl,
@ -112,12 +113,12 @@ describe('deploy tests', () => {
fn: () => fn: () =>
deployBlueGreen(kubectl, ingressFilepath, RouteStrategy.INGRESS), deployBlueGreen(kubectl, ingressFilepath, RouteStrategy.INGRESS),
setup: () => { setup: () => {
jest vi.spyOn(routeTester, 'routeBlueGreenForDeploy').mockImplementation(
.spyOn(routeTester, 'routeBlueGreenForDeploy') () => Promise.resolve(mockBgDeployment)
.mockImplementation(() => Promise.resolve(mockBgDeployment)) )
jest vi.spyOn(TSutils, 'getTrafficSplitAPIVersion').mockImplementation(
.spyOn(TSutils, 'getTrafficSplitAPIVersion') () => Promise.resolve('v1alpha3')
.mockImplementation(() => Promise.resolve('v1alpha3')) )
} }
}, },
{ {
@ -125,24 +126,24 @@ describe('deploy tests', () => {
fn: () => fn: () =>
deployBlueGreen(kubectl, ingressFilepath, RouteStrategy.SERVICE), deployBlueGreen(kubectl, ingressFilepath, RouteStrategy.SERVICE),
setup: () => { setup: () => {
jest vi.spyOn(routeTester, 'routeBlueGreenForDeploy').mockImplementation(
.spyOn(routeTester, 'routeBlueGreenForDeploy') () => Promise.resolve(mockBgDeployment)
.mockImplementation(() => Promise.resolve(mockBgDeployment)) )
jest vi.spyOn(TSutils, 'getTrafficSplitAPIVersion').mockImplementation(
.spyOn(TSutils, 'getTrafficSplitAPIVersion') () => Promise.resolve('v1alpha3')
.mockImplementation(() => Promise.resolve('v1alpha3')) )
} }
}, },
{ {
name: 'should throw error when kubectl apply fails during blue/green deployment with SMI strategy', name: 'should throw error when kubectl apply fails during blue/green deployment with SMI strategy',
fn: () => deployBlueGreen(kubectl, ingressFilepath, RouteStrategy.SMI), fn: () => deployBlueGreen(kubectl, ingressFilepath, RouteStrategy.SMI),
setup: () => { setup: () => {
jest vi.spyOn(routeTester, 'routeBlueGreenForDeploy').mockImplementation(
.spyOn(routeTester, 'routeBlueGreenForDeploy') () => Promise.resolve(mockBgDeployment)
.mockImplementation(() => Promise.resolve(mockBgDeployment)) )
jest vi.spyOn(TSutils, 'getTrafficSplitAPIVersion').mockImplementation(
.spyOn(TSutils, 'getTrafficSplitAPIVersion') () => Promise.resolve('v1alpha3')
.mockImplementation(() => Promise.resolve('v1alpha3')) )
} }
}, },
{ {
@ -181,8 +182,7 @@ describe('deploy timeout tests', () => {
let kubectl: Kubectl let kubectl: Kubectl
beforeEach(() => { beforeEach(() => {
//@ts-ignore vi.mocked(Kubectl).mockClear()
Kubectl.mockClear()
kubectl = new Kubectl('') kubectl = new Kubectl('')
}) })
@ -190,16 +190,16 @@ describe('deploy timeout tests', () => {
const timeout = '300s' const timeout = '300s'
// Mock the helper functions that are actually called // Mock the helper functions that are actually called
const deployWithLabelSpy = jest const deployWithLabelSpy = vi
.spyOn(bgHelper, 'deployWithLabel') .spyOn(bgHelper, 'deployWithLabel')
.mockResolvedValue(mockBgDeployment) .mockResolvedValue(mockBgDeployment)
const deployObjectsSpy = jest const deployObjectsSpy = vi
.spyOn(bgHelper, 'deployObjects') .spyOn(bgHelper, 'deployObjects')
.mockResolvedValue(mockDeployResult) .mockResolvedValue(mockDeployResult)
const setupSMISpy = jest const setupSMISpy = vi
.spyOn(smiHelper, 'setupSMI') .spyOn(smiHelper, 'setupSMI')
.mockResolvedValue(mockBgDeployment) .mockResolvedValue(mockBgDeployment)
const routeSpy = jest const routeSpy = vi
.spyOn(routeTester, 'routeBlueGreenForDeploy') .spyOn(routeTester, 'routeBlueGreenForDeploy')
.mockResolvedValue(mockBgDeployment) .mockResolvedValue(mockBgDeployment)
@ -258,10 +258,10 @@ describe('deploy timeout tests', () => {
const timeout = '240s' const timeout = '240s'
// Mock the dependencies // Mock the dependencies
const deployWithLabelSpy = jest const deployWithLabelSpy = vi
.spyOn(bgHelper, 'deployWithLabel') .spyOn(bgHelper, 'deployWithLabel')
.mockResolvedValue(mockBgDeployment) .mockResolvedValue(mockBgDeployment)
const deployObjectsSpy = jest const deployObjectsSpy = vi
.spyOn(bgHelper, 'deployObjects') .spyOn(bgHelper, 'deployObjects')
.mockResolvedValue(mockDeployResult) .mockResolvedValue(mockDeployResult)
@ -290,10 +290,10 @@ describe('deploy timeout tests', () => {
const timeout = '180s' const timeout = '180s'
// Mock the dependencies // Mock the dependencies
const deployWithLabelSpy = jest const deployWithLabelSpy = vi
.spyOn(bgHelper, 'deployWithLabel') .spyOn(bgHelper, 'deployWithLabel')
.mockResolvedValue(mockBgDeployment) .mockResolvedValue(mockBgDeployment)
const deployObjectsSpy = jest const deployObjectsSpy = vi
.spyOn(bgHelper, 'deployObjects') .spyOn(bgHelper, 'deployObjects')
.mockResolvedValue(mockDeployResult) .mockResolvedValue(mockDeployResult)
@ -322,13 +322,13 @@ describe('deploy timeout tests', () => {
const timeout = '360s' const timeout = '360s'
// Mock the dependencies // Mock the dependencies
const setupSMISpy = jest const setupSMISpy = vi
.spyOn(smiHelper, 'setupSMI') .spyOn(smiHelper, 'setupSMI')
.mockResolvedValue(mockBgDeployment) .mockResolvedValue(mockBgDeployment)
const deployObjectsSpy = jest const deployObjectsSpy = vi
.spyOn(bgHelper, 'deployObjects') .spyOn(bgHelper, 'deployObjects')
.mockResolvedValue(mockDeployResult) .mockResolvedValue(mockDeployResult)
const deployWithLabelSpy = jest const deployWithLabelSpy = vi
.spyOn(bgHelper, 'deployWithLabel') .spyOn(bgHelper, 'deployWithLabel')
.mockResolvedValue(mockBgDeployment) .mockResolvedValue(mockBgDeployment)
@ -354,10 +354,10 @@ describe('deploy timeout tests', () => {
}) })
test('deploy functions without timeout should pass undefined', async () => { test('deploy functions without timeout should pass undefined', async () => {
const deployWithLabelSpy = jest const deployWithLabelSpy = vi
.spyOn(bgHelper, 'deployWithLabel') .spyOn(bgHelper, 'deployWithLabel')
.mockResolvedValue(mockBgDeployment) .mockResolvedValue(mockBgDeployment)
const deployObjectsSpy = jest const deployObjectsSpy = vi
.spyOn(bgHelper, 'deployObjects') .spyOn(bgHelper, 'deployObjects')
.mockResolvedValue(mockDeployResult) .mockResolvedValue(mockDeployResult)

View File

@ -1,23 +1,23 @@
import * as core from '@actions/core' import * as core from '@actions/core'
import {Kubectl} from '../../types/kubectl' import {Kubectl} from '../../types/kubectl.js'
import { import {
BlueGreenDeployment, BlueGreenDeployment,
BlueGreenManifests BlueGreenManifests
} from '../../types/blueGreenTypes' } from '../../types/blueGreenTypes.js'
import {RouteStrategy} from '../../types/routeStrategy' import {RouteStrategy} from '../../types/routeStrategy.js'
import { import {
deployWithLabel, deployWithLabel,
getManifestObjects, getManifestObjects,
GREEN_LABEL_VALUE, GREEN_LABEL_VALUE,
deployObjects deployObjects
} from './blueGreenHelper' } from './blueGreenHelper.js'
import {setupSMI} from './smiBlueGreenHelper' import {setupSMI} from './smiBlueGreenHelper.js'
import {routeBlueGreenForDeploy} from './route' import {routeBlueGreenForDeploy} from './route.js'
import {DeployResult} from '../../types/deployResult' import {DeployResult} from '../../types/deployResult.js'
export async function deployBlueGreen( export async function deployBlueGreen(
kubectl: Kubectl, kubectl: Kubectl,

View File

@ -1,27 +1,27 @@
import {getManifestObjects, GREEN_LABEL_VALUE} from './blueGreenHelper' import {vi} from 'vitest'
import * as bgHelper from './blueGreenHelper' import {getManifestObjects, GREEN_LABEL_VALUE} from './blueGreenHelper.js'
import * as bgHelper from './blueGreenHelper.js'
import { import {
getUpdatedBlueGreenIngress, getUpdatedBlueGreenIngress,
isIngressRouted, isIngressRouted,
validateIngresses validateIngresses
} from './ingressBlueGreenHelper' } from './ingressBlueGreenHelper.js'
import {Kubectl} from '../../types/kubectl' import {Kubectl} from '../../types/kubectl.js'
import * as fileHelper from '../../utilities/fileUtils' import * as fileHelper from '../../utilities/fileUtils.js'
const betaFilepath = ['test/unit/manifests/test-ingress.yml'] const betaFilepath = ['test/unit/manifests/test-ingress.yml']
const ingressFilepath = ['test/unit/manifests/test-ingress-new.yml'] const ingressFilepath = ['test/unit/manifests/test-ingress-new.yml']
const kubectl = new Kubectl('') const kubectl = new Kubectl('')
jest.mock('../../types/kubectl') vi.mock('../../types/kubectl')
describe('ingress blue green helpers', () => { describe('ingress blue green helpers', () => {
let testObjects let testObjects
beforeEach(() => { beforeEach(() => {
//@ts-ignore vi.mocked(Kubectl).mockClear()
Kubectl.mockClear()
testObjects = getManifestObjects(ingressFilepath) testObjects = getManifestObjects(ingressFilepath)
jest vi.spyOn(fileHelper, 'writeObjectsToFile').mockImplementationOnce(() => [
.spyOn(fileHelper, 'writeObjectsToFile') ''
.mockImplementationOnce(() => ['']) ])
}) })
test('it should correctly classify ingresses', () => { test('it should correctly classify ingresses', () => {
@ -72,7 +72,7 @@ describe('ingress blue green helpers', () => {
test('it should validate ingresses', async () => { test('it should validate ingresses', async () => {
// what if nothing gets returned from fetchResource? // what if nothing gets returned from fetchResource?
jest.spyOn(bgHelper, 'fetchResource').mockImplementation() vi.spyOn(bgHelper, 'fetchResource').mockResolvedValue(null)
let validResponse = await validateIngresses( let validResponse = await validateIngresses(
kubectl, kubectl,
testObjects.ingressEntityList, testObjects.ingressEntityList,
@ -89,9 +89,9 @@ describe('ingress blue green helpers', () => {
const mockLabels = new Map<string, string>() const mockLabels = new Map<string, string>()
mockLabels[bgHelper.BLUE_GREEN_VERSION_LABEL] = GREEN_LABEL_VALUE mockLabels[bgHelper.BLUE_GREEN_VERSION_LABEL] = GREEN_LABEL_VALUE
mockIngress.metadata.labels = mockLabels mockIngress.metadata.labels = mockLabels
jest vi.spyOn(bgHelper, 'fetchResource').mockImplementation(() =>
.spyOn(bgHelper, 'fetchResource') Promise.resolve(mockIngress)
.mockImplementation(() => Promise.resolve(mockIngress)) )
validResponse = await validateIngresses( validResponse = await validateIngresses(
kubectl, kubectl,
testObjects.ingressEntityList, testObjects.ingressEntityList,

View File

@ -1,12 +1,12 @@
import * as core from '@actions/core' import * as core from '@actions/core'
import {K8sIngress} from '../../types/k8sObject' import {K8sIngress} from '../../types/k8sObject.js'
import { import {
addBlueGreenLabelsAndAnnotations, addBlueGreenLabelsAndAnnotations,
BLUE_GREEN_VERSION_LABEL, BLUE_GREEN_VERSION_LABEL,
GREEN_LABEL_VALUE, GREEN_LABEL_VALUE,
fetchResource fetchResource
} from './blueGreenHelper' } from './blueGreenHelper.js'
import {Kubectl} from '../../types/kubectl' import {Kubectl} from '../../types/kubectl.js'
const BACKEND = 'backend' const BACKEND = 'backend'

View File

@ -1,20 +1,22 @@
import {getManifestObjects} from './blueGreenHelper' import {vi} from 'vitest'
import type {MockInstance} from 'vitest'
import {getManifestObjects} from './blueGreenHelper.js'
import { import {
promoteBlueGreenIngress, promoteBlueGreenIngress,
promoteBlueGreenService, promoteBlueGreenService,
promoteBlueGreenSMI promoteBlueGreenSMI
} from './promote' } from './promote.js'
import {TrafficSplitObject} from '../../types/k8sObject' import {TrafficSplitObject} from '../../types/k8sObject.js'
import * as servicesTester from './serviceBlueGreenHelper' import * as servicesTester from './serviceBlueGreenHelper.js'
import {Kubectl} from '../../types/kubectl' import {Kubectl} from '../../types/kubectl.js'
import {MAX_VAL, MIN_VAL, TRAFFIC_SPLIT_OBJECT} from './smiBlueGreenHelper' import {MAX_VAL, MIN_VAL, TRAFFIC_SPLIT_OBJECT} from './smiBlueGreenHelper.js'
import * as smiTester from './smiBlueGreenHelper' import * as smiTester from './smiBlueGreenHelper.js'
import * as bgHelper from './blueGreenHelper' import * as bgHelper from './blueGreenHelper.js'
import {ExecOutput} from '@actions/exec' import {ExecOutput} from '@actions/exec'
const ingressFilepath = ['test/unit/manifests/test-ingress-new.yml'] const ingressFilepath = ['test/unit/manifests/test-ingress-new.yml']
jest.mock('../../types/kubectl') vi.mock('../../types/kubectl')
// Shared variables used across all test suites // Shared variables used across all test suites
let testObjects: any let testObjects: any
@ -42,13 +44,12 @@ const mockBgDeployment = {
} }
describe('promote tests', () => { describe('promote tests', () => {
let kubectlApplySpy: jest.SpyInstance let kubectlApplySpy: MockInstance
beforeEach(() => { beforeEach(() => {
//@ts-ignore vi.mocked(Kubectl).mockClear()
Kubectl.mockClear()
testObjects = getManifestObjects(ingressFilepath) testObjects = getManifestObjects(ingressFilepath)
kubectlApplySpy = jest.spyOn(kubectl, 'apply') kubectlApplySpy = vi.spyOn(kubectl, 'apply')
}) })
test('promote blue/green ingress', async () => { test('promote blue/green ingress', async () => {
@ -57,7 +58,7 @@ describe('promote tests', () => {
const mockLabels = new Map<string, string>() const mockLabels = new Map<string, string>()
mockLabels[bgHelper.BLUE_GREEN_VERSION_LABEL] = bgHelper.GREEN_LABEL_VALUE mockLabels[bgHelper.BLUE_GREEN_VERSION_LABEL] = bgHelper.GREEN_LABEL_VALUE
jest.spyOn(bgHelper, 'fetchResource').mockImplementation(() => vi.spyOn(bgHelper, 'fetchResource').mockImplementation(() =>
Promise.resolve({ Promise.resolve({
kind: 'Ingress', kind: 'Ingress',
spec: {}, spec: {},
@ -82,7 +83,7 @@ describe('promote tests', () => {
test('fail to promote invalid blue/green ingress', async () => { test('fail to promote invalid blue/green ingress', async () => {
const mockLabels = new Map<string, string>() const mockLabels = new Map<string, string>()
mockLabels[bgHelper.BLUE_GREEN_VERSION_LABEL] = bgHelper.NONE_LABEL_VALUE mockLabels[bgHelper.BLUE_GREEN_VERSION_LABEL] = bgHelper.NONE_LABEL_VALUE
jest.spyOn(bgHelper, 'fetchResource').mockImplementation(() => vi.spyOn(bgHelper, 'fetchResource').mockImplementation(() =>
Promise.resolve({ Promise.resolve({
kind: 'Ingress', kind: 'Ingress',
spec: {}, spec: {},
@ -100,7 +101,7 @@ describe('promote tests', () => {
const mockLabels = new Map<string, string>() const mockLabels = new Map<string, string>()
mockLabels[bgHelper.BLUE_GREEN_VERSION_LABEL] = bgHelper.GREEN_LABEL_VALUE mockLabels[bgHelper.BLUE_GREEN_VERSION_LABEL] = bgHelper.GREEN_LABEL_VALUE
jest.spyOn(bgHelper, 'fetchResource').mockImplementation(() => vi.spyOn(bgHelper, 'fetchResource').mockImplementation(() =>
Promise.resolve({ Promise.resolve({
kind: 'Service', kind: 'Service',
spec: {selector: mockLabels}, spec: {selector: mockLabels},
@ -120,16 +121,16 @@ describe('promote tests', () => {
test('fail to promote invalid blue/green service', async () => { test('fail to promote invalid blue/green service', async () => {
const mockLabels = new Map<string, string>() const mockLabels = new Map<string, string>()
mockLabels[bgHelper.BLUE_GREEN_VERSION_LABEL] = bgHelper.NONE_LABEL_VALUE mockLabels[bgHelper.BLUE_GREEN_VERSION_LABEL] = bgHelper.NONE_LABEL_VALUE
jest.spyOn(bgHelper, 'fetchResource').mockImplementation(() => vi.spyOn(bgHelper, 'fetchResource').mockImplementation(() =>
Promise.resolve({ Promise.resolve({
kind: 'Service', kind: 'Service',
spec: {}, spec: {},
metadata: {labels: mockLabels, name: 'nginx-ingress-green'} metadata: {labels: mockLabels, name: 'nginx-ingress-green'}
}) })
) )
jest vi.spyOn(servicesTester, 'validateServicesState').mockImplementationOnce(
.spyOn(servicesTester, 'validateServicesState') () => Promise.resolve(false)
.mockImplementationOnce(() => Promise.resolve(false)) )
await expect( await expect(
promoteBlueGreenService(kubectl, testObjects) promoteBlueGreenService(kubectl, testObjects)
@ -164,9 +165,9 @@ describe('promote tests', () => {
] ]
} }
} }
jest vi.spyOn(bgHelper, 'fetchResource').mockImplementation(() =>
.spyOn(bgHelper, 'fetchResource') Promise.resolve(mockTsObject)
.mockImplementation(() => Promise.resolve(mockTsObject)) )
const deployResult = await promoteBlueGreenSMI(kubectl, testObjects) const deployResult = await promoteBlueGreenSMI(kubectl, testObjects)
@ -182,11 +183,11 @@ describe('promote tests', () => {
test('promote blue/green SMI with bad trafficsplit', async () => { test('promote blue/green SMI with bad trafficsplit', async () => {
const mockLabels = new Map<string, string>() const mockLabels = new Map<string, string>()
mockLabels[bgHelper.BLUE_GREEN_VERSION_LABEL] = bgHelper.NONE_LABEL_VALUE mockLabels[bgHelper.BLUE_GREEN_VERSION_LABEL] = bgHelper.NONE_LABEL_VALUE
jest vi.spyOn(smiTester, 'validateTrafficSplitsState').mockImplementation(() =>
.spyOn(smiTester, 'validateTrafficSplitsState') Promise.resolve(false)
.mockImplementation(() => Promise.resolve(false)) )
expect(promoteBlueGreenSMI(kubectl, testObjects)).rejects.toThrow() await expect(promoteBlueGreenSMI(kubectl, testObjects)).rejects.toThrow()
}) })
// Consolidated error tests // Consolidated error tests
@ -198,7 +199,7 @@ describe('promote tests', () => {
const mockLabels = new Map<string, string>() const mockLabels = new Map<string, string>()
mockLabels[bgHelper.BLUE_GREEN_VERSION_LABEL] = mockLabels[bgHelper.BLUE_GREEN_VERSION_LABEL] =
bgHelper.GREEN_LABEL_VALUE bgHelper.GREEN_LABEL_VALUE
jest.spyOn(bgHelper, 'fetchResource').mockImplementation(() => vi.spyOn(bgHelper, 'fetchResource').mockImplementation(() =>
Promise.resolve({ Promise.resolve({
kind: 'Ingress', kind: 'Ingress',
spec: {}, spec: {},
@ -214,16 +215,16 @@ describe('promote tests', () => {
const mockLabels = new Map<string, string>() const mockLabels = new Map<string, string>()
mockLabels[bgHelper.BLUE_GREEN_VERSION_LABEL] = mockLabels[bgHelper.BLUE_GREEN_VERSION_LABEL] =
bgHelper.GREEN_LABEL_VALUE bgHelper.GREEN_LABEL_VALUE
jest.spyOn(bgHelper, 'fetchResource').mockImplementation(() => vi.spyOn(bgHelper, 'fetchResource').mockImplementation(() =>
Promise.resolve({ Promise.resolve({
kind: 'Service', kind: 'Service',
spec: {selector: mockLabels}, spec: {selector: mockLabels},
metadata: {labels: mockLabels, name: 'nginx-service-green'} metadata: {labels: mockLabels, name: 'nginx-service-green'}
}) })
) )
jest vi.spyOn(servicesTester, 'validateServicesState').mockResolvedValue(
.spyOn(servicesTester, 'validateServicesState') true
.mockResolvedValue(true) )
} }
}, },
{ {
@ -246,12 +247,10 @@ describe('promote tests', () => {
] ]
} }
} }
jest vi.spyOn(bgHelper, 'fetchResource').mockResolvedValue(mockTsObject)
.spyOn(bgHelper, 'fetchResource') vi.spyOn(smiTester, 'validateTrafficSplitsState').mockResolvedValue(
.mockResolvedValue(mockTsObject) true
jest )
.spyOn(smiTester, 'validateTrafficSplitsState')
.mockResolvedValue(true)
} }
} }
])('$name', async ({fn, setup}) => { ])('$name', async ({fn, setup}) => {
@ -279,15 +278,12 @@ describe('promote tests', () => {
// Timeout tests // Timeout tests
describe('promote timeout tests', () => { describe('promote timeout tests', () => {
beforeEach(() => { beforeEach(() => {
// @ts-ignore vi.mocked(Kubectl).mockClear()
Kubectl.mockClear()
testObjects = getManifestObjects(ingressFilepath) testObjects = getManifestObjects(ingressFilepath)
}) })
const mockDeployWithLabel = () => const mockDeployWithLabel = () =>
jest vi.spyOn(bgHelper, 'deployWithLabel').mockResolvedValue(mockBgDeployment)
.spyOn(bgHelper, 'deployWithLabel')
.mockResolvedValue(mockBgDeployment)
const setupFetchResource = ( const setupFetchResource = (
kind: string, kind: string,
@ -297,7 +293,7 @@ describe('promote timeout tests', () => {
const mockLabels = new Map<string, string>() const mockLabels = new Map<string, string>()
mockLabels[bgHelper.BLUE_GREEN_VERSION_LABEL] = labelValue mockLabels[bgHelper.BLUE_GREEN_VERSION_LABEL] = labelValue
jest.spyOn(bgHelper, 'fetchResource').mockResolvedValue({ vi.spyOn(bgHelper, 'fetchResource').mockResolvedValue({
kind, kind,
spec: {}, spec: {},
metadata: {labels: mockLabels, name} metadata: {labels: mockLabels, name}
@ -330,9 +326,9 @@ describe('promote timeout tests', () => {
'nginx-service-green', 'nginx-service-green',
bgHelper.GREEN_LABEL_VALUE bgHelper.GREEN_LABEL_VALUE
) )
jest vi.spyOn(servicesTester, 'validateServicesState').mockResolvedValue(
.spyOn(servicesTester, 'validateServicesState') true
.mockResolvedValue(true) )
} }
}, },
{ {
@ -359,12 +355,10 @@ describe('promote timeout tests', () => {
} }
} }
jest vi.spyOn(bgHelper, 'fetchResource').mockResolvedValue(mockTsObject)
.spyOn(bgHelper, 'fetchResource') vi.spyOn(smiTester, 'validateTrafficSplitsState').mockResolvedValue(
.mockResolvedValue(mockTsObject) true
jest )
.spyOn(smiTester, 'validateTrafficSplitsState')
.mockResolvedValue(true)
} }
} }
])('$name', async ({fn, timeout, setup}) => { ])('$name', async ({fn, timeout, setup}) => {

View File

@ -1,13 +1,13 @@
import * as core from '@actions/core' import * as core from '@actions/core'
import {Kubectl} from '../../types/kubectl' import {Kubectl} from '../../types/kubectl.js'
import {BlueGreenDeployment} from '../../types/blueGreenTypes' import {BlueGreenDeployment} from '../../types/blueGreenTypes.js'
import {deployWithLabel, NONE_LABEL_VALUE} from './blueGreenHelper' import {deployWithLabel, NONE_LABEL_VALUE} from './blueGreenHelper.js'
import {validateIngresses} from './ingressBlueGreenHelper' import {validateIngresses} from './ingressBlueGreenHelper.js'
import {validateServicesState} from './serviceBlueGreenHelper' import {validateServicesState} from './serviceBlueGreenHelper.js'
import {validateTrafficSplitsState} from './smiBlueGreenHelper' import {validateTrafficSplitsState} from './smiBlueGreenHelper.js'
export async function promoteBlueGreenIngress( export async function promoteBlueGreenIngress(
kubectl: Kubectl, kubectl: Kubectl,

View File

@ -1,21 +1,23 @@
import {getManifestObjects} from './blueGreenHelper' import {vi} from 'vitest'
import {Kubectl} from '../../types/kubectl' import type {MockInstance} from 'vitest'
import {getManifestObjects} from './blueGreenHelper.js'
import {Kubectl} from '../../types/kubectl.js'
import * as TSutils from '../../utilities/trafficSplitUtils' import * as TSutils from '../../utilities/trafficSplitUtils.js'
import { import {
rejectBlueGreenIngress, rejectBlueGreenIngress,
rejectBlueGreenService, rejectBlueGreenService,
rejectBlueGreenSMI rejectBlueGreenSMI
} from './reject' } from './reject.js'
import * as bgHelper from './blueGreenHelper' import * as bgHelper from './blueGreenHelper.js'
import * as routeHelper from './route' import * as routeHelper from './route.js'
const ingressFilepath = ['test/unit/manifests/test-ingress-new.yml'] const ingressFilepath = ['test/unit/manifests/test-ingress-new.yml']
const kubectl = new Kubectl('') const kubectl = new Kubectl('')
const TEST_TIMEOUT_SHORT = '60s' const TEST_TIMEOUT_SHORT = '60s'
const TEST_TIMEOUT_LONG = '120s' const TEST_TIMEOUT_LONG = '120s'
jest.mock('../../types/kubectl') vi.mock('../../types/kubectl')
// Shared mock objects following DRY principle // Shared mock objects following DRY principle
const mockSuccessResult = { const mockSuccessResult = {
@ -54,14 +56,13 @@ const mockDeleteResult = [
describe('reject tests', () => { describe('reject tests', () => {
let testObjects: any let testObjects: any
let kubectlApplySpy: jest.SpyInstance let kubectlApplySpy: MockInstance
beforeEach(() => { beforeEach(() => {
//@ts-ignore vi.mocked(Kubectl).mockClear()
Kubectl.mockClear() vi.restoreAllMocks()
jest.restoreAllMocks()
testObjects = getManifestObjects(ingressFilepath) testObjects = getManifestObjects(ingressFilepath)
kubectlApplySpy = jest.spyOn(kubectl, 'apply') kubectlApplySpy = vi.spyOn(kubectl, 'apply')
}) })
test('reject blue/green ingress', async () => { test('reject blue/green ingress', async () => {
@ -89,13 +90,13 @@ describe('reject tests', () => {
test('reject blue/green ingress with timeout', async () => { test('reject blue/green ingress with timeout', async () => {
// Mock routeBlueGreenIngressUnchanged and deleteGreenObjects // Mock routeBlueGreenIngressUnchanged and deleteGreenObjects
jest vi.spyOn(routeHelper, 'routeBlueGreenIngressUnchanged').mockResolvedValue(
.spyOn(routeHelper, 'routeBlueGreenIngressUnchanged') mockBgDeployment
.mockResolvedValue(mockBgDeployment) )
jest vi.spyOn(bgHelper, 'deleteGreenObjects').mockResolvedValue(
.spyOn(bgHelper, 'deleteGreenObjects') mockDeleteResult
.mockResolvedValue(mockDeleteResult) )
const value = await rejectBlueGreenIngress( const value = await rejectBlueGreenIngress(
kubectl, kubectl,
@ -138,9 +139,9 @@ describe('reject tests', () => {
test('reject blue/green service', async () => { test('reject blue/green service', async () => {
kubectlApplySpy.mockResolvedValue(mockSuccessResult) kubectlApplySpy.mockResolvedValue(mockSuccessResult)
jest vi.spyOn(bgHelper, 'deleteGreenObjects').mockResolvedValue(
.spyOn(bgHelper, 'deleteGreenObjects') mockDeleteResult
.mockResolvedValue(mockDeleteResult) )
const value = await rejectBlueGreenService( const value = await rejectBlueGreenService(
kubectl, kubectl,
@ -163,7 +164,7 @@ describe('reject tests', () => {
test('reject blue/green service with timeout', async () => { test('reject blue/green service with timeout', async () => {
// Mock routeBlueGreenService and deleteGreenObjects // Mock routeBlueGreenService and deleteGreenObjects
jest.spyOn(routeHelper, 'routeBlueGreenService').mockResolvedValue({ vi.spyOn(routeHelper, 'routeBlueGreenService').mockResolvedValue({
deployResult: { deployResult: {
execResult: {stdout: '', stderr: '', exitCode: 0}, execResult: {stdout: '', stderr: '', exitCode: 0},
manifestFiles: [] manifestFiles: []
@ -180,11 +181,9 @@ describe('reject tests', () => {
] ]
}) })
jest vi.spyOn(bgHelper, 'deleteGreenObjects').mockResolvedValue([
.spyOn(bgHelper, 'deleteGreenObjects') {name: 'nginx-deployment-green', kind: 'Deployment'}
.mockResolvedValue([ ])
{name: 'nginx-deployment-green', kind: 'Deployment'}
])
const value = await rejectBlueGreenService( const value = await rejectBlueGreenService(
kubectl, kubectl,
@ -213,9 +212,9 @@ describe('reject tests', () => {
// Mock kubectl.apply to return successful result // Mock kubectl.apply to return successful result
kubectlApplySpy.mockResolvedValue(mockSuccessResult) kubectlApplySpy.mockResolvedValue(mockSuccessResult)
jest vi.spyOn(TSutils, 'getTrafficSplitAPIVersion').mockImplementation(() =>
.spyOn(TSutils, 'getTrafficSplitAPIVersion') Promise.resolve('v1alpha3')
.mockImplementation(() => Promise.resolve('v1alpha3')) )
const rejectResult = await rejectBlueGreenSMI(kubectl, testObjects) const rejectResult = await rejectBlueGreenSMI(kubectl, testObjects)
expect(rejectResult.deleteResult).toHaveLength(2) expect(rejectResult.deleteResult).toHaveLength(2)
}) })
@ -226,27 +225,27 @@ describe('reject tests', () => {
name: 'should throw error when kubectl apply fails during blue/green ingress rejection', name: 'should throw error when kubectl apply fails during blue/green ingress rejection',
fn: () => rejectBlueGreenIngress(kubectl, testObjects), fn: () => rejectBlueGreenIngress(kubectl, testObjects),
setup: () => { setup: () => {
jest vi.spyOn(bgHelper, 'deleteGreenObjects').mockResolvedValue(
.spyOn(bgHelper, 'deleteGreenObjects') mockDeleteResult
.mockResolvedValue(mockDeleteResult) )
} }
}, },
{ {
name: 'should throw error when kubectl apply fails during blue/green service rejection', name: 'should throw error when kubectl apply fails during blue/green service rejection',
fn: () => rejectBlueGreenService(kubectl, testObjects), fn: () => rejectBlueGreenService(kubectl, testObjects),
setup: () => { setup: () => {
jest vi.spyOn(bgHelper, 'deleteGreenObjects').mockResolvedValue(
.spyOn(bgHelper, 'deleteGreenObjects') mockDeleteResult
.mockResolvedValue(mockDeleteResult) )
} }
}, },
{ {
name: 'should throw error when kubectl apply fails during blue/green SMI rejection', name: 'should throw error when kubectl apply fails during blue/green SMI rejection',
fn: () => rejectBlueGreenSMI(kubectl, testObjects), fn: () => rejectBlueGreenSMI(kubectl, testObjects),
setup: () => { setup: () => {
jest vi.spyOn(TSutils, 'getTrafficSplitAPIVersion').mockImplementation(
.spyOn(TSutils, 'getTrafficSplitAPIVersion') () => Promise.resolve('v1alpha3')
.mockImplementation(() => Promise.resolve('v1alpha3')) )
} }
} }
])('$name', async ({fn, setup}) => { ])('$name', async ({fn, setup}) => {

View File

@ -1,14 +1,14 @@
import {K8sDeleteObject} from '../../types/k8sObject' import {K8sDeleteObject} from '../../types/k8sObject.js'
import {Kubectl} from '../../types/kubectl' import {Kubectl} from '../../types/kubectl.js'
import { import {
BlueGreenDeployment, BlueGreenDeployment,
BlueGreenManifests, BlueGreenManifests,
BlueGreenRejectResult BlueGreenRejectResult
} from '../../types/blueGreenTypes' } from '../../types/blueGreenTypes.js'
import {deleteGreenObjects, NONE_LABEL_VALUE} from './blueGreenHelper' import {deleteGreenObjects, NONE_LABEL_VALUE} from './blueGreenHelper.js'
import {routeBlueGreenSMI} from './route' import {routeBlueGreenSMI} from './route.js'
import {cleanupSMI} from './smiBlueGreenHelper' import {cleanupSMI} from './smiBlueGreenHelper.js'
import {routeBlueGreenIngressUnchanged, routeBlueGreenService} from './route' import {routeBlueGreenIngressUnchanged, routeBlueGreenService} from './route.js'
export async function rejectBlueGreenIngress( export async function rejectBlueGreenIngress(
kubectl: Kubectl, kubectl: Kubectl,

View File

@ -1,24 +1,28 @@
import {K8sIngress, TrafficSplitObject} from '../../types/k8sObject' import {vi} from 'vitest'
import {Kubectl} from '../../types/kubectl' import type {MockInstance} from 'vitest'
import * as fileHelper from '../../utilities/fileUtils' import {K8sIngress, TrafficSplitObject} from '../../types/k8sObject.js'
import * as TSutils from '../../utilities/trafficSplitUtils' import {Kubectl} from '../../types/kubectl.js'
import {RouteStrategy} from '../../types/routeStrategy' import * as fileHelper from '../../utilities/fileUtils.js'
import {BlueGreenManifests} from '../../types/blueGreenTypes' import * as TSutils from '../../utilities/trafficSplitUtils.js'
import {RouteStrategy} from '../../types/routeStrategy.js'
import {BlueGreenManifests} from '../../types/blueGreenTypes.js'
import { import {
BLUE_GREEN_VERSION_LABEL, BLUE_GREEN_VERSION_LABEL,
getManifestObjects, getManifestObjects,
GREEN_LABEL_VALUE GREEN_LABEL_VALUE
} from './blueGreenHelper' } from './blueGreenHelper.js'
import * as bgHelper from './blueGreenHelper.js'
import * as smiHelper from './smiBlueGreenHelper.js'
import { import {
routeBlueGreenIngress, routeBlueGreenIngress,
routeBlueGreenService, routeBlueGreenService,
routeBlueGreenForDeploy, routeBlueGreenForDeploy,
routeBlueGreenSMI, routeBlueGreenSMI,
routeBlueGreenIngressUnchanged routeBlueGreenIngressUnchanged
} from './route' } from './route.js'
jest.mock('../../types/kubectl') vi.mock('../../types/kubectl')
const ingressFilepath = ['test/unit/manifests/test-ingress-new.yml'] const ingressFilepath = ['test/unit/manifests/test-ingress-new.yml']
const kc = new Kubectl('') const kc = new Kubectl('')
@ -37,16 +41,15 @@ const mockFailureResult = {
describe('route function tests', () => { describe('route function tests', () => {
let testObjects: BlueGreenManifests let testObjects: BlueGreenManifests
let kubectlApplySpy: jest.SpyInstance let kubectlApplySpy: MockInstance
beforeEach(() => { beforeEach(() => {
//@ts-ignore vi.mocked(Kubectl).mockClear()
Kubectl.mockClear()
testObjects = getManifestObjects(ingressFilepath) testObjects = getManifestObjects(ingressFilepath)
kubectlApplySpy = jest.spyOn(kc, 'apply') kubectlApplySpy = vi.spyOn(kc, 'apply')
jest vi.spyOn(fileHelper, 'writeObjectsToFile').mockImplementationOnce(() => [
.spyOn(fileHelper, 'writeObjectsToFile') ''
.mockImplementationOnce(() => ['']) ])
}) })
test('correctly prepares blue/green ingresses for deployment', async () => { test('correctly prepares blue/green ingresses for deployment', async () => {
@ -96,9 +99,9 @@ describe('route function tests', () => {
}) })
test('correctly identifies route pattern and acts accordingly', async () => { test('correctly identifies route pattern and acts accordingly', async () => {
jest vi.spyOn(TSutils, 'getTrafficSplitAPIVersion').mockImplementation(() =>
.spyOn(TSutils, 'getTrafficSplitAPIVersion') Promise.resolve('v1alpha3')
.mockImplementation(() => Promise.resolve('v1alpha3')) )
const ingressResult = await routeBlueGreenForDeploy( const ingressResult = await routeBlueGreenForDeploy(
kc, kc,
@ -164,9 +167,9 @@ describe('route function tests', () => {
testObjects.serviceEntityList testObjects.serviceEntityList
), ),
setup: () => { setup: () => {
jest vi.spyOn(TSutils, 'getTrafficSplitAPIVersion').mockImplementation(
.spyOn(TSutils, 'getTrafficSplitAPIVersion') () => Promise.resolve('v1alpha3')
.mockImplementation(() => Promise.resolve('v1alpha3')) )
} }
}, },
{ {
@ -194,24 +197,23 @@ describe('route timeout tests', () => {
let testObjects: BlueGreenManifests let testObjects: BlueGreenManifests
beforeEach(() => { beforeEach(() => {
//@ts-ignore vi.mocked(Kubectl).mockClear()
Kubectl.mockClear()
testObjects = getManifestObjects(ingressFilepath) testObjects = getManifestObjects(ingressFilepath)
jest vi.spyOn(fileHelper, 'writeObjectsToFile').mockImplementationOnce(() => [
.spyOn(fileHelper, 'writeObjectsToFile') ''
.mockImplementationOnce(() => ['']) ])
}) })
afterEach(() => { afterEach(() => {
jest.restoreAllMocks() vi.restoreAllMocks()
}) })
test('routeBlueGreenService with timeout', async () => { test('routeBlueGreenService with timeout', async () => {
const timeout = '240s' const timeout = '240s'
// Mock deployObjects to capture timeout parameter // Mock deployObjects to capture timeout parameter
const deployObjectsSpy = jest const deployObjectsSpy = vi
.spyOn(require('./blueGreenHelper'), 'deployObjects') .spyOn(bgHelper, 'deployObjects')
.mockResolvedValue({ .mockResolvedValue({
execResult: mockSuccessResult, execResult: mockSuccessResult,
manifestFiles: [] manifestFiles: []
@ -237,23 +239,29 @@ describe('route timeout tests', () => {
test('routeBlueGreenSMI with timeout', async () => { test('routeBlueGreenSMI with timeout', async () => {
const timeout = '300s' const timeout = '300s'
jest vi.spyOn(TSutils, 'getTrafficSplitAPIVersion').mockImplementation(() =>
.spyOn(TSutils, 'getTrafficSplitAPIVersion') Promise.resolve('v1alpha3')
.mockImplementation(() => Promise.resolve('v1alpha3')) )
// Mock deployObjects and createTrafficSplitObject to capture timeout parameter // Mock deployObjects and createTrafficSplitObject to capture timeout parameter
const deployObjectsSpy = jest const deployObjectsSpy = vi
.spyOn(require('./blueGreenHelper'), 'deployObjects') .spyOn(bgHelper, 'deployObjects')
.mockResolvedValue({ .mockResolvedValue({
execResult: mockSuccessResult, execResult: mockSuccessResult,
manifestFiles: [] manifestFiles: []
}) })
const createTrafficSplitSpy = jest const createTrafficSplitSpy = vi
.spyOn(require('./smiBlueGreenHelper'), 'createTrafficSplitObject') .spyOn(smiHelper, 'createTrafficSplitObject')
.mockResolvedValue({ .mockResolvedValue({
metadata: {name: 'nginx-service-trafficsplit'}, apiVersion: 'split.smi-spec.io/v1alpha3',
spec: {backends: []} kind: 'TrafficSplit',
metadata: {
name: 'nginx-service-trafficsplit',
labels: new Map(),
annotations: new Map()
},
spec: {service: 'nginx-service', backends: []}
}) })
const value = await routeBlueGreenSMI( const value = await routeBlueGreenSMI(
@ -284,8 +292,8 @@ describe('route timeout tests', () => {
const timeout = '180s' const timeout = '180s'
// Mock deployObjects to capture timeout parameter // Mock deployObjects to capture timeout parameter
const deployObjectsSpy = jest const deployObjectsSpy = vi
.spyOn(require('./blueGreenHelper'), 'deployObjects') .spyOn(bgHelper, 'deployObjects')
.mockResolvedValue({ .mockResolvedValue({
execResult: mockSuccessResult, execResult: mockSuccessResult,
manifestFiles: [] manifestFiles: []
@ -309,8 +317,8 @@ describe('route timeout tests', () => {
}) })
test('route functions without timeout should pass undefined', async () => { test('route functions without timeout should pass undefined', async () => {
const deployObjectsSpy = jest const deployObjectsSpy = vi
.spyOn(require('./blueGreenHelper'), 'deployObjects') .spyOn(bgHelper, 'deployObjects')
.mockResolvedValue({ .mockResolvedValue({
execResult: mockSuccessResult, execResult: mockSuccessResult,
manifestFiles: [] manifestFiles: []

View File

@ -1,26 +1,26 @@
import {sleep} from '../../utilities/timeUtils' import {sleep} from '../../utilities/timeUtils.js'
import {RouteStrategy} from '../../types/routeStrategy' import {RouteStrategy} from '../../types/routeStrategy.js'
import {Kubectl} from '../../types/kubectl' import {Kubectl} from '../../types/kubectl.js'
import { import {
BlueGreenDeployment, BlueGreenDeployment,
BlueGreenManifests BlueGreenManifests
} from '../../types/blueGreenTypes' } from '../../types/blueGreenTypes.js'
import { import {
getManifestObjects, getManifestObjects,
GREEN_LABEL_VALUE, GREEN_LABEL_VALUE,
deployObjects deployObjects
} from './blueGreenHelper' } from './blueGreenHelper.js'
import { import {
getUpdatedBlueGreenIngress, getUpdatedBlueGreenIngress,
isIngressRouted isIngressRouted
} from './ingressBlueGreenHelper' } from './ingressBlueGreenHelper.js'
import {getUpdatedBlueGreenService} from './serviceBlueGreenHelper' import {getUpdatedBlueGreenService} from './serviceBlueGreenHelper.js'
import {createTrafficSplitObject} from './smiBlueGreenHelper' import {createTrafficSplitObject} from './smiBlueGreenHelper.js'
import * as core from '@actions/core' import * as core from '@actions/core'
import {K8sObject, TrafficSplitObject} from '../../types/k8sObject' import {K8sObject, TrafficSplitObject} from '../../types/k8sObject.js'
import {getBufferTime} from '../../inputUtils' import {getBufferTime} from '../../inputUtils.js'
export async function routeBlueGreenForDeploy( export async function routeBlueGreenForDeploy(
kubectl: Kubectl, kubectl: Kubectl,

View File

@ -1,26 +1,26 @@
import {vi} from 'vitest'
import * as core from '@actions/core' import * as core from '@actions/core'
import { import {
BLUE_GREEN_VERSION_LABEL, BLUE_GREEN_VERSION_LABEL,
getManifestObjects, getManifestObjects,
GREEN_LABEL_VALUE GREEN_LABEL_VALUE
} from './blueGreenHelper' } from './blueGreenHelper.js'
import * as bgHelper from './blueGreenHelper' import * as bgHelper from './blueGreenHelper.js'
import {Kubectl} from '../../types/kubectl' import {Kubectl} from '../../types/kubectl.js'
import { import {
getServiceSpecLabel, getServiceSpecLabel,
getUpdatedBlueGreenService, getUpdatedBlueGreenService,
validateServicesState validateServicesState
} from './serviceBlueGreenHelper' } from './serviceBlueGreenHelper.js'
let testObjects let testObjects
const ingressFilepath = ['test/unit/manifests/test-ingress-new.yml'] const ingressFilepath = ['test/unit/manifests/test-ingress-new.yml']
jest.mock('../../types/kubectl') vi.mock('../../types/kubectl')
const kubectl = new Kubectl('') const kubectl = new Kubectl('')
describe('blue/green service helper tests', () => { describe('blue/green service helper tests', () => {
beforeEach(() => { beforeEach(() => {
//@ts-ignore vi.mocked(Kubectl).mockClear()
Kubectl.mockClear()
testObjects = getManifestObjects(ingressFilepath) testObjects = getManifestObjects(ingressFilepath)
}) })
@ -42,7 +42,7 @@ describe('blue/green service helper tests', () => {
mockLabels[BLUE_GREEN_VERSION_LABEL] = bgHelper.GREEN_LABEL_VALUE mockLabels[BLUE_GREEN_VERSION_LABEL] = bgHelper.GREEN_LABEL_VALUE
const mockSelectors = new Map<string, string>() const mockSelectors = new Map<string, string>()
mockSelectors[BLUE_GREEN_VERSION_LABEL] = GREEN_LABEL_VALUE mockSelectors[BLUE_GREEN_VERSION_LABEL] = GREEN_LABEL_VALUE
jest.spyOn(bgHelper, 'fetchResource').mockImplementation(() => vi.spyOn(bgHelper, 'fetchResource').mockImplementation(() =>
Promise.resolve({ Promise.resolve({
kind: 'Service', kind: 'Service',
spec: {selector: mockSelectors}, spec: {selector: mockSelectors},

View File

@ -1,12 +1,12 @@
import * as core from '@actions/core' import * as core from '@actions/core'
import {K8sServiceObject} from '../../types/k8sObject' import {K8sServiceObject} from '../../types/k8sObject.js'
import {Kubectl} from '../../types/kubectl' import {Kubectl} from '../../types/kubectl.js'
import { import {
addBlueGreenLabelsAndAnnotations, addBlueGreenLabelsAndAnnotations,
BLUE_GREEN_VERSION_LABEL, BLUE_GREEN_VERSION_LABEL,
fetchResource, fetchResource,
GREEN_LABEL_VALUE GREEN_LABEL_VALUE
} from './blueGreenHelper' } from './blueGreenHelper.js'
// add green labels to configure existing service // add green labels to configure existing service
export function getUpdatedBlueGreenService( export function getUpdatedBlueGreenService(

View File

@ -1,15 +1,16 @@
import {TrafficSplitObject} from '../../types/k8sObject' import {vi} from 'vitest'
import {Kubectl} from '../../types/kubectl' import {TrafficSplitObject} from '../../types/k8sObject.js'
import * as fileHelper from '../../utilities/fileUtils' import {Kubectl} from '../../types/kubectl.js'
import * as TSutils from '../../utilities/trafficSplitUtils' import * as fileHelper from '../../utilities/fileUtils.js'
import * as TSutils from '../../utilities/trafficSplitUtils.js'
import {BlueGreenManifests} from '../../types/blueGreenTypes' import {BlueGreenManifests} from '../../types/blueGreenTypes.js'
import { import {
BLUE_GREEN_VERSION_LABEL, BLUE_GREEN_VERSION_LABEL,
getManifestObjects, getManifestObjects,
GREEN_LABEL_VALUE, GREEN_LABEL_VALUE,
NONE_LABEL_VALUE NONE_LABEL_VALUE
} from './blueGreenHelper' } from './blueGreenHelper.js'
import { import {
cleanupSMI, cleanupSMI,
@ -21,10 +22,10 @@ import {
setupSMI, setupSMI,
TRAFFIC_SPLIT_OBJECT, TRAFFIC_SPLIT_OBJECT,
validateTrafficSplitsState validateTrafficSplitsState
} from './smiBlueGreenHelper' } from './smiBlueGreenHelper.js'
import * as bgHelper from './blueGreenHelper' import * as bgHelper from './blueGreenHelper.js'
jest.mock('../../types/kubectl') vi.mock('../../types/kubectl')
const kc = new Kubectl('') const kc = new Kubectl('')
const ingressFilepath = ['test/unit/manifests/test-ingress-new.yml'] const ingressFilepath = ['test/unit/manifests/test-ingress-new.yml']
@ -68,21 +69,20 @@ const mockTsObject: TrafficSplitObject = {
describe('SMI Helper tests', () => { describe('SMI Helper tests', () => {
let testObjects: BlueGreenManifests let testObjects: BlueGreenManifests
beforeEach(() => { beforeEach(() => {
//@ts-ignore vi.mocked(Kubectl).mockClear()
Kubectl.mockClear()
jest vi.spyOn(TSutils, 'getTrafficSplitAPIVersion').mockImplementation(() =>
.spyOn(TSutils, 'getTrafficSplitAPIVersion') Promise.resolve('')
.mockImplementation(() => Promise.resolve('')) )
testObjects = getManifestObjects(ingressFilepath) testObjects = getManifestObjects(ingressFilepath)
jest vi.spyOn(fileHelper, 'writeObjectsToFile').mockImplementationOnce(() => [
.spyOn(fileHelper, 'writeObjectsToFile') ''
.mockImplementationOnce(() => ['']) ])
}) })
test('setupSMI tests', async () => { test('setupSMI tests', async () => {
jest.spyOn(kc, 'apply').mockResolvedValue(mockSuccessResult) vi.spyOn(kc, 'apply').mockResolvedValue(mockSuccessResult)
const smiResults = await setupSMI(kc, testObjects.serviceEntityList) const smiResults = await setupSMI(kc, testObjects.serviceEntityList)
@ -174,9 +174,9 @@ describe('SMI Helper tests', () => {
}) })
test('validateTrafficSplitsState', async () => { test('validateTrafficSplitsState', async () => {
jest vi.spyOn(bgHelper, 'fetchResource').mockImplementation(() =>
.spyOn(bgHelper, 'fetchResource') Promise.resolve(mockTsObject)
.mockImplementation(() => Promise.resolve(mockTsObject)) )
let valResult = await validateTrafficSplitsState( let valResult = await validateTrafficSplitsState(
kc, kc,
@ -187,9 +187,9 @@ describe('SMI Helper tests', () => {
const mockTsCopy = JSON.parse(JSON.stringify(mockTsObject)) const mockTsCopy = JSON.parse(JSON.stringify(mockTsObject))
mockTsCopy.spec.backends[0].weight = MAX_VAL mockTsCopy.spec.backends[0].weight = MAX_VAL
jest vi.spyOn(bgHelper, 'fetchResource').mockImplementation(() =>
.spyOn(bgHelper, 'fetchResource') Promise.resolve(mockTsCopy)
.mockImplementation(() => Promise.resolve(mockTsCopy)) )
valResult = await validateTrafficSplitsState( valResult = await validateTrafficSplitsState(
kc, kc,
@ -197,7 +197,7 @@ describe('SMI Helper tests', () => {
) )
expect(valResult).toBe(false) expect(valResult).toBe(false)
jest.spyOn(bgHelper, 'fetchResource').mockImplementation() vi.spyOn(bgHelper, 'fetchResource').mockResolvedValue(null)
valResult = await validateTrafficSplitsState( valResult = await validateTrafficSplitsState(
kc, kc,
testObjects.serviceEntityList testObjects.serviceEntityList
@ -218,7 +218,7 @@ describe('SMI Helper tests', () => {
name: 'should throw error when kubectl apply fails during SMI setup', name: 'should throw error when kubectl apply fails during SMI setup',
fn: () => setupSMI(kc, testObjects.serviceEntityList), fn: () => setupSMI(kc, testObjects.serviceEntityList),
setup: () => { setup: () => {
jest.spyOn(kc, 'apply').mockResolvedValue(mockFailureResult) vi.spyOn(kc, 'apply').mockResolvedValue(mockFailureResult)
} }
} }
])('$name', async ({fn, setup}) => { ])('$name', async ({fn, setup}) => {
@ -229,7 +229,7 @@ describe('SMI Helper tests', () => {
// Timeout-specific tests // Timeout-specific tests
test('setupSMI with timeout test', async () => { test('setupSMI with timeout test', async () => {
const deployObjectsSpy = jest const deployObjectsSpy = vi
.spyOn(bgHelper, 'deployObjects') .spyOn(bgHelper, 'deployObjects')
.mockResolvedValue({ .mockResolvedValue({
execResult: mockSuccessResult, execResult: mockSuccessResult,
@ -257,7 +257,7 @@ describe('SMI Helper tests', () => {
}) })
test('createTrafficSplitObject with timeout test', async () => { test('createTrafficSplitObject with timeout test', async () => {
const deleteObjectsSpy = jest const deleteObjectsSpy = vi
.spyOn(bgHelper, 'deleteObjects') .spyOn(bgHelper, 'deleteObjects')
.mockResolvedValue() .mockResolvedValue()
@ -288,7 +288,7 @@ describe('SMI Helper tests', () => {
}) })
test('createTrafficSplitObject with GREEN_LABEL_VALUE and timeout test', async () => { test('createTrafficSplitObject with GREEN_LABEL_VALUE and timeout test', async () => {
const deleteObjectsSpy = jest const deleteObjectsSpy = vi
.spyOn(bgHelper, 'deleteObjects') .spyOn(bgHelper, 'deleteObjects')
.mockResolvedValue() .mockResolvedValue()
@ -321,7 +321,7 @@ describe('SMI Helper tests', () => {
}) })
test('cleanupSMI with timeout test', async () => { test('cleanupSMI with timeout test', async () => {
const deleteObjectsSpy = jest const deleteObjectsSpy = vi
.spyOn(bgHelper, 'deleteObjects') .spyOn(bgHelper, 'deleteObjects')
.mockResolvedValue() .mockResolvedValue()
@ -352,7 +352,7 @@ describe('SMI Helper tests', () => {
}) })
test('setupSMI without timeout test', async () => { test('setupSMI without timeout test', async () => {
const deployObjectsSpy = jest const deployObjectsSpy = vi
.spyOn(bgHelper, 'deployObjects') .spyOn(bgHelper, 'deployObjects')
.mockResolvedValue({ .mockResolvedValue({
execResult: mockSuccessResult, execResult: mockSuccessResult,
@ -375,7 +375,7 @@ describe('SMI Helper tests', () => {
}) })
test('createTrafficSplitObject without timeout test', async () => { test('createTrafficSplitObject without timeout test', async () => {
const deleteObjectsSpy = jest const deleteObjectsSpy = vi
.spyOn(bgHelper, 'deleteObjects') .spyOn(bgHelper, 'deleteObjects')
.mockResolvedValue() .mockResolvedValue()
@ -398,7 +398,7 @@ describe('SMI Helper tests', () => {
}) })
test('cleanupSMI without timeout test', async () => { test('cleanupSMI without timeout test', async () => {
const deleteObjectsSpy = jest const deleteObjectsSpy = vi
.spyOn(bgHelper, 'deleteObjects') .spyOn(bgHelper, 'deleteObjects')
.mockResolvedValue() .mockResolvedValue()

View File

@ -1,6 +1,6 @@
import * as core from '@actions/core' import * as core from '@actions/core'
import {Kubectl} from '../../types/kubectl' import {Kubectl} from '../../types/kubectl.js'
import * as kubectlUtils from '../../utilities/trafficSplitUtils' import * as kubectlUtils from '../../utilities/trafficSplitUtils.js'
import { import {
deleteObjects, deleteObjects,
deployObjects, deployObjects,
@ -11,15 +11,15 @@ import {
GREEN_SUFFIX, GREEN_SUFFIX,
NONE_LABEL_VALUE, NONE_LABEL_VALUE,
STABLE_SUFFIX STABLE_SUFFIX
} from './blueGreenHelper' } from './blueGreenHelper.js'
import {BlueGreenDeployment} from '../../types/blueGreenTypes' import {BlueGreenDeployment} from '../../types/blueGreenTypes.js'
import { import {
K8sDeleteObject, K8sDeleteObject,
K8sObject, K8sObject,
TrafficSplitObject TrafficSplitObject
} from '../../types/k8sObject' } from '../../types/k8sObject.js'
import {DeployResult} from '../../types/deployResult' import {DeployResult} from '../../types/deployResult.js'
import {inputAnnotations} from '../../inputUtils' import {inputAnnotations} from '../../inputUtils.js'
export const TRAFFIC_SPLIT_OBJECT_NAME_SUFFIX = '-trafficsplit' export const TRAFFIC_SPLIT_OBJECT_NAME_SUFFIX = '-trafficsplit'
export const TRAFFIC_SPLIT_OBJECT = 'TrafficSplit' export const TRAFFIC_SPLIT_OBJECT = 'TrafficSplit'

View File

@ -1,4 +1,4 @@
import {Kubectl} from '../../types/kubectl' import {Kubectl} from '../../types/kubectl.js'
import * as fs from 'fs' import * as fs from 'fs'
import * as yaml from 'js-yaml' import * as yaml from 'js-yaml'
import * as core from '@actions/core' import * as core from '@actions/core'
@ -7,15 +7,15 @@ import {
isDeploymentEntity, isDeploymentEntity,
isServiceEntity, isServiceEntity,
KubernetesWorkload KubernetesWorkload
} from '../../types/kubernetesTypes' } from '../../types/kubernetesTypes.js'
import * as utils from '../../utilities/manifestUpdateUtils' import * as utils from '../../utilities/manifestUpdateUtils.js'
import { import {
updateObjectAnnotations, updateObjectAnnotations,
updateObjectLabels, updateObjectLabels,
updateSelectorLabels updateSelectorLabels
} from '../../utilities/manifestUpdateUtils' } from '../../utilities/manifestUpdateUtils.js'
import {updateSpecLabels} from '../../utilities/manifestSpecLabelUtils' import {updateSpecLabels} from '../../utilities/manifestSpecLabelUtils.js'
import {checkForErrors} from '../../utilities/kubectlUtils' import {checkForErrors} from '../../utilities/kubectlUtils.js'
export const CANARY_VERSION_LABEL = 'workflow/version' export const CANARY_VERSION_LABEL = 'workflow/version'
const BASELINE_SUFFIX = '-baseline' const BASELINE_SUFFIX = '-baseline'

View File

@ -1,11 +1,15 @@
import {vi} from 'vitest'
import type {MockInstance} from 'vitest'
vi.mock('@actions/core')
import * as core from '@actions/core' import * as core from '@actions/core'
import {Kubectl} from '../../types/kubectl' import {Kubectl} from '../../types/kubectl.js'
import { import {
deployPodCanary, deployPodCanary,
calculateReplicaCountForCanary calculateReplicaCountForCanary
} from './podCanaryHelper' } from './podCanaryHelper.js'
jest.mock('../../types/kubectl') vi.mock('../../types/kubectl')
const kc = new Kubectl('') const kc = new Kubectl('')
@ -35,18 +39,17 @@ const TIMEOUT_300S = '300s'
describe('Pod Canary Helper tests', () => { describe('Pod Canary Helper tests', () => {
let mockFilePaths: string[] let mockFilePaths: string[]
let kubectlApplySpy: jest.SpyInstance let kubectlApplySpy: MockInstance
beforeEach(() => { beforeEach(() => {
//@ts-ignore vi.mocked(Kubectl).mockClear()
Kubectl.mockClear() vi.restoreAllMocks()
jest.restoreAllMocks()
mockFilePaths = testManifestFiles mockFilePaths = testManifestFiles
kubectlApplySpy = jest.spyOn(kc, 'apply') kubectlApplySpy = vi.spyOn(kc, 'apply')
// Mock core.getInput with default values // Mock core.getInput with default values
jest.spyOn(core, 'getInput').mockImplementation((name: string) => { vi.spyOn(core, 'getInput').mockImplementation((name: string) => {
switch (name) { switch (name) {
case 'percentage': case 'percentage':
return VALID_PERCENTAGE.toString() return VALID_PERCENTAGE.toString()
@ -61,7 +64,7 @@ describe('Pod Canary Helper tests', () => {
}) })
afterEach(() => { afterEach(() => {
jest.restoreAllMocks() vi.restoreAllMocks()
kubectlApplySpy.mockClear() kubectlApplySpy.mockClear()
}) })
@ -114,7 +117,7 @@ describe('Pod Canary Helper tests', () => {
}) })
test('should throw error for invalid low percentage', async () => { test('should throw error for invalid low percentage', async () => {
jest.spyOn(core, 'getInput').mockImplementation((name: string) => { vi.spyOn(core, 'getInput').mockImplementation((name: string) => {
if (name === 'percentage') return INVALID_LOW_PERCENTAGE.toString() if (name === 'percentage') return INVALID_LOW_PERCENTAGE.toString()
return '' return ''
}) })
@ -127,7 +130,7 @@ describe('Pod Canary Helper tests', () => {
}) })
test('should throw error for invalid high percentage', async () => { test('should throw error for invalid high percentage', async () => {
jest.spyOn(core, 'getInput').mockImplementation((name: string) => { vi.spyOn(core, 'getInput').mockImplementation((name: string) => {
if (name === 'percentage') return INVALID_HIGH_PERCENTAGE.toString() if (name === 'percentage') return INVALID_HIGH_PERCENTAGE.toString()
return '' return ''
}) })
@ -143,7 +146,7 @@ describe('Pod Canary Helper tests', () => {
kubectlApplySpy.mockResolvedValue(mockSuccessResult) kubectlApplySpy.mockResolvedValue(mockSuccessResult)
// Test minimum valid percentage // Test minimum valid percentage
jest.spyOn(core, 'getInput').mockImplementation((name: string) => { vi.spyOn(core, 'getInput').mockImplementation((name: string) => {
if (name === 'percentage') return MIN_PERCENTAGE.toString() if (name === 'percentage') return MIN_PERCENTAGE.toString()
return '' return ''
}) })
@ -152,7 +155,7 @@ describe('Pod Canary Helper tests', () => {
expect(resultMin.execResult).toEqual(mockSuccessResult) expect(resultMin.execResult).toEqual(mockSuccessResult)
// Test maximum valid percentage // Test maximum valid percentage
jest.spyOn(core, 'getInput').mockImplementation((name: string) => { vi.spyOn(core, 'getInput').mockImplementation((name: string) => {
if (name === 'percentage') return MAX_PERCENTAGE.toString() if (name === 'percentage') return MAX_PERCENTAGE.toString()
return '' return ''
}) })
@ -162,7 +165,7 @@ describe('Pod Canary Helper tests', () => {
}) })
test('should handle force deployment option', async () => { test('should handle force deployment option', async () => {
jest.spyOn(core, 'getInput').mockImplementation((name: string) => { vi.spyOn(core, 'getInput').mockImplementation((name: string) => {
switch (name) { switch (name) {
case 'percentage': case 'percentage':
return VALID_PERCENTAGE.toString() return VALID_PERCENTAGE.toString()
@ -186,7 +189,7 @@ describe('Pod Canary Helper tests', () => {
}) })
test('should handle server-side apply option', async () => { test('should handle server-side apply option', async () => {
jest.spyOn(core, 'getInput').mockImplementation((name: string) => { vi.spyOn(core, 'getInput').mockImplementation((name: string) => {
switch (name) { switch (name) {
case 'percentage': case 'percentage':
return VALID_PERCENTAGE.toString() return VALID_PERCENTAGE.toString()

View File

@ -1,15 +1,15 @@
import {Kubectl} from '../../types/kubectl' import {Kubectl} from '../../types/kubectl.js'
import * as core from '@actions/core' import * as core from '@actions/core'
import * as fs from 'fs' import * as fs from 'fs'
import * as yaml from 'js-yaml' import * as yaml from 'js-yaml'
import * as fileHelper from '../../utilities/fileUtils' import * as fileHelper from '../../utilities/fileUtils.js'
import * as canaryDeploymentHelper from './canaryHelper' import * as canaryDeploymentHelper from './canaryHelper.js'
import {isDeploymentEntity} from '../../types/kubernetesTypes' import {isDeploymentEntity} from '../../types/kubernetesTypes.js'
import {getReplicaCount} from '../../utilities/manifestUpdateUtils' import {getReplicaCount} from '../../utilities/manifestUpdateUtils.js'
import {DeployResult} from '../../types/deployResult' import {DeployResult} from '../../types/deployResult.js'
import {K8sObject} from '../../types/k8sObject' import {K8sObject} from '../../types/k8sObject.js'
import {checkForErrors} from '../../utilities/kubectlUtils' import {checkForErrors} from '../../utilities/kubectlUtils.js'
export async function deployPodCanary( export async function deployPodCanary(
filePaths: string[], filePaths: string[],

View File

@ -1,13 +1,34 @@
import {vi} from 'vitest'
import type {MockInstance} from 'vitest'
vi.mock('@actions/core', async (importOriginal) => {
const actual: any = await importOriginal()
return {
...actual,
getInput: vi.fn().mockReturnValue(''),
debug: vi.fn(),
info: vi.fn(),
warning: vi.fn(),
error: vi.fn(),
setFailed: vi.fn(),
setOutput: vi.fn(),
group: vi
.fn()
.mockImplementation(
async (_name: string, fn: () => Promise<void>) => await fn()
)
}
})
import * as core from '@actions/core' import * as core from '@actions/core'
import * as fs from 'fs' import * as fs from 'fs'
import {Kubectl} from '../../types/kubectl' import {Kubectl} from '../../types/kubectl.js'
import { import {
deploySMICanary, deploySMICanary,
redirectTrafficToCanaryDeployment, redirectTrafficToCanaryDeployment,
redirectTrafficToStableDeployment redirectTrafficToStableDeployment
} from './smiCanaryHelper' } from './smiCanaryHelper.js'
jest.mock('../../types/kubectl') vi.mock('../../types/kubectl')
const kc = new Kubectl('') const kc = new Kubectl('')
@ -40,22 +61,21 @@ const TIMEOUT_240S = '240s'
describe('SMI Canary Helper tests', () => { describe('SMI Canary Helper tests', () => {
let mockFilePaths: string[] let mockFilePaths: string[]
let kubectlApplySpy: jest.SpyInstance let kubectlApplySpy: MockInstance
let kubectlExecuteCommandSpy: jest.SpyInstance let kubectlExecuteCommandSpy: MockInstance
beforeEach(() => { beforeEach(() => {
//@ts-ignore vi.mocked(Kubectl).mockClear()
Kubectl.mockClear() vi.restoreAllMocks()
jest.restoreAllMocks()
mockFilePaths = testManifestFiles mockFilePaths = testManifestFiles
kubectlApplySpy = jest.spyOn(kc, 'apply') kubectlApplySpy = vi.spyOn(kc, 'apply')
kubectlExecuteCommandSpy = jest kubectlExecuteCommandSpy = vi
.spyOn(kc, 'executeCommand') .spyOn(kc, 'executeCommand')
.mockResolvedValue(mockExecuteCommandResult) .mockResolvedValue(mockExecuteCommandResult)
// Mock core.getInput with default values // Mock core.getInput with default values
jest.spyOn(core, 'getInput').mockImplementation((name: string) => { vi.spyOn(core, 'getInput').mockImplementation((name: string) => {
switch (name) { switch (name) {
case 'percentage': case 'percentage':
return '50' return '50'
@ -72,7 +92,7 @@ describe('SMI Canary Helper tests', () => {
}) })
afterEach(() => { afterEach(() => {
jest.restoreAllMocks() vi.restoreAllMocks()
kubectlApplySpy.mockClear() kubectlApplySpy.mockClear()
}) })
@ -106,7 +126,7 @@ describe('SMI Canary Helper tests', () => {
}) })
test('should handle custom replica count from input', async () => { test('should handle custom replica count from input', async () => {
jest.spyOn(core, 'getInput').mockImplementation((name: string) => { vi.spyOn(core, 'getInput').mockImplementation((name: string) => {
switch (name) { switch (name) {
case 'baseline-and-canary-replicas': case 'baseline-and-canary-replicas':
return VALID_REPLICA_COUNT.toString() return VALID_REPLICA_COUNT.toString()

View File

@ -1,17 +1,20 @@
import {Kubectl} from '../../types/kubectl' import {Kubectl} from '../../types/kubectl.js'
import * as core from '@actions/core' import * as core from '@actions/core'
import * as fs from 'fs' import * as fs from 'fs'
import * as yaml from 'js-yaml' import * as yaml from 'js-yaml'
import * as fileHelper from '../../utilities/fileUtils' import * as fileHelper from '../../utilities/fileUtils.js'
import * as kubectlUtils from '../../utilities/trafficSplitUtils' import * as kubectlUtils from '../../utilities/trafficSplitUtils.js'
import * as canaryDeploymentHelper from './canaryHelper' import * as canaryDeploymentHelper from './canaryHelper.js'
import * as podCanaryHelper from './podCanaryHelper' import * as podCanaryHelper from './podCanaryHelper.js'
import {isDeploymentEntity, isServiceEntity} from '../../types/kubernetesTypes' import {
import {checkForErrors} from '../../utilities/kubectlUtils' isDeploymentEntity,
import {inputAnnotations} from '../../inputUtils' isServiceEntity
import {DeployResult} from '../../types/deployResult' } from '../../types/kubernetesTypes.js'
import {K8sObject} from '../../types/k8sObject' import {checkForErrors} from '../../utilities/kubectlUtils.js'
import {inputAnnotations} from '../../inputUtils.js'
import {DeployResult} from '../../types/deployResult.js'
import {K8sObject} from '../../types/k8sObject.js'
const TRAFFIC_SPLIT_OBJECT_NAME_SUFFIX = '-workflow-rollout' const TRAFFIC_SPLIT_OBJECT_NAME_SUFFIX = '-workflow-rollout'
const TRAFFIC_SPLIT_OBJECT = 'TrafficSplit' const TRAFFIC_SPLIT_OBJECT = 'TrafficSplit'

View File

@ -1,41 +1,41 @@
import * as fs from 'fs' import * as fs from 'fs'
import * as yaml from 'js-yaml' import * as yaml from 'js-yaml'
import * as canaryDeploymentHelper from './canary/canaryHelper' import * as canaryDeploymentHelper from './canary/canaryHelper.js'
import * as models from '../types/kubernetesTypes' import * as models from '../types/kubernetesTypes.js'
import {isDeploymentEntity} from '../types/kubernetesTypes' import {isDeploymentEntity} from '../types/kubernetesTypes.js'
import * as fileHelper from '../utilities/fileUtils' import * as fileHelper from '../utilities/fileUtils.js'
import * as KubernetesManifestUtility from '../utilities/manifestStabilityUtils' import * as KubernetesManifestUtility from '../utilities/manifestStabilityUtils.js'
import {Kubectl, Resource} from '../types/kubectl' import {Kubectl, Resource} from '../types/kubectl.js'
import {deployPodCanary} from './canary/podCanaryHelper' import {deployPodCanary} from './canary/podCanaryHelper.js'
import {deploySMICanary} from './canary/smiCanaryHelper' import {deploySMICanary} from './canary/smiCanaryHelper.js'
import {DeploymentConfig} from '../types/deploymentConfig' import {DeploymentConfig} from '../types/deploymentConfig.js'
import {deployBlueGreen} from './blueGreen/deploy' import {deployBlueGreen} from './blueGreen/deploy.js'
import {DeploymentStrategy} from '../types/deploymentStrategy' import {DeploymentStrategy} from '../types/deploymentStrategy.js'
import * as core from '@actions/core' import * as core from '@actions/core'
import { import {
parseTrafficSplitMethod, parseTrafficSplitMethod,
TrafficSplitMethod TrafficSplitMethod
} from '../types/trafficSplitMethod' } from '../types/trafficSplitMethod.js'
import {parseRouteStrategy} from '../types/routeStrategy' import {parseRouteStrategy} from '../types/routeStrategy.js'
import {ExecOutput} from '@actions/exec' import {ExecOutput} from '@actions/exec'
import { import {
getWorkflowAnnotationKeyLabel, getWorkflowAnnotationKeyLabel,
getWorkflowAnnotations, getWorkflowAnnotations,
cleanLabel cleanLabel
} from '../utilities/workflowAnnotationUtils' } from '../utilities/workflowAnnotationUtils.js'
import { import {
annotateChildPods, annotateChildPods,
checkForErrors, checkForErrors,
getLastSuccessfulRunSha getLastSuccessfulRunSha
} from '../utilities/kubectlUtils' } from '../utilities/kubectlUtils.js'
import { import {
getWorkflowFilePath, getWorkflowFilePath,
normalizeWorkflowStrLabel normalizeWorkflowStrLabel
} from '../utilities/githubUtils' } from '../utilities/githubUtils.js'
import {getDeploymentConfig} from '../utilities/dockerUtils' import {getDeploymentConfig} from '../utilities/dockerUtils.js'
import {DeployResult} from '../types/deployResult' import {DeployResult} from '../types/deployResult.js'
import {ClusterType} from '../inputUtils' import {ClusterType} from '../inputUtils.js'
export async function deployManifests( export async function deployManifests(
files: string[], files: string[],

View File

@ -1,4 +1,4 @@
import {Action, parseAction} from './action' import {Action, parseAction} from './action.js'
describe('Action type', () => { describe('Action type', () => {
test('it has required values', () => { test('it has required values', () => {

View File

@ -1,5 +1,5 @@
import {DeployResult} from './deployResult' import {DeployResult} from './deployResult.js'
import {K8sObject, K8sDeleteObject} from './k8sObject' import {K8sObject, K8sDeleteObject} from './k8sObject.js'
export interface BlueGreenDeployment { export interface BlueGreenDeployment {
deployResult: DeployResult deployResult: DeployResult

View File

@ -1,4 +1,7 @@
import {DeploymentStrategy, parseDeploymentStrategy} from './deploymentStrategy' import {
DeploymentStrategy,
parseDeploymentStrategy
} from './deploymentStrategy.js'
describe('Deployment strategy type', () => { describe('Deployment strategy type', () => {
test('it has required values', () => { test('it has required values', () => {

View File

@ -1,4 +1,7 @@
import {DockerExec} from './docker' import {vi} from 'vitest'
vi.mock('@actions/exec')
import {DockerExec} from './docker.js'
import * as actions from '@actions/exec' import * as actions from '@actions/exec'
const dockerPath = 'dockerPath' const dockerPath = 'dockerPath'
@ -12,7 +15,7 @@ describe('Docker class', () => {
const execReturn = {exitCode: 0, stdout: 'Output', stderr: ''} const execReturn = {exitCode: 0, stdout: 'Output', stderr: ''}
beforeEach(() => { beforeEach(() => {
jest.spyOn(actions, 'getExecOutput').mockImplementation(async () => { vi.spyOn(actions, 'getExecOutput').mockImplementation(async () => {
return execReturn return execReturn
}) })
}) })
@ -60,7 +63,7 @@ describe('Docker class', () => {
const execReturn = {exitCode: 3, stdout: '', stderr: ''} const execReturn = {exitCode: 3, stdout: '', stderr: ''}
beforeEach(() => { beforeEach(() => {
jest.spyOn(actions, 'getExecOutput').mockImplementation(async () => { vi.spyOn(actions, 'getExecOutput').mockImplementation(async () => {
return execReturn return execReturn
}) })
}) })
@ -80,7 +83,7 @@ describe('Docker class', () => {
const execReturn = {exitCode: 0, stdout: '', stderr: 'Output'} const execReturn = {exitCode: 0, stdout: '', stderr: 'Output'}
beforeEach(() => { beforeEach(() => {
jest.spyOn(actions, 'getExecOutput').mockImplementation(async () => { vi.spyOn(actions, 'getExecOutput').mockImplementation(async () => {
return execReturn return execReturn
}) })
}) })

View File

@ -1,4 +1,10 @@
import {getKubectlPath, Kubectl} from './kubectl' import {vi} from 'vitest'
vi.mock('@actions/exec')
vi.mock('@actions/io')
vi.mock('@actions/core')
vi.mock('@actions/tool-cache')
import {getKubectlPath, Kubectl} from './kubectl.js'
import * as exec from '@actions/exec' import * as exec from '@actions/exec'
import * as io from '@actions/io' import * as io from '@actions/io'
import * as core from '@actions/core' import * as core from '@actions/core'
@ -9,27 +15,27 @@ describe('Kubectl path', () => {
const path = 'path' const path = 'path'
it('gets the kubectl path', async () => { it('gets the kubectl path', async () => {
jest.spyOn(core, 'getInput').mockImplementationOnce(() => '') vi.spyOn(core, 'getInput').mockImplementationOnce(() => '')
jest.spyOn(io, 'which').mockImplementationOnce(async () => path) vi.spyOn(io, 'which').mockImplementationOnce(async () => path)
expect(await getKubectlPath()).toBe(path) expect(await getKubectlPath()).toBe(path)
}) })
it('gets the kubectl path with version', async () => { it('gets the kubectl path with version', async () => {
jest.spyOn(core, 'getInput').mockImplementationOnce(() => version) vi.spyOn(core, 'getInput').mockImplementationOnce(() => version)
jest.spyOn(toolCache, 'find').mockImplementationOnce(() => path) vi.spyOn(toolCache, 'find').mockImplementationOnce(() => path)
expect(await getKubectlPath()).toBe(path) expect(await getKubectlPath()).toBe(path)
}) })
it('throws if kubectl not found', async () => { it('throws if kubectl not found', async () => {
// without version // without version
jest.spyOn(io, 'which').mockImplementationOnce(async () => '') vi.spyOn(io, 'which').mockImplementationOnce(async () => '')
await expect(() => getKubectlPath()).rejects.toThrow() await expect(() => getKubectlPath()).rejects.toThrow()
// with verision // with verision
jest.spyOn(core, 'getInput').mockImplementationOnce(() => '') vi.spyOn(core, 'getInput').mockImplementationOnce(() => '')
jest.spyOn(io, 'which').mockImplementationOnce(async () => '') vi.spyOn(io, 'which').mockImplementationOnce(async () => '')
await expect(() => getKubectlPath()).rejects.toThrow() await expect(() => getKubectlPath()).rejects.toThrow()
}) })
}) })
@ -46,7 +52,7 @@ describe('Kubectl class', () => {
const mockExecReturn = {exitCode: 0, stdout: 'Output', stderr: ''} const mockExecReturn = {exitCode: 0, stdout: 'Output', stderr: ''}
beforeEach(() => { beforeEach(() => {
jest.spyOn(exec, 'getExecOutput').mockImplementation(async () => { vi.spyOn(exec, 'getExecOutput').mockImplementation(async () => {
return mockExecReturn return mockExecReturn
}) })
}) })
@ -637,7 +643,7 @@ describe('Kubectl class', () => {
stderr: '' stderr: ''
} }
jest.spyOn(exec, 'getExecOutput').mockImplementationOnce(async () => { vi.spyOn(exec, 'getExecOutput').mockImplementationOnce(async () => {
return describeReturn return describeReturn
}) })
@ -650,7 +656,7 @@ describe('Kubectl class', () => {
const skipTls = true const skipTls = true
const kubectl = new Kubectl(kubectlPath, testNamespace, skipTls) const kubectl = new Kubectl(kubectlPath, testNamespace, skipTls)
jest.spyOn(exec, 'getExecOutput').mockImplementation(async () => { vi.spyOn(exec, 'getExecOutput').mockImplementation(async () => {
return {exitCode: 0, stderr: '', stdout: ''} return {exitCode: 0, stderr: '', stdout: ''}
}) })
@ -677,7 +683,7 @@ describe('Kubectl namespace handling', () => {
const execReturn = {exitCode: 0, stdout: 'Output', stderr: ''} const execReturn = {exitCode: 0, stdout: 'Output', stderr: ''}
beforeEach(() => { beforeEach(() => {
jest.spyOn(exec, 'getExecOutput').mockResolvedValue(execReturn) vi.spyOn(exec, 'getExecOutput').mockResolvedValue(execReturn)
}) })
const runApply = async (namespace?: string) => { const runApply = async (namespace?: string) => {

View File

@ -1,5 +1,5 @@
import {ExecOutput, getExecOutput} from '@actions/exec' import {ExecOutput, getExecOutput} from '@actions/exec'
import {createInlineArray} from '../utilities/arrayUtils' import {createInlineArray} from '../utilities/arrayUtils.js'
import * as core from '@actions/core' import * as core from '@actions/core'
import * as toolCache from '@actions/tool-cache' import * as toolCache from '@actions/tool-cache'
import * as io from '@actions/io' import * as io from '@actions/io'

View File

@ -10,7 +10,7 @@ import {
ServiceTypes, ServiceTypes,
WORKLOAD_TYPES, WORKLOAD_TYPES,
WORKLOAD_TYPES_WITH_ROLLOUT_STATUS WORKLOAD_TYPES_WITH_ROLLOUT_STATUS
} from './kubernetesTypes' } from './kubernetesTypes.js'
describe('Kubernetes types', () => { describe('Kubernetes types', () => {
it('contains kubernetes workloads', () => { it('contains kubernetes workloads', () => {

View File

@ -1,10 +1,13 @@
import * as fileUtils from '../utilities/fileUtils' import {vi} from 'vitest'
vi.mock('@actions/exec')
import * as fileUtils from '../utilities/fileUtils.js'
import fs from 'node:fs' import fs from 'node:fs'
import { import {
PrivateKubectl, PrivateKubectl,
extractFileNames, extractFileNames,
replaceFileNamesWithShallowNamesRelativeToTemp replaceFileNamesWithShallowNamesRelativeToTemp
} from './privatekubectl' } from './privatekubectl.js'
import * as exec from '@actions/exec' import * as exec from '@actions/exec'
describe('Private kubectl', () => { describe('Private kubectl', () => {
@ -17,14 +20,14 @@ describe('Private kubectl', () => {
'resourceName' 'resourceName'
) )
const spy = jest const spy = vi
.spyOn(fileUtils, 'getTempDirectory') .spyOn(fileUtils, 'getTempDirectory')
.mockImplementation(() => { .mockImplementation(() => {
return '/tmp' return '/tmp'
}) })
jest.spyOn(fs, 'writeFileSync').mockImplementation(() => {}) vi.spyOn(fs, 'writeFileSync').mockImplementation(() => {})
jest.spyOn(fs, 'readFileSync').mockImplementation((filename) => { vi.spyOn(fs, 'readFileSync').mockImplementation((filename) => {
return 'test contents' return 'test contents'
}) })
@ -48,7 +51,7 @@ describe('Private kubectl', () => {
test('Should throw well defined Error on error from Azure', async () => { test('Should throw well defined Error on error from Azure', async () => {
const errorMsg = 'An error message' const errorMsg = 'An error message'
jest.spyOn(exec, 'getExecOutput').mockImplementation(async () => { vi.spyOn(exec, 'getExecOutput').mockImplementation(async () => {
return {exitCode: 1, stdout: '', stderr: errorMsg} return {exitCode: 1, stdout: '', stderr: errorMsg}
}) })

View File

@ -1,10 +1,10 @@
import {Kubectl} from './kubectl' import {Kubectl} from './kubectl.js'
import minimist from 'minimist' import minimist from 'minimist'
import {ExecOptions, ExecOutput, getExecOutput} from '@actions/exec' import {ExecOptions, ExecOutput, getExecOutput} from '@actions/exec'
import * as core from '@actions/core' import * as core from '@actions/core'
import fs from 'node:fs' import fs from 'node:fs'
import * as path from 'path' import * as path from 'path'
import {getTempDirectory} from '../utilities/fileUtils' import {getTempDirectory} from '../utilities/fileUtils.js'
export class PrivateKubectl extends Kubectl { export class PrivateKubectl extends Kubectl {
protected async execute(args: string[], silent: boolean = false) { protected async execute(args: string[], silent: boolean = false) {

View File

@ -1,4 +1,4 @@
import {parseRouteStrategy, RouteStrategy} from './routeStrategy' import {parseRouteStrategy, RouteStrategy} from './routeStrategy.js'
describe('Route strategy type', () => { describe('Route strategy type', () => {
test('it has required values', () => { test('it has required values', () => {

View File

@ -1,4 +1,7 @@
import {parseTrafficSplitMethod, TrafficSplitMethod} from './trafficSplitMethod' import {
parseTrafficSplitMethod,
TrafficSplitMethod
} from './trafficSplitMethod.js'
describe('Traffic split method type', () => { describe('Traffic split method type', () => {
test('it has required values', () => { test('it has required values', () => {

View File

@ -1,4 +1,4 @@
import {createInlineArray} from './arrayUtils' import {createInlineArray} from './arrayUtils.js'
describe('array utilities', () => { describe('array utilities', () => {
it('creates an inline array', () => { it('creates an inline array', () => {

View File

@ -1,15 +1,18 @@
import {vi} from 'vitest'
vi.mock('@actions/io')
import * as io from '@actions/io' import * as io from '@actions/io'
import {checkDockerPath} from './dockerUtils' import {checkDockerPath} from './dockerUtils.js'
describe('docker utilities', () => { describe('docker utilities', () => {
it('checks if docker is installed', async () => { it('checks if docker is installed', async () => {
// docker installed // docker installed
const path = 'path' const path = 'path'
jest.spyOn(io, 'which').mockImplementationOnce(async () => path) vi.spyOn(io, 'which').mockImplementationOnce(async () => path)
expect(() => checkDockerPath()).not.toThrow() expect(() => checkDockerPath()).not.toThrow()
// docker not installed // docker not installed
jest.spyOn(io, 'which').mockImplementationOnce(async () => { vi.spyOn(io, 'which').mockImplementationOnce(async () => {
throw new Error('not found') throw new Error('not found')
}) })
await expect(() => checkDockerPath()).rejects.toThrow() await expect(() => checkDockerPath()).rejects.toThrow()

View File

@ -1,8 +1,8 @@
import * as io from '@actions/io' import * as io from '@actions/io'
import {DeploymentConfig} from '../types/deploymentConfig' import {DeploymentConfig} from '../types/deploymentConfig.js'
import * as core from '@actions/core' import * as core from '@actions/core'
import {DockerExec} from '../types/docker' import {DockerExec} from '../types/docker.js'
import {getNormalizedPath} from './githubUtils' import {getNormalizedPath} from './githubUtils.js'
export async function getDeploymentConfig(): Promise<DeploymentConfig> { export async function getDeploymentConfig(): Promise<DeploymentConfig> {
let helmChartPaths: string[] = let helmChartPaths: string[] =

View File

@ -1,9 +1,10 @@
import {parseDuration} from './durationUtils' import {vi, type Mocked} from 'vitest'
import {parseDuration} from './durationUtils.js'
import * as core from '@actions/core' import * as core from '@actions/core'
// Mock core.debug // Mock core.debug
jest.mock('@actions/core') vi.mock('@actions/core')
const mockCore = core as jest.Mocked<typeof core> const mockCore = core as Mocked<typeof core>
// Test data constants // Test data constants
const VALID_TIMEOUTS = { const VALID_TIMEOUTS = {
@ -46,7 +47,7 @@ const expectInvalidTimeout = (input: string, expectedError: string) => {
describe('validateTimeoutDuration', () => { describe('validateTimeoutDuration', () => {
beforeEach(() => { beforeEach(() => {
jest.clearAllMocks() vi.clearAllMocks()
}) })
describe('valid timeout formats', () => { describe('valid timeout formats', () => {
@ -90,7 +91,7 @@ describe('validateTimeoutDuration', () => {
'No unit specified for timeout "5", assuming minutes' 'No unit specified for timeout "5", assuming minutes'
) )
jest.clearAllMocks() vi.clearAllMocks()
parseDuration('30s') parseDuration('30s')
expect(mockCore.debug).not.toHaveBeenCalled() expect(mockCore.debug).not.toHaveBeenCalled()

View File

@ -1,9 +1,10 @@
import * as fileUtils from './fileUtils' import {vi} from 'vitest'
import * as fileUtils from './fileUtils.js'
import * as yaml from 'js-yaml' import * as yaml from 'js-yaml'
import fs from 'node:fs' import fs from 'node:fs'
import * as path from 'path' import * as path from 'path'
import {K8sObject} from '../types/k8sObject' import {K8sObject} from '../types/k8sObject.js'
const sampleYamlUrl = const sampleYamlUrl =
'https://raw.githubusercontent.com/kubernetes/website/main/content/en/examples/controllers/nginx-deployment.yaml' 'https://raw.githubusercontent.com/kubernetes/website/main/content/en/examples/controllers/nginx-deployment.yaml'
@ -69,7 +70,7 @@ describe('File utils', () => {
'manifest_test_dir' 'manifest_test_dir'
) )
expect( await expect(
fileUtils.getFilesFromDirectoriesAndURLs([badPath, goodPath]) fileUtils.getFilesFromDirectoriesAndURLs([badPath, goodPath])
).rejects.toThrow() ).rejects.toThrow()
}) })
@ -108,8 +109,8 @@ describe('File utils', () => {
describe('moving files to temp', () => { describe('moving files to temp', () => {
it('correctly moves the contents of a file to the temporary directory', () => { it('correctly moves the contents of a file to the temporary directory', () => {
jest.spyOn(fs, 'writeFileSync').mockImplementation(() => {}) vi.spyOn(fs, 'writeFileSync').mockImplementation(() => {})
jest.spyOn(fs, 'readFileSync').mockImplementation((filename) => { vi.spyOn(fs, 'readFileSync').mockImplementation((filename) => {
return 'test contents' return 'test contents'
}) })
const originalFilePath = path.join('path', 'in', 'repo') const originalFilePath = path.join('path', 'in', 'repo')

View File

@ -4,10 +4,10 @@ import * as path from 'path'
import * as core from '@actions/core' import * as core from '@actions/core'
import * as os from 'os' import * as os from 'os'
import * as yaml from 'js-yaml' import * as yaml from 'js-yaml'
import {Errorable, succeeded, failed, Failed} from '../types/errorable' import {Errorable, succeeded, failed, Failed} from '../types/errorable.js'
import {getCurrentTime} from './timeUtils' import {getCurrentTime} from './timeUtils.js'
import {isHttpUrl} from './githubUtils' import {isHttpUrl} from './githubUtils.js'
import {K8sObject} from '../types/k8sObject' import {K8sObject} from '../types/k8sObject.js'
export const urlFileKind = 'urlfile' export const urlFileKind = 'urlfile'

View File

@ -2,7 +2,7 @@ import {
getNormalizedPath, getNormalizedPath,
isHttpUrl, isHttpUrl,
normalizeWorkflowStrLabel normalizeWorkflowStrLabel
} from './githubUtils' } from './githubUtils.js'
describe('Github utils', () => { describe('Github utils', () => {
it('normalizes workflow string labels', () => { it('normalizes workflow string labels', () => {

View File

@ -1,4 +1,4 @@
import {GitHubClient, OkStatusCode} from '../types/githubClient' import {GitHubClient, OkStatusCode} from '../types/githubClient.js'
import * as core from '@actions/core' import * as core from '@actions/core'
export async function getWorkflowFilePath( export async function getWorkflowFilePath(

View File

@ -1,6 +1,9 @@
import {vi} from 'vitest'
vi.mock('@actions/core')
import * as core from '@actions/core' import * as core from '@actions/core'
import {ExecOutput} from '@actions/exec' import {ExecOutput} from '@actions/exec'
import {checkForErrors} from './kubectlUtils' import {checkForErrors} from './kubectlUtils.js'
describe('Kubectl utils', () => { describe('Kubectl utils', () => {
it('checks for errors', () => { it('checks for errors', () => {
@ -39,7 +42,8 @@ describe('Kubectl utils', () => {
).toThrow() ).toThrow()
// with warn behavior // with warn behavior
jest.spyOn(core, 'warning').mockImplementation(() => {}) const warnSpy = vi.spyOn(core, 'warning').mockImplementation(() => {})
warnSpy.mockClear()
let warningCalls = 0 let warningCalls = 0
expect(() => checkForErrors([success], true)).not.toThrow() expect(() => checkForErrors([success], true)).not.toThrow()
expect(core.warning).toHaveBeenCalledTimes(warningCalls) expect(core.warning).toHaveBeenCalledTimes(warningCalls)

View File

@ -1,6 +1,6 @@
import * as core from '@actions/core' import * as core from '@actions/core'
import {ExecOutput} from '@actions/exec' import {ExecOutput} from '@actions/exec'
import {Kubectl} from '../types/kubectl' import {Kubectl} from '../types/kubectl.js'
const NAMESPACE = 'namespace' const NAMESPACE = 'namespace'

View File

@ -1,4 +1,4 @@
import {KubernetesWorkload} from '../types/kubernetesTypes' import {KubernetesWorkload} from '../types/kubernetesTypes.js'
export function getImagePullSecrets(inputObject: any) { export function getImagePullSecrets(inputObject: any) {
const kind = inputObject?.kind?.toLowerCase() const kind = inputObject?.kind?.toLowerCase()

View File

@ -3,7 +3,7 @@ import {
isServiceEntity, isServiceEntity,
KubernetesWorkload, KubernetesWorkload,
NullInputObjectError NullInputObjectError
} from '../types/kubernetesTypes' } from '../types/kubernetesTypes.js'
export function updateSpecLabels( export function updateSpecLabels(
inputObject: any, inputObject: any,

View File

@ -1,10 +1,29 @@
import * as manifestStabilityUtils from './manifestStabilityUtils' import {vi} from 'vitest'
import {Kubectl} from '../types/kubectl' import type {MockInstance} from 'vitest'
import {ResourceTypeFleet, ResourceTypeManagedCluster} from '../actions/deploy' vi.mock('@actions/core', async (importOriginal) => {
const actual: any = await importOriginal()
return {
...actual,
getInput: vi.fn().mockReturnValue(''),
debug: vi.fn(),
info: vi.fn(),
warning: vi.fn(),
error: vi.fn(),
setFailed: vi.fn(),
setOutput: vi.fn()
}
})
import * as manifestStabilityUtils from './manifestStabilityUtils.js'
import {Kubectl} from '../types/kubectl.js'
import {
ResourceTypeFleet,
ResourceTypeManagedCluster
} from '../actions/deploy.js'
import {ExecOutput} from '@actions/exec' import {ExecOutput} from '@actions/exec'
import {exitCode, stdout} from 'process' import {exitCode, stdout} from 'process'
import * as core from '@actions/core' import * as core from '@actions/core'
import * as timeUtils from './timeUtils' import * as timeUtils from './timeUtils.js'
describe('manifestStabilityUtils', () => { describe('manifestStabilityUtils', () => {
const kc = new Kubectl('') const kc = new Kubectl('')
@ -17,8 +36,8 @@ describe('manifestStabilityUtils', () => {
] ]
it('should return immediately if the resource type is fleet', async () => { it('should return immediately if the resource type is fleet', async () => {
const spy = jest.spyOn(manifestStabilityUtils, 'checkManifestStability') const spy = vi.spyOn(manifestStabilityUtils, 'checkManifestStability')
const checkRolloutStatusSpy = jest.spyOn(kc, 'checkRolloutStatus') const checkRolloutStatusSpy = vi.spyOn(kc, 'checkRolloutStatus')
await manifestStabilityUtils.checkManifestStability( await manifestStabilityUtils.checkManifestStability(
kc, kc,
resources, resources,
@ -30,8 +49,8 @@ describe('manifestStabilityUtils', () => {
}) })
it('should run fully if the resource type is managedCluster', async () => { it('should run fully if the resource type is managedCluster', async () => {
const spy = jest.spyOn(manifestStabilityUtils, 'checkManifestStability') const spy = vi.spyOn(manifestStabilityUtils, 'checkManifestStability')
const checkRolloutStatusSpy = jest const checkRolloutStatusSpy = vi
.spyOn(kc, 'checkRolloutStatus') .spyOn(kc, 'checkRolloutStatus')
.mockImplementation(() => { .mockImplementation(() => {
return new Promise<ExecOutput>((resolve, reject) => { return new Promise<ExecOutput>((resolve, reject) => {
@ -54,7 +73,7 @@ describe('manifestStabilityUtils', () => {
it('should pass timeout to checkRolloutStatus when provided', async () => { it('should pass timeout to checkRolloutStatus when provided', async () => {
const timeout = '300s' const timeout = '300s'
const checkRolloutStatusSpy = jest const checkRolloutStatusSpy = vi
.spyOn(kc, 'checkRolloutStatus') .spyOn(kc, 'checkRolloutStatus')
.mockImplementation(() => { .mockImplementation(() => {
return new Promise<ExecOutput>((resolve, reject) => { return new Promise<ExecOutput>((resolve, reject) => {
@ -82,7 +101,7 @@ describe('manifestStabilityUtils', () => {
}) })
it('should call checkRolloutStatus without timeout when not provided', async () => { it('should call checkRolloutStatus without timeout when not provided', async () => {
const checkRolloutStatusSpy = jest const checkRolloutStatusSpy = vi
.spyOn(kc, 'checkRolloutStatus') .spyOn(kc, 'checkRolloutStatus')
.mockImplementation(() => { .mockImplementation(() => {
return new Promise<ExecOutput>((resolve, reject) => { return new Promise<ExecOutput>((resolve, reject) => {
@ -111,19 +130,19 @@ describe('manifestStabilityUtils', () => {
describe('checkManifestStability failure and resource-specific scenarios', () => { describe('checkManifestStability failure and resource-specific scenarios', () => {
let kc: Kubectl let kc: Kubectl
let coreErrorSpy: jest.SpyInstance let coreErrorSpy: MockInstance
let coreInfoSpy: jest.SpyInstance let coreInfoSpy: MockInstance
let coreWarningSpy: jest.SpyInstance let coreWarningSpy: MockInstance
beforeEach(() => { beforeEach(() => {
kc = new Kubectl('', 'default') kc = new Kubectl('', 'default')
coreErrorSpy = jest.spyOn(core, 'error').mockImplementation() coreErrorSpy = vi.spyOn(core, 'error').mockImplementation(() => {})
coreInfoSpy = jest.spyOn(core, 'info').mockImplementation() coreInfoSpy = vi.spyOn(core, 'info').mockImplementation(() => {})
coreWarningSpy = jest.spyOn(core, 'warning').mockImplementation() coreWarningSpy = vi.spyOn(core, 'warning').mockImplementation(() => {})
}) })
afterEach(() => { afterEach(() => {
jest.restoreAllMocks() vi.restoreAllMocks()
}) })
it('should call describe and collect errors when a rollout fails', async () => { it('should call describe and collect errors when a rollout fails', async () => {
@ -135,10 +154,10 @@ describe('checkManifestStability failure and resource-specific scenarios', () =>
'Events:\n Type\tReason\tMessage\n Normal\tScalingReplicaSet\tScaled up replica set failing-app-123 to 1' 'Events:\n Type\tReason\tMessage\n Normal\tScalingReplicaSet\tScaled up replica set failing-app-123 to 1'
// Arrange: Mock rollout to fail and describe to succeed // Arrange: Mock rollout to fail and describe to succeed
const checkRolloutStatusSpy = jest const checkRolloutStatusSpy = vi
.spyOn(kc, 'checkRolloutStatus') .spyOn(kc, 'checkRolloutStatus')
.mockRejectedValue(rolloutError) .mockRejectedValue(rolloutError)
const describeSpy = jest.spyOn(kc, 'describe').mockResolvedValue({ const describeSpy = vi.spyOn(kc, 'describe').mockResolvedValue({
stdout: describeOutput, stdout: describeOutput,
stderr: '', stderr: '',
exitCode: 0 exitCode: 0
@ -177,10 +196,10 @@ describe('checkManifestStability failure and resource-specific scenarios', () =>
'Events:\n Type\tReason\tMessage\n Normal\tScalingReplicaSet\tScaled up replica set failing-app-123 to 1' 'Events:\n Type\tReason\tMessage\n Normal\tScalingReplicaSet\tScaled up replica set failing-app-123 to 1'
// Arrange: Mock rollout to fail and describe to succeed // Arrange: Mock rollout to fail and describe to succeed
const checkRolloutStatusSpy = jest const checkRolloutStatusSpy = vi
.spyOn(kc, 'checkRolloutStatus') .spyOn(kc, 'checkRolloutStatus')
.mockRejectedValue(rolloutError) .mockRejectedValue(rolloutError)
const describeSpy = jest.spyOn(kc, 'describe').mockResolvedValue({ const describeSpy = vi.spyOn(kc, 'describe').mockResolvedValue({
stdout: describeOutput, stdout: describeOutput,
stderr: '', stderr: '',
exitCode: 0 exitCode: 0
@ -216,10 +235,10 @@ describe('checkManifestStability failure and resource-specific scenarios', () =>
const resources = [{type: 'Pod', name: 'test-pod', namespace: 'default'}] const resources = [{type: 'Pod', name: 'test-pod', namespace: 'default'}]
// Arrange: Spy on checkPodStatus and checkRolloutStatus // Arrange: Spy on checkPodStatus and checkRolloutStatus
const checkPodStatusSpy = jest const checkPodStatusSpy = vi
.spyOn(manifestStabilityUtils, 'checkPodStatus') .spyOn(manifestStabilityUtils, 'checkPodStatus')
.mockResolvedValue() // Assume pod becomes ready .mockResolvedValue() // Assume pod becomes ready
const checkRolloutStatusSpy = jest.spyOn(kc, 'checkRolloutStatus') const checkRolloutStatusSpy = vi.spyOn(kc, 'checkRolloutStatus')
// Act // Act
await manifestStabilityUtils.checkManifestStability( await manifestStabilityUtils.checkManifestStability(
@ -240,10 +259,10 @@ describe('checkManifestStability failure and resource-specific scenarios', () =>
const podError = new Error('Pod rollout failed') const podError = new Error('Pod rollout failed')
// Arrange: Mock checkPodStatus to fail // Arrange: Mock checkPodStatus to fail
const checkPodStatusSpy = jest const checkPodStatusSpy = vi
.spyOn(manifestStabilityUtils, 'checkPodStatus') .spyOn(manifestStabilityUtils, 'checkPodStatus')
.mockRejectedValue(podError) .mockRejectedValue(podError)
const describeSpy = jest.spyOn(kc, 'describe').mockResolvedValue({ const describeSpy = vi.spyOn(kc, 'describe').mockResolvedValue({
stdout: 'describe output', stdout: 'describe output',
stderr: '', stderr: '',
exitCode: 0 exitCode: 0
@ -271,7 +290,7 @@ describe('checkManifestStability failure and resource-specific scenarios', () =>
it('should wait for external IP for a LoadBalancer service', async () => { it('should wait for external IP for a LoadBalancer service', async () => {
//Spying on sleep to avoid actual delays in tests //Spying on sleep to avoid actual delays in tests
jest.spyOn(timeUtils, 'sleep').mockResolvedValue(undefined) vi.spyOn(timeUtils, 'sleep').mockResolvedValue(undefined)
const resources = [ const resources = [
{type: 'service', name: 'test-svc', namespace: 'default'} {type: 'service', name: 'test-svc', namespace: 'default'}
] ]
@ -285,7 +304,7 @@ describe('checkManifestStability failure and resource-specific scenarios', () =>
} }
// Arrange: Mock getResource to simulate the IP being assigned on the second poll // Arrange: Mock getResource to simulate the IP being assigned on the second poll
const getResourceSpy = jest const getResourceSpy = vi
.spyOn(kc, 'getResource') .spyOn(kc, 'getResource')
// First call: Initial service check // First call: Initial service check
.mockResolvedValueOnce({ .mockResolvedValueOnce({
@ -328,10 +347,10 @@ describe('checkManifestStability failure and resource-specific scenarios', () =>
// Arrange: Mock getService to fail, and describe to succeed // Arrange: Mock getService to fail, and describe to succeed
// Note: We mock getResource because getService is a private helper // Note: We mock getResource because getService is a private helper
const getResourceSpy = jest const getResourceSpy = vi
.spyOn(kc, 'getResource') .spyOn(kc, 'getResource')
.mockRejectedValue(getServiceError) .mockRejectedValue(getServiceError)
const describeSpy = jest.spyOn(kc, 'describe').mockResolvedValue({ const describeSpy = vi.spyOn(kc, 'describe').mockResolvedValue({
stdout: 'describe output', stdout: 'describe output',
stderr: '', stderr: '',
exitCode: 0 exitCode: 0
@ -369,7 +388,7 @@ describe('checkManifestStability failure and resource-specific scenarios', () =>
} }
// Arrange // Arrange
const getResourceSpy = jest.spyOn(kc, 'getResource').mockResolvedValue({ const getResourceSpy = vi.spyOn(kc, 'getResource').mockResolvedValue({
stdout: JSON.stringify(clusterIpService), stdout: JSON.stringify(clusterIpService),
stderr: '', stderr: '',
exitCode: 0 exitCode: 0
@ -392,19 +411,19 @@ describe('checkManifestStability failure and resource-specific scenarios', () =>
describe('checkManifestStability additional scenarios', () => { describe('checkManifestStability additional scenarios', () => {
let kc: Kubectl let kc: Kubectl
let coreErrorSpy: jest.SpyInstance let coreErrorSpy: MockInstance
let coreInfoSpy: jest.SpyInstance let coreInfoSpy: MockInstance
let coreWarningSpy: jest.SpyInstance let coreWarningSpy: MockInstance
beforeEach(() => { beforeEach(() => {
kc = new Kubectl('') kc = new Kubectl('')
coreErrorSpy = jest.spyOn(core, 'error').mockImplementation() coreErrorSpy = vi.spyOn(core, 'error').mockImplementation(() => {})
coreInfoSpy = jest.spyOn(core, 'info').mockImplementation() coreInfoSpy = vi.spyOn(core, 'info').mockImplementation(() => {})
coreWarningSpy = jest.spyOn(core, 'warning').mockImplementation() coreWarningSpy = vi.spyOn(core, 'warning').mockImplementation(() => {})
}) })
afterEach(() => { afterEach(() => {
jest.restoreAllMocks() vi.restoreAllMocks()
}) })
it('should aggregate errors from deployment and pod failures', async () => { it('should aggregate errors from deployment and pod failures', async () => {
@ -416,15 +435,15 @@ describe('checkManifestStability additional scenarios', () => {
const podError = new Error('Pod not ready in time') const podError = new Error('Pod not ready in time')
// Arrange: Mock failures // Arrange: Mock failures
const checkRolloutStatusSpy = jest const checkRolloutStatusSpy = vi
.spyOn(kc, 'checkRolloutStatus') .spyOn(kc, 'checkRolloutStatus')
.mockRejectedValue(deploymentError) .mockRejectedValue(deploymentError)
// For pod: simulate a pod check failure // For pod: simulate a pod check failure
const checkPodStatusSpy = jest const checkPodStatusSpy = vi
.spyOn(manifestStabilityUtils, 'checkPodStatus') .spyOn(manifestStabilityUtils, 'checkPodStatus')
.mockRejectedValue(podError) .mockRejectedValue(podError)
// For both, simulate a successful describe call to provide additional details // For both, simulate a successful describe call to provide additional details
const describeSpy = jest.spyOn(kc, 'describe').mockResolvedValue({ const describeSpy = vi.spyOn(kc, 'describe').mockResolvedValue({
stdout: 'describe aggregated output', stdout: 'describe aggregated output',
stderr: '', stderr: '',
exitCode: 0 exitCode: 0
@ -463,25 +482,25 @@ describe('checkManifestStability additional scenarios', () => {
// Arrange: // Arrange:
// Deployment rollout succeeds // Deployment rollout succeeds
jest.spyOn(kc, 'checkRolloutStatus').mockResolvedValue({ vi.spyOn(kc, 'checkRolloutStatus').mockResolvedValue({
exitCode: 0, exitCode: 0,
stderr: '', stderr: '',
stdout: '' stdout: ''
}) })
// Pod becomes ready // Pod becomes ready
jest.spyOn(manifestStabilityUtils, 'checkPodStatus').mockResolvedValue() vi.spyOn(manifestStabilityUtils, 'checkPodStatus').mockResolvedValue()
// Simulate a LoadBalancer service that already has an external IP // Simulate a LoadBalancer service that already has an external IP
const stableService = { const stableService = {
spec: {type: 'LoadBalancer'}, spec: {type: 'LoadBalancer'},
status: {loadBalancer: {ingress: [{ip: '1.2.3.4'}]}} status: {loadBalancer: {ingress: [{ip: '1.2.3.4'}]}}
} }
jest.spyOn(kc, 'getResource').mockResolvedValue({ vi.spyOn(kc, 'getResource').mockResolvedValue({
stdout: JSON.stringify(stableService), stdout: JSON.stringify(stableService),
stderr: '', stderr: '',
exitCode: 0 exitCode: 0
}) })
// Provide a describe result to avoid warnings // Provide a describe result to avoid warnings
jest.spyOn(kc, 'describe').mockResolvedValue({ vi.spyOn(kc, 'describe').mockResolvedValue({
stdout: 'describe output stable', stdout: 'describe output stable',
stderr: '', stderr: '',
exitCode: 0 exitCode: 0

View File

@ -1,10 +1,10 @@
import * as core from '@actions/core' import * as core from '@actions/core'
import * as KubernetesConstants from '../types/kubernetesTypes' import * as KubernetesConstants from '../types/kubernetesTypes.js'
import {Kubectl, Resource} from '../types/kubectl' import {Kubectl, Resource} from '../types/kubectl.js'
import {checkForErrors} from './kubectlUtils' import {checkForErrors} from './kubectlUtils.js'
import {sleep} from './timeUtils' import {sleep} from './timeUtils.js'
import {ResourceTypeFleet} from '../actions/deploy' import {ResourceTypeFleet} from '../actions/deploy.js'
import {ClusterType} from '../inputUtils' import {ClusterType} from '../inputUtils.js'
const IS_SILENT = false const IS_SILENT = false
const POD = 'pod' const POD = 'pod'

View File

@ -1,14 +1,17 @@
import * as fileUtils from './fileUtils' import {vi} from 'vitest'
import * as manifestUpdateUtils from './manifestUpdateUtils' vi.mock('fs')
import * as fileUtils from './fileUtils.js'
import * as manifestUpdateUtils from './manifestUpdateUtils.js'
import * as path from 'path' import * as path from 'path'
import * as fs from 'fs' import * as fs from 'fs'
describe('manifestUpdateUtils', () => { describe('manifestUpdateUtils', () => {
jest.spyOn(fileUtils, 'moveFileToTmpDir').mockImplementation((filename) => { vi.spyOn(fileUtils, 'moveFileToTmpDir').mockImplementation((filename) => {
return path.join('/tmp', filename) return path.join('/tmp', filename)
}) })
jest.spyOn(fs, 'writeFileSync').mockImplementation(() => {}) vi.spyOn(fs, 'writeFileSync').mockImplementation(() => {})
jest.spyOn(fs, 'readFileSync').mockImplementation((filename) => { vi.spyOn(fs, 'readFileSync').mockImplementation((filename) => {
return 'test contents' return 'test contents'
}) })

View File

@ -2,25 +2,25 @@ import * as core from '@actions/core'
import * as fs from 'fs' import * as fs from 'fs'
import * as yaml from 'js-yaml' import * as yaml from 'js-yaml'
import * as path from 'path' import * as path from 'path'
import * as fileHelper from './fileUtils' import * as fileHelper from './fileUtils.js'
import {moveFileToTmpDir} from './fileUtils' import {moveFileToTmpDir} from './fileUtils.js'
import { import {
InputObjectKindNotDefinedError, InputObjectKindNotDefinedError,
InputObjectMetadataNotDefinedError, InputObjectMetadataNotDefinedError,
isWorkloadEntity, isWorkloadEntity,
KubernetesWorkload, KubernetesWorkload,
NullInputObjectError NullInputObjectError
} from '../types/kubernetesTypes' } from '../types/kubernetesTypes.js'
import { import {
getSpecSelectorLabels, getSpecSelectorLabels,
setSpecSelectorLabels setSpecSelectorLabels
} from './manifestSpecLabelUtils' } from './manifestSpecLabelUtils.js'
import { import {
getImagePullSecrets, getImagePullSecrets,
setImagePullSecrets setImagePullSecrets
} from './manifestPullSecretUtils' } from './manifestPullSecretUtils.js'
import {Resource} from '../types/kubectl' import {Resource} from '../types/kubectl.js'
import {K8sObject} from '../types/k8sObject' import {K8sObject} from '../types/k8sObject.js'
export function updateManifestFiles(manifestFilePaths: string[]) { export function updateManifestFiles(manifestFilePaths: string[]) {
if (manifestFilePaths?.length === 0) { if (manifestFilePaths?.length === 0) {

View File

@ -1,11 +1,12 @@
import { import {
getImagePullSecrets, getImagePullSecrets,
setImagePullSecrets setImagePullSecrets
} from './manifestPullSecretUtils' } from './manifestPullSecretUtils.js'
import {updateSpecLabels} from './manifestSpecLabelUtils' import {updateSpecLabels} from './manifestSpecLabelUtils.js'
import {getReplicaCount} from './manifestUpdateUtils' import {getReplicaCount} from './manifestUpdateUtils.js'
import * as yaml from 'js-yaml' import * as yaml from 'js-yaml'
import * as fs from 'fs' import * as fs from 'fs'
import {isWorkloadEntity, isDeploymentEntity} from '../types/kubernetesTypes.js'
describe('ScaledJob Support', () => { describe('ScaledJob Support', () => {
let scaledJobObject: any let scaledJobObject: any
@ -57,13 +58,11 @@ describe('ScaledJob Support', () => {
describe('Workload Classification', () => { describe('Workload Classification', () => {
it('should classify ScaledJob as workload entity', () => { it('should classify ScaledJob as workload entity', () => {
const {isWorkloadEntity} = require('../types/kubernetesTypes')
expect(isWorkloadEntity('ScaledJob')).toBe(true) expect(isWorkloadEntity('ScaledJob')).toBe(true)
expect(isWorkloadEntity('scaledjob')).toBe(true) expect(isWorkloadEntity('scaledjob')).toBe(true)
}) })
it('should not classify ScaledJob as deployment entity', () => { it('should not classify ScaledJob as deployment entity', () => {
const {isDeploymentEntity} = require('../types/kubernetesTypes')
expect(isDeploymentEntity('scaledjob')).toBe(false) expect(isDeploymentEntity('scaledjob')).toBe(false)
expect(isDeploymentEntity('ScaledJob')).toBe(false) expect(isDeploymentEntity('ScaledJob')).toBe(false)
}) })

View File

@ -1,4 +1,4 @@
import {Kubectl} from '../types/kubectl' import {Kubectl} from '../types/kubectl.js'
const trafficSplitAPIVersionPrefix = 'split.smi-spec.io' const trafficSplitAPIVersionPrefix = 'split.smi-spec.io'

View File

@ -2,7 +2,7 @@ import {
cleanLabel, cleanLabel,
removeInvalidLabelCharacters, removeInvalidLabelCharacters,
VALID_LABEL_REGEX VALID_LABEL_REGEX
} from '../utilities/workflowAnnotationUtils' } from '../utilities/workflowAnnotationUtils.js'
describe('WorkflowAnnotationUtils', () => { describe('WorkflowAnnotationUtils', () => {
describe('cleanLabel', () => { describe('cleanLabel', () => {

View File

@ -1,4 +1,4 @@
import {DeploymentConfig} from '../types/deploymentConfig' import {DeploymentConfig} from '../types/deploymentConfig.js'
const ANNOTATION_PREFIX = 'actions.github.com' const ANNOTATION_PREFIX = 'actions.github.com'

View File

@ -1,8 +1,15 @@
{ {
"compilerOptions": { "compilerOptions": {
"target": "ES6", "target": "ES2020",
"module": "commonjs", "module": "NodeNext",
"esModuleInterop": true "moduleResolution": "NodeNext",
"types": ["vitest/globals"],
"esModuleInterop": true,
"strict": false,
"skipLibCheck": true,
"resolveJsonModule": true,
"declaration": false,
"sourceMap": true
}, },
"exclude": ["node_modules", "test", "src/**/*.test.ts"] "exclude": ["node_modules", "test", "lib"]
} }

11
vitest.config.ts Normal file
View File

@ -0,0 +1,11 @@
import {defineConfig} from 'vitest/config'
export default defineConfig({
test: {
globals: true,
environment: 'node',
include: ['**/*.test.ts'],
testTimeout: 9000,
clearMocks: true
}
})