-
Notifications
You must be signed in to change notification settings - Fork 722
Expand file tree
/
Copy pathdispatch.test.ts
More file actions
330 lines (286 loc) · 13.5 KB
/
dispatch.test.ts
File metadata and controls
330 lines (286 loc) · 13.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
import { getParameter } from '@aws-github-runner/aws-ssm-util';
import nock from 'nock';
import { WorkflowJobEvent } from '@octokit/webhooks-types';
import workFlowJobEvent from '../../test/resources/github_workflowjob_event.json';
import runnerConfig from '../../test/resources/multi_runner_configurations.json';
import { RunnerConfig, sendActionRequest } from '../sqs';
import { canRunJob, dispatch } from './dispatch';
import { ConfigDispatcher } from '../ConfigLoader';
import { logger } from '@aws-github-runner/aws-powertools-util';
import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest';
vi.mock('../sqs');
vi.mock('@aws-github-runner/aws-ssm-util');
const GITHUB_APP_WEBHOOK_SECRET = 'TEST_SECRET';
const cleanEnv = process.env;
describe('Dispatcher', () => {
let originalError: Console['error'];
let config: ConfigDispatcher;
beforeEach(async () => {
logger.setLogLevel('DEBUG');
process.env = { ...cleanEnv };
nock.disableNetConnect();
originalError = console.error;
console.error = vi.fn();
vi.clearAllMocks();
vi.resetAllMocks();
mockSSMResponse();
config = await createConfig(undefined, runnerConfig);
});
afterEach(() => {
console.error = originalError;
});
describe('handle work flow job events ', () => {
it('should not handle "workflow_job" events with actions other than queued (action = started)', async () => {
const event = { ...workFlowJobEvent, action: 'started' } as unknown as WorkflowJobEvent;
const resp = await dispatch(event, 'workflow_job', config);
expect(resp.statusCode).toBe(201);
expect(sendActionRequest).not.toHaveBeenCalled();
});
it('should not handle workflow_job events from unlisted repositories', async () => {
const event = workFlowJobEvent as unknown as WorkflowJobEvent;
config = await createConfig(['NotCodertocat/Hello-World']);
await expect(dispatch(event, 'push', config)).rejects.toMatchObject({
statusCode: 403,
});
expect(sendActionRequest).not.toHaveBeenCalled();
});
it('should handle workflow_job events with a valid installation id', async () => {
config = await createConfig(['github-aws-runners/terraform-aws-github-runner']);
const event = { ...workFlowJobEvent, installation: { id: 123 } } as unknown as WorkflowJobEvent;
const resp = await dispatch(event, 'workflow_job', config);
expect(resp.statusCode).toBe(201);
expect(sendActionRequest).toHaveBeenCalled();
});
it('should handle workflow_job events from allow listed repositories', async () => {
config = await createConfig(['github-aws-runners/terraform-aws-github-runner']);
const event = workFlowJobEvent as unknown as WorkflowJobEvent;
const resp = await dispatch(event, 'workflow_job', config);
expect(resp.statusCode).toBe(201);
expect(sendActionRequest).toHaveBeenCalled();
});
it('should match labels', async () => {
config = await createConfig(undefined, [
{
...runnerConfig[0],
matcherConfig: {
labelMatchers: [['self-hosted', 'test']],
exactMatch: true,
},
},
runnerConfig[1],
]);
const event = {
...workFlowJobEvent,
workflow_job: {
...workFlowJobEvent.workflow_job,
labels: ['self-hosted', 'Test'],
},
} as unknown as WorkflowJobEvent;
const resp = await dispatch(event, 'workflow_job', config);
expect(resp.statusCode).toBe(201);
expect(sendActionRequest).toHaveBeenCalledWith({
id: event.workflow_job.id,
repositoryName: event.repository.name,
repositoryOwner: event.repository.owner.login,
eventType: 'workflow_job',
installationId: 0,
queueId: runnerConfig[0].id,
repoOwnerType: 'Organization',
});
});
it('should sort matcher with exact first.', async () => {
config = await createConfig(undefined, [
{
...runnerConfig[0],
matcherConfig: {
labelMatchers: [['self-hosted', 'match', 'not-select']],
exactMatch: false,
},
},
{
...runnerConfig[0],
matcherConfig: {
labelMatchers: [['self-hosted', 'no-match']],
exactMatch: true,
},
},
{
...runnerConfig[0],
id: 'match',
matcherConfig: {
labelMatchers: [['self-hosted', 'match']],
exactMatch: true,
},
},
runnerConfig[1],
]);
const event = {
...workFlowJobEvent,
workflow_job: {
...workFlowJobEvent.workflow_job,
labels: ['self-hosted', 'match'],
},
} as unknown as WorkflowJobEvent;
const resp = await dispatch(event, 'workflow_job', config);
expect(resp.statusCode).toBe(201);
expect(sendActionRequest).toHaveBeenCalledWith({
id: event.workflow_job.id,
repositoryName: event.repository.name,
repositoryOwner: event.repository.owner.login,
eventType: 'workflow_job',
installationId: 0,
queueId: 'match',
repoOwnerType: 'Organization',
});
});
it('should not accept jobs where not all labels are supported (single matcher).', async () => {
config = await createConfig(undefined, [
{
...runnerConfig[0],
matcherConfig: {
labelMatchers: [['self-hosted', 'x64', 'linux']],
exactMatch: true,
},
},
]);
const event = {
...workFlowJobEvent,
workflow_job: {
...workFlowJobEvent.workflow_job,
labels: ['self-hosted', 'linux', 'x64', 'on-demand'],
},
} as unknown as WorkflowJobEvent;
const resp = await dispatch(event, 'workflow_job', config);
expect(resp.statusCode).toBe(202);
expect(sendActionRequest).not.toHaveBeenCalled();
});
});
describe('decides can run job based on label and config (canRunJob)', () => {
it('should accept job with an exact match and identical labels.', () => {
const workflowLabels = ['self-hosted', 'linux', 'x64', 'ubuntu-latest'];
const runnerLabels = [['self-hosted', 'linux', 'x64', 'ubuntu-latest']];
expect(canRunJob(workflowLabels, runnerLabels, true)).toBe(true);
});
it('should accept job with an exact match and identical labels, ignoring cases.', () => {
const workflowLabels = ['self-Hosted', 'Linux', 'X64', 'ubuntu-Latest'];
const runnerLabels = [['self-hosted', 'linux', 'x64', 'ubuntu-latest']];
expect(canRunJob(workflowLabels, runnerLabels, true)).toBe(true);
});
it('should accept job with an exact match and runner supports requested capabilities.', () => {
const workflowLabels = ['self-hosted', 'linux', 'x64'];
const runnerLabels = [['self-hosted', 'linux', 'x64', 'ubuntu-latest']];
expect(canRunJob(workflowLabels, runnerLabels, true)).toBe(true);
});
it('should NOT accept job with an exact match and runner not matching requested capabilities.', () => {
const workflowLabels = ['self-hosted', 'linux', 'x64', 'ubuntu-latest'];
const runnerLabels = [['self-hosted', 'linux', 'x64']];
expect(canRunJob(workflowLabels, runnerLabels, true)).toBe(false);
});
it('should accept job with for a non exact match. Any label that matches will accept the job.', () => {
const workflowLabels = ['self-hosted', 'linux', 'x64', 'ubuntu-latest', 'gpu'];
const runnerLabels = [['gpu']];
expect(canRunJob(workflowLabels, runnerLabels, false)).toBe(true);
});
it('should NOT accept job with for an exact match. Not all requested capabilities are supported.', () => {
const workflowLabels = ['self-hosted', 'linux', 'x64', 'ubuntu-latest', 'gpu'];
const runnerLabels = [['gpu']];
expect(canRunJob(workflowLabels, runnerLabels, true)).toBe(false);
});
it('should not accept jobs not providing labels if exact match is.', () => {
const workflowLabels: string[] = [];
const runnerLabels = [['self-hosted', 'linux', 'x64']];
expect(canRunJob(workflowLabels, runnerLabels, true)).toBe(false);
});
it('should accept jobs not providing labels and exact match is set to false.', () => {
const workflowLabels: string[] = [];
const runnerLabels = [['self-hosted', 'linux', 'x64']];
expect(canRunJob(workflowLabels, runnerLabels, false)).toBe(true);
});
it('should match when runner has more labels than workflow requests with exactMatch=true (unidirectional).', () => {
const workflowLabels = ['self-hosted', 'linux', 'x64', 'staging', 'ubuntu-2404'];
const runnerLabels = [['self-hosted', 'linux', 'x64', 'staging', 'ubuntu-2404', 'on-demand']];
expect(canRunJob(workflowLabels, runnerLabels, true)).toBe(true);
});
it('should match when labels are exactly identical with exactMatch=true.', () => {
const workflowLabels = ['self-hosted', 'linux', 'on-demand'];
const runnerLabels = [['self-hosted', 'linux', 'on-demand']];
expect(canRunJob(workflowLabels, runnerLabels, true)).toBe(true);
});
it('should match with exactMatch=true when labels are in different order.', () => {
const workflowLabels = ['linux', 'self-hosted', 'x64'];
const runnerLabels = [['self-hosted', 'linux', 'x64']];
expect(canRunJob(workflowLabels, runnerLabels, true)).toBe(true);
});
it('should match with exactMatch=true when labels are completely shuffled.', () => {
const workflowLabels = ['x64', 'ubuntu-latest', 'self-hosted', 'linux'];
const runnerLabels = [['self-hosted', 'linux', 'x64', 'ubuntu-latest']];
expect(canRunJob(workflowLabels, runnerLabels, true)).toBe(true);
});
it('should match with exactMatch=false when labels are in different order.', () => {
const workflowLabels = ['gpu', 'self-hosted'];
const runnerLabels = [['self-hosted', 'gpu']];
expect(canRunJob(workflowLabels, runnerLabels, false)).toBe(true);
});
// bidirectionalLabelMatch tests
it('should NOT match when runner has more labels than workflow requests (bidirectionalLabelMatch=true).', () => {
const workflowLabels = ['self-hosted', 'linux', 'x64', 'staging', 'ubuntu-2404'];
const runnerLabels = [['self-hosted', 'linux', 'x64', 'staging', 'ubuntu-2404', 'on-demand']];
expect(canRunJob(workflowLabels, runnerLabels, false, true)).toBe(false);
});
it('should NOT match when workflow has more labels than runner (bidirectionalLabelMatch=true).', () => {
const workflowLabels = ['self-hosted', 'linux', 'x64', 'ubuntu-latest', 'gpu'];
const runnerLabels = [['self-hosted', 'linux', 'x64']];
expect(canRunJob(workflowLabels, runnerLabels, false, true)).toBe(false);
});
it('should match when labels are exactly identical with bidirectionalLabelMatch=true.', () => {
const workflowLabels = ['self-hosted', 'linux', 'on-demand'];
const runnerLabels = [['self-hosted', 'linux', 'on-demand']];
expect(canRunJob(workflowLabels, runnerLabels, false, true)).toBe(true);
});
it('should match with bidirectionalLabelMatch=true when labels are in different order.', () => {
const workflowLabels = ['linux', 'self-hosted', 'x64'];
const runnerLabels = [['self-hosted', 'linux', 'x64']];
expect(canRunJob(workflowLabels, runnerLabels, false, true)).toBe(true);
});
it('should match with bidirectionalLabelMatch=true when labels are completely shuffled.', () => {
const workflowLabels = ['x64', 'ubuntu-latest', 'self-hosted', 'linux'];
const runnerLabels = [['self-hosted', 'linux', 'x64', 'ubuntu-latest']];
expect(canRunJob(workflowLabels, runnerLabels, false, true)).toBe(true);
});
it('should match with bidirectionalLabelMatch=true ignoring case.', () => {
const workflowLabels = ['Self-Hosted', 'Linux', 'X64'];
const runnerLabels = [['self-hosted', 'linux', 'x64']];
expect(canRunJob(workflowLabels, runnerLabels, false, true)).toBe(true);
});
it('should NOT match empty workflow labels with bidirectionalLabelMatch=true.', () => {
const workflowLabels: string[] = [];
const runnerLabels = [['self-hosted', 'linux', 'x64']];
expect(canRunJob(workflowLabels, runnerLabels, false, true)).toBe(false);
});
it('bidirectionalLabelMatch takes precedence over exactMatch when both are true.', () => {
const workflowLabels = ['self-hosted', 'linux', 'x64'];
const runnerLabels = [['self-hosted', 'linux', 'x64', 'ubuntu-latest']];
// exactMatch alone would accept this (runner has extra labels), but bidirectional should reject
expect(canRunJob(workflowLabels, runnerLabels, true, true)).toBe(false);
});
});
});
function mockSSMResponse(runnerConfigInput?: RunnerConfig) {
process.env.PARAMETER_RUNNER_MATCHER_CONFIG_PATH = '/github-runner/runner-matcher-config';
const mockedGet = vi.mocked(getParameter);
mockedGet.mockImplementation((parameter_name) => {
const value =
parameter_name == '/github-runner/runner-matcher-config'
? JSON.stringify(runnerConfigInput ?? runnerConfig)
: GITHUB_APP_WEBHOOK_SECRET;
return Promise.resolve(value);
});
}
async function createConfig(repositoryAllowList?: string[], runnerConfig?: RunnerConfig): Promise<ConfigDispatcher> {
if (repositoryAllowList) {
process.env.REPOSITORY_ALLOW_LIST = JSON.stringify(repositoryAllowList);
}
ConfigDispatcher.reset();
mockSSMResponse(runnerConfig);
return await ConfigDispatcher.load();
}