Skip to content

Commit b523198

Browse files
authored
add integration tests (#12)
* add integration tests * fix
1 parent 1571297 commit b523198

File tree

4 files changed

+131
-16
lines changed

4 files changed

+131
-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: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
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('Lambda function exists', async () => {
38+
try {
39+
const command = new GetFunctionCommand({
40+
FunctionName: 'my-lambda-rds-query-helper'
41+
});
42+
const response = await lambdaClient.send(command);
43+
expect(response.Configuration?.FunctionName).toBe('my-lambda-rds-query-helper');
44+
} catch (error) {
45+
expect(error).toBeFalsy();
46+
}
47+
});
48+
49+
test('RDS instance exists', async () => {
50+
try {
51+
const command = new DescribeDBInstancesCommand({});
52+
const response = await rdsClient.send(command);
53+
expect(response.DBInstances?.length).toBeGreaterThan(0);
54+
const instance = response.DBInstances?.find((db: DBInstance) => db.Engine === 'mysql');
55+
expect(instance).toBeDefined();
56+
} catch (error) {
57+
expect(error).toBeFalsy();
58+
}
59+
});
60+
61+
test('Secrets Manager secret exists', async () => {
62+
try {
63+
const command = new GetSecretValueCommand({
64+
SecretId: '/rdsinitexample/rds/creds/mysql-01'
65+
});
66+
const response = await secretsClient.send(command);
67+
expect(response.SecretString).toBeDefined();
68+
} catch (error) {
69+
expect(error).toBeFalsy();
70+
}
71+
});
72+
73+
test('Lambda function returns correct response for show tables', async () => {
74+
const payload = {
75+
sqlQuery: 'show tables',
76+
secretName: '/rdsinitexample/rds/creds/mysql-01'
77+
};
78+
79+
try {
80+
const command = new InvokeCommand({
81+
FunctionName: 'my-lambda-rds-query-helper',
82+
Payload: Buffer.from(JSON.stringify(payload))
83+
});
84+
85+
const response = await lambdaClient.send(command);
86+
const result = JSON.parse(Buffer.from(response.Payload!).toString());
87+
expect(result.status).toBe('SUCCESS');
88+
expect(Array.isArray(result.results)).toBeTruthy();
89+
// Verify the expected tables are present
90+
const tables = result.results.map((r: any) => r.Tables_in_main);
91+
expect(tables).toContain('Books');
92+
expect(tables).toContain('OrderDetails');
93+
} catch (error) {
94+
expect(error).toBeFalsy();
95+
}
96+
});
97+
98+
test('Lambda function returns correct response for select query', async () => {
99+
const payload = {
100+
sqlQuery: 'select Author from Books',
101+
secretName: '/rdsinitexample/rds/creds/mysql-01'
102+
};
103+
104+
try {
105+
const command = new InvokeCommand({
106+
FunctionName: 'my-lambda-rds-query-helper',
107+
Payload: Buffer.from(JSON.stringify(payload))
108+
});
109+
110+
const response = await lambdaClient.send(command);
111+
const result = JSON.parse(Buffer.from(response.Payload!).toString());
112+
expect(result.status).toBe('SUCCESS');
113+
expect(Array.isArray(result.results)).toBeTruthy();
114+
// Verify we have author data
115+
const authors = result.results.map((r: any) => r.Author);
116+
expect(authors).toContain('Jane Doe');
117+
expect(authors).toContain('LocalStack');
118+
} catch (error) {
119+
expect(error).toBeFalsy();
120+
}
121+
});
122+
});

0 commit comments

Comments
 (0)