Skip to content

Commit 40118f0

Browse files
committed
Add test for monitoring
1 parent a737feb commit 40118f0

File tree

5 files changed

+130
-30
lines changed

5 files changed

+130
-30
lines changed

tests/database/index.test.ts

Lines changed: 45 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,30 @@
11
import { describe, it, before, after } from 'node:test';
22
import { DescribeDBSubnetGroupsCommand, RDSClient } from '@aws-sdk/client-rds';
3-
import { DescribeKeyCommand, GetKeyRotationStatusCommand, KMSClient } from '@aws-sdk/client-kms';
4-
import { DescribeSecurityGroupsCommand, EC2Client, IpPermission } from '@aws-sdk/client-ec2';
3+
import {
4+
DescribeKeyCommand,
5+
GetKeyRotationStatusCommand,
6+
KMSClient,
7+
} from '@aws-sdk/client-kms';
8+
import {
9+
DescribeSecurityGroupsCommand,
10+
EC2Client,
11+
IpPermission,
12+
} from '@aws-sdk/client-ec2';
513
import * as assert from 'node:assert';
614
import * as automation from '../automation';
715
import * as config from './infrastructure/config';
816
import { DatabaseTestContext } from './test-context';
9-
import { InlineProgramArgs } from "@pulumi/pulumi/automation";
17+
import { IAMClient } from '@aws-sdk/client-iam';
18+
import { InlineProgramArgs } from '@pulumi/pulumi/automation';
19+
import { testDbWithMonitoring } from './monitoring.test';
1020

1121
const programArgs: InlineProgramArgs = {
1222
stackName: 'dev',
1323
projectName: 'icb-test-database',
1424
program: () => import('./infrastructure'),
1525
};
1626

17-
// TODO: Add tests for monitoring role & encrypted snapshot copy
27+
// TODO: Add tests for encrypted snapshot copy
1828

1929
describe('Database component deployment', () => {
2030
const region = process.env.AWS_REGION;
@@ -29,8 +39,9 @@ describe('Database component deployment', () => {
2939
rds: new RDSClient({ region }),
3040
ec2: new EC2Client({ region }),
3141
kms: new KMSClient({ region }),
32-
}
33-
}
42+
iam: new IAMClient({ region }),
43+
},
44+
};
3445

