|
| 1 | +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +import { App, Stack } from 'aws-cdk-lib'; |
| 5 | +import { Template } from 'aws-cdk-lib/assertions'; |
| 6 | +import { describe, it, expect, afterEach } from 'vitest'; |
| 7 | + |
| 8 | +import { TEST_NAMESPACE } from '../../../constants/testConstants.js'; |
| 9 | +import { DynamoDBTable } from '../../storage/dynamoDB.js'; |
| 10 | +import { getDeploymentMode, isDevMode } from '../deploymentModeHelper.js'; |
| 11 | +import { LogGroupCategory, LogGroupsHelper } from '../logGroupsHelper.js'; |
| 12 | + |
| 13 | +// Mock the KmsHelper to avoid having the static KMS key shared between stacks |
| 14 | +vi.mock('../kmsHelper.js', async (importOriginal) => { |
| 15 | + const actual = await importOriginal<typeof import('../kmsHelper.js')>(); |
| 16 | + const { Key } = await import('aws-cdk-lib/aws-kms'); |
| 17 | + return { |
| 18 | + ...actual, |
| 19 | + KmsHelper: { |
| 20 | + ...actual.KmsHelper, |
| 21 | + get: vi.fn().mockImplementation((scope, namespace) => { |
| 22 | + return new Key(scope, `MockKmsKey${namespace}${Date.now()}`, { |
| 23 | + description: `Mock KMS key for testing ${namespace}`, |
| 24 | + }); |
| 25 | + }), |
| 26 | + }, |
| 27 | + }; |
| 28 | +}); |
| 29 | + |
| 30 | +type LogGroupsHelperWithReset = typeof LogGroupsHelper & { |
| 31 | + reset: () => void; |
| 32 | +}; |
| 33 | + |
| 34 | +type LogGroupsModuleWithReset = typeof import('../logGroupsHelper.js') & { |
| 35 | + LogGroupsHelper: LogGroupsHelperWithReset; |
| 36 | +}; |
| 37 | + |
| 38 | +const asLogGroupsHelperWithReset = (): LogGroupsHelperWithReset => { |
| 39 | + return LogGroupsHelper as LogGroupsHelperWithReset; |
| 40 | +}; |
| 41 | + |
| 42 | +vi.mock('../logGroupsHelper.js', async () => { |
| 43 | + const actual = await vi.importActual('../logGroupsHelper.js'); |
| 44 | + |
| 45 | + const mockedActual = actual as LogGroupsModuleWithReset; |
| 46 | + mockedActual.LogGroupsHelper.reset = () => { |
| 47 | + // @ts-expect-error - accessing private static property for testing |
| 48 | + (mockedActual.LogGroupsHelper as LogGroupsHelper).logGroups = []; |
| 49 | + // @ts-expect-error - accessing private static property for testing |
| 50 | + (mockedActual.LogGroupsHelper as LogGroupsHelper).logGroupsByCategory = new Map(); |
| 51 | + }; |
| 52 | + |
| 53 | + return mockedActual; |
| 54 | +}); |
| 55 | + |
| 56 | +describe('deploymentModeHelper', () => { |
| 57 | + afterEach(() => { |
| 58 | + asLogGroupsHelperWithReset().reset(); |
| 59 | + }); |
| 60 | + |
| 61 | + describe('getDeploymentMode', () => { |
| 62 | + it('should return empty string when DEPLOYMENT_MODE context is not set', () => { |
| 63 | + const app = new App(); |
| 64 | + const stack = new Stack(app, 'TestStack'); |
| 65 | + |
| 66 | + expect(getDeploymentMode(stack)).toBe(''); |
| 67 | + }); |
| 68 | + |
| 69 | + it('should return the DEPLOYMENT_MODE context value when set', () => { |
| 70 | + const app = new App({ |
| 71 | + context: { |
| 72 | + DEPLOYMENT_MODE: 'dev', |
| 73 | + }, |
| 74 | + }); |
| 75 | + const stack = new Stack(app, 'TestStack'); |
| 76 | + |
| 77 | + expect(getDeploymentMode(stack)).toBe('dev'); |
| 78 | + }); |
| 79 | + |
| 80 | + it('should return the exact value including case', () => { |
| 81 | + const app = new App({ |
| 82 | + context: { |
| 83 | + DEPLOYMENT_MODE: 'PROD', |
| 84 | + }, |
| 85 | + }); |
| 86 | + const stack = new Stack(app, 'TestStack'); |
| 87 | + |
| 88 | + expect(getDeploymentMode(stack)).toBe('PROD'); |
| 89 | + }); |
| 90 | + }); |
| 91 | + |
| 92 | + describe('isDevMode', () => { |
| 93 | + it('should return false when DEPLOYMENT_MODE is not set', () => { |
| 94 | + const app = new App(); |
| 95 | + const stack = new Stack(app, 'TestStack'); |
| 96 | + |
| 97 | + expect(isDevMode(stack)).toBe(false); |
| 98 | + }); |
| 99 | + |
| 100 | + it('should return true when DEPLOYMENT_MODE is "dev"', () => { |
| 101 | + const app = new App({ |
| 102 | + context: { |
| 103 | + DEPLOYMENT_MODE: 'dev', |
| 104 | + }, |
| 105 | + }); |
| 106 | + const stack = new Stack(app, 'TestStack'); |
| 107 | + |
| 108 | + expect(isDevMode(stack)).toBe(true); |
| 109 | + }); |
| 110 | + |
| 111 | + it('should return true when DEPLOYMENT_MODE is "DEV" (case insensitive)', () => { |
| 112 | + const app = new App({ |
| 113 | + context: { |
| 114 | + DEPLOYMENT_MODE: 'DEV', |
| 115 | + }, |
| 116 | + }); |
| 117 | + const stack = new Stack(app, 'TestStack'); |
| 118 | + |
| 119 | + expect(isDevMode(stack)).toBe(true); |
| 120 | + }); |
| 121 | + |
| 122 | + it('should return true when DEPLOYMENT_MODE is "Dev" (case insensitive)', () => { |
| 123 | + const app = new App({ |
| 124 | + context: { |
| 125 | + DEPLOYMENT_MODE: 'Dev', |
| 126 | + }, |
| 127 | + }); |
| 128 | + const stack = new Stack(app, 'TestStack'); |
| 129 | + |
| 130 | + expect(isDevMode(stack)).toBe(true); |
| 131 | + }); |
| 132 | + |
| 133 | + it('should return false when DEPLOYMENT_MODE is "prod"', () => { |
| 134 | + const app = new App({ |
| 135 | + context: { |
| 136 | + DEPLOYMENT_MODE: 'prod', |
| 137 | + }, |
| 138 | + }); |
| 139 | + const stack = new Stack(app, 'TestStack'); |
| 140 | + |
| 141 | + expect(isDevMode(stack)).toBe(false); |
| 142 | + }); |
| 143 | + |
| 144 | + it('should return false when DEPLOYMENT_MODE is empty string', () => { |
| 145 | + const app = new App({ |
| 146 | + context: { |
| 147 | + DEPLOYMENT_MODE: '', |
| 148 | + }, |
| 149 | + }); |
| 150 | + const stack = new Stack(app, 'TestStack'); |
| 151 | + |
| 152 | + expect(isDevMode(stack)).toBe(false); |
| 153 | + }); |
| 154 | + }); |
| 155 | + |
| 156 | + describe('DynamoDB table removal policy based on deployment mode', () => { |
| 157 | + it('should set RemovalPolicy.RETAIN when DEPLOYMENT_MODE is not set', () => { |
| 158 | + const app = new App(); |
| 159 | + const stack = new Stack(app, 'TestStack'); |
| 160 | + |
| 161 | + new DynamoDBTable(stack, 'TestDynamoDB', { |
| 162 | + namespace: TEST_NAMESPACE, |
| 163 | + }); |
| 164 | + |
| 165 | + const template = Template.fromStack(stack); |
| 166 | + expect(() => { |
| 167 | + template.hasResource('AWS::DynamoDB::GlobalTable', { |
| 168 | + DeletionPolicy: 'Retain', |
| 169 | + UpdateReplacePolicy: 'Retain', |
| 170 | + }); |
| 171 | + }).not.toThrow(); |
| 172 | + }); |
| 173 | + |
| 174 | + it('should set RemovalPolicy.DESTROY when DEPLOYMENT_MODE is "dev"', () => { |
| 175 | + const app = new App({ |
| 176 | + context: { |
| 177 | + DEPLOYMENT_MODE: 'dev', |
| 178 | + }, |
| 179 | + }); |
| 180 | + const stack = new Stack(app, 'TestStack'); |
| 181 | + |
| 182 | + new DynamoDBTable(stack, 'TestDynamoDB', { |
| 183 | + namespace: TEST_NAMESPACE, |
| 184 | + }); |
| 185 | + |
| 186 | + const template = Template.fromStack(stack); |
| 187 | + expect(() => { |
| 188 | + template.hasResource('AWS::DynamoDB::GlobalTable', { |
| 189 | + DeletionPolicy: 'Delete', |
| 190 | + UpdateReplacePolicy: 'Delete', |
| 191 | + }); |
| 192 | + }).not.toThrow(); |
| 193 | + }); |
| 194 | + |
| 195 | + it('should set RemovalPolicy.RETAIN when DEPLOYMENT_MODE is "prod"', () => { |
| 196 | + const app = new App({ |
| 197 | + context: { |
| 198 | + DEPLOYMENT_MODE: 'prod', |
| 199 | + }, |
| 200 | + }); |
| 201 | + const stack = new Stack(app, 'TestStack'); |
| 202 | + |
| 203 | + new DynamoDBTable(stack, 'TestDynamoDB', { |
| 204 | + namespace: TEST_NAMESPACE, |
| 205 | + }); |
| 206 | + |
| 207 | + const template = Template.fromStack(stack); |
| 208 | + expect(() => { |
| 209 | + template.hasResource('AWS::DynamoDB::GlobalTable', { |
| 210 | + DeletionPolicy: 'Retain', |
| 211 | + UpdateReplacePolicy: 'Retain', |
| 212 | + }); |
| 213 | + }).not.toThrow(); |
| 214 | + }); |
| 215 | + }); |
| 216 | + |
| 217 | + describe('LogGroup removal policy based on deployment mode', () => { |
| 218 | + it('should set RemovalPolicy.RETAIN when DEPLOYMENT_MODE is not set', () => { |
| 219 | + const app = new App(); |
| 220 | + const stack = new Stack(app, 'TestStack'); |
| 221 | + |
| 222 | + LogGroupsHelper.getOrCreateLogGroup(stack, 'TestLogGroup', { |
| 223 | + logGroupCategory: LogGroupCategory.DEFAULT, |
| 224 | + namespace: TEST_NAMESPACE, |
| 225 | + }); |
| 226 | + |
| 227 | + const template = Template.fromStack(stack); |
| 228 | + expect(() => { |
| 229 | + template.hasResource('AWS::Logs::LogGroup', { |
| 230 | + DeletionPolicy: 'Retain', |
| 231 | + UpdateReplacePolicy: 'Retain', |
| 232 | + }); |
| 233 | + }).not.toThrow(); |
| 234 | + }); |
| 235 | + |
| 236 | + it('should set RemovalPolicy.DESTROY when DEPLOYMENT_MODE is "dev"', () => { |
| 237 | + const app = new App({ |
| 238 | + context: { |
| 239 | + DEPLOYMENT_MODE: 'dev', |
| 240 | + }, |
| 241 | + }); |
| 242 | + const stack = new Stack(app, 'TestStack'); |
| 243 | + |
| 244 | + LogGroupsHelper.getOrCreateLogGroup(stack, 'TestLogGroup', { |
| 245 | + logGroupCategory: LogGroupCategory.WORKFLOW, |
| 246 | + namespace: TEST_NAMESPACE, |
| 247 | + }); |
| 248 | + |
| 249 | + const template = Template.fromStack(stack); |
| 250 | + expect(() => { |
| 251 | + template.hasResource('AWS::Logs::LogGroup', { |
| 252 | + DeletionPolicy: 'Delete', |
| 253 | + UpdateReplacePolicy: 'Delete', |
| 254 | + }); |
| 255 | + }).not.toThrow(); |
| 256 | + }); |
| 257 | + |
| 258 | + it('should set RemovalPolicy.RETAIN when DEPLOYMENT_MODE is "prod"', () => { |
| 259 | + const app = new App({ |
| 260 | + context: { |
| 261 | + DEPLOYMENT_MODE: 'prod', |
| 262 | + }, |
| 263 | + }); |
| 264 | + const stack = new Stack(app, 'TestStack'); |
| 265 | + |
| 266 | + LogGroupsHelper.getOrCreateLogGroup(stack, 'TestLogGroup', { |
| 267 | + logGroupCategory: LogGroupCategory.API, |
| 268 | + namespace: TEST_NAMESPACE, |
| 269 | + }); |
| 270 | + |
| 271 | + const template = Template.fromStack(stack); |
| 272 | + expect(() => { |
| 273 | + template.hasResource('AWS::Logs::LogGroup', { |
| 274 | + DeletionPolicy: 'Retain', |
| 275 | + UpdateReplacePolicy: 'Retain', |
| 276 | + }); |
| 277 | + }).not.toThrow(); |
| 278 | + }); |
| 279 | + }); |
| 280 | +}); |
0 commit comments