Skip to content
This repository was archived by the owner on Jul 16, 2024. It is now read-only.

Commit b318063

Browse files
flochazChazal
andauthored
Split unit and integ tests (#192)
* split unit and integ tests * Fix Flyway runner integ test * fix build Co-authored-by: Chazal <chazalf@88665a14a6c3.ant.amazon.com>
1 parent 51dce88 commit b318063

29 files changed

+819
-39
lines changed

core/.projen/deps.json

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

core/.projen/tasks.json

Lines changed: 17 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

core/.projenrc.js

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ const project = new AwsCdkConstructLibrary({
1515
homepage: 'https://aws-samples.github.io/aws-analytics-reference-architecture/',
1616
copyrightPeriod: `2021-${new Date().getFullYear()}`,
1717
copyrightOwner: 'Amazon.com, Inc. or its affiliates. All Rights Reserved.',
18-
1918
keywords: [
2019
'aws',
2120
'constructs',
@@ -70,8 +69,16 @@ const project = new AwsCdkConstructLibrary({
7069
'@types/js-yaml',
7170
'@types/jest',
7271
'esbuild',
72+
'aws-cdk@1.130.0',
73+
'jest-runner-groups',
7374
],
7475

76+
jestOptions: {
77+
jestConfig: {
78+
runner: 'groups',
79+
},
80+
},
81+
7582
bundledDeps: [
7683
'js-yaml',
7784
'uuid',
@@ -95,6 +102,17 @@ const project = new AwsCdkConstructLibrary({
95102

96103
});
97104

105+
project.testTask.reset('jest --group=unit');
106+
project.testTask.spawn('eslint');
107+
108+
project.addTask('test:unit', {
109+
exec: 'jest --group=unit',
110+
});
111+
112+
project.addTask('test:integ', {
113+
exec: 'jest --group=integ',
114+
});
115+
98116
const testDeploy = project.addTask('test:deploy', {
99117
exec: 'cdk deploy --app=./lib/integ.default.js',
100118
});

core/API.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -743,6 +743,17 @@ new FlywayRunner(scope: Construct, id: string, props: FlywayRunnerProps)
743743

744744

745745

746+
#### Properties <a name="Properties"></a>
747+
748+
##### `flywayRunner`<sup>Required</sup> <a name="aws-analytics-reference-architecture.FlywayRunner.property.flywayRunner"></a>
749+
750+
```typescript
751+
public readonly flywayRunner: CustomResource;
752+
```
753+
754+
- *Type:* [`@aws-cdk/core.CustomResource`](#@aws-cdk/core.CustomResource)
755+
756+
---
746757

747758

748759
### SingletonBucket <a name="aws-analytics-reference-architecture.SingletonBucket"></a>

core/package.json

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

core/src/db-schema-manager/flyway-runner.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,8 @@ export interface FlywayRunnerProps {
8585
* ```
8686
*/
8787
export class FlywayRunner extends cdk.Construct {
88+
public readonly flywayRunner: CustomResource;
89+
8890
constructor(scope: cdk.Construct, id: string, props: FlywayRunnerProps) {
8991
super(scope, id);
9092

@@ -138,7 +140,7 @@ export class FlywayRunner extends cdk.Construct {
138140
vpc: props.vpc,
139141
});
140142

141-
new CustomResource(this, 'trigger', {
143+
this.flywayRunner =new CustomResource(this, 'trigger', {
142144
serviceToken: flywayCustomResourceProvider.serviceToken,
143145
properties: {
144146
flywayRequest: {

core/src/db-schema-manager/resources/flyway-lambda/.settings/org.eclipse.buildship.core.prefs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
arguments=
22
auto.sync=false
33
build.scans.enabled=false
4-
connection.gradle.distribution=GRADLE_DISTRIBUTION(VERSION(7.0-rc-1))
4+
connection.gradle.distribution=GRADLE_DISTRIBUTION(WRAPPER)
55
connection.project.dir=
66
eclipse.preferences.version=1
77
gradle.user.home=

core/src/db-schema-manager/resources/flyway-lambda/src/main/java/com/geekoosh/flyway/FlywayCustomResourceHandler.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ public Map handleRequest(CloudFormationCustomResourceEvent event, Context contex
3535
public Map handleRequest(CloudFormationCustomResourceEvent event, AmazonCloudFormation cloudFormationClient) {
3636
String physicalResourceId = event.getPhysicalResourceId();
3737
String requestType = event.getRequestType();
38+
HashMap<String, Object> schemaVersion = new HashMap<>();
39+
schemaVersion.put("version", "0");
3840
switch (requestType) {
3941
/*
4042
For create requests, attempt to connect to the on-premise Active Directory
@@ -45,7 +47,9 @@ public Map handleRequest(CloudFormationCustomResourceEvent event, AmazonCloudFor
4547
DescribeStackResourceResult currentResource = cloudFormationClient.describeStackResource(new DescribeStackResourceRequest().withStackName(event.getStackId()).withLogicalResourceId(event.getLogicalResourceId()));
4648
String currentResourceStatus = currentResource.getStackResourceDetail().getResourceStatus();
4749
if(!currentResourceStatus.equals("UPDATE_ROLLBACK_IN_PROGRESS")) {
48-
callFlywayService();
50+
Response run = callFlywayService();
51+
schemaVersion.put("version", run.getInfo().getCurrent().getVersion().getVersion());
52+
4953
}
5054
break;
5155
case "Delete":
@@ -55,8 +59,10 @@ public Map handleRequest(CloudFormationCustomResourceEvent event, AmazonCloudFor
5559
}
5660

5761
HashMap<String, Object> cfnResponse = new HashMap<>();
62+
5863
cfnResponse.put("PhysicalResourceId", physicalResourceId);
5964
cfnResponse.put("RequestType", event.getRequestType());
65+
cfnResponse.put("Data", schemaVersion);
6066

6167
System.out.println("result: Successfully " + event.getRequestType() + " migrations");
6268
System.out.println("response: " + cfnResponse);
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: MIT-0
3+
/**
4+
* Tests FlywayRunner
5+
*
6+
* @group integ/redshift/flyway-runner
7+
*/
8+
9+
import * as path from 'path';
10+
import * as ec2 from '@aws-cdk/aws-ec2';
11+
import * as redshift from '@aws-cdk/aws-redshift';
12+
import * as cdk from '@aws-cdk/core';
13+
import { SdkProvider } from 'aws-cdk/lib/api/aws-auth';
14+
import { CloudFormationDeployments } from 'aws-cdk/lib/api/cloudformation-deployments';
15+
16+
import { FlywayRunner } from '../../src/db-schema-manager';
17+
18+
// GIVEN
19+
const integTestApp = new cdk.App();
20+
const stack = new cdk.Stack(integTestApp, 'FlywayRunnerE2eTest');
21+
22+
const vpc = new ec2.Vpc(stack, 'Vpc');
23+
24+
const dbName = 'testdb';
25+
const cluster = new redshift.Cluster(stack, 'Redshift', {
26+
removalPolicy: cdk.RemovalPolicy.DESTROY,
27+
masterUser: {
28+
masterUsername: 'admin',
29+
},
30+
vpc,
31+
defaultDatabaseName: dbName,
32+
});
33+
34+
const runner = new FlywayRunner(stack, 'testMigration', {
35+
migrationScriptsFolderAbsolutePath: path.join(__dirname, './resources/sql'),
36+
cluster: cluster,
37+
vpc: vpc,
38+
databaseName: dbName,
39+
});
40+
41+
new cdk.CfnOutput(stack, 'schemaVersion', {
42+
value: runner.flywayRunner.getAtt('version').toString(),
43+
exportName: 'schemaVersion',
44+
});
45+
46+
describe('deploy succeed', () => {
47+
it('can be deploy succcessfully', async () => {
48+
// GIVEN
49+
const stackArtifact = integTestApp.synth().getStackByName(stack.stackName);
50+
51+
const sdkProvider = await SdkProvider.withAwsCliCompatibleDefaults({
52+
profile: process.env.AWS_PROFILE,
53+
});
54+
const cloudFormation = new CloudFormationDeployments({ sdkProvider });
55+
56+
// WHEN
57+
const deployResult = await cloudFormation.deployStack({
58+
stack: stackArtifact,
59+
});
60+
61+
// THEN
62+
expect(deployResult.outputs.schemaVersion).toEqual('2');
63+
}, 9000000);
64+
});
65+
66+
afterAll(async () => {
67+
const stackArtifact = integTestApp.synth().getStackByName(stack.stackName);
68+
69+
const sdkProvider = await SdkProvider.withAwsCliCompatibleDefaults({
70+
profile: process.env.AWS_PROFILE,
71+
});
72+
const cloudFormation = new CloudFormationDeployments({ sdkProvider });
73+
74+
await cloudFormation.destroyStack({
75+
stack: stackArtifact,
76+
});
77+
});

0 commit comments

Comments
 (0)