diff --git a/src/experimental/patterns/ec2-app.test.ts b/src/experimental/patterns/ec2-app.test.ts index 6883f86a7a..b24e1fdfc8 100644 --- a/src/experimental/patterns/ec2-app.test.ts +++ b/src/experimental/patterns/ec2-app.test.ts @@ -3,11 +3,10 @@ import { Match, Template } from "aws-cdk-lib/assertions"; import type { CfnAutoScalingGroup } from "aws-cdk-lib/aws-autoscaling"; import { CfnScalingPolicy } from "aws-cdk-lib/aws-autoscaling"; import { InstanceClass, InstanceSize, InstanceType, UserData } from "aws-cdk-lib/aws-ec2"; -import { CloudFormationStackArtifact } from "aws-cdk-lib/cx-api"; import { AccessScope } from "../../constants"; import { GuUserData } from "../../constructs/autoscaling"; import { GuStack } from "../../constructs/core"; -import { simpleGuStackForTesting } from "../../utils/test"; +import { getTemplateAfterAspectInvocation, simpleGuStackForTesting } from "../../utils/test"; import type { GuEc2AppExperimentalProps } from "./ec2-app"; import { GuEc2AppExperimental, @@ -15,31 +14,6 @@ import { RollingUpdateDurations, } from "./ec2-app"; -/** - * `Aspects` appear to run only at synth time. - * This means we must synth the stack to see the results of the `Aspect`. - * - * @see https://github.com/aws/aws-cdk/issues/29047 - * - * @param stack the stack to synthesise - */ -function getTemplateAfterAspectInvocation(stack: GuStack): Template { - const app = App.of(stack); - - if (!app) { - throw new Error(`Unable to locate the enclosing App from GuStack ${stack.node.id}`); - } - - const { artifacts } = app.synth(); - const cfnStack = artifacts.find((_): _ is CloudFormationStackArtifact => _ instanceof CloudFormationStackArtifact); - - if (!cfnStack) { - throw new Error("Unable to locate a CloudFormationStackArtifact"); - } - - return Template.fromJSON(cfnStack.template as Record); -} - // TODO test User Data includes a build number describe("The GuEc2AppExperimental pattern", () => { function initialProps(scope: GuStack, app: string = "test-gu-ec2-app"): GuEc2AppExperimentalProps { diff --git a/src/utils/test/index.ts b/src/utils/test/index.ts index b33d36e620..d05b79e64d 100644 --- a/src/utils/test/index.ts +++ b/src/utils/test/index.ts @@ -1,3 +1,4 @@ export * from "./assertions"; export * from "./simple-gu-stack"; export * from "./attach-policy-to-test-role"; +export * from "./template"; diff --git a/src/utils/test/template.ts b/src/utils/test/template.ts new file mode 100644 index 0000000000..b52285680e --- /dev/null +++ b/src/utils/test/template.ts @@ -0,0 +1,29 @@ +import { App } from "aws-cdk-lib"; +import { Template } from "aws-cdk-lib/assertions"; +import { CloudFormationStackArtifact } from "aws-cdk-lib/cx-api"; +import type { GuStack } from "../../constructs/core"; + +/** + * `Aspects` appear to run only at synth time. + * This means we must synth the stack to see the results of the `Aspect`. + * + * @see https://github.com/aws/aws-cdk/issues/29047 + * + * @param stack the stack to synthesise + */ +export function getTemplateAfterAspectInvocation(stack: GuStack): Template { + const app = App.of(stack); + + if (!app) { + throw new Error(`Unable to locate the enclosing App from GuStack ${stack.node.id}`); + } + + const { artifacts } = app.synth(); + const cfnStack = artifacts.find((_): _ is CloudFormationStackArtifact => _ instanceof CloudFormationStackArtifact); + + if (!cfnStack) { + throw new Error("Unable to locate a CloudFormationStackArtifact"); + } + + return Template.fromJSON(cfnStack.template as Record); +}