Skip to content

Commit 769ffe7

Browse files
author
Eugene Cheung
authored
chore: force stable keys for Lambda assets in unit tests [skip ci] (#98)
Addresses a workflow failure in #83. --- _By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license_
1 parent 474c6ac commit 769ffe7

File tree

9 files changed

+58
-212
lines changed

9 files changed

+58
-212
lines changed

test/dashboard/widget/BitmapWidget.test.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,15 @@ import { Template } from "monocdk/assertions";
33
import { GraphWidget } from "monocdk/aws-cloudwatch";
44

55
import { BitmapWidgetRenderingSupport } from "../../../lib";
6+
import { forceStableAssetKeys } from "../../utils/StableTestKeys";
67

78
test("support", () => {
89
const stack = new Stack();
910

1011
new BitmapWidgetRenderingSupport(stack, "Support");
1112

13+
forceStableAssetKeys(stack);
14+
1215
expect(Template.fromStack(stack)).toMatchSnapshot();
1316
});
1417

test/dashboard/widget/__snapshots__/BitmapWidget.test.ts.snap

Lines changed: 2 additions & 66 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/facade/MonitoringAspect.test.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import {
3434
MonitoringFacade,
3535
MonitoringAspectProps,
3636
} from "../../lib";
37+
import { forceStableAssetKeys } from "../utils/StableTestKeys";
3738

3839
function createDummyMonitoringFacade(stack: Stack): MonitoringFacade {
3940
return new MonitoringFacade(stack, "MonitoringFacade", {
@@ -414,6 +415,8 @@ describe("MonitoringAspect", () => {
414415
// WHEN
415416
facade.monitorScope(stack, defaultAspectProps);
416417

418+
forceStableAssetKeys(stack);
419+
417420
// THEN
418421
expect(Template.fromStack(stack)).toMatchSnapshot();
419422
});
@@ -468,6 +471,8 @@ describe("MonitoringAspect", () => {
468471
// WHEN
469472
facade.monitorScope(stack, defaultAspectProps);
470473

474+
forceStableAssetKeys(stack);
475+
471476
// THEN
472477
expect(Template.fromStack(stack)).toMatchSnapshot();
473478
});

test/monitoring/aws-secretsmanager/SecretsManagerSecretMonitoring.test.ts

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,17 @@ import {
77
SecretsManagerMetricsPublisher,
88
SecretsManagerSecretMonitoring,
99
} from "../../../lib";
10+
import { forceStableAssetKeys } from "../../utils/StableTestKeys";
1011
import { TestMonitoringScope } from "../TestMonitoringScope";
1112

13+
beforeEach(() => {
14+
// reset cached publisher instances before each test
15+
// test stacks have duplicate uniqueIds since they don't belong to a single construct tree
16+
(SecretsManagerMetricsPublisher as any).instances = {};
17+
});
18+
19+
afterAll = beforeEach;
20+
1221
test("snapshot test", () => {
1322
const stack = new Stack();
1423
stack.node.setContext(cx_api.SECRETS_MANAGER_PARSE_OWNED_SECRET_NAME, true);
@@ -72,6 +81,9 @@ test("snapshot test", () => {
7281
});
7382

7483
expect(numAlarmsCreated).toStrictEqual(4);
84+
85+
forceStableAssetKeys(stack);
86+
7587
expect(Template.fromStack(stack)).toMatchSnapshot();
7688
});
7789

@@ -104,14 +116,8 @@ test("each stack in an app gets its own publisher instance", () => {
104116
});
105117
}
106118

119+
forceStableAssetKeys(stack);
120+
107121
expect(Template.fromStack(stack)).toMatchSnapshot();
108122
}
109123
});
110-
111-
beforeEach(() => {
112-
// reset cached publisher instances before each test
113-
// test stacks have duplicate uniqueIds since they don't belong to a single construct tree
114-
(SecretsManagerMetricsPublisher as any).instances = {};
115-
});
116-
117-
afterAll = beforeEach;

test/monitoring/aws-secretsmanager/__snapshots__/SecretsManagerSecretMonitoring.test.ts.snap

Lines changed: 4 additions & 132 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/monitoring/aws-synthetics/SyntheticsCanaryMonitoring.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { Canary, Code, Runtime, Schedule, Test } from "monocdk/aws-synthetics";
44

55
import { AlarmWithAnnotation } from "../../../lib";
66
import { SyntheticsCanaryMonitoring } from "../../../lib/monitoring/aws-synthetics";
7-
import { addMonitoringDashboardsToStack } from "../SnapshotUtil";
7+
import { addMonitoringDashboardsToStack } from "../../utils/SnapshotUtil";
88
import { TestMonitoringScope } from "../TestMonitoringScope";
99

1010
test("snapshot test: no alarms", () => {

test/monitoring/aws-wafv2/WafV2Monitoring.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { Template } from "monocdk/assertions";
33
import { CfnWebACL } from "monocdk/aws-wafv2";
44

55
import { WafV2Monitoring } from "../../../lib";
6-
import { addMonitoringDashboardsToStack } from "../SnapshotUtil";
6+
import { addMonitoringDashboardsToStack } from "../../utils/SnapshotUtil";
77
import { TestMonitoringScope } from "../TestMonitoringScope";
88

99
test("snapshot test: no alarms", () => {

test/monitoring/SnapshotUtil.ts renamed to test/utils/SnapshotUtil.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
/**
2-
* Executes a snapshot test for widgets, summary widgets and alarm widgets.
3-
* @param monitoring monitoring to test
4-
*/
51
import { Stack } from "monocdk";
62
import { Dashboard } from "monocdk/aws-cloudwatch";
73
import { Monitoring } from "../../lib";
84

5+
/**
6+
* Executes a snapshot test for widgets, summary widgets and alarm widgets.
7+
*
8+
* @param monitoring monitoring to test
9+
*/
910
export function addMonitoringDashboardsToStack(
1011
stack: Stack,
1112
monitoring: Monitoring

test/utils/StableTestKeys.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { Aspects, CfnResource, IConstruct, Stack } from "monocdk";
2+
3+
/**
4+
* Overwrite potentially unstable properties for unit testing purposes.
5+
*
6+
* @param stack stack to replace properties
7+
*/
8+
export function forceStableAssetKeys(stack: Stack) {
9+
Aspects.of(stack).add({
10+
visit({ node }: IConstruct) {
11+
if (node.defaultChild && CfnResource.isCfnResource(node.defaultChild)) {
12+
switch (node.defaultChild.cfnResourceType) {
13+
case "AWS::Lambda::Function":
14+
node.defaultChild.addPropertyOverride(
15+
"Code.S3Key",
16+
"Fixed key that does not depend on asset changes"
17+
);
18+
break;
19+
}
20+
}
21+
},
22+
});
23+
}

0 commit comments

Comments
 (0)