-
Notifications
You must be signed in to change notification settings - Fork 4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(toolkit): destroy tests (#33002)
Add tests for the destroy action ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
- Loading branch information
Showing
1 changed file
with
99 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
import * as chalk from 'chalk'; | ||
import { StackSelectionStrategy } from '../../lib'; | ||
import { Toolkit } from '../../lib/toolkit'; | ||
import { builderFixture, TestIoHost } from '../_helpers'; | ||
|
||
const ioHost = new TestIoHost(); | ||
const toolkit = new Toolkit({ ioHost }); | ||
jest.spyOn(toolkit, 'rollback').mockResolvedValue(); | ||
|
||
let mockDestroyStack = jest.fn().mockResolvedValue({}); | ||
|
||
jest.mock('../../lib/api/aws-cdk', () => { | ||
return { | ||
...jest.requireActual('../../lib/api/aws-cdk'), | ||
Deployments: jest.fn().mockImplementation(() => ({ | ||
destroyStack: mockDestroyStack, | ||
// resolveEnvironment: jest.fn().mockResolvedValue({}), | ||
// isSingleAssetPublished: jest.fn().mockResolvedValue(true), | ||
// readCurrentTemplate: jest.fn().mockResolvedValue({ Resources: {} }), | ||
})), | ||
}; | ||
}); | ||
|
||
beforeEach(() => { | ||
ioHost.notifySpy.mockClear(); | ||
ioHost.requestSpy.mockClear(); | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
describe('destroy', () => { | ||
test('destroy from builder', async () => { | ||
// WHEN | ||
const cx = await builderFixture(toolkit, 'stack-with-bucket'); | ||
await toolkit.destroy(cx, { stacks: { strategy: StackSelectionStrategy.ALL_STACKS } }); | ||
|
||
// THEN | ||
successfulDestroy(); | ||
}); | ||
|
||
test('request response before destroying', async () => { | ||
// WHEN | ||
const cx = await builderFixture(toolkit, 'stack-with-role'); | ||
await toolkit.destroy(cx, { stacks: { strategy: StackSelectionStrategy.ALL_STACKS } }); | ||
|
||
// THEN | ||
expect(ioHost.requestSpy).toHaveBeenCalledWith(expect.objectContaining({ | ||
action: 'destroy', | ||
level: 'info', | ||
code: 'CDK_TOOLKIT_I7010', | ||
message: expect.stringContaining('Are you sure you want to delete'), | ||
})); | ||
}); | ||
|
||
test('multiple stacks', async () => { | ||
// WHEN | ||
const cx = await builderFixture(toolkit, 'two-empty-stacks'); | ||
await toolkit.destroy(cx, { stacks: { strategy: StackSelectionStrategy.ALL_STACKS } }); | ||
|
||
// THEN | ||
expect(ioHost.notifySpy).toHaveBeenCalledWith(expect.objectContaining({ | ||
action: 'destroy', | ||
level: 'info', | ||
message: expect.stringContaining(`${chalk.blue('Stack2')}${chalk.green(': destroyed')}`), | ||
})); | ||
expect(ioHost.notifySpy).toHaveBeenCalledWith(expect.objectContaining({ | ||
action: 'destroy', | ||
level: 'info', | ||
message: expect.stringContaining(`${chalk.blue('Stack1')}${chalk.green(': destroyed')}`), | ||
})); | ||
}); | ||
|
||
test('destroy deployment fails', async () => { | ||
// GIVEN | ||
mockDestroyStack = jest.fn().mockRejectedValue({}); | ||
|
||
// WHEN | ||
const cx = await builderFixture(toolkit, 'stack-with-role'); | ||
try { | ||
await toolkit.destroy(cx, { stacks: { strategy: StackSelectionStrategy.ALL_STACKS } }); | ||
} catch (e) { | ||
// We know this will error, ignore it | ||
} | ||
|
||
// THEN | ||
expect(ioHost.notifySpy).toHaveBeenCalledWith(expect.objectContaining({ | ||
action: 'destroy', | ||
level: 'error', | ||
message: expect.stringContaining('destroy failed'), | ||
})); | ||
}); | ||
}); | ||
|
||
function successfulDestroy() { | ||
expect(ioHost.notifySpy).toHaveBeenCalledWith(expect.objectContaining({ | ||
action: 'destroy', | ||
level: 'info', | ||
message: expect.stringContaining('destroyed'), | ||
})); | ||
} |