Skip to content
2 changes: 2 additions & 0 deletions tests/database/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import { KMSClient } from '@aws-sdk/client-kms';
import { RDSClient } from '@aws-sdk/client-rds';
import { requireEnv } from '../util';
import { testConfigurableDb } from './configurable-db.test';
import { testSnapshotDb } from './snapshot-db.test';

const programArgs: InlineProgramArgs = {
stackName: 'dev',
Expand Down Expand Up @@ -207,4 +208,5 @@ describe('Database component deployment', () => {
});

describe('With configurable options', () => testConfigurableDb(ctx));
describe('With snapshot', () => testSnapshotDb(ctx));
});
37 changes: 36 additions & 1 deletion tests/database/infrastructure/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,39 @@ const configurableDb = new DatabaseBuilder(`${config.appName}-configurable-db`)
.withTags(config.tags)
.build({ parent });

export { vpc, defaultDb, kms, paramGroup, configurableDb };
const snapshot = defaultDb.instance.dbInstanceIdentifier.apply(
dbInstanceIdentifier => {
if (!dbInstanceIdentifier) return;
return new aws.rds.Snapshot(
`${config.appName}-snapshot`,
{
dbInstanceIdentifier: dbInstanceIdentifier,
dbSnapshotIdentifier: `${config.appName}-snapshot-id`,
tags: config.tags,
},
{ parent },
);
},
);

const snapshotDb = snapshot.apply(snapshot => {
if (!snapshot) return;
return new DatabaseBuilder(`${config.appName}-snapshot-db`)
.withInstance({
applyImmediately: true,
})
.withVpc(vpc.vpc)
.withTags(config.tags)
.withSnapshot(snapshot.id)
.build({ parent });
});

export {
vpc,
defaultDb,
kms,
paramGroup,
configurableDb,
snapshot,
snapshotDb,
};
41 changes: 41 additions & 0 deletions tests/database/snapshot-db.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import * as assert from 'node:assert';
import { DatabaseTestContext } from './test-context';
import { it } from 'node:test';

export function testSnapshotDb(ctx: DatabaseTestContext) {
it('should create and properly configure encrypted snapshot copy', () => {
const snapshotDb = ctx.outputs.snapshotDb.value;
const snapshot = ctx.outputs.snapshot.value;

assert.ok(
snapshotDb.encryptedSnapshotCopy,
'Encrtyped snapshot copy should exist',
);

assert.strictEqual(
snapshotDb.encryptedSnapshotCopy.sourceDbSnapshotIdentifier,
snapshot.dbSnapshotArn,
'Encrtyped snapshot copy should have correct source db snapshot identifier',
);
assert.strictEqual(
snapshotDb.encryptedSnapshotCopy.targetDbSnapshotIdentifier,
`${snapshot.id}-encrypted-copy`,
'Encrtyped snapshot copy should have the correct target db snapshot identifier',
);
assert.strictEqual(
snapshotDb.encryptedSnapshotCopy.kmsKeyId,
snapshotDb.kmsKeyId,
'Encrtyped snapshot copy should have the correct ksm key id',
);
});

it('should properly configure instance', () => {
const snapshotDb = ctx.outputs.snapshotDb.value;

assert.strictEqual(
snapshotDb.instance.dbSnapshotIdentifier,
snapshotDb.encryptedSnapshotCopy.targetDbSnapshotIdentifier,
'Db snapshot identifier should be set correctly',
);
});
}
6 changes: 5 additions & 1 deletion tests/database/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ import { DatabaseTestContext } from './test-context';
export async function cleanupSnapshots(ctx: DatabaseTestContext) {
const spinner = createSpinner('Deleting snapshots...').start();

const dbs = [ctx.outputs.defaultDb.value, ctx.outputs.configurableDb.value];
const dbs = [
ctx.outputs.defaultDb.value,
ctx.outputs.configurableDb.value,
ctx.outputs.snapshotDb.value,
];
await Promise.all(
dbs.map(db => deleteSnapshot(ctx, db.instance.dbInstanceIdentifier)),
);
Expand Down