|
1 | 1 | import { logger } from '@aws-github-runner/aws-powertools-util';
|
2 | 2 | import { APIGatewayEvent, Context } from 'aws-lambda';
|
3 | 3 | import { mocked } from 'jest-mock';
|
| 4 | +import { WorkflowJobEvent } from '@octokit/webhooks-types'; |
4 | 5 |
|
5 |
| -import { githubWebhook } from './lambda'; |
6 |
| -import { handle } from './webhook'; |
| 6 | +import { dispatchToRunners, eventBridgeWebhook, githubWebhook } from './lambda'; |
| 7 | +import { handle, publishOnEventBridge } from './webhook'; |
7 | 8 | import ValidationError from './ValidationError';
|
8 | 9 | import { getParameter } from '@aws-github-runner/aws-ssm-util';
|
| 10 | +import { dispatch } from './runners/dispatch'; |
| 11 | +import { EventWrapper } from './types'; |
| 12 | +import { rejects } from 'assert'; |
9 | 13 |
|
10 | 14 | const event: APIGatewayEvent = {
|
11 | 15 | body: JSON.stringify(''),
|
@@ -73,40 +77,132 @@ const context: Context = {
|
73 | 77 | },
|
74 | 78 | };
|
75 | 79 |
|
| 80 | + |
| 81 | +jest.mock('./runners/dispatch'); |
76 | 82 | jest.mock('./webhook');
|
77 | 83 | jest.mock('@aws-github-runner/aws-ssm-util');
|
78 | 84 |
|
79 |
| -describe('Test scale up lambda wrapper.', () => { |
| 85 | +describe('Test webhook lambda wrapper.', () => { |
80 | 86 | beforeEach(() => {
|
| 87 | + // We mock all SSM request to resolve to a non empty array. Since we mock all implemeantions |
| 88 | + // relying on the config opbject that is enought to test the handlers. |
81 | 89 | const mockedGet = mocked(getParameter);
|
82 |
| - mockedGet.mockResolvedValue('[]'); |
| 90 | + mockedGet.mockResolvedValue('["abc"]'); |
| 91 | + jest.clearAllMocks(); |
83 | 92 | });
|
84 |
| - it('Happy flow, resolve.', async () => { |
85 |
| - const mock = mocked(handle); |
86 |
| - mock.mockImplementation(() => { |
87 |
| - return new Promise((resolve) => { |
88 |
| - resolve({ body: 'test', statusCode: 200 }); |
| 93 | + |
| 94 | + describe('Test webhook lambda wrapper.', () => { |
| 95 | + it('Happy flow, resolve.', async () => { |
| 96 | + const mock = mocked(handle); |
| 97 | + mock.mockImplementation(() => { |
| 98 | + return new Promise((resolve) => { |
| 99 | + resolve({ body: 'test', statusCode: 200 }); |
| 100 | + }); |
89 | 101 | });
|
| 102 | + |
| 103 | + const result = await githubWebhook(event, context); |
| 104 | + expect(result).toEqual({ body: 'test', statusCode: 200 }); |
90 | 105 | });
|
91 | 106 |
|
92 |
| - const result = await githubWebhook(event, context); |
93 |
| - expect(result).toEqual({ body: 'test', statusCode: 200 }); |
| 107 | + it('An expected error, resolve.', async () => { |
| 108 | + const mock = mocked(handle); |
| 109 | + mock.mockRejectedValue(new ValidationError(400, 'some error')); |
| 110 | + |
| 111 | + const result = await githubWebhook(event, context); |
| 112 | + expect(result).toMatchObject({ body: 'some error', statusCode: 400 }); |
| 113 | + }); |
| 114 | + |
| 115 | + it('Errors are not thrown.', async () => { |
| 116 | + const mock = mocked(handle); |
| 117 | + const logSpy = jest.spyOn(logger, 'error'); |
| 118 | + mock.mockRejectedValue(new Error('some error')); |
| 119 | + const result = await githubWebhook(event, context); |
| 120 | + expect(result).toMatchObject({ body: 'Check the Lambda logs for the error details.', statusCode: 500 }); |
| 121 | + expect(logSpy).toHaveBeenCalledTimes(1); |
| 122 | + }); |
94 | 123 | });
|
95 | 124 |
|
96 |
| - it('An expected error, resolve.', async () => { |
97 |
| - const mock = mocked(handle); |
98 |
| - mock.mockRejectedValue(new ValidationError(400, 'some error')); |
99 | 125 |
|
100 |
| - const result = await githubWebhook(event, context); |
101 |
| - expect(result).toMatchObject({ body: 'some error', statusCode: 400 }); |
| 126 | + |
| 127 | + describe('Lmmbda eventBridgeWebhook.', () => { |
| 128 | + beforeEach(() => { |
| 129 | + process.env.EVENT_BUS_NAME = 'test'; |
| 130 | + }); |
| 131 | + |
| 132 | + it('Happy flow, resolve.', async () => { |
| 133 | + const mock = mocked(publishOnEventBridge); |
| 134 | + mock.mockImplementation(() => { |
| 135 | + return new Promise((resolve) => { |
| 136 | + resolve({ body: 'test', statusCode: 200 }); |
| 137 | + }); |
| 138 | + }); |
| 139 | + |
| 140 | + const result = await eventBridgeWebhook(event, context); |
| 141 | + expect(result).toEqual({ body: 'test', statusCode: 200 }); |
| 142 | + }); |
| 143 | + |
| 144 | + |
| 145 | + it('Reject events .', async () => { |
| 146 | + const mock = mocked(publishOnEventBridge); |
| 147 | + mock.mockRejectedValue(new Error('some error')); |
| 148 | + |
| 149 | + mock.mockRejectedValue(new ValidationError(400, 'some error')); |
| 150 | + |
| 151 | + const result = await eventBridgeWebhook(event, context); |
| 152 | + expect(result).toMatchObject({ body: 'some error', statusCode: 400 }); |
| 153 | + }); |
| 154 | + |
| 155 | + it('Errors are not thrown.', async () => { |
| 156 | + const mock = mocked(publishOnEventBridge); |
| 157 | + const logSpy = jest.spyOn(logger, 'error'); |
| 158 | + mock.mockRejectedValue(new Error('some error')); |
| 159 | + const result = await eventBridgeWebhook(event, context); |
| 160 | + expect(result).toMatchObject({ body: 'Check the Lambda logs for the error details.', statusCode: 500 }); |
| 161 | + expect(logSpy).toHaveBeenCalledTimes(1); |
| 162 | + }); |
102 | 163 | });
|
103 | 164 |
|
104 |
| - it('Errors are not thrown.', async () => { |
105 |
| - const mock = mocked(handle); |
106 |
| - const logSpy = jest.spyOn(logger, 'error'); |
107 |
| - mock.mockRejectedValue(new Error('some error')); |
108 |
| - const result = await githubWebhook(event, context); |
109 |
| - expect(result).toMatchObject({ body: 'Check the Lambda logs for the error details.', statusCode: 500 }); |
110 |
| - expect(logSpy).toHaveBeenCalledTimes(1); |
| 165 | + describe('Lmmbda dispatchToRunners.', () => { |
| 166 | + it('Happy flow, resolve.', async () => { |
| 167 | + const mock = mocked(dispatch); |
| 168 | + mock.mockImplementation(() => { |
| 169 | + return new Promise((resolve) => { |
| 170 | + resolve({ body: 'test', statusCode: 200 }); |
| 171 | + }); |
| 172 | + }); |
| 173 | + |
| 174 | + const testEvent = { |
| 175 | + 'detail-type': 'workflow_job', |
| 176 | + } as unknown as EventWrapper<WorkflowJobEvent>; |
| 177 | + |
| 178 | + await expect(dispatchToRunners(testEvent, context)).resolves.not.toThrow(); |
| 179 | + }); |
| 180 | + |
| 181 | + it('Rejects non workflow_job events.', async () => { |
| 182 | + const mock = mocked(dispatch); |
| 183 | + mock.mockImplementation(() => { |
| 184 | + return new Promise((resolve) => { |
| 185 | + resolve({ body: 'test', statusCode: 200 }); |
| 186 | + }); |
| 187 | + }); |
| 188 | + |
| 189 | + const testEvent = { |
| 190 | + 'detail-type': 'non_workflow_job', |
| 191 | + } as unknown as EventWrapper<WorkflowJobEvent>; |
| 192 | + |
| 193 | + await expect(dispatchToRunners(testEvent, context)).rejects.toThrow('Incorrect Event detail-type only workflow_job is accepted'); |
| 194 | + }); |
| 195 | + |
| 196 | + |
| 197 | + it('Rejects any event causing an error.', async () => { |
| 198 | + const mock = mocked(dispatch); |
| 199 | + mock.mockRejectedValue(new Error('some error')); |
| 200 | + |
| 201 | + const testEvent = { |
| 202 | + 'detail-type': 'workflow_job', |
| 203 | + } as unknown as EventWrapper<WorkflowJobEvent>; |
| 204 | + |
| 205 | + await expect(dispatchToRunners(testEvent, context)).rejects.toThrow(); |
| 206 | + }); |
111 | 207 | });
|
112 | 208 | });
|
0 commit comments