3546
before(async () => {
3647
ctx.outputs = await automation.deploy(programArgs);
@@ -51,11 +62,31 @@ describe('Database component deployment', () => {
5162
assert.ok(database.dbSubnetGroup, 'Subnet group should be defined');
5263
assert.ok(database.kms, 'Encryption key should be defined');
5364
assert.ok(database.password, 'Password should be defined');
54-
assert.strictEqual(database.instance.dbName, config.dbName, 'Db name argument should be set correctly');
55-
assert.strictEqual(database.instance.username, config.username, 'Username argument should be set correctly');
56-
assert.strictEqual(database.instance.password, config.password, 'Password argument should be set correctly');
57-
assert.strictEqual(database.instance.applyImmediately, config.applyImmediately, 'Apply immediately argument should be set correctly');
58-
assert.strictEqual(database.instance.skipFinalSnapshot, config.skipFinalSnapshot, 'Skip final snapshot argument should be set correctly');
65+
assert.strictEqual(
66+
database.instance.dbName,
67+
config.dbName,
68+
'Db name argument should be set correctly',
69+
);
70+
assert.strictEqual(
71+
database.instance.username,
72+
config.username,
73+
'Username argument should be set correctly',
74+
);
75+
assert.strictEqual(
76+
database.instance.password,
77+
config.password,
78+
'Password argument should be set correctly',
79+
);
80+
assert.strictEqual(
81+
database.instance.applyImmediately,
82+
config.applyImmediately,
83+
'Apply immediately argument should be set correctly',
84+
);
85+
assert.strictEqual(
86+
database.instance.skipFinalSnapshot,
87+
config.skipFinalSnapshot,
88+
'Skip final snapshot argument should be set correctly',
89+
);
5990
});
6091

6192
it('should create subnet group in the correct VPC', async () => {
@@ -133,11 +164,7 @@ describe('Database component deployment', () => {
133164
'ENCRYPT_DECRYPT',
134165
'KMS key should be used for encryption/decryption',
135166
);
136-
assert.strictEqual(
137-
KeyMetadata.Enabled,
138-
true,
139-
'KMS key should be enabled',
140-
);
167+
assert.strictEqual(KeyMetadata.Enabled, true, 'KMS key should be enabled');
141168
assert.strictEqual(
142169
KeyMetadata.MultiRegion,
143170
false,
@@ -152,4 +179,6 @@ describe('Database component deployment', () => {
152179
'KMS key rotation should be enabled',
153180
);
154181
});
182+
183+
describe('With monitoring', () => testDbWithMonitoring(ctx));
155184
});

tests/database/infrastructure/config.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
export const projectName = 'database-test-project';
2-
export const instanceName = 'database-test-instance';
1+
export const projectName = 'db-test-project';
2+
export const instanceName = 'db-test-instance';
33
export const dbName = 'databasetestdb';
44
export const username = 'databasetestusername';
55
export const password = 'databasetestpassword';
Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,27 @@
11
import { next as studion } from '@studion/infra-code-blocks';
22
import * as config from './config';
33

4-
export const vpc = new studion.Vpc(config.projectName, {});
4+
const vpc = new studion.Vpc(config.projectName, {});
55

6-
export const database = new studion.DatabaseBuilder(config.instanceName)
7-
.configure(
8-
config.dbName,
9-
config.username,
10-
{
11-
password: config.password,
12-
applyImmediately: config.applyImmediately,
13-
skipFinalSnapshot: config.skipFinalSnapshot
14-
},
15-
)
6+
const database = new studion.DatabaseBuilder(config.instanceName)
7+
.configure(config.dbName, config.username, {
8+
password: config.password,
9+
applyImmediately: config.applyImmediately,
10+
skipFinalSnapshot: config.skipFinalSnapshot,
11+
})
1612
.withVpc(vpc.vpc)
1713
.build();
14+
15+
const dbWithMonitoring = new studion.DatabaseBuilder(
16+
`${config.instanceName}-w-monitoring`,
17+
)
18+
.configure(config.dbName, config.username, {
19+
password: config.password,
20+
applyImmediately: config.applyImmediately,
21+
skipFinalSnapshot: config.skipFinalSnapshot,
22+
})
23+
.withVpc(vpc.vpc)
24+
.withMonitoring()
25+
.build();
26+
27+
export { vpc, database, dbWithMonitoring };

tests/database/monitoring.test.ts

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import {
2+
GetRoleCommand,
3+
ListAttachedRolePoliciesCommand,
4+
} from '@aws-sdk/client-iam';
5+
import * as assert from 'node:assert';
6+
import { DatabaseTestContext } from './test-context';
7+
import { it } from 'node:test';
8+
9+
export function testDbWithMonitoring(ctx: DatabaseTestContext) {
10+
it('should properly configure monitoring options', () => {
11+
const dbWithMonitoring = ctx.outputs.dbWithMonitoring.value;
12+
13+
assert.strictEqual(
14+
dbWithMonitoring.instance.performanceInsightsEnabled,
15+
true,
16+
'Performance insights should be enabled',
17+
);
18+
assert.strictEqual(
19+
dbWithMonitoring.instance.performanceInsightsRetentionPeriod,
20+
7,
21+
'Performance insights retention period should be set correctly',
22+
);
23+
assert.strictEqual(
24+
dbWithMonitoring.instance.monitoringInterval,
25+
60,
26+
'Monitoring interval should be set correctly',
27+
);
28+
assert.ok(
29+
dbWithMonitoring.instance.monitoringRoleArn,
30+
'Monitoring role ARN should exist',
31+
);
32+
});
33+
34+
it('should create monitoring IAM role and attach correct policy', async () => {
35+
const dbWithMonitoring = ctx.outputs.dbWithMonitoring.value;
36+
const roleName = dbWithMonitoring.monitoringRole.name;
37+
38+
const roleCommand = new GetRoleCommand({
39+
RoleName: roleName,
40+
});
41+
const { Role } = await ctx.clients.iam.send(roleCommand);
42+
assert.ok(Role, 'Monitoring IAM role should exist');
43+
44+
const policyCommand = new ListAttachedRolePoliciesCommand({
45+
RoleName: roleName,
46+
});
47+
const { AttachedPolicies } = await ctx.clients.iam.send(policyCommand);
48+
assert.ok(
49+
AttachedPolicies && AttachedPolicies.length > 0,
50+
'Attached policies should exist',
51+
);
52+
const [attachedPolicy] = AttachedPolicies;
53+
assert.strictEqual(
54+
attachedPolicy.PolicyArn,
55+
'arn:aws:iam::aws:policy/service-role/AmazonRDSEnhancedMonitoringRole',
56+
'Monitoring IAM role should have correct policy attached',
57+
);
58+
});
59+
}

tests/database/test-context.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { EC2Client } from '@aws-sdk/client-ec2';
2+
import { IAMClient } from '@aws-sdk/client-iam';
23
import { KMSClient } from '@aws-sdk/client-kms';
34
import { OutputMap } from '@pulumi/pulumi/automation';
45
import { RDSClient } from '@aws-sdk/client-rds';
@@ -25,7 +26,8 @@ interface AwsContext {
2526
clients: {
2627
rds: RDSClient;
2728
ec2: EC2Client;
28-
kms: KMSClient
29+
kms: KMSClient;
30+
iam: IAMClient;
2931
};
3032
}
3133

0 commit comments

Comments
 (0)