|
| 1 | +/*! |
| 2 | + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +import assert from 'assert' |
| 7 | +import { SinonSandbox, SinonStub, createSandbox } from 'sinon' |
| 8 | +import { commands } from 'vscode' |
| 9 | +import { ChangeSetDeletion } from '../../../../../awsService/cloudformation/stacks/actions/changeSetDeletionWorkflow' |
| 10 | +import { |
| 11 | + StackActionPhase, |
| 12 | + StackActionState, |
| 13 | +} from '../../../../../awsService/cloudformation/stacks/actions/stackActionRequestType' |
| 14 | +import { commandKey } from '../../../../../awsService/cloudformation/utils' |
| 15 | +import { globals } from '../../../../../shared' |
| 16 | + |
| 17 | +describe('ChangeSetDeletion', function () { |
| 18 | + let sandbox: SinonSandbox |
| 19 | + |
| 20 | + beforeEach(function () { |
| 21 | + sandbox = createSandbox() |
| 22 | + }) |
| 23 | + |
| 24 | + afterEach(function () { |
| 25 | + sandbox.restore() |
| 26 | + }) |
| 27 | + |
| 28 | + describe('delete', function () { |
| 29 | + let mockClient: any |
| 30 | + let executeCommandStub: SinonStub |
| 31 | + let getChangeSetDeletionStatusStub: SinonStub |
| 32 | + let describeChangeSetDeletionStatusStub: SinonStub |
| 33 | + let setIntervalStub: SinonStub |
| 34 | + |
| 35 | + beforeEach(function () { |
| 36 | + mockClient = { sendRequest: sandbox.stub().resolves({}) } |
| 37 | + executeCommandStub = sandbox.stub(commands, 'executeCommand').resolves() |
| 38 | + |
| 39 | + const stackActionApi = require('../../../../../awsService/cloudformation/stacks/actions/stackActionApi') |
| 40 | + getChangeSetDeletionStatusStub = sandbox.stub(stackActionApi, 'getChangeSetDeletionStatus') |
| 41 | + describeChangeSetDeletionStatusStub = sandbox.stub(stackActionApi, 'describeChangeSetDeletionStatus') |
| 42 | + sandbox.stub(stackActionApi, 'deleteChangeSet').resolves() |
| 43 | + |
| 44 | + setIntervalStub = sandbox.stub(globals.clock, 'setInterval').callsFake((callback: () => void) => { |
| 45 | + setImmediate(() => callback()) |
| 46 | + return 1 as any |
| 47 | + }) |
| 48 | + sandbox.stub(globals.clock, 'clearInterval') |
| 49 | + }) |
| 50 | + |
| 51 | + it('should call refresh command after successful deletion', async function () { |
| 52 | + getChangeSetDeletionStatusStub.resolves({ |
| 53 | + phase: StackActionPhase.DELETION_COMPLETE, |
| 54 | + state: StackActionState.SUCCESSFUL, |
| 55 | + }) |
| 56 | + |
| 57 | + const deletion = new ChangeSetDeletion('test-stack', 'test-changeset', mockClient) |
| 58 | + await deletion.delete() |
| 59 | + await new Promise((resolve) => setImmediate(resolve)) |
| 60 | + |
| 61 | + assert.ok(executeCommandStub.calledWith(commandKey('stacks.refresh'))) |
| 62 | + }) |
| 63 | + |
| 64 | + it('should call refresh command after failed deletion', async function () { |
| 65 | + getChangeSetDeletionStatusStub.resolves({ phase: StackActionPhase.DELETION_FAILED }) |
| 66 | + describeChangeSetDeletionStatusStub.resolves({ FailureReason: 'Test failure' }) |
| 67 | + |
| 68 | + const deletion = new ChangeSetDeletion('test-stack', 'test-changeset', mockClient) |
| 69 | + await deletion.delete() |
| 70 | + await new Promise((resolve) => setImmediate(resolve)) |
| 71 | + |
| 72 | + assert.ok(executeCommandStub.calledWith(commandKey('stacks.refresh'))) |
| 73 | + }) |
| 74 | + |
| 75 | + it('should not call refresh command when polling encounters error', async function () { |
| 76 | + getChangeSetDeletionStatusStub.rejects(new Error('Polling error')) |
| 77 | + |
| 78 | + const deletion = new ChangeSetDeletion('test-stack', 'test-changeset', mockClient) |
| 79 | + await deletion.delete() |
| 80 | + await new Promise((resolve) => setImmediate(resolve)) |
| 81 | + |
| 82 | + assert.ok(executeCommandStub.calledWith(commandKey('stacks.refresh'))) |
| 83 | + }) |
| 84 | + }) |
| 85 | +}) |
0 commit comments