Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion tests/database/default-db.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export function testDefaultDb(ctx: DatabaseTestContext) {
assert.ok(database, 'Database should be defined');
assert.strictEqual(
database.name,
`${ctx.config.appName}-default`,
`${ctx.config.appName}-default-db`,
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change is here because I changed how databases are named inside this PR (see infrastructure file), as this should be the last PR regarding tests. I can move this to previous PR's if needed

'Database should have correct name',
);

Expand Down
2 changes: 2 additions & 0 deletions tests/database/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { RDSClient } from '@aws-sdk/client-rds';
import { requireEnv } from '../util';
import { testCustomDb } from './custom-db.test';
import { testDefaultDb } from './default-db.test';
import { testSnapshotDb } from './snapshot-db.test';

const programArgs: InlineProgramArgs = {
stackName: 'dev',
Expand Down Expand Up @@ -39,5 +40,6 @@ describe('Database component deployment', () => {

describe('Default database', () => testDefaultDb(ctx));
describe('Custom database', () => testCustomDb(ctx));
describe('Snapshot database', () => testSnapshotDb(ctx));
after(() => cleanupSnapshots(ctx));
});
31 changes: 27 additions & 4 deletions tests/database/infrastructure/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import * as config from './config';

const vpc = new studion.Vpc(`${config.appName}-vpc`, {});

const defaultDb = new DatabaseBuilder(`${config.appName}-default`)
const defaultDb = new DatabaseBuilder(`${config.appName}-default-db`)
.withInstance({
dbName: config.dbName,
})
Expand All @@ -15,7 +15,7 @@ const defaultDb = new DatabaseBuilder(`${config.appName}-default`)
.withVpc(vpc.vpc)
.build();

const kms = new aws.kms.Key(`${config.appName}-kms`, {
const kms = new aws.kms.Key(`${config.appName}-kms-key`, {
description: `${config.appName} RDS encryption key`,
customerMasterKeySpec: 'SYMMETRIC_DEFAULT',
isEnabled: true,
Expand All @@ -33,7 +33,7 @@ const paramGroup = new aws.rds.ParameterGroup(
},
);

const customDb = new DatabaseBuilder(`${config.appName}-custom`)
const customDb = new DatabaseBuilder(`${config.appName}-custom-db`)
.withInstance({
dbName: config.dbName,
applyImmediately: config.applyImmediately,
Expand All @@ -55,4 +55,27 @@ const customDb = new DatabaseBuilder(`${config.appName}-custom`)
.withTags(config.tags)
.build();

export { vpc, defaultDb, kms, paramGroup, customDb };
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,
});
},
);

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();
});

export { vpc, defaultDb, kms, paramGroup, customDb, 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/utils/cleanup-snapshots.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.customDb.value];
const dbs = [
ctx.outputs.defaultDb.value,
ctx.outputs.customDb.value,
ctx.outputs.snapshotDb.value,
];
await Promise.all(
dbs.map(db => deleteSnapshot(ctx, db.instance.dbInstanceIdentifier)),
);
Expand Down