Skip to content

Commit 7395c39

Browse files
Added error check for canary promote actions (#432)
* Added checkForErrors so canary promote action fails when there is an error * Added tests for checkForErrors * Probable integration error fix * Probable integration error fix * Revert changes back * Added checkForErrors unit tests * Fixed multiple tests issue --------- Co-authored-by: Suneha Bose <[email protected]>
1 parent f17d855 commit 7395c39

File tree

13 files changed

+1084
-142
lines changed

13 files changed

+1084
-142
lines changed

src/strategyHelpers/blueGreen/blueGreenHelper.test.ts

Lines changed: 84 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import {
22
deployWithLabel,
33
deleteGreenObjects,
4+
deployObjects,
45
fetchResource,
56
getDeploymentMatchLabels,
67
getManifestObjects,
@@ -134,12 +135,20 @@ describe('bluegreenhelper functions', () => {
134135
})
135136

136137
test('correctly makes labeled workloads', async () => {
138+
const kubectlApplySpy = jest.spyOn(kubectl, 'apply').mockResolvedValue({
139+
stdout: 'deployment.apps/nginx-deployment created',
140+
stderr: '',
141+
exitCode: 0
142+
})
143+
137144
const cwlResult: BlueGreenDeployment = await deployWithLabel(
138145
kubectl,
139146
testObjects.deploymentEntityList,
140147
GREEN_LABEL_VALUE
141148
)
142149
expect(cwlResult.deployResult.manifestFiles[0]).toBe('')
150+
151+
kubectlApplySpy.mockRestore()
143152
})
144153

145154
test('correctly makes new blue green object (getNewBlueGreenObject and addBlueGreenLabelsAndAnnotations)', () => {
@@ -219,20 +228,23 @@ describe('bluegreenhelper functions', () => {
219228
}
220229
})
221230

222-
test('returns null when fetch fails to unset k8s objects', async () => {
231+
test('returns undefined when fetch fails to unset k8s objects', async () => {
223232
const mockExecOutput = {
224-
stdout: 'this should not matter',
233+
stdout: JSON.stringify(testObjects.deploymentEntityList[0]),
225234
exitCode: 0,
226-
stderr: 'this is a fake error'
235+
stderr: ''
227236
} as ExecOutput
237+
238+
jest.spyOn(kubectl, 'getResource').mockResolvedValue(mockExecOutput)
228239
jest
229240
.spyOn(manifestUpdateUtils, 'UnsetClusterSpecificDetails')
230241
.mockImplementation(() => {
231242
throw new Error('test error')
232243
})
244+
233245
expect(
234246
await fetchResource(kubectl, 'nginx-deployment', 'Deployment')
235-
).toBe(null)
247+
).toBeUndefined()
236248
})
237249

238250
test('gets deployment labels', () => {
@@ -252,4 +264,72 @@ describe('bluegreenhelper functions', () => {
252264
getDeploymentMatchLabels(testObjects.deploymentEntityList[0])['app']
253265
).toBe('nginx')
254266
})
267+
268+
describe('deployObjects', () => {
269+
let mockObjects: any[]
270+
let kubectlApplySpy: jest.SpyInstance
271+
272+
const mockSuccessResult: ExecOutput = {
273+
stdout: 'deployment.apps/nginx-deployment created',
274+
stderr: '',
275+
exitCode: 0
276+
}
277+
278+
const mockFailureResult: ExecOutput = {
279+
stdout: '',
280+
stderr: 'error: deployment failed',
281+
exitCode: 1
282+
}
283+
284+
beforeEach(() => {
285+
// //@ts-ignore
286+
// Kubectl.mockClear()
287+
mockObjects = [testObjects.deploymentEntityList[0]]
288+
kubectlApplySpy = jest.spyOn(kubectl, 'apply')
289+
})
290+
291+
afterEach(() => {
292+
jest.clearAllMocks()
293+
})
294+
295+
it('should return execution result and manifest files when kubectl apply succeeds', async () => {
296+
kubectlApplySpy.mockClear()
297+
kubectlApplySpy.mockResolvedValue(mockSuccessResult)
298+
299+
const result = await deployObjects(kubectl, mockObjects)
300+
301+
expect(result.execResult).toEqual(mockSuccessResult)
302+
const timeoutArg = kubectlApplySpy.mock.calls[0][3]
303+
expect(
304+
typeof timeoutArg === 'string' || timeoutArg === undefined
305+
).toBe(true)
306+
307+
expect(kubectlApplySpy).toHaveBeenCalledWith(
308+
expect.any(Array),
309+
expect.any(Boolean),
310+
expect.any(Boolean),
311+
timeoutArg
312+
)
313+
expect(kubectlApplySpy).toHaveBeenCalledTimes(1)
314+
})
315+
316+
it('should throw an error when kubectl apply fails with non-zero exit code', async () => {
317+
kubectlApplySpy.mockClear()
318+
kubectlApplySpy.mockResolvedValue(mockFailureResult)
319+
320+
await expect(deployObjects(kubectl, mockObjects)).rejects.toThrow()
321+
const timeoutArg = kubectlApplySpy.mock.calls[0][3]
322+
expect(
323+
typeof timeoutArg === 'string' || timeoutArg === undefined
324+
).toBe(true)
325+
326+
expect(kubectlApplySpy).toHaveBeenCalledWith(
327+
expect.any(Array),
328+
expect.any(Boolean),
329+
expect.any(Boolean),
330+
timeoutArg
331+
)
332+
expect(kubectlApplySpy).toHaveBeenCalledTimes(1)
333+
})
334+
})
255335
})

src/strategyHelpers/blueGreen/blueGreenHelper.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,5 +296,6 @@ export async function deployObjects(
296296
timeout
297297
)
298298

299+
checkForErrors([execResult])
299300
return {execResult, manifestFiles}
300301
}

0 commit comments

Comments
 (0)