-
Notifications
You must be signed in to change notification settings - Fork 722
Expand file tree
/
Copy pathderegister.test.ts
More file actions
295 lines (236 loc) · 9.33 KB
/
deregister.test.ts
File metadata and controls
295 lines (236 loc) · 9.33 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
import { Instance } from '@aws-sdk/client-ec2';
import { describe, it, expect, beforeEach, vi } from 'vitest';
import { deregisterRunner, createThrottleOptions } from './deregister';
import { Config } from './ConfigResolver';
import type { EndpointDefaults } from '@octokit/types';
const mockGetParameter = vi.fn();
vi.mock('@aws-github-runner/aws-ssm-util', () => ({
getParameter: (...args: unknown[]) => mockGetParameter(...args),
}));
const mockCreateAppAuth = vi.fn();
vi.mock('@octokit/auth-app', () => ({
createAppAuth: (...args: unknown[]) => mockCreateAppAuth(...args),
}));
const mockPaginate = {
iterator: vi.fn(),
};
const mockActions = {
listSelfHostedRunnersForOrg: vi.fn(),
listSelfHostedRunnersForRepo: vi.fn(),
deleteSelfHostedRunnerFromOrg: vi.fn(),
deleteSelfHostedRunnerFromRepo: vi.fn(),
};
const mockApps = {
getOrgInstallation: vi.fn(),
getRepoInstallation: vi.fn(),
};
function MockOctokit() {
return {
actions: mockActions,
apps: mockApps,
paginate: mockPaginate,
};
}
MockOctokit.plugin = vi.fn().mockReturnValue(MockOctokit);
vi.mock('@octokit/rest', () => ({
Octokit: MockOctokit,
}));
vi.mock('@octokit/plugin-throttling', () => ({
throttling: vi.fn(),
}));
vi.mock('@octokit/request', () => ({
request: {
defaults: vi.fn().mockReturnValue(vi.fn()),
},
}));
const baseConfig: Config = {
createSpotWarningMetric: false,
createSpotTerminationMetric: true,
tagFilters: { 'ghr:environment': 'test' },
prefix: 'runners',
enableRunnerDeregistration: true,
ghesApiUrl: '',
};
const orgInstance: Instance = {
InstanceId: 'i-12345678901234567',
InstanceType: 't2.micro',
Tags: [
{ Key: 'Name', Value: 'test-instance' },
{ Key: 'ghr:environment', Value: 'test' },
{ Key: 'ghr:Owner', Value: 'test-org' },
{ Key: 'ghr:Type', Value: 'Org' },
],
State: { Name: 'running' },
LaunchTime: new Date('2021-01-01'),
};
const repoInstance: Instance = {
InstanceId: 'i-repo12345678901234',
InstanceType: 't2.micro',
Tags: [
{ Key: 'Name', Value: 'test-repo-instance' },
{ Key: 'ghr:environment', Value: 'test' },
{ Key: 'ghr:Owner', Value: 'test-org/test-repo' },
{ Key: 'ghr:Type', Value: 'Repo' },
],
State: { Name: 'running' },
LaunchTime: new Date('2021-01-01'),
};
function setupAuthMocks() {
const appPrivateKey = Buffer.from('fake-private-key').toString('base64');
mockGetParameter.mockImplementation((name: string) => {
if (name === 'github-app-id') return Promise.resolve('12345');
if (name === 'github-app-key') return Promise.resolve(appPrivateKey);
return Promise.reject(new Error(`Unknown parameter: ${name}`));
});
// App auth returns app token
const mockAuth = vi.fn();
mockAuth.mockImplementation((opts: { type: string }) => {
if (opts.type === 'app') {
return Promise.resolve({ token: 'app-token' });
}
return Promise.resolve({ token: 'installation-token' });
});
mockCreateAppAuth.mockReturnValue(mockAuth);
}
describe('deregisterRunner', () => {
beforeEach(() => {
vi.clearAllMocks();
process.env.PARAMETER_GITHUB_APP_ID_NAME = 'github-app-id';
process.env.PARAMETER_GITHUB_APP_KEY_BASE64_NAME = 'github-app-key';
setupAuthMocks();
});
it('should skip deregistration when disabled', async () => {
await deregisterRunner(orgInstance, { ...baseConfig, enableRunnerDeregistration: false });
expect(mockGetParameter).not.toHaveBeenCalled();
});
it('should skip deregistration when instance ID is missing', async () => {
const instance: Instance = { ...orgInstance, InstanceId: undefined };
await deregisterRunner(instance, baseConfig);
expect(mockGetParameter).not.toHaveBeenCalled();
});
it('should skip deregistration when ghr:Owner tag is missing', async () => {
const instance: Instance = {
...orgInstance,
Tags: [{ Key: 'Name', Value: 'test' }],
};
await deregisterRunner(instance, baseConfig);
// Auth should not be called since we bail early
expect(mockCreateAppAuth).not.toHaveBeenCalled();
});
it('should deregister an org runner successfully', async () => {
mockApps.getOrgInstallation.mockResolvedValue({ data: { id: 999 } });
async function* fakeIterator() {
yield { data: [{ id: 42, name: `runner-i-12345678901234567` }] };
}
mockPaginate.iterator.mockReturnValue(fakeIterator());
mockActions.deleteSelfHostedRunnerFromOrg.mockResolvedValue({});
await deregisterRunner(orgInstance, baseConfig);
expect(mockApps.getOrgInstallation).toHaveBeenCalledWith({ org: 'test-org' });
expect(mockActions.deleteSelfHostedRunnerFromOrg).toHaveBeenCalledWith({
org: 'test-org',
runner_id: 42,
});
});
it('should deregister a repo runner successfully', async () => {
mockApps.getRepoInstallation.mockResolvedValue({ data: { id: 888 } });
async function* fakeIterator() {
yield { data: [{ id: 55, name: `runner-i-repo12345678901234` }] };
}
mockPaginate.iterator.mockReturnValue(fakeIterator());
mockActions.deleteSelfHostedRunnerFromRepo.mockResolvedValue({});
await deregisterRunner(repoInstance, baseConfig);
expect(mockApps.getRepoInstallation).toHaveBeenCalledWith({ owner: 'test-org', repo: 'test-repo' });
expect(mockActions.deleteSelfHostedRunnerFromRepo).toHaveBeenCalledWith({
owner: 'test-org',
repo: 'test-repo',
runner_id: 55,
});
});
it('should handle runner not found gracefully', async () => {
mockApps.getOrgInstallation.mockResolvedValue({ data: { id: 999 } });
async function* fakeIterator() {
yield { data: [{ id: 42, name: 'runner-other-instance' }] };
}
mockPaginate.iterator.mockReturnValue(fakeIterator());
await deregisterRunner(orgInstance, baseConfig);
expect(mockActions.deleteSelfHostedRunnerFromOrg).not.toHaveBeenCalled();
});
it('should handle GitHub API errors gracefully', async () => {
mockApps.getOrgInstallation.mockRejectedValue(new Error('GitHub API error'));
await deregisterRunner(orgInstance, baseConfig);
// Should not throw — error is caught internally
expect(mockActions.deleteSelfHostedRunnerFromOrg).not.toHaveBeenCalled();
});
it('should default to Org runner type when ghr:Type tag is missing', async () => {
const instance: Instance = {
...orgInstance,
Tags: [
{ Key: 'ghr:environment', Value: 'test' },
{ Key: 'ghr:Owner', Value: 'test-org' },
],
};
mockApps.getOrgInstallation.mockResolvedValue({ data: { id: 999 } });
async function* fakeIterator() {
yield { data: [{ id: 42, name: `runner-i-12345678901234567` }] };
}
mockPaginate.iterator.mockReturnValue(fakeIterator());
mockActions.deleteSelfHostedRunnerFromOrg.mockResolvedValue({});
await deregisterRunner(instance, baseConfig);
expect(mockApps.getOrgInstallation).toHaveBeenCalledWith({ org: 'test-org' });
expect(mockActions.deleteSelfHostedRunnerFromOrg).toHaveBeenCalledWith({
org: 'test-org',
runner_id: 42,
});
});
it('should use GHES API URL when configured', async () => {
const ghesConfig = { ...baseConfig, ghesApiUrl: 'https://github.internal.co/api/v3' };
mockApps.getOrgInstallation.mockResolvedValue({ data: { id: 999 } });
async function* fakeIterator() {
yield { data: [{ id: 42, name: `runner-i-12345678901234567` }] };
}
mockPaginate.iterator.mockReturnValue(fakeIterator());
mockActions.deleteSelfHostedRunnerFromOrg.mockResolvedValue({});
await deregisterRunner(orgInstance, ghesConfig);
expect(mockActions.deleteSelfHostedRunnerFromOrg).toHaveBeenCalled();
});
it('should paginate through multiple pages to find runner', async () => {
mockApps.getOrgInstallation.mockResolvedValue({ data: { id: 999 } });
async function* fakeIterator() {
yield { data: [{ id: 1, name: 'runner-other-1' }] };
yield { data: [{ id: 2, name: 'runner-other-2' }] };
yield { data: [{ id: 42, name: `runner-i-12345678901234567` }] };
}
mockPaginate.iterator.mockReturnValue(fakeIterator());
mockActions.deleteSelfHostedRunnerFromOrg.mockResolvedValue({});
await deregisterRunner(orgInstance, baseConfig);
expect(mockActions.deleteSelfHostedRunnerFromOrg).toHaveBeenCalledWith({
org: 'test-org',
runner_id: 42,
});
});
it('should handle repo runner not found gracefully', async () => {
mockApps.getRepoInstallation.mockResolvedValue({ data: { id: 888 } });
async function* fakeIterator() {
yield { data: [{ id: 99, name: 'runner-other-instance' }] };
}
mockPaginate.iterator.mockReturnValue(fakeIterator());
await deregisterRunner(repoInstance, baseConfig);
expect(mockActions.deleteSelfHostedRunnerFromRepo).not.toHaveBeenCalled();
});
it('should handle instance with no tags', async () => {
const instance: Instance = {
InstanceId: 'i-12345678901234567',
Tags: undefined,
};
await deregisterRunner(instance, baseConfig);
expect(mockCreateAppAuth).not.toHaveBeenCalled();
});
});
describe('createThrottleOptions', () => {
it('should return false for rate limit and log warning', () => {
const options = createThrottleOptions();
const endpointDefaults = { method: 'GET', url: '/test' } as Required<EndpointDefaults>;
expect(options.onRateLimit(60, endpointDefaults)).toBe(false);
expect(options.onSecondaryRateLimit(60, endpointDefaults)).toBe(false);
});
});