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
877 changes: 725 additions & 152 deletions package-lock.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
"@aws-sdk/client-efs": "^3.758.0",
"@aws-sdk/client-elastic-load-balancing-v2": "^3.764.0",
"@aws-sdk/client-elasticache": "^3.901.0",
"@aws-sdk/client-iam": "^3.952.0",
"@aws-sdk/client-kms": "^3.943.0",
"@aws-sdk/client-rds": "^3.943.0",
"@aws-sdk/client-route-53": "^3.782.0",
Expand Down
145 changes: 145 additions & 0 deletions tests/database/custom-db.test.ts
Copy link
Member

Choose a reason for hiding this comment

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

I feel like we are missing real integration tests here using Describe commands like we do in other tests. Also we are not actually testing if we can connect to the db like we do for example in upstash or elasticache tests.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I will add this to the previous PR also 👍

Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
import {
GetRoleCommand,
ListAttachedRolePoliciesCommand,
} from '@aws-sdk/client-iam';
import * as assert from 'node:assert';
import { DatabaseTestContext } from './test-context';
import { it } from 'node:test';
import { ListTagsForResourceCommand } from '@aws-sdk/client-rds';

export function testCustomDb(ctx: DatabaseTestContext) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Name of this suite doesn't tell much. What actually is being tested here are various configuration options, so renaming custom to configurable or configurationOptions would be more appropriate.

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

assert.strictEqual(
customDb.instance.applyImmediately,
ctx.config.applyImmediately,
'Apply immediately argument should be set correctly',
);
assert.strictEqual(
customDb.instance.allowMajorVersionUpgrade,
ctx.config.allowMajorVersionUpgrade,
'Allow major version upgrade argument should be set correctly',
);
assert.strictEqual(
customDb.instance.autoMinorVersionUpgrade,
ctx.config.autoMinorVersionUpgrade,
'Auto minor version upgrade argument should be set correctly',
);
});

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

assert.ok(customDb.password, 'Password should exist');
assert.strictEqual(
customDb.instance.masterUserPassword,
ctx.config.dbPassword,
'Master user password should be set correctly',
);
});

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

assert.strictEqual(
customDb.instance.allocatedStorage,
ctx.config.allocatedStorage.toString(),
'Allocated storage argument should be set correctly',
);
assert.strictEqual(
customDb.instance.maxAllocatedStorage,
ctx.config.maxAllocatedStorage,
'Max allocated storage argument should be set correctly',
);
});

it('should properly configure monitoring options', () => {
const customDb = ctx.outputs.customDb.value;

assert.strictEqual(
customDb.instance.enablePerformanceInsights,
true,
'Performance insights should be enabled',
);
assert.strictEqual(
customDb.instance.performanceInsightsRetentionPeriod,
7,
'Performance insights retention period should be set correctly',
);
assert.strictEqual(
customDb.instance.monitoringInterval,
60,
'Monitoring interval should be set correctly',
);
assert.ok(
customDb.instance.monitoringRoleArn,
'Monitoring role ARN should exist',
);
});

it('should create monitoring IAM role and attach correct policy', async () => {
const customDb = ctx.outputs.customDb.value;
const roleName = customDb.monitoringRole.name;

const roleCommand = new GetRoleCommand({
RoleName: roleName,
});
const { Role } = await ctx.clients.iam.send(roleCommand);
assert.ok(Role, 'Monitoring IAM role should exist');

const policyCommand = new ListAttachedRolePoliciesCommand({
RoleName: roleName,
});
const { AttachedPolicies } = await ctx.clients.iam.send(policyCommand);
assert.ok(
AttachedPolicies && AttachedPolicies.length > 0,
'Attached policies should exist',
);
const [attachedPolicy] = AttachedPolicies;
assert.strictEqual(
attachedPolicy.PolicyArn,
'arn:aws:iam::aws:policy/service-role/AmazonRDSEnhancedMonitoringRole',
'Monitoring IAM role should have correct policy attached',
);
});

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

assert.ok(customDb.kmsKeyId, 'Kms key id should exist');
assert.strictEqual(
customDb.instance.kmsKeyId,
kms.arn,
'Kms key id should be set correctly',
);
});

it('should properly configure parameter group', () => {
const customDb = ctx.outputs.customDb.value;
const paramGroup = ctx.outputs.paramGroup.value;

assert.strictEqual(
customDb.instance.dbParameterGroupName,
paramGroup.name,
'Parameter group name should be set correctly',
);
});

it('should properly configure tags', async () => {
const customDb = ctx.outputs.customDb.value;

const command = new ListTagsForResourceCommand({
ResourceName: customDb.instance.dbInstanceArn,
});
const { TagList } = await ctx.clients.rds.send(command);
assert.ok(TagList && TagList.length > 0, 'Tags should exist');

Object.entries(ctx.config.tags).map(([Key, Value]) => {
const tag = TagList.find(tag => tag.Key === Key);
Copy link
Member

Choose a reason for hiding this comment

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

tag implicitly has any type here

assert.ok(tag, `${Key} tag should exist`);
assert.strictEqual(tag.Value, Value, `${Key} tag should set correctly`);
});
});
}
4 changes: 4 additions & 0 deletions tests/database/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ import { cleanupSnapshots } from './utils/cleanup-snapshots';
import * as config from './infrastructure/config';
import { DatabaseTestContext } from './test-context';
import { EC2Client } from '@aws-sdk/client-ec2';
import { IAMClient } from '@aws-sdk/client-iam';
import { InlineProgramArgs } from '@pulumi/pulumi/automation';
import { KMSClient } from '@aws-sdk/client-kms';
import { RDSClient } from '@aws-sdk/client-rds';
import { requireEnv } from '../util';
import { testCustomDb } from './custom-db.test';
import { testDefaultDb } from './default-db.test';

