|
| 1 | +import { describe, expect, it } from 'vitest'; |
| 2 | +import { rdsStoppedInstanceRule } from '../src/aws/rds/stopped-instance.js'; |
| 3 | +import type { AwsRdsInstance } from '../src/index.js'; |
| 4 | +import { LiveResourceBag } from '../src/index.js'; |
| 5 | + |
| 6 | +const createInstance = (overrides: Partial<AwsRdsInstance> = {}): AwsRdsInstance => ({ |
| 7 | + accountId: '123456789012', |
| 8 | + dbInstanceIdentifier: 'stopped-db', |
| 9 | + dbInstanceStatus: 'stopped', |
| 10 | + engine: 'postgres', |
| 11 | + engineVersion: '16.2', |
| 12 | + instanceClass: 'db.m7g.large', |
| 13 | + instanceCreateTime: '2025-01-01T00:00:00.000Z', |
| 14 | + multiAz: false, |
| 15 | + region: 'us-east-1', |
| 16 | + ...overrides, |
| 17 | +}); |
| 18 | + |
| 19 | +describe('rdsStoppedInstanceRule', () => { |
| 20 | + it('flags stopped DB instances in discovery mode', () => { |
| 21 | + const finding = rdsStoppedInstanceRule.evaluateLive?.({ |
| 22 | + catalog: { |
| 23 | + indexType: 'LOCAL', |
| 24 | + resources: [], |
| 25 | + searchRegion: 'us-east-1', |
| 26 | + }, |
| 27 | + resources: new LiveResourceBag({ |
| 28 | + 'aws-rds-instances': [createInstance()], |
| 29 | + }), |
| 30 | + }); |
| 31 | + |
| 32 | + expect(finding).toEqual({ |
| 33 | + ruleId: 'CLDBRN-AWS-RDS-9', |
| 34 | + service: 'rds', |
| 35 | + source: 'discovery', |
| 36 | + message: 'Stopped RDS DB instances should be reviewed for cleanup.', |
| 37 | + findings: [ |
| 38 | + { |
| 39 | + accountId: '123456789012', |
| 40 | + region: 'us-east-1', |
| 41 | + resourceId: 'stopped-db', |
| 42 | + }, |
| 43 | + ], |
| 44 | + }); |
| 45 | + }); |
| 46 | + |
| 47 | + it('skips DB instances that are not stopped', () => { |
| 48 | + const finding = rdsStoppedInstanceRule.evaluateLive?.({ |
| 49 | + catalog: { |
| 50 | + indexType: 'LOCAL', |
| 51 | + resources: [], |
| 52 | + searchRegion: 'us-east-1', |
| 53 | + }, |
| 54 | + resources: new LiveResourceBag({ |
| 55 | + 'aws-rds-instances': [createInstance({ dbInstanceIdentifier: 'running-db', dbInstanceStatus: 'available' })], |
| 56 | + }), |
| 57 | + }); |
| 58 | + |
| 59 | + expect(finding).toBeNull(); |
| 60 | + }); |
| 61 | + |
| 62 | + it('returns only stopped DB instances from mixed discovery results', () => { |
| 63 | + const finding = rdsStoppedInstanceRule.evaluateLive?.({ |
| 64 | + catalog: { |
| 65 | + indexType: 'LOCAL', |
| 66 | + resources: [], |
| 67 | + searchRegion: 'us-east-1', |
| 68 | + }, |
| 69 | + resources: new LiveResourceBag({ |
| 70 | + 'aws-rds-instances': [ |
| 71 | + createInstance(), |
| 72 | + createInstance({ dbInstanceIdentifier: 'running-db', dbInstanceStatus: 'available' }), |
| 73 | + createInstance({ dbInstanceIdentifier: 'stopped-db-2', region: 'eu-west-1' }), |
| 74 | + ], |
| 75 | + }), |
| 76 | + }); |
| 77 | + |
| 78 | + expect(finding?.findings).toEqual([ |
| 79 | + { |
| 80 | + accountId: '123456789012', |
| 81 | + region: 'us-east-1', |
| 82 | + resourceId: 'stopped-db', |
| 83 | + }, |
| 84 | + { |
| 85 | + accountId: '123456789012', |
| 86 | + region: 'eu-west-1', |
| 87 | + resourceId: 'stopped-db-2', |
| 88 | + }, |
| 89 | + ]); |
| 90 | + }); |
| 91 | +}); |
0 commit comments