Skip to content

Commit

Permalink
add integ test to test too many sources
Browse files Browse the repository at this point in the history
  • Loading branch information
moelasmar committed Sep 16, 2024
1 parent 1299bc8 commit e610d05
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import * as os from 'os';
import * as fs from 'fs';
import * as path from 'path';
import * as s3 from 'aws-cdk-lib/aws-s3';
import * as cdk from 'aws-cdk-lib';
import * as integ from '@aws-cdk/integ-tests-alpha';
import { Construct } from 'constructs';
import * as s3deploy from 'aws-cdk-lib/aws-s3-deployment';
import { CfnOutput, Fn } from 'aws-cdk-lib';
import { ExpectedResult } from '@aws-cdk/integ-tests-alpha';

const numFiles = 50;

class TestBucketDeployment extends cdk.Stack {
public readonly destinationBucket: s3.IBucket;
constructor(scope: Construct, id: string, props?: cdk.StackProps) {
super(scope, id, props);

this.destinationBucket = new s3.Bucket(this, 'Destination', {
removalPolicy: cdk.RemovalPolicy.DESTROY,
autoDeleteObjects: true, // needed for integration test cleanup
});

const sources = [];
for (let i = 0; i < numFiles; i++) {
const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'tmpcdk'));
fs.mkdirSync(tempDir, { recursive: true });
const fileName = `${i+1}.txt`;
const filePath = path.join(tempDir, fileName);
fs.writeFileSync(filePath, `This is file number ${i + 1}`);
sources.push(s3deploy.Source.asset(tempDir));
}

const deploymentBucket = new s3deploy.BucketDeployment(this, 'DeployMe', {
sources: sources,
destinationBucket: this.destinationBucket,
memoryLimit: 2048,
retainOnDelete: false, // default is true, which will block the integration test cleanup
outputObjectKeys: false,
});

new CfnOutput(this, 'customResourceData', {
value: Fn.join(',', deploymentBucket.objectKeys),
});
}
}

const app = new cdk.App();
const testCase = new TestBucketDeployment(app, 'test-bucket-deployments-too-many-sources');

// Assert that DeployMeWithoutExtractingFilesOnDestination deploys a zip file to bucket4
const integTest = new integ.IntegTest(app, 'integ-test-bucket-deployments', {
testCases: [testCase],
diffAssets: true,
});

for (let i = 0; i < numFiles; i++) {
const apiCall = integTest.assertions.awsApiCall('S3', 'getObject', {
Bucket: testCase.destinationBucket.bucketName,
Key: `${i+1}.txt`,
});
apiCall.provider.addToRolePolicy({
Effect: 'Allow',
Action: ['s3:GetObject', 's3:ListBucket'],
Resource: ['*'],
});
apiCall.assertAtPath('Body', ExpectedResult.stringLikeRegexp(`This is file number ${i + 1}`));
}

app.synth();
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ def cfn_error(message=None):
cfn_send(event, context, CFN_SUCCESS, physicalResourceId=physical_id, responseData={
# Passing through the ARN sequences dependencees on the deployment
'DestinationBucketArn': props.get('DestinationBucketArn'),
**({'SourceObjectKeys': props.get('SourceObjectKeys')} if output_object_keys else {})
**({'SourceObjectKeys': props.get('SourceObjectKeys')} if output_object_keys else {'SourceObjectKeys': []})
})
except KeyError as e:
cfn_error("invalid request. Missing key %s" % str(e))
Expand Down Expand Up @@ -334,4 +334,3 @@ def replace_markers(filename, markers):
# # delete the original file and rename the new one to the original
os.remove(filename)
os.rename(outfile, filename)

2 changes: 1 addition & 1 deletion packages/aws-cdk-lib/aws-s3-deployment/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,7 @@ new cdk.CfnOutput(this, 'ObjectKey', {

## Controlling the Output of Source Object Keys

By default, the keys of the source objects copied to the destination bucket are returned in the Data property of the custom resource. However, you can disable this behavior by setting the outputObjectKeys property to false. This is particularly useful when the number of objects is too large and might exceed the size limit of the data property.
By default, the keys of the source objects copied to the destination bucket are returned in the Data property of the custom resource. However, you can disable this behavior by setting the outputObjectKeys property to false. This is particularly useful when the number of objects is too large and might exceed the size limit of the responseData property.

```ts
import * as cdk from 'aws-cdk-lib';
Expand Down

0 comments on commit e610d05

Please sign in to comment.