|
1 | 1 | import * as fs from 'fs';
|
2 | 2 | import * as path from 'path';
|
| 3 | +import { Template } from '@aws-cdk/assertions'; |
| 4 | +import * as lambda from '@aws-cdk/aws-lambda'; |
3 | 5 | import * as s3 from '@aws-cdk/aws-s3';
|
4 | 6 | import * as s3_assets from '@aws-cdk/aws-s3-assets';
|
5 | 7 | import * as sns from '@aws-cdk/aws-sns';
|
@@ -32,13 +34,69 @@ describe('ProductStack', () => {
|
32 | 34 | assetBucket: testAssetBucket,
|
33 | 35 | });
|
34 | 36 |
|
35 |
| - // WHEN |
36 |
| - new s3_assets.Asset(productStack, 'testAsset', { |
37 |
| - path: path.join(__dirname, 'assets'), |
| 37 | + new lambda.Function(productStack, 'HelloHandler', { |
| 38 | + runtime: lambda.Runtime.PYTHON_3_9, |
| 39 | + code: lambda.Code.fromAsset(path.join(__dirname, 'assets')), |
| 40 | + handler: 'index.handler', |
38 | 41 | });
|
39 | 42 |
|
| 43 | + // WHEN |
| 44 | + const assembly = app.synth(); |
| 45 | + |
40 | 46 | // THEN
|
41 | 47 | expect(productStack._getAssetBucket()).toBeDefined();
|
| 48 | + const template = JSON.parse(fs.readFileSync(path.join(assembly.directory, productStack.templateFile), 'utf-8')); |
| 49 | + Template.fromJSON(template).hasResourceProperties('AWS::Lambda::Function', { |
| 50 | + Code: { |
| 51 | + S3Bucket: 'test-asset-bucket', |
| 52 | + S3Key: 'd3833f63e813b3a96ea04c8c50ca98209330867f5f6ac358efca11f85a3476c2.zip', |
| 53 | + }, |
| 54 | + }); |
| 55 | + }); |
| 56 | + |
| 57 | + test('Used defined Asset bucket in product stack with nested assets', () => { |
| 58 | + // GIVEN |
| 59 | + const app = new cdk.App( |
| 60 | + { outdir: 'cdk.out' }, |
| 61 | + ); |
| 62 | + const mainStack = new cdk.Stack(app, 'MyStack'); |
| 63 | + let templateFileUrl = ''; |
| 64 | + class PortfolioStage extends cdk.Stage { |
| 65 | + constructor(scope: Construct, id: string) { |
| 66 | + super(scope, id); |
| 67 | + |
| 68 | + const portfolioStack: cdk.Stack = new cdk.Stack(this, 'NestedStack'); |
| 69 | + |
| 70 | + const testAssetBucket = new s3.Bucket(portfolioStack, 'TestAssetBucket', { |
| 71 | + bucketName: 'test-asset-bucket', |
| 72 | + }); |
| 73 | + const productStack = new servicecatalog.ProductStack(portfolioStack, 'MyProductStack', { |
| 74 | + assetBucket: testAssetBucket, |
| 75 | + }); |
| 76 | + |
| 77 | + new lambda.Function(productStack, 'HelloHandler', { |
| 78 | + runtime: lambda.Runtime.PYTHON_3_9, |
| 79 | + code: lambda.Code.fromAsset(path.join(__dirname, 'assets')), |
| 80 | + handler: 'index.handler', |
| 81 | + }); |
| 82 | + |
| 83 | + expect(productStack._getAssetBucket()).toBeDefined(); |
| 84 | + templateFileUrl = productStack.templateFile; |
| 85 | + } |
| 86 | + } |
| 87 | + const portfolioStage = new PortfolioStage(mainStack, 'PortfolioStage'); |
| 88 | + |
| 89 | + // WHEN |
| 90 | + app.synth(); |
| 91 | + |
| 92 | + //THEN |
| 93 | + const template = JSON.parse(fs.readFileSync(path.join(portfolioStage.outdir, templateFileUrl), 'utf-8')); |
| 94 | + Template.fromJSON(template).hasResourceProperties('AWS::Lambda::Function', { |
| 95 | + Code: { |
| 96 | + S3Bucket: 'test-asset-bucket', |
| 97 | + S3Key: 'd3833f63e813b3a96ea04c8c50ca98209330867f5f6ac358efca11f85a3476c2.zip', |
| 98 | + }, |
| 99 | + }); |
42 | 100 | });
|
43 | 101 |
|
44 | 102 | test('fails if bucketName is not specified in product stack with assets', () => {
|
|
0 commit comments