Skip to content

Commit

Permalink
add test utility to normalize asset names
Browse files Browse the repository at this point in the history
  • Loading branch information
nateglims committed Sep 12, 2023
1 parent 03715f7 commit 7003947
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 17 deletions.
8 changes: 4 additions & 4 deletions test/__snapshots__/build-image-data.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ exports[`Build Image Data Snapshot 1`] = `
"Properties": {
"Content": {
"S3Bucket": "cdk-hnb659fds-assets-111111111111-eu-central-1",
"S3Key": "arbitrary-file.zip",
"S3Key": "arbitrary-file.ext",
},
"Description": "/opt/awscli/aws",
},
Expand All @@ -38,7 +38,7 @@ exports[`Build Image Data Snapshot 1`] = `
"cdk-hnb659fds-assets-111111111111-eu-central-1",
],
"SourceObjectKeys": [
"arbitrary-file.zip",
"arbitrary-file.ext",
],
},
"Type": "Custom::CDKBucketDeployment",
Expand Down Expand Up @@ -266,7 +266,7 @@ exports[`Build Image Data Snapshot 1`] = `
"Properties": {
"Code": {
"S3Bucket": "cdk-hnb659fds-assets-111111111111-eu-central-1",
"S3Key": "arbitrary-file.zip",
"S3Key": "arbitrary-file.ext",
},
"Environment": {
"Variables": {
Expand Down Expand Up @@ -297,7 +297,7 @@ exports[`Build Image Data Snapshot 1`] = `
"Properties": {
"Code": {
"S3Bucket": "cdk-hnb659fds-assets-111111111111-eu-central-1",
"S3Key": "arbitrary-file.zip",
"S3Key": "arbitrary-file.ext",
},
"Description": {
"Fn::Join": [
Expand Down
8 changes: 4 additions & 4 deletions test/__snapshots__/demo-pipeline.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ exports[`Demo Pipeline Poky AMI Pipeline - check role name trim 1`] = `
{
"Name": "SCRIPT_URL",
"Type": "PLAINTEXT",
"Value": "s3://cdk-hnb659fds-assets-12341234-eu-central-1/cb07978d00156c655bc2617e9ff6d544ec346fc8bfd37b01b644b4694d399265.sh",
"Value": "s3://cdk-hnb659fds-assets-12341234-eu-central-1/arbitrary-file.ext",
},
],
"Image": {
Expand Down Expand Up @@ -1936,7 +1936,7 @@ def handler(event, context):
"BranchName": "main",
"S3": {
"Bucket": "cdk-hnb659fds-assets-12341234-eu-central-1",
"Key": "8328421057d696d22d2b958f640836605dbbf21ce1473e4db341cffcd075ae25.zip",
"Key": "arbitrary-file.ext",
},
},
"RepositoryName": "layer-repo-PokyAmiPipeline2ExportsOutputFnGetAttPipelineVpc0543904ACidrBlock70DEC992",
Expand Down Expand Up @@ -2291,7 +2291,7 @@ exports[`Demo Pipeline Snapshot Poky AMI Pipeline 1`] = `
{
"Name": "SCRIPT_URL",
"Type": "PLAINTEXT",
"Value": "s3://cdk-hnb659fds-assets-12341234-eu-central-1/cb07978d00156c655bc2617e9ff6d544ec346fc8bfd37b01b644b4694d399265.sh",
"Value": "s3://cdk-hnb659fds-assets-12341234-eu-central-1/arbitrary-file.ext",
},
],
"Image": {
Expand Down Expand Up @@ -4056,7 +4056,7 @@ def handler(event, context):
"BranchName": "main",
"S3": {
"Bucket": "cdk-hnb659fds-assets-12341234-eu-central-1",
"Key": "8328421057d696d22d2b958f640836605dbbf21ce1473e4db341cffcd075ae25.zip",
"Key": "arbitrary-file.ext",
},
},
"RepositoryName": "layer-repo-MyTestStack",
Expand Down
9 changes: 2 additions & 7 deletions test/build-image-data.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as cdk from 'aws-cdk-lib';
import { Template } from 'aws-cdk-lib/assertions';
import { BuildImageDataStack } from '../lib/build-image-data';
import { normalizedTemplateFromStack } from './util';

Check failure on line 4 in test/build-image-data.test.ts

View workflow job for this annotation

GitHub Actions / Run-CDK-Tests (16.x)

Delete `·`

Check failure on line 4 in test/build-image-data.test.ts

View workflow job for this annotation

GitHub Actions / Run-CDK-Tests (16.x)

Delete `·`

Check failure on line 4 in test/build-image-data.test.ts

View workflow job for this annotation

GitHub Actions / Run-CDK-Tests (20.x)

Delete `·`

describe('Build Image Data', () => {
const props = {
Expand All @@ -27,13 +28,7 @@ describe('Build Image Data', () => {
const app = new cdk.App();
const stack = new BuildImageDataStack(app, 'MyTestStack', props);
/* We must change some randomly generated file names used in the S3 asset construct. */
const templateWithRandomKeys = Template.fromStack(stack);
const templateWithConstKeys = JSON.parse(
JSON.stringify(templateWithRandomKeys.toJSON()).replace(
/[a-z0-9]{64}\.zip/g,
'arbitrary-file.zip'
)
);
const templateWithConstKeys = normalizedTemplateFromStack(stack);
expect(templateWithConstKeys).toMatchSnapshot();
});
});
5 changes: 3 additions & 2 deletions test/demo-pipeline.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { DemoPipelineStack } from '../lib/demo-pipeline';
import { Repository } from 'aws-cdk-lib/aws-ecr';
import { Vpc } from 'aws-cdk-lib/aws-ec2';
import { ProjectKind } from '../lib';
import { normalizedTemplateFromStack } from './util';

describe('Demo Pipeline', () => {
const env = { account: '12341234', region: 'eu-central-1' };
Expand Down Expand Up @@ -35,7 +36,7 @@ describe('Demo Pipeline', () => {
vpc,
projectKind: ProjectKind.PokyAmi,
});
const template = Template.fromStack(stack);
const template = normalizedTemplateFromStack(stack);
expect(template).toMatchSnapshot();
});

Expand All @@ -55,7 +56,7 @@ describe('Demo Pipeline', () => {
projectKind: ProjectKind.PokyAmi,
}
);
const template = Template.fromStack(stack);
const template = normalizedTemplateFromStack(stack);
expect(template).toMatchSnapshot();
});
});
17 changes: 17 additions & 0 deletions test/util.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import * as cdk from 'aws-cdk-lib';
import { Template } from 'aws-cdk-lib/assertions';

