Skip to content

Commit c4bacca

Browse files
committed
add integration tests
1 parent 1571297 commit c4bacca

File tree

4 files changed

+138
-16
lines changed

4 files changed

+138
-16
lines changed

.github/workflows/main.yml

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -61,21 +61,9 @@ jobs:
6161
make bootstrap
6262
make deploy
6363
64-
- name: Smoke Test
64+
- name: Integration Tests
6565
run: |
66-
awslocal --version
67-
awslocal lambda invoke --cli-binary-format raw-in-base64-out --function-name my-lambda-rds-query-helper --payload '{"sqlQuery": "show tables", "secretName":"/rdsinitexample/rds/creds/mysql-01"}' output
68-
echo "show tables:"
69-
cat output
70-
awslocal lambda invoke --cli-binary-format raw-in-base64-out --function-name my-lambda-rds-query-helper --payload '{"sqlQuery": "select Author from Books", "secretName":"/rdsinitexample/rds/creds/mysql-01"}' output
71-
echo "select Author from Books:"
72-
cat output
73-
return_status=$(cat output | jq -r .status)
74-
if [ "SUCCESS" != ${return_status} ]; then
75-
echo "unexpected response: ${return_status}"
76-
cat output
77-
exit 1
78-
fi
66+
npm test
7967
8068
- name: Show Logs
8169
if: always()

jest.config.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
module.exports = {
2-
roots: ['<rootDir>/test'],
2+
testEnvironment: 'node',
3+
roots: ['<rootDir>/tests'],
34
testMatch: ['**/*.test.ts'],
45
transform: {
56
'^.+\\.tsx?$': 'ts-jest'

package.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,11 @@
1818
"jest": "^27.2.4",
1919
"ts-jest": "^27.0.5",
2020
"ts-node": "^10.2.1",
21-
"typescript": "^4.4.3"
21+
"typescript": "^4.4.3",
22+
"@aws-sdk/client-lambda": "^3.0.0",
23+
"@aws-sdk/client-rds": "^3.0.0",
24+
"@aws-sdk/client-secrets-manager": "^3.0.0",
25+
"@aws-sdk/client-s3": "^3.0.0"
2226
},
2327
"dependencies": {
2428
"aws-cdk-lib": "^2.0.0",

tests/cdk-rds.test.ts

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
import { Template } from 'aws-cdk-lib/assertions';
2+
import * as cdk from 'aws-cdk-lib';
3+
import {
4+
LambdaClient,
5+
InvokeCommand,
6+
GetFunctionCommand
7+
} from '@aws-sdk/client-lambda';
8+
import {
9+
RDSClient,
10+
DescribeDBInstancesCommand,
11+
DBInstance
12+
} from '@aws-sdk/client-rds';
13+
import {
14+
SecretsManagerClient,
15+
GetSecretValueCommand
16+
} from '@aws-sdk/client-secrets-manager';
17+
import {
18+
S3Client,
19+
ListBucketsCommand
20+
} from '@aws-sdk/client-s3';
21+
22+
const config = {
23+
endpoint: 'http://localhost:4566',
24+
region: 'us-east-1',
25+
credentials: {
26+
accessKeyId: 'test',
27+
secretAccessKey: 'test'
28+
}
29+
};
30+
31+
const lambdaClient = new LambdaClient(config);
32+
const rdsClient = new RDSClient(config);
33+
const secretsClient = new SecretsManagerClient(config);
34+
const s3Client = new S3Client(config);
35+
36+
describe('CDK RDS Stack', () => {
37+
test('Stack synthesizes successfully', () => {
38+
const app = new cdk.App();
39+
const stack = new cdk.Stack(app, 'TestStack');
40+
const template = Template.fromStack(stack);
41+
expect(template).toBeDefined();
42+
});
43+
44+
test('Lambda function exists', async () => {
45+
try {
46+
const command = new GetFunctionCommand({
47+
FunctionName: 'my-lambda-rds-query-helper'
48+
});
49+
const response = await lambdaClient.send(command);
50+
expect(response.Configuration?.FunctionName).toBe('my-lambda-rds-query-helper');
51+
} catch (error) {
52+
expect(error).toBeFalsy();
53+
}
54+
});
55+
56+
test('RDS instance exists', async () => {
57+
try {
58+
const command = new DescribeDBInstancesCommand({});
59+
const response = await rdsClient.send(command);
60+
expect(response.DBInstances?.length).toBeGreaterThan(0);
61+
const instance = response.DBInstances?.find((db: DBInstance) => db.Engine === 'mysql');
62+
expect(instance).toBeDefined();
63+
} catch (error) {
64+
expect(error).toBeFalsy();
65+
}
66+
});
67+
68+
test('Secrets Manager secret exists', async () => {
69+
try {
70+
const command = new GetSecretValueCommand({
71+
SecretId: '/rdsinitexample/rds/creds/mysql-01'
72+
});
73+
const response = await secretsClient.send(command);
74+
expect(response.SecretString).toBeDefined();
75+
} catch (error) {
76+
expect(error).toBeFalsy();
77+
}
78+
});
79+
80+
test('Lambda function returns correct response for show tables', async () => {
81+
const payload = {
82+
sqlQuery: 'show tables',
83+
secretName: '/rdsinitexample/rds/creds/mysql-01'
84+
};
85+
86+
try {
87+
const command = new InvokeCommand({
88+
FunctionName: 'my-lambda-rds-query-helper',
89+
Payload: Buffer.from(JSON.stringify(payload))
90+
});
91+
92+
const response = await lambdaClient.send(command);
93+
const result = JSON.parse(Buffer.from(response.Payload!).toString());
94+
expect(result.status).toBe('SUCCESS');
95+
expect(Array.isArray(result.results)).toBeTruthy();
96+
// Verify the expected tables are present
97+
const tables = result.results.map((r: any) => r.Tables_in_main);
98+
expect(tables).toContain('Books');
99+
expect(tables).toContain('OrderDetails');
100+
} catch (error) {
101+
expect(error).toBeFalsy();
102+
}
103+
});
104+
105+
test('Lambda function returns correct response for select query', async () => {
106+
const payload = {
107+
sqlQuery: 'select Author from Books',
108+
secretName: '/rdsinitexample/rds/creds/mysql-01'
109+
};
110+
111+
try {
112+
const command = new InvokeCommand({
113+
FunctionName: 'my-lambda-rds-query-helper',
114+
Payload: Buffer.from(JSON.stringify(payload))
115+
});
116+
117+
const response = await lambdaClient.send(command);
118+
const result = JSON.parse(Buffer.from(response.Payload!).toString());
119+
expect(result.status).toBe('SUCCESS');
120+
expect(Array.isArray(result.results)).toBeTruthy();
121+
// Verify we have author data
122+
const authors = result.results.map((r: any) => r.Author);
123+
expect(authors).toContain('Jane Doe');
124+
expect(authors).toContain('LocalStack');
125+
} catch (error) {
126+
expect(error).toBeFalsy();
127+
}
128+
});
129+
});

0 commit comments

Comments
 (0)