|
| 1 | +import { AmplifyGen2MigrationValidations } from '../../../commands/gen2-migration/_validations'; |
| 2 | +import { $TSContext } from '@aws-amplify/amplify-cli-core'; |
| 3 | +import { Logger } from '../../../commands/gen2-migration'; |
| 4 | + |
| 5 | +jest.mock('@aws-sdk/client-cloudformation'); |
| 6 | +jest.mock('bottleneck', () => { |
| 7 | + return jest.fn().mockImplementation(() => ({ |
| 8 | + schedule: jest.fn((fn) => fn()), |
| 9 | + })); |
| 10 | +}); |
| 11 | + |
| 12 | +const mockSyncCloudBackendFromS3 = jest.fn(); |
| 13 | +const mockGetClient = jest.fn(); |
| 14 | +jest.mock('../../../commands/drift-detection/services', () => ({ |
| 15 | + CloudFormationService: jest.fn().mockImplementation(() => ({ |
| 16 | + syncCloudBackendFromS3: mockSyncCloudBackendFromS3, |
| 17 | + getClient: mockGetClient, |
| 18 | + })), |
| 19 | +})); |
| 20 | + |
| 21 | +const mockDetectTemplateDrift = jest.fn(); |
| 22 | +jest.mock('../../../commands/drift-detection/detect-template-drift', () => ({ |
| 23 | + detectTemplateDrift: (...args: any[]) => mockDetectTemplateDrift(...args), |
| 24 | +})); |
| 25 | + |
| 26 | +describe('AmplifyGen2MigrationValidations - validateTemplateDrift', () => { |
| 27 | + let mockContext: $TSContext; |
| 28 | + let validations: AmplifyGen2MigrationValidations; |
| 29 | + const mockCfnClient = {}; |
| 30 | + |
| 31 | + beforeEach(() => { |
| 32 | + mockContext = {} as $TSContext; |
| 33 | + validations = new AmplifyGen2MigrationValidations(new Logger('mock', 'mock', 'mock'), 'amplify-test-stack', 'dev', mockContext); |
| 34 | + mockGetClient.mockResolvedValue(mockCfnClient); |
| 35 | + jest.clearAllMocks(); |
| 36 | + }); |
| 37 | + |
| 38 | + it('should pass when no template drift is detected', async () => { |
| 39 | + mockSyncCloudBackendFromS3.mockResolvedValue(true); |
| 40 | + mockDetectTemplateDrift.mockResolvedValue({ |
| 41 | + changes: [], |
| 42 | + skipped: false, |
| 43 | + }); |
| 44 | + |
| 45 | + await expect(validations.validateTemplateDrift()).resolves.not.toThrow(); |
| 46 | + expect(mockSyncCloudBackendFromS3).toHaveBeenCalledWith(mockContext); |
| 47 | + expect(mockDetectTemplateDrift).toHaveBeenCalledWith('amplify-test-stack', expect.any(Object), mockCfnClient); |
| 48 | + }); |
| 49 | + |
| 50 | + it('should throw MigrationError when template drift is detected', async () => { |
| 51 | + mockSyncCloudBackendFromS3.mockResolvedValue(true); |
| 52 | + mockDetectTemplateDrift.mockResolvedValue({ |
| 53 | + changes: [{ LogicalResourceId: 'SomeResource', Action: 'Modify' }], |
| 54 | + skipped: false, |
| 55 | + }); |
| 56 | + |
| 57 | + await expect(validations.validateTemplateDrift()).rejects.toMatchObject({ |
| 58 | + name: 'MigrationError', |
| 59 | + message: 'Template drift detected', |
| 60 | + }); |
| 61 | + }); |
| 62 | + |
| 63 | + it('should throw MigrationError when S3 sync fails', async () => { |
| 64 | + mockSyncCloudBackendFromS3.mockResolvedValue(false); |
| 65 | + |
| 66 | + await expect(validations.validateTemplateDrift()).rejects.toMatchObject({ |
| 67 | + name: 'MigrationError', |
| 68 | + message: 'Failed to sync cloud backend from S3', |
| 69 | + resolution: 'Ensure the project is deployed and S3 bucket is accessible.', |
| 70 | + }); |
| 71 | + expect(mockDetectTemplateDrift).not.toHaveBeenCalled(); |
| 72 | + }); |
| 73 | + |
| 74 | + it('should throw MigrationError when template drift detection is skipped', async () => { |
| 75 | + mockSyncCloudBackendFromS3.mockResolvedValue(true); |
| 76 | + mockDetectTemplateDrift.mockResolvedValue({ |
| 77 | + changes: [], |
| 78 | + skipped: true, |
| 79 | + skipReason: 'No cached CloudFormation template found', |
| 80 | + }); |
| 81 | + |
| 82 | + await expect(validations.validateTemplateDrift()).rejects.toMatchObject({ |
| 83 | + name: 'MigrationError', |
| 84 | + message: 'Template drift detection was skipped: No cached CloudFormation template found', |
| 85 | + }); |
| 86 | + }); |
| 87 | + |
| 88 | + it('should throw MigrationError when detection is skipped due to nested stack errors', async () => { |
| 89 | + mockSyncCloudBackendFromS3.mockResolvedValue(true); |
| 90 | + mockDetectTemplateDrift.mockResolvedValue({ |
| 91 | + changes: [], |
| 92 | + skipped: true, |
| 93 | + skipReason: 'One or more nested stacks could not be analyzed', |
| 94 | + }); |
| 95 | + |
| 96 | + await expect(validations.validateTemplateDrift()).rejects.toMatchObject({ |
| 97 | + name: 'MigrationError', |
| 98 | + message: 'Template drift detection was skipped: One or more nested stacks could not be analyzed', |
| 99 | + }); |
| 100 | + }); |
| 101 | + |
| 102 | + it('should pass the correct stack name and CFN client to detectTemplateDrift', async () => { |
| 103 | + mockSyncCloudBackendFromS3.mockResolvedValue(true); |
| 104 | + mockDetectTemplateDrift.mockResolvedValue({ |
| 105 | + changes: [], |
| 106 | + skipped: false, |
| 107 | + }); |
| 108 | + |
| 109 | + await validations.validateTemplateDrift(); |
| 110 | + |
| 111 | + expect(mockGetClient).toHaveBeenCalledWith(mockContext); |
| 112 | + expect(mockDetectTemplateDrift).toHaveBeenCalledWith( |
| 113 | + 'amplify-test-stack', |
| 114 | + expect.objectContaining({ |
| 115 | + info: expect.any(Function), |
| 116 | + debug: expect.any(Function), |
| 117 | + warn: expect.any(Function), |
| 118 | + blankLine: expect.any(Function), |
| 119 | + success: expect.any(Function), |
| 120 | + error: expect.any(Function), |
| 121 | + }), |
| 122 | + mockCfnClient, |
| 123 | + ); |
| 124 | + }); |
| 125 | +}); |
0 commit comments