|
| 1 | +import * as chalk from 'chalk'; |
| 2 | +import { StackSelectionStrategy } from '../../lib'; |
| 3 | +import { Toolkit } from '../../lib/toolkit'; |
| 4 | +import { builderFixture, TestIoHost } from '../_helpers'; |
| 5 | + |
| 6 | +const ioHost = new TestIoHost(); |
| 7 | +const toolkit = new Toolkit({ ioHost }); |
| 8 | +jest.spyOn(toolkit, 'rollback').mockResolvedValue(); |
| 9 | + |
| 10 | +let mockDestroyStack = jest.fn().mockResolvedValue({}); |
| 11 | + |
| 12 | +jest.mock('../../lib/api/aws-cdk', () => { |
| 13 | + return { |
| 14 | + ...jest.requireActual('../../lib/api/aws-cdk'), |
| 15 | + Deployments: jest.fn().mockImplementation(() => ({ |
| 16 | + destroyStack: mockDestroyStack, |
| 17 | + // resolveEnvironment: jest.fn().mockResolvedValue({}), |
| 18 | + // isSingleAssetPublished: jest.fn().mockResolvedValue(true), |
| 19 | + // readCurrentTemplate: jest.fn().mockResolvedValue({ Resources: {} }), |
| 20 | + })), |
| 21 | + }; |
| 22 | +}); |
| 23 | + |
| 24 | +beforeEach(() => { |
| 25 | + ioHost.notifySpy.mockClear(); |
| 26 | + ioHost.requestSpy.mockClear(); |
| 27 | + jest.clearAllMocks(); |
| 28 | +}); |
| 29 | + |
| 30 | +describe('destroy', () => { |
| 31 | + test('destroy from builder', async () => { |
| 32 | + // WHEN |
| 33 | + const cx = await builderFixture(toolkit, 'stack-with-bucket'); |
| 34 | + await toolkit.destroy(cx, { stacks: { strategy: StackSelectionStrategy.ALL_STACKS } }); |
| 35 | + |
| 36 | + // THEN |
| 37 | + successfulDestroy(); |
| 38 | + }); |
| 39 | + |
| 40 | + test('request response before destroying', async () => { |
| 41 | + // WHEN |
| 42 | + const cx = await builderFixture(toolkit, 'stack-with-role'); |
| 43 | + await toolkit.destroy(cx, { stacks: { strategy: StackSelectionStrategy.ALL_STACKS } }); |
| 44 | + |
| 45 | + // THEN |
| 46 | + expect(ioHost.requestSpy).toHaveBeenCalledWith(expect.objectContaining({ |
| 47 | + action: 'destroy', |
| 48 | + level: 'info', |
| 49 | + code: 'CDK_TOOLKIT_I7010', |
| 50 | + message: expect.stringContaining('Are you sure you want to delete'), |
| 51 | + })); |
| 52 | + }); |
| 53 | + |
| 54 | + test('multiple stacks', async () => { |
| 55 | + // WHEN |
| 56 | + const cx = await builderFixture(toolkit, 'two-empty-stacks'); |
| 57 | + await toolkit.destroy(cx, { stacks: { strategy: StackSelectionStrategy.ALL_STACKS } }); |
| 58 | + |
| 59 | + // THEN |
| 60 | + expect(ioHost.notifySpy).toHaveBeenCalledWith(expect.objectContaining({ |
| 61 | + action: 'destroy', |
| 62 | + level: 'info', |
| 63 | + message: expect.stringContaining(`${chalk.blue('Stack2')}${chalk.green(': destroyed')}`), |
| 64 | + })); |
| 65 | + expect(ioHost.notifySpy).toHaveBeenCalledWith(expect.objectContaining({ |
| 66 | + action: 'destroy', |
| 67 | + level: 'info', |
| 68 | + message: expect.stringContaining(`${chalk.blue('Stack1')}${chalk.green(': destroyed')}`), |
| 69 | + })); |
| 70 | + }); |
| 71 | + |
| 72 | + test('destroy deployment fails', async () => { |
| 73 | + // GIVEN |
| 74 | + mockDestroyStack = jest.fn().mockRejectedValue({}); |
| 75 | + |
| 76 | + // WHEN |
| 77 | + const cx = await builderFixture(toolkit, 'stack-with-role'); |
| 78 | + try { |
| 79 | + await toolkit.destroy(cx, { stacks: { strategy: StackSelectionStrategy.ALL_STACKS } }); |
| 80 | + } catch (e) { |
| 81 | + // We know this will error, ignore it |
| 82 | + } |
| 83 | + |
| 84 | + // THEN |
| 85 | + expect(ioHost.notifySpy).toHaveBeenCalledWith(expect.objectContaining({ |
| 86 | + action: 'destroy', |
| 87 | + level: 'error', |
| 88 | + message: expect.stringContaining('destroy failed'), |
| 89 | + })); |
| 90 | + }); |
| 91 | +}); |
| 92 | + |
| 93 | +function successfulDestroy() { |
| 94 | + expect(ioHost.notifySpy).toHaveBeenCalledWith(expect.objectContaining({ |
| 95 | + action: 'destroy', |
| 96 | + level: 'info', |
| 97 | + message: expect.stringContaining('destroyed'), |
| 98 | + })); |
| 99 | +} |
0 commit comments