|
| 1 | +import * as fs from 'fs'; |
| 2 | +import * as path from 'path'; |
| 3 | +import { ArtifactMetadataEntryType, ArtifactType, type AssetManifest, type AssetMetadataEntry, type AwsCloudFormationStackProperties, type MetadataEntry, type MissingContext } from '@aws-cdk/cloud-assembly-schema'; |
| 4 | +import { CloudAssembly, CloudAssemblyBuilder, type CloudFormationStackArtifact, type StackMetadata } from '@aws-cdk/cx-api'; |
| 5 | + |
| 6 | +export const DEFAULT_FAKE_TEMPLATE = { No: 'Resources' }; |
| 7 | + |
| 8 | +const SOME_RECENT_SCHEMA_VERSION = '30.0.0'; |
| 9 | + |
| 10 | +export interface TestStackArtifact { |
| 11 | + stackName: string; |
| 12 | + template?: any; |
| 13 | + env?: string; |
| 14 | + depends?: string[]; |
| 15 | + metadata?: StackMetadata; |
| 16 | + notificationArns?: string[]; |
| 17 | + |
| 18 | + /** Old-style assets */ |
| 19 | + assets?: AssetMetadataEntry[]; |
| 20 | + properties?: Partial<AwsCloudFormationStackProperties>; |
| 21 | + terminationProtection?: boolean; |
| 22 | + displayName?: string; |
| 23 | + |
| 24 | + /** New-style assets */ |
| 25 | + assetManifest?: AssetManifest; |
| 26 | +} |
| 27 | + |
| 28 | +export interface TestAssembly { |
| 29 | + stacks: TestStackArtifact[]; |
| 30 | + missing?: MissingContext[]; |
| 31 | + nestedAssemblies?: TestAssembly[]; |
| 32 | + schemaVersion?: string; |
| 33 | +} |
| 34 | + |
| 35 | +function clone(obj: any) { |
| 36 | + return JSON.parse(JSON.stringify(obj)); |
| 37 | +} |
| 38 | + |
| 39 | +function addAttributes(assembly: TestAssembly, builder: CloudAssemblyBuilder) { |
| 40 | + for (const stack of assembly.stacks) { |
| 41 | + const templateFile = `${stack.stackName}.template.json`; |
| 42 | + const template = stack.template ?? DEFAULT_FAKE_TEMPLATE; |
| 43 | + fs.writeFileSync(path.join(builder.outdir, templateFile), JSON.stringify(template, undefined, 2)); |
| 44 | + addNestedStacks(templateFile, builder.outdir, template); |
| 45 | + |
| 46 | + // we call patchStackTags here to simulate the tags formatter |
| 47 | + // that is used when building real manifest files. |
| 48 | + const metadata: { [path: string]: MetadataEntry[] } = patchStackTags({ ...stack.metadata }); |
| 49 | + for (const asset of stack.assets || []) { |
| 50 | + metadata[asset.id] = [{ type: ArtifactMetadataEntryType.ASSET, data: asset }]; |
| 51 | + } |
| 52 | + |
| 53 | + for (const missing of assembly.missing || []) { |
| 54 | + builder.addMissing(missing); |
| 55 | + } |
| 56 | + |
| 57 | + const dependencies = [...(stack.depends ?? [])]; |
| 58 | + |
| 59 | + if (stack.assetManifest) { |
| 60 | + const manifestFile = `${stack.stackName}.assets.json`; |
| 61 | + fs.writeFileSync(path.join(builder.outdir, manifestFile), JSON.stringify(stack.assetManifest, undefined, 2)); |
| 62 | + dependencies.push(`${stack.stackName}.assets`); |
| 63 | + builder.addArtifact(`${stack.stackName}.assets`, { |
| 64 | + type: ArtifactType.ASSET_MANIFEST, |
| 65 | + environment: stack.env || 'aws://123456789012/here', |
| 66 | + properties: { |
| 67 | + file: manifestFile, |
| 68 | + }, |
| 69 | + }); |
| 70 | + } |
| 71 | + |
| 72 | + builder.addArtifact(stack.stackName, { |
| 73 | + type: ArtifactType.AWS_CLOUDFORMATION_STACK, |
| 74 | + environment: stack.env || 'aws://123456789012/here', |
| 75 | + |
| 76 | + dependencies, |
| 77 | + metadata, |
| 78 | + properties: { |
| 79 | + ...stack.properties, |
| 80 | + templateFile, |
| 81 | + terminationProtection: stack.terminationProtection, |
| 82 | + notificationArns: stack.notificationArns, |
| 83 | + }, |
| 84 | + displayName: stack.displayName, |
| 85 | + }); |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +function addNestedStacks(templatePath: string, outdir: string, rootStackTemplate?: any) { |
| 90 | + let template = rootStackTemplate; |
| 91 | + |
| 92 | + if (!template) { |
| 93 | + const templatePathWithDir = path.join('nested-stack-templates', templatePath); |
| 94 | + template = JSON.parse(fs.readFileSync(path.join(__dirname, '..', templatePathWithDir)).toString()); |
| 95 | + fs.writeFileSync(path.join(outdir, templatePath), JSON.stringify(template, undefined, 2)); |
| 96 | + } |
| 97 | + |
| 98 | + for (const logicalId of Object.keys(template.Resources ?? {})) { |
| 99 | + if (template.Resources[logicalId].Type === 'AWS::CloudFormation::Stack') { |
| 100 | + if (template.Resources[logicalId].Metadata && template.Resources[logicalId].Metadata['aws:asset:path']) { |
| 101 | + const nestedTemplatePath = template.Resources[logicalId].Metadata['aws:asset:path']; |
| 102 | + addNestedStacks(nestedTemplatePath, outdir); |
| 103 | + } |
| 104 | + } |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +function rewriteManifestVersion(directory: string, version: string) { |
| 109 | + const manifestFile = `${directory}/manifest.json`; |
| 110 | + const contents = JSON.parse(fs.readFileSync(`${directory}/manifest.json`, 'utf-8')); |
| 111 | + contents.version = version; |
| 112 | + fs.writeFileSync(manifestFile, JSON.stringify(contents, undefined, 2)); |
| 113 | +} |
| 114 | + |
| 115 | +function cxapiAssemblyWithForcedVersion(asm: CloudAssembly, version: string) { |
| 116 | + rewriteManifestVersion(asm.directory, version); |
| 117 | + return new CloudAssembly(asm.directory, { skipVersionCheck: true }); |
| 118 | +} |
| 119 | + |
| 120 | +export function testAssembly(assembly: TestAssembly): CloudAssembly { |
| 121 | + const builder = new CloudAssemblyBuilder(); |
| 122 | + addAttributes(assembly, builder); |
| 123 | + |
| 124 | + if (assembly.nestedAssemblies != null && assembly.nestedAssemblies.length > 0) { |
| 125 | + assembly.nestedAssemblies?.forEach((nestedAssembly: TestAssembly, i: number) => { |
| 126 | + const nestedAssemblyBuilder = builder.createNestedAssembly(`nested${i}`, `nested${i}`); |
| 127 | + addAttributes(nestedAssembly, nestedAssemblyBuilder); |
| 128 | + nestedAssemblyBuilder.buildAssembly(); |
| 129 | + }); |
| 130 | + } |
| 131 | + |
| 132 | + const asm = builder.buildAssembly(); |
| 133 | + return cxapiAssemblyWithForcedVersion(asm, assembly.schemaVersion ?? SOME_RECENT_SCHEMA_VERSION); |
| 134 | +} |
| 135 | + |
| 136 | +/** |
| 137 | + * Transform stack tags from how they are decalred in source code (lower cased) |
| 138 | + * to how they are stored on disk (upper cased). In real synthesis this is done |
| 139 | + * by a special tags formatter. |
| 140 | + * |
| 141 | + * @see aws-cdk-lib/lib/stack.ts |
| 142 | + */ |
| 143 | +function patchStackTags(metadata: { [path: string]: MetadataEntry[] }): { |
| 144 | + [path: string]: MetadataEntry[]; |
| 145 | +} { |
| 146 | + const cloned = clone(metadata) as { [path: string]: MetadataEntry[] }; |
| 147 | + |
| 148 | + for (const metadataEntries of Object.values(cloned)) { |
| 149 | + for (const metadataEntry of metadataEntries) { |
| 150 | + if (metadataEntry.type === ArtifactMetadataEntryType.STACK_TAGS && metadataEntry.data) { |
| 151 | + const metadataAny = metadataEntry as any; |
| 152 | + |
| 153 | + metadataAny.data = metadataAny.data.map((t: any) => { |
| 154 | + return { Key: t.key, Value: t.value }; |
| 155 | + }); |
| 156 | + } |
| 157 | + } |
| 158 | + } |
| 159 | + return cloned; |
| 160 | +} |
| 161 | + |
| 162 | +export function testStack(stack: TestStackArtifact): CloudFormationStackArtifact { |
| 163 | + const assembly = testAssembly({ stacks: [stack] }); |
| 164 | + return assembly.getStackByName(stack.stackName); |
| 165 | +} |
0 commit comments