diff --git a/tests/database/index.test.ts b/tests/database/index.test.ts index f17139e..b9d02fc 100644 --- a/tests/database/index.test.ts +++ b/tests/database/index.test.ts @@ -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', @@ -207,4 +208,5 @@ describe('Database component deployment', () => { }); describe('With configurable options', () => testConfigurableDb(ctx)); + describe('With snapshot', () => testSnapshotDb(ctx)); }); diff --git a/tests/database/infrastructure/index.ts b/tests/database/infrastructure/index.ts index 80f069b..0b9b677 100644 --- a/tests/database/infrastructure/index.ts +++ b/tests/database/infrastructure/index.ts @@ -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, +}; diff --git a/tests/database/snapshot-db.test.ts b/tests/database/snapshot-db.test.ts new file mode 100644 index 0000000..cb2695e --- /dev/null +++ b/tests/database/snapshot-db.test.ts @@ -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', + ); + }); +} diff --git a/tests/database/util.ts b/tests/database/util.ts index dee6b20..e60a2e4 100644 --- a/tests/database/util.ts +++ b/tests/database/util.ts @@ -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)), );