|
| 1 | +const mockCodeDeployCreate = jest.fn(); |
| 2 | +const mockCodeDeployUpdate = jest.fn(); |
| 3 | +const mockCodeDeployTagResource = jest.fn(); |
| 4 | +const mockCodeDeployUntagResource = jest.fn(); |
| 5 | + |
| 6 | +jest.mock('aws-sdk', () => { |
| 7 | + return { |
| 8 | + CodeDeploy: jest.fn().mockImplementation(() => { |
| 9 | + return { |
| 10 | + createDeploymentGroup: mockCodeDeployCreate.mockReturnValue({ |
| 11 | + promise: jest.fn().mockResolvedValue({ |
| 12 | + service: { |
| 13 | + serviceArn: 'arn:aws:ecs:us-east-1:012345678910:service/MyCluster/MyService', |
| 14 | + serviceName: 'MyService', |
| 15 | + }, |
| 16 | + }), |
| 17 | + }), |
| 18 | + updateDeploymentGroup: mockCodeDeployUpdate.mockReturnValue({ |
| 19 | + promise: jest.fn().mockResolvedValue({ |
| 20 | + service: { |
| 21 | + serviceArn: 'arn:aws:ecs:us-east-1:012345678910:service/MyCluster/MyService', |
| 22 | + serviceName: 'MyService', |
| 23 | + }, |
| 24 | + }), |
| 25 | + }), |
| 26 | + tagResource: mockCodeDeployTagResource.mockReturnValue({ |
| 27 | + promise: jest.fn().mockResolvedValue({}), |
| 28 | + }), |
| 29 | + untagResource: mockCodeDeployUntagResource.mockReturnValue({ |
| 30 | + promise: jest.fn().mockResolvedValue({}), |
| 31 | + }), |
| 32 | + }; |
| 33 | + }), |
| 34 | + }; |
| 35 | +}); |
| 36 | + |
| 37 | +import { handleCreate, handleUpdate } from '../../../lambdas/ecs-deployment-group'; |
| 38 | +import { defaultContext } from '../__fixtures__/defaultContext'; |
| 39 | +import { defaultEvent } from '../__fixtures__/defaultEvent'; |
| 40 | +import { defaultLogger } from '../__fixtures__/defaultLogger'; |
| 41 | + |
| 42 | +const defaultEcsDeploymentGroupProperties = { |
| 43 | + ApplicationName: 'Foo', |
| 44 | + DeploymentGroupName: 'Foo', |
| 45 | + ServiceRoleArn: 'arn:aws:iam::012345678910:role/MyRole', |
| 46 | + EcsServices: [ |
| 47 | + { |
| 48 | + ServiceName: 'Foo', |
| 49 | + ClusterName: 'Foo', |
| 50 | + }, |
| 51 | + ], |
| 52 | + TargetGroupNames: ['Foo'], |
| 53 | + ProdTrafficListenerArn: 'arn:aws:elasticloadbalancing::012345678910:listener/app/MyApp/foo/prod', |
| 54 | + TestTrafficListenerArn: 'arn:aws:elasticloadbalancing::012345678910:listener/app/MyApp/foo/test', |
| 55 | + TerminationWaitTimeInMinutes: 5, |
| 56 | + ArnForDeploymentGroup: 'arn:aws:ecs:us-east-1:012345678910:service/MyCluster/MyService', |
| 57 | +}; |
| 58 | + |
| 59 | +describe('createHandler', () => { |
| 60 | + it('sends tags with create request', async () => { |
| 61 | + await handleCreate( |
| 62 | + { |
| 63 | + ...defaultEvent, |
| 64 | + RequestType: 'Create', |
| 65 | + ResourceProperties: { |
| 66 | + ServiceToken: 'foo', |
| 67 | + ...defaultEcsDeploymentGroupProperties, |
| 68 | + Tags: [ |
| 69 | + { Key: 'foo', Value: 'bar' }, |
| 70 | + { Key: 'k', Value: 'west' }, |
| 71 | + ], |
| 72 | + }, |
| 73 | + }, |
| 74 | + defaultContext, |
| 75 | + defaultLogger, |
| 76 | + ); |
| 77 | + |
| 78 | + expect(mockCodeDeployCreate).toHaveBeenCalledWith( |
| 79 | + expect.objectContaining({ |
| 80 | + tags: [ |
| 81 | + { Key: 'foo', Value: 'bar' }, |
| 82 | + { Key: 'k', Value: 'west' }, |
| 83 | + ], |
| 84 | + }), |
| 85 | + ); |
| 86 | + }); |
| 87 | +}); |
| 88 | + |
| 89 | +describe('updateHandler', () => { |
| 90 | + it('sends data update request', async () => { |
| 91 | + await handleUpdate( |
| 92 | + { |
| 93 | + ...defaultEvent, |
| 94 | + RequestType: 'Update', |
| 95 | + PhysicalResourceId: 'foo', |
| 96 | + ResourceProperties: { |
| 97 | + ServiceToken: 'foo', |
| 98 | + ...defaultEcsDeploymentGroupProperties, |
| 99 | + Tags: [ |
| 100 | + { Key: 'dis', Value: 'dat' }, |
| 101 | + { Key: 'k', Value: 'west' }, |
| 102 | + { Key: 'ye', Value: 'west' }, |
| 103 | + ], |
| 104 | + }, |
| 105 | + OldResourceProperties: { |
| 106 | + ServiceToken: 'foo', |
| 107 | + ...defaultEcsDeploymentGroupProperties, |
| 108 | + Tags: [ |
| 109 | + { Key: 'foo', Value: 'bar' }, |
| 110 | + { Key: 'k', Value: 'WEST' }, |
| 111 | + { Key: 'ye', Value: 'west' }, |
| 112 | + ], |
| 113 | + }, |
| 114 | + }, |
| 115 | + defaultContext, |
| 116 | + defaultLogger, |
| 117 | + ); |
| 118 | + |
| 119 | + expect(mockCodeDeployUpdate).toHaveBeenCalled(); |
| 120 | + |
| 121 | + expect(mockCodeDeployUntagResource).toHaveBeenCalledWith({ |
| 122 | + ResourceArn: 'arn:aws:ecs:us-east-1:012345678910:service/MyCluster/MyService', |
| 123 | + TagKeys: ['foo'], |
| 124 | + }); |
| 125 | + |
| 126 | + expect(mockCodeDeployTagResource).toHaveBeenCalledWith( |
| 127 | + expect.objectContaining({ |
| 128 | + ResourceArn: 'arn:aws:ecs:us-east-1:012345678910:service/MyCluster/MyService', |
| 129 | + Tags: [ |
| 130 | + { Key: 'dis', Value: 'dat' }, |
| 131 | + { Key: 'k', Value: 'west' }, |
| 132 | + { Key: 'ye', Value: 'west' }, |
| 133 | + ], |
| 134 | + }), |
| 135 | + ); |
| 136 | + }); |
| 137 | +}); |
0 commit comments