|
| 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