|
| 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 | +import { ListTagsForResourceCommand } from '@aws-sdk/client-rds'; |
| 9 | + |
| 10 | +export function testCustomDb(ctx: DatabaseTestContext) { |
| 11 | + it('should properly configure instance', () => { |
| 12 | + const customDb = ctx.outputs.customDb.value; |
| 13 | + |
| 14 | + assert.strictEqual( |
| 15 | + customDb.instance.applyImmediately, |
| 16 | + ctx.config.applyImmediately, |
| 17 | + 'Apply immediately argument should be set correctly', |
| 18 | + ); |
| 19 | + assert.strictEqual( |
| 20 | + customDb.instance.allowMajorVersionUpgrade, |
| 21 | + ctx.config.allowMajorVersionUpgrade, |
| 22 | + 'Allow major version upgrade argument should be set correctly', |
| 23 | + ); |
| 24 | + assert.strictEqual( |
| 25 | + customDb.instance.autoMinorVersionUpgrade, |
| 26 | + ctx.config.autoMinorVersionUpgrade, |
| 27 | + 'Auto minor version upgrade argument should be set correctly', |
| 28 | + ); |
| 29 | + }); |
| 30 | + |
| 31 | + it('should properly configure password', () => { |
| 32 | + const customDb = ctx.outputs.customDb.value; |
| 33 | + |
| 34 | + assert.ok(customDb.password, 'Password should exist'); |
| 35 | + assert.strictEqual( |
| 36 | + customDb.instance.masterUserPassword, |
| 37 | + ctx.config.dbPassword, |
| 38 | + 'Master user password should be set correctly', |
| 39 | + ); |
| 40 | + }); |
| 41 | + |
| 42 | + it('should properly configure storage', () => { |
| 43 | + const customDb = ctx.outputs.customDb.value; |
| 44 | + |
| 45 | + assert.strictEqual( |
| 46 | + customDb.instance.allocatedStorage, |
| 47 | + ctx.config.allocatedStorage.toString(), |
| 48 | + 'Allocated storage argument should be set correctly', |
| 49 | + ); |
| 50 | + assert.strictEqual( |
| 51 | + customDb.instance.maxAllocatedStorage, |
| 52 | + ctx.config.maxAllocatedStorage, |
| 53 | + 'Max allocated storage argument should be set correctly', |
| 54 | + ); |
| 55 | + }); |
| 56 | + |
| 57 | + it('should properly configure monitoring options', () => { |
| 58 | + const customDb = ctx.outputs.customDb.value; |
| 59 | + |
| 60 | + assert.strictEqual( |
| 61 | + customDb.instance.enablePerformanceInsights, |
| 62 | + true, |
| 63 | + 'Performance insights should be enabled', |
| 64 | + ); |
| 65 | + assert.strictEqual( |
| 66 | + customDb.instance.performanceInsightsRetentionPeriod, |
| 67 | + 7, |
| 68 | + 'Performance insights retention period should be set correctly', |
| 69 | + ); |
| 70 | + assert.strictEqual( |
| 71 | + customDb.instance.monitoringInterval, |
| 72 | + 60, |
| 73 | + 'Monitoring interval should be set correctly', |
| 74 | + ); |
| 75 | + assert.ok( |
| 76 | + customDb.instance.monitoringRoleArn, |
| 77 | + 'Monitoring role ARN should exist', |
| 78 | + ); |
| 79 | + }); |
| 80 | + |
| 81 | + it('should create monitoring IAM role and attach correct policy', async () => { |
| 82 | + const customDb = ctx.outputs.customDb.value; |
| 83 | + const roleName = customDb.monitoringRole.name; |
| 84 | + |
| 85 | + const roleCommand = new GetRoleCommand({ |
| 86 | + RoleName: roleName, |
| 87 | + }); |
| 88 | + const { Role } = await ctx.clients.iam.send(roleCommand); |
| 89 | + assert.ok(Role, 'Monitoring IAM role should exist'); |
| 90 | + |
| 91 | + const policyCommand = new ListAttachedRolePoliciesCommand({ |
| 92 | + RoleName: roleName, |
| 93 | + }); |
| 94 | + const { AttachedPolicies } = await ctx.clients.iam.send(policyCommand); |
| 95 | + assert.ok( |
| 96 | + AttachedPolicies && AttachedPolicies.length > 0, |
| 97 | + 'Attached policies should exist', |
| 98 | + ); |
| 99 | + const [attachedPolicy] = AttachedPolicies; |
| 100 | + assert.strictEqual( |
| 101 | + attachedPolicy.PolicyArn, |
| 102 | + 'arn:aws:iam::aws:policy/service-role/AmazonRDSEnhancedMonitoringRole', |
| 103 | + 'Monitoring IAM role should have correct policy attached', |
| 104 | + ); |
| 105 | + }); |
| 106 | + |
| 107 | + it('should properly configure kms', () => { |
| 108 | + const customDb = ctx.outputs.customDb.value; |
| 109 | + const kms = ctx.outputs.kms.value; |
| 110 | + |
| 111 | + assert.ok(customDb.kmsKeyId, 'Kms key id should exist'); |
| 112 | + assert.strictEqual( |
| 113 | + customDb.instance.kmsKeyId, |
| 114 | + kms.arn, |
| 115 | + 'Kms key id should be set correctly', |
| 116 | + ); |
| 117 | + }); |
| 118 | + |
| 119 | + it('should properly configure parameter group', () => { |
| 120 | + const customDb = ctx.outputs.customDb.value; |
| 121 | + const paramGroup = ctx.outputs.paramGroup.value; |
| 122 | + |
| 123 | + assert.strictEqual( |
| 124 | + customDb.instance.dbParameterGroupName, |
| 125 | + paramGroup.name, |
| 126 | + 'Parameter group name should be set correctly', |
| 127 | + ); |
| 128 | + }); |
| 129 | + |
| 130 | + it('should properly configure tags', async () => { |
| 131 | + const customDb = ctx.outputs.customDb.value; |
| 132 | + |
| 133 | + const command = new ListTagsForResourceCommand({ |
| 134 | + ResourceName: customDb.instance.dbInstanceArn, |
| 135 | + }); |
| 136 | + const { TagList } = await ctx.clients.rds.send(command); |
| 137 | + assert.ok(TagList && TagList.length > 0, 'Tags should exist'); |
| 138 | + |
| 139 | + Object.entries(ctx.config.tags).map(([Key, Value]) => { |
| 140 | + const tag = TagList.find(tag => tag.Key === Key); |
| 141 | + assert.ok(tag, `${Key} tag should exist`); |
| 142 | + assert.strictEqual(tag.Value, Value, `${Key} tag should set correctly`); |
| 143 | + }); |
| 144 | + }); |
| 145 | +} |
0 commit comments