Skip to content

Commit

Permalink
refactor: Extract getTemplateAfterAspectInvocation for more usage
Browse files Browse the repository at this point in the history
  • Loading branch information
akash1810 committed Oct 21, 2024
1 parent f0d5ee3 commit c71c5c3
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 27 deletions.
28 changes: 1 addition & 27 deletions src/experimental/patterns/ec2-app.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,43 +3,17 @@ 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,
HorizontallyScalingDeploymentPropertiesExperimental,
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<string, unknown>);
}

// TODO test User Data includes a build number
describe("The GuEc2AppExperimental pattern", () => {
function initialProps(scope: GuStack, app: string = "test-gu-ec2-app"): GuEc2AppExperimentalProps {
Expand Down
1 change: 1 addition & 0 deletions src/utils/test/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from "./assertions";
export * from "./simple-gu-stack";
export * from "./attach-policy-to-test-role";
export * from "./template";
29 changes: 29 additions & 0 deletions src/utils/test/template.ts
Original file line number Diff line number Diff line change
@@ -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<string, unknown>);
}

0 comments on commit c71c5c3

Please sign in to comment.