|
1 | 1 | import { AmplifyGen2MigrationValidations } from '../../../commands/gen2-migration/_validations'; |
2 | 2 | import { $TSContext } from '@aws-amplify/amplify-cli-core'; |
3 | | -import { DescribeChangeSetOutput } from '@aws-sdk/client-cloudformation'; |
| 3 | +import { CloudFormationClient, DescribeChangeSetOutput } from '@aws-sdk/client-cloudformation'; |
| 4 | + |
| 5 | +jest.mock('@aws-sdk/client-cloudformation'); |
4 | 6 |
|
5 | 7 | describe('AmplifyGen2MigrationValidations', () => { |
6 | 8 | let mockContext: $TSContext; |
@@ -317,4 +319,175 @@ describe('AmplifyGen2MigrationValidations', () => { |
317 | 319 | }); |
318 | 320 | }); |
319 | 321 | }); |
| 322 | + |
| 323 | + describe('validateStatefulResources - nested stacks', () => { |
| 324 | + let mockSend: jest.Mock; |
| 325 | + |
| 326 | + beforeEach(() => { |
| 327 | + mockSend = jest.fn(); |
| 328 | + (CloudFormationClient as jest.Mock).mockImplementation(() => ({ |
| 329 | + send: mockSend, |
| 330 | + })); |
| 331 | + }); |
| 332 | + |
| 333 | + afterEach(() => { |
| 334 | + jest.clearAllMocks(); |
| 335 | + }); |
| 336 | + |
| 337 | + it('should throw when nested stack contains stateful resources', async () => { |
| 338 | + mockSend.mockResolvedValueOnce({ |
| 339 | + StackResources: [ |
| 340 | + { |
| 341 | + ResourceType: 'AWS::DynamoDB::Table', |
| 342 | + PhysicalResourceId: 'MyTable', |
| 343 | + LogicalResourceId: 'Table', |
| 344 | + }, |
| 345 | + ], |
| 346 | + }); |
| 347 | + |
| 348 | + const changeSet: DescribeChangeSetOutput = { |
| 349 | + Changes: [ |
| 350 | + { |
| 351 | + Type: 'Resource', |
| 352 | + ResourceChange: { |
| 353 | + Action: 'Remove', |
| 354 | + ResourceType: 'AWS::CloudFormation::Stack', |
| 355 | + LogicalResourceId: 'AuthStack', |
| 356 | + PhysicalResourceId: 'auth-stack', |
| 357 | + }, |
| 358 | + }, |
| 359 | + ], |
| 360 | + }; |
| 361 | + |
| 362 | + await expect(validations.validateStatefulResources(changeSet)).rejects.toMatchObject({ |
| 363 | + name: 'DestructiveMigrationError', |
| 364 | + message: 'Stateful resources scheduled for deletion: AuthStack (AWS::CloudFormation::Stack).', |
| 365 | + }); |
| 366 | + }); |
| 367 | + |
| 368 | + it('should pass when nested stack contains only stateless resources', async () => { |
| 369 | + mockSend.mockResolvedValueOnce({ |
| 370 | + StackResources: [ |
| 371 | + { |
| 372 | + ResourceType: 'AWS::Lambda::Function', |
| 373 | + PhysicalResourceId: 'MyFunction', |
| 374 | + LogicalResourceId: 'Function', |
| 375 | + }, |
| 376 | + ], |
| 377 | + }); |
| 378 | + |
| 379 | + const changeSet: DescribeChangeSetOutput = { |
| 380 | + Changes: [ |
| 381 | + { |
| 382 | + Type: 'Resource', |
| 383 | + ResourceChange: { |
| 384 | + Action: 'Remove', |
| 385 | + ResourceType: 'AWS::CloudFormation::Stack', |
| 386 | + LogicalResourceId: 'LambdaStack', |
| 387 | + PhysicalResourceId: 'lambda-stack', |
| 388 | + }, |
| 389 | + }, |
| 390 | + ], |
| 391 | + }; |
| 392 | + |
| 393 | + await expect(validations.validateStatefulResources(changeSet)).resolves.not.toThrow(); |
| 394 | + }); |
| 395 | + |
| 396 | + it('should handle multiple levels of nested stacks', async () => { |
| 397 | + mockSend.mockResolvedValueOnce({ |
| 398 | + StackResources: [ |
| 399 | + { |
| 400 | + ResourceType: 'AWS::CloudFormation::Stack', |
| 401 | + PhysicalResourceId: 'storage-nested-stack', |
| 402 | + LogicalResourceId: 'StorageNestedStack', |
| 403 | + }, |
| 404 | + ], |
| 405 | + }); |
| 406 | + |
| 407 | + mockSend.mockResolvedValueOnce({ |
| 408 | + StackResources: [ |
| 409 | + { |
| 410 | + ResourceType: 'AWS::S3::Bucket', |
| 411 | + PhysicalResourceId: 'my-bucket', |
| 412 | + LogicalResourceId: 'Bucket', |
| 413 | + }, |
| 414 | + ], |
| 415 | + }); |
| 416 | + |
| 417 | + const changeSet: DescribeChangeSetOutput = { |
| 418 | + Changes: [ |
| 419 | + { |
| 420 | + Type: 'Resource', |
| 421 | + ResourceChange: { |
| 422 | + Action: 'Remove', |
| 423 | + ResourceType: 'AWS::CloudFormation::Stack', |
| 424 | + LogicalResourceId: 'StorageStack', |
| 425 | + PhysicalResourceId: 'storage-stack', |
| 426 | + }, |
| 427 | + }, |
| 428 | + ], |
| 429 | + }; |
| 430 | + |
| 431 | + await expect(validations.validateStatefulResources(changeSet)).rejects.toMatchObject({ |
| 432 | + name: 'DestructiveMigrationError', |
| 433 | + }); |
| 434 | + }); |
| 435 | + |
| 436 | + it('should pass when nested stack is missing PhysicalResourceId', async () => { |
| 437 | + const changeSet: DescribeChangeSetOutput = { |
| 438 | + Changes: [ |
| 439 | + { |
| 440 | + Type: 'Resource', |
| 441 | + ResourceChange: { |
| 442 | + Action: 'Remove', |
| 443 | + ResourceType: 'AWS::CloudFormation::Stack', |
| 444 | + LogicalResourceId: 'IncompleteStack', |
| 445 | + PhysicalResourceId: undefined, |
| 446 | + }, |
| 447 | + }, |
| 448 | + ], |
| 449 | + }; |
| 450 | + |
| 451 | + await expect(validations.validateStatefulResources(changeSet)).resolves.not.toThrow(); |
| 452 | + }); |
| 453 | + |
| 454 | + it('should handle mixed direct and nested stateful resources', async () => { |
| 455 | + mockSend.mockResolvedValueOnce({ |
| 456 | + StackResources: [ |
| 457 | + { |
| 458 | + ResourceType: 'AWS::Cognito::UserPool', |
| 459 | + PhysicalResourceId: 'user-pool', |
| 460 | + LogicalResourceId: 'UserPool', |
| 461 | + }, |
| 462 | + ], |
| 463 | + }); |
| 464 | + |
| 465 | + const changeSet: DescribeChangeSetOutput = { |
| 466 | + Changes: [ |
| 467 | + { |
| 468 | + Type: 'Resource', |
| 469 | + ResourceChange: { |
| 470 | + Action: 'Remove', |
| 471 | + ResourceType: 'AWS::DynamoDB::Table', |
| 472 | + LogicalResourceId: 'DirectTable', |
| 473 | + }, |
| 474 | + }, |
| 475 | + { |
| 476 | + Type: 'Resource', |
| 477 | + ResourceChange: { |
| 478 | + Action: 'Remove', |
| 479 | + ResourceType: 'AWS::CloudFormation::Stack', |
| 480 | + LogicalResourceId: 'AuthStack', |
| 481 | + PhysicalResourceId: 'auth-stack', |
| 482 | + }, |
| 483 | + }, |
| 484 | + ], |
| 485 | + }; |
| 486 | + |
| 487 | + await expect(validations.validateStatefulResources(changeSet)).rejects.toMatchObject({ |
| 488 | + name: 'DestructiveMigrationError', |
| 489 | + message: expect.stringContaining('DirectTable'), |
| 490 | + }); |
| 491 | + }); |
| 492 | + }); |
320 | 493 | }); |
0 commit comments