-
Notifications
You must be signed in to change notification settings - Fork 4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: make tests independent of asset hashes (#8969)
Introduce a `canonicalizeTemplate` function in `@aws-cdk/assert` which translates templates to a common form w.r.t. asset hashes. Use this in some tests and in `cdk-integ-assert` to make the tests succeed if all that is different is the specific asset hash. It is switched on by putting `/// !cdk-integ pragma:ignore-assets` at the top of the integ test file. Currently only supports legacy assets, should be updated for new-style assets when we switch to those. This change is necessary to unblock other build improvements/changes such as #8946. ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
- Loading branch information
Showing
10 changed files
with
283 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/** | ||
* Reduce template to a normal form where asset references have been normalized | ||
* | ||
* This makes it possible to compare templates if all that's different between | ||
* them is the hashes of the asset values. | ||
* | ||
* Currently only handles parameterized assets, but can (and should) | ||
* be adapted to handle convention-mode assets as well when we start using | ||
* more of those. | ||
*/ | ||
export function canonicalizeTemplate(template: any): any { | ||
// For the weird case where we have an array of templates... | ||
if (Array.isArray(template)) { | ||
return template.map(canonicalizeTemplate); | ||
} | ||
|
||
// Find assets via parameters | ||
const stringSubstitutions = new Array<[RegExp, string]>(); | ||
const paramRe = /^AssetParameters([a-zA-Z0-9]{64})(S3Bucket|S3VersionKey|ArtifactHash)([a-zA-Z0-9]{8})$/; | ||
|
||
const assetsSeen = new Set<string>(); | ||
for (const paramName of Object.keys(template?.Parameters || {})) { | ||
const m = paramRe.exec(paramName); | ||
if (!m) { continue; } | ||
if (assetsSeen.has(m[1])) { continue; } | ||
|
||
assetsSeen.add(m[1]); | ||
const ix = assetsSeen.size; | ||
|
||
// Full parameter reference | ||
stringSubstitutions.push([ | ||
new RegExp(`AssetParameters${m[1]}(S3Bucket|S3VersionKey|ArtifactHash)([a-zA-Z0-9]{8})`), | ||
`Asset${ix}$1`, | ||
]); | ||
// Substring asset hash reference | ||
stringSubstitutions.push([ | ||
new RegExp(`${m[1]}`), | ||
`Asset${ix}Hash`, | ||
]); | ||
} | ||
|
||
// Substitute them out | ||
return substitute(template); | ||
|
||
function substitute(what: any): any { | ||
if (Array.isArray(what)) { | ||
return what.map(substitute); | ||
} | ||
|
||
if (typeof what === 'object' && what !== null) { | ||
const ret: any = {}; | ||
for (const [k, v] of Object.entries(what)) { | ||
ret[stringSub(k)] = substitute(v); | ||
} | ||
return ret; | ||
} | ||
|
||
if (typeof what === 'string') { | ||
return stringSub(what); | ||
} | ||
|
||
return what; | ||
} | ||
|
||
function stringSub(x: string) { | ||
for (const [re, replacement] of stringSubstitutions) { | ||
x = x.replace(re, replacement); | ||
} | ||
return x; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
135 changes: 135 additions & 0 deletions
135
packages/@aws-cdk/assert/test/canonicalize-assets.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
import { canonicalizeTemplate } from '../lib'; | ||
|
||
test('Canonicalize asset parameters and references to them', () => { | ||
const template = { | ||
Resources: { | ||
AResource: { | ||
Type: 'Some::Resource', | ||
Properties: { | ||
SomeValue: { Ref: 'AssetParametersea46702e1c05b2735e48e826d630f7bf6acdf7e55d6fa8d9fa8df858d5542161S3Bucket0C424907' }, | ||
}, | ||
}, | ||
}, | ||
Parameters: { | ||
AssetParametersea46702e1c05b2735e48e826d630f7bf6acdf7e55d6fa8d9fa8df858d5542161S3Bucket0C424907: { | ||
Type: 'String', | ||
Description: 'S3 bucket for asset \'ea46702e1c05b2735e48e826d630f7bf6acdf7e55d6fa8d9fa8df858d5542161\'', | ||
}, | ||
AssetParametersea46702e1c05b2735e48e826d630f7bf6acdf7e55d6fa8d9fa8df858d5542161S3VersionKey6841F1F8: { | ||
Type: 'String', | ||
Description: 'S3 key for asset version \'ea46702e1c05b2735e48e826d630f7bf6acdf7e55d6fa8d9fa8df858d5542161\'', | ||
}, | ||
AssetParametersea46702e1c05b2735e48e826d630f7bf6acdf7e55d6fa8d9fa8df858d5542161ArtifactHash67B22EF2: { | ||
Type: 'String', | ||
Description: 'Artifact hash for asset \'ea46702e1c05b2735e48e826d630f7bf6acdf7e55d6fa8d9fa8df858d5542161\'', | ||
}, | ||
}, | ||
}; | ||
|
||
const expected = { | ||
Resources: { | ||
AResource: { | ||
Type: 'Some::Resource', | ||
Properties: { | ||
SomeValue: { Ref: 'Asset1S3Bucket' }, | ||
}, | ||
}, | ||
}, | ||
Parameters: { | ||
Asset1S3Bucket: { | ||
Type: 'String', | ||
Description: 'S3 bucket for asset \'Asset1Hash\'', | ||
}, | ||
Asset1S3VersionKey: { | ||
Type: 'String', | ||
Description: 'S3 key for asset version \'Asset1Hash\'', | ||
}, | ||
Asset1ArtifactHash: { | ||
Type: 'String', | ||
Description: 'Artifact hash for asset \'Asset1Hash\'', | ||
}, | ||
}, | ||
}; | ||
|
||
expect(canonicalizeTemplate(template)).toEqual(expected); | ||
}); | ||
|
||
test('Distinguished 2 different assets', () => { | ||
const template = { | ||
Resources: { | ||
AResource: { | ||
Type: 'Some::Resource', | ||
Properties: { | ||
SomeValue: { Ref: 'AssetParametersea46702e1c05b2735e48e826d630f7bf6acdf7e55d6fa8d9fa8df858d5542161S3Bucket0C424907' }, | ||
OtherValue: { Ref: 'AssetParameters1111111111111111111111111111111111122222222222222222222222222222ArtifactHash67B22EF2' }, | ||
}, | ||
}, | ||
}, | ||
Parameters: { | ||
AssetParametersea46702e1c05b2735e48e826d630f7bf6acdf7e55d6fa8d9fa8df858d5542161S3Bucket0C424907: { | ||
Type: 'String', | ||
Description: 'S3 bucket for asset \'ea46702e1c05b2735e48e826d630f7bf6acdf7e55d6fa8d9fa8df858d5542161\'', | ||
}, | ||
AssetParametersea46702e1c05b2735e48e826d630f7bf6acdf7e55d6fa8d9fa8df858d5542161S3VersionKey6841F1F8: { | ||
Type: 'String', | ||
Description: 'S3 key for asset version \'ea46702e1c05b2735e48e826d630f7bf6acdf7e55d6fa8d9fa8df858d5542161\'', | ||
}, | ||
AssetParametersea46702e1c05b2735e48e826d630f7bf6acdf7e55d6fa8d9fa8df858d5542161ArtifactHash67B22EF2: { | ||
Type: 'String', | ||
Description: 'Artifact hash for asset \'ea46702e1c05b2735e48e826d630f7bf6acdf7e55d6fa8d9fa8df858d5542161\'', | ||
}, | ||
AssetParameters1111111111111111111111111111111111122222222222222222222222222222S3Bucket0C424907: { | ||
Type: 'String', | ||
Description: 'S3 bucket for asset \'1111111111111111111111111111111111122222222222222222222222222222\'', | ||
}, | ||
AssetParameters1111111111111111111111111111111111122222222222222222222222222222S3VersionKey6841F1F8: { | ||
Type: 'String', | ||
Description: 'S3 key for asset version \'1111111111111111111111111111111111122222222222222222222222222222\'', | ||
}, | ||
AssetParameters1111111111111111111111111111111111122222222222222222222222222222ArtifactHash67B22EF2: { | ||
Type: 'String', | ||
Description: 'Artifact hash for asset \'1111111111111111111111111111111111122222222222222222222222222222\'', | ||
}, | ||
}, | ||
}; | ||
|
||
const expected = { | ||
Resources: { | ||
AResource: { | ||
Type: 'Some::Resource', | ||
Properties: { | ||
SomeValue: { Ref: 'Asset1S3Bucket' }, | ||
OtherValue: { Ref: 'Asset2ArtifactHash' }, | ||
}, | ||
}, | ||
}, | ||
Parameters: { | ||
Asset1S3Bucket: { | ||
Type: 'String', | ||
Description: 'S3 bucket for asset \'Asset1Hash\'', | ||
}, | ||
Asset1S3VersionKey: { | ||
Type: 'String', | ||
Description: 'S3 key for asset version \'Asset1Hash\'', | ||
}, | ||
Asset1ArtifactHash: { | ||
Type: 'String', | ||
Description: 'Artifact hash for asset \'Asset1Hash\'', | ||
}, | ||
Asset2S3Bucket: { | ||
Type: 'String', | ||
Description: 'S3 bucket for asset \'Asset2Hash\'', | ||
}, | ||
Asset2S3VersionKey: { | ||
Type: 'String', | ||
Description: 'S3 key for asset version \'Asset2Hash\'', | ||
}, | ||
Asset2ArtifactHash: { | ||
Type: 'String', | ||
Description: 'Artifact hash for asset \'Asset2Hash\'', | ||
}, | ||
}, | ||
}; | ||
|
||
expect(canonicalizeTemplate(template)).toEqual(expected); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.