const programArgs: InlineProgramArgs = {
Expand All @@ -24,6 +26,7 @@ const ctx: DatabaseTestContext = {
rds: new RDSClient({ region }),
ec2: new EC2Client({ region }),
kms: new KMSClient({ region }),
iam: new IAMClient({ region }),
},
};

Expand All @@ -38,4 +41,5 @@ describe('Database component deployment', () => {
});

describe('Default database', () => testDefaultDb(ctx));
describe('Custom database', () => testCustomDb(ctx));
});
13 changes: 13 additions & 0 deletions tests/database/infrastructure/config.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
import * as pulumi from '@pulumi/pulumi';

export const appName = 'db-test';
export const stackName = pulumi.getStack();
export const tags = {
Project: appName,
Environment: stackName,
};
export const dbName = 'dbname';
export const dbUsername = 'dbusername';
export const dbPassword = 'dbpassword';
export const applyImmediately = true;
export const allowMajorVersionUpgrade = true;
export const autoMinorVersionUpgrade = false;
export const allocatedStorage = 10;
export const maxAllocatedStorage = 50;
55 changes: 48 additions & 7 deletions tests/database/infrastructure/index.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,58 @@
import { appName, dbName, dbUsername } from './config';
import { next as studion } from '@studion/infra-code-blocks';
import * as aws from '@pulumi/aws';
import * as config from './config';
import { DatabaseBuilder } from '../../../dist/v2/components/database/builder';
import { next as studion } from '@studion/infra-code-blocks';

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

const defaultDb = new DatabaseBuilder(`${config.appName}-default`)
.withInstance({
dbName: config.dbName,
})
.withCredentials({
username: config.dbUsername,
})
.withVpc(vpc.vpc)
.build();

const kms = new aws.kms.Key(`${config.appName}-kms`, {
description: `${config.appName} RDS encryption key`,
customerMasterKeySpec: 'SYMMETRIC_DEFAULT',
isEnabled: true,
keyUsage: 'ENCRYPT_DECRYPT',
multiRegion: false,
enableKeyRotation: true,
tags: config.tags,
});

const vpc = new studion.Vpc(`${appName}-vpc`, {});
const paramGroup = new aws.rds.ParameterGroup(
`${config.appName}-parameter-group`,
{
family: 'postgres17',
tags: config.tags,
},
);

const defaultDb = new DatabaseBuilder(`${appName}-default`)
const customDb = new DatabaseBuilder(`${config.appName}-custom`)
.withInstance({
dbName,
dbName: config.dbName,
applyImmediately: config.applyImmediately,
allowMajorVersionUpgrade: config.allowMajorVersionUpgrade,
autoMinorVersionUpgrade: config.autoMinorVersionUpgrade,
})
.withCredentials({
username: dbUsername,
username: config.dbUsername,
password: config.dbPassword,
})
.withStorage({
allocatedStorage: config.allocatedStorage,
maxAllocatedStorage: config.maxAllocatedStorage,
})
.withVpc(vpc.vpc)
.withMonitoring()
.withKms(kms.arn)
.withParameterGroup(paramGroup.name)
.withTags(config.tags)
.build();

export { vpc, defaultDb };
export { vpc, defaultDb, kms, paramGroup, customDb };
13 changes: 13 additions & 0 deletions tests/database/test-context.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { EC2Client } from '@aws-sdk/client-ec2';
import { IAMClient } from '@aws-sdk/client-iam';
import { KMSClient } from '@aws-sdk/client-kms';
import { OutputMap } from '@pulumi/pulumi/automation';
import { RDSClient } from '@aws-sdk/client-rds';
Expand All @@ -9,8 +10,19 @@ interface ConfigContext {

interface DatabaseTestConfig {
appName: string;
stackName: string;
tags: {
Project: string;
Environment: string;
};
dbName: string;
dbUsername: string;
dbPassword: string;
applyImmediately: boolean;
allowMajorVersionUpgrade: boolean;
autoMinorVersionUpgrade: boolean;
allocatedStorage: number;
maxAllocatedStorage: number;
}

interface PulumiProgramContext {
Expand All @@ -22,6 +34,7 @@ interface AwsContext {
rds: RDSClient;
ec2: EC2Client;
kms: KMSClient;
iam: IAMClient;
};
}

Expand Down
6 changes: 4 additions & 2 deletions tests/database/utils/cleanup-snapshots.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ import { DatabaseTestContext } from '../test-context';
export async function cleanupSnapshots(ctx: DatabaseTestContext) {
const spinner = createSpinner('Deleting snapshots...').start();

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

spinner.success({ text: 'Snapshots deleted' });
}
Expand Down