/**
* Remove hashed asset values and put a normalized name in instead.
*/
export function normalizedTemplateFromStack(stack: cdk.Stack): Template {
const templateWithRandomKeys = Template.fromStack(stack);

Check failure on line 8 in test/util.ts

View workflow job for this annotation

GitHub Actions / Run-CDK-Tests (16.x)

Delete `··`

Check failure on line 8 in test/util.ts

View workflow job for this annotation

GitHub Actions / Run-CDK-Tests (16.x)

Delete `··`

Check failure on line 8 in test/util.ts

View workflow job for this annotation

GitHub Actions / Run-CDK-Tests (20.x)

Delete `··`
const templateWithConstKeys = JSON.parse(

Check failure on line 9 in test/util.ts

View workflow job for this annotation

GitHub Actions / Run-CDK-Tests (16.x)

Delete `··`

Check failure on line 9 in test/util.ts

View workflow job for this annotation

GitHub Actions / Run-CDK-Tests (16.x)

Delete `··`

Check failure on line 9 in test/util.ts

View workflow job for this annotation

GitHub Actions / Run-CDK-Tests (20.x)

Delete `··`
JSON.stringify(templateWithRandomKeys.toJSON()).replace(

Check failure on line 10 in test/util.ts

View workflow job for this annotation

GitHub Actions / Run-CDK-Tests (16.x)

Replace `········` with `····`

Check failure on line 10 in test/util.ts

View workflow job for this annotation

GitHub Actions / Run-CDK-Tests (16.x)

Replace `········` with `····`

Check failure on line 10 in test/util.ts

View workflow job for this annotation

GitHub Actions / Run-CDK-Tests (20.x)

Replace `········` with `····`
/[a-z0-9]{64}\.(zip|sh)/g,

Check failure on line 11 in test/util.ts

View workflow job for this annotation

GitHub Actions / Run-CDK-Tests (16.x)

Delete `······`

Check failure on line 11 in test/util.ts

View workflow job for this annotation

GitHub Actions / Run-CDK-Tests (16.x)

Delete `······`

Check failure on line 11 in test/util.ts

View workflow job for this annotation

GitHub Actions / Run-CDK-Tests (20.x)

Delete `······`
'arbitrary-file.ext'

Check failure on line 12 in test/util.ts

View workflow job for this annotation

GitHub Actions / Run-CDK-Tests (16.x)

Delete `······`

Check failure on line 12 in test/util.ts

View workflow job for this annotation

GitHub Actions / Run-CDK-Tests (16.x)

Delete `······`

Check failure on line 12 in test/util.ts

View workflow job for this annotation

GitHub Actions / Run-CDK-Tests (20.x)

Delete `······`
)

Check failure on line 13 in test/util.ts

View workflow job for this annotation

GitHub Actions / Run-CDK-Tests (16.x)

Delete `····`

Check failure on line 13 in test/util.ts

View workflow job for this annotation

GitHub Actions / Run-CDK-Tests (16.x)

Delete `····`

Check failure on line 13 in test/util.ts

View workflow job for this annotation

GitHub Actions / Run-CDK-Tests (20.x)

Delete `····`
);

Check failure on line 14 in test/util.ts

View workflow job for this annotation

GitHub Actions / Run-CDK-Tests (16.x)

Delete `··`

Check failure on line 14 in test/util.ts

View workflow job for this annotation

GitHub Actions / Run-CDK-Tests (16.x)

Delete `··`

Check failure on line 14 in test/util.ts

View workflow job for this annotation

GitHub Actions / Run-CDK-Tests (20.x)

Delete `··`

return templateWithConstKeys;

Check failure on line 16 in test/util.ts

View workflow job for this annotation

GitHub Actions / Run-CDK-Tests (16.x)

Delete `··`

Check failure on line 16 in test/util.ts

View workflow job for this annotation

GitHub Actions / Run-CDK-Tests (16.x)

Delete `··`

Check failure on line 16 in test/util.ts

View workflow job for this annotation

GitHub Actions / Run-CDK-Tests (20.x)

Delete `··`
}

0 comments on commit 7003947

Please sign in to comment.