Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions packages/cdkConstructs/src/changesets/checkDestructiveChanges.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {
CloudFormationClient,
DescribeStacksCommand,
DescribeChangeSetCommand,
DescribeChangeSetCommandOutput,
Change as CloudFormationChange
Expand Down Expand Up @@ -128,6 +129,18 @@ export async function checkDestructiveChangeSet(
}

const client = new CloudFormationClient({region})

try {
await client.send(new DescribeStacksCommand({StackName: stackName}))
} catch (error) {
Comment thread
anthony-nhs marked this conversation as resolved.
if (error instanceof Error && error.name === "ValidationError" && error.message.includes("does not exist")) {
console.log(`Stack ${stackName} does not exist. Skipping destructive change check.`)
return
}
Comment thread
anthony-nhs marked this conversation as resolved.

throw error
}

const command = new DescribeChangeSetCommand({
ChangeSetName: changeSetName,
StackName: stackName
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,16 @@ vi.mock("@aws-sdk/client-cloudformation", () => {
}
}

return {CloudFormationClient, DescribeChangeSetCommand}
class DescribeStacksCommand {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
input: any
// eslint-disable-next-line @typescript-eslint/no-explicit-any
constructor(input: any) {
this.input = input
}
}

return {CloudFormationClient, DescribeChangeSetCommand, DescribeStacksCommand}
})

const __filename = fileURLToPath(import.meta.url)
Expand Down Expand Up @@ -190,30 +199,42 @@ describe("checkDestructiveChanges", () => {
describe("checkDestructiveChangeSet", () => {
const logSpy = vi.spyOn(console, "log").mockImplementation(() => undefined)
const errorSpy = vi.spyOn(console, "error").mockImplementation(() => undefined)
const mockStackExists = () => {
mockCloudFormationSend.mockResolvedValueOnce({
Stacks: [
{
StackName: "stack"
}
]
})
}

afterEach(() => {
logSpy.mockReset()
errorSpy.mockReset()
})

test("logs success when no destructive changes are present", async () => {
mockStackExists()
mockCloudFormationSend.mockResolvedValueOnce(safeChangeSet)

await expect(checkDestructiveChangeSet("cs", "stack", "eu-west-2")).resolves.toBeUndefined()

expect(mockCloudFormationSend).toHaveBeenCalledTimes(1)
const command = mockCloudFormationSend.mock.calls[0][0] as { input: { ChangeSetName: string; StackName: string } }
expect(mockCloudFormationSend).toHaveBeenCalledTimes(2)
const command = mockCloudFormationSend.mock.calls[1][0] as { input: { ChangeSetName: string; StackName: string } }
expect(command.input).toEqual({ChangeSetName: "cs", StackName: "stack"})
Comment thread
anthony-nhs marked this conversation as resolved.
expect(logSpy).toHaveBeenCalledWith("Change set cs for stack stack has no destructive changes that are not waived.")
expect(errorSpy).not.toHaveBeenCalled()
})

test("logs details and throws when destructive changes exist", async () => {
mockStackExists()
mockCloudFormationSend.mockResolvedValueOnce(destructiveChangeSet)

await expect(checkDestructiveChangeSet("cs", "stack", "eu-west-2"))
.rejects.toThrow("Change set cs contains destructive changes")

expect(mockCloudFormationSend).toHaveBeenCalledTimes(1)
expect(mockCloudFormationSend).toHaveBeenCalledTimes(2)
expect(logSpy).not.toHaveBeenCalled()
expect(errorSpy).toHaveBeenCalledWith("Resources that require attention:")
})
Expand All @@ -234,6 +255,7 @@ describe("checkDestructiveChangeSet", () => {
}
]
}
mockStackExists()
mockCloudFormationSend.mockResolvedValueOnce(changeSet)

const allowedChanges: Array<AllowedDestructiveChange> = [
Expand All @@ -253,7 +275,7 @@ describe("checkDestructiveChangeSet", () => {
await expect(checkDestructiveChangeSet("cs", "stack", "eu-west-2", allowedChanges))
.resolves.toBeUndefined()

expect(mockCloudFormationSend).toHaveBeenCalledTimes(1)
expect(mockCloudFormationSend).toHaveBeenCalledTimes(2)
expect(logSpy).toHaveBeenCalledWith(expect.stringContaining("Allowing destructive change ResourceToRemove"))
expect(logSpy).toHaveBeenCalledWith("Change set cs for stack stack has no destructive changes that are not waived.")
expect(errorSpy).not.toHaveBeenCalled()
Expand All @@ -275,6 +297,7 @@ describe("checkDestructiveChangeSet", () => {
}
]
}
mockStackExists()
mockCloudFormationSend.mockResolvedValueOnce(changeSet)

const allowedChanges: Array<AllowedDestructiveChange> = [
Expand Down Expand Up @@ -315,6 +338,7 @@ describe("checkDestructiveChangeSet", () => {
}
]
}
mockStackExists()
mockCloudFormationSend.mockResolvedValueOnce(changeSet)

const allowedChanges: Array<AllowedDestructiveChange> = [
Expand All @@ -336,4 +360,17 @@ describe("checkDestructiveChangeSet", () => {

expect(errorSpy).toHaveBeenCalledWith("Resources that require attention:")
})

test("logs and exits without error when the stack does not exist", async () => {
const stackMissingError = new Error("Stack with id stack does not exist")
stackMissingError.name = "ValidationError"

mockCloudFormationSend.mockRejectedValueOnce(stackMissingError)

await expect(checkDestructiveChangeSet("cs", "stack", "eu-west-2")).resolves.toBeUndefined()

expect(mockCloudFormationSend).toHaveBeenCalledTimes(1)
expect(logSpy).toHaveBeenCalledWith("Stack stack does not exist. Skipping destructive change check.")
expect(errorSpy).not.toHaveBeenCalled()
})
})