Skip to content

Commit 1ca2fdc

Browse files
authored
refactor(runners): tests for runners (#3042)
* refactor: fix incorrect unit tests * add table with test instances overview * cleanup * cleanup
1 parent e6782e2 commit 1ca2fdc

File tree

1 file changed

+50
-40
lines changed

1 file changed

+50
-40
lines changed

modules/runners/lambdas/runners/src/scale-runners/scale-down.test.ts

Lines changed: 50 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,23 @@ let DEFAULT_RUNNERS_ORG_TO_BE_REMOVED: RunnerInfo[];
6868
let DEFAULT_RUNNERS_ORPHANED: RunnerInfo[];
6969
let DEFAULT_REPO_RUNNERS_ORPHANED: RunnerInfo[];
7070
let DEFAULT_ORG_RUNNERS_ORPHANED: RunnerInfo[];
71+
72+
// Add table of DEFAULT_RUNNERS_ORIGINAL without launchTime and owner
73+
// instanceId | type | notes
74+
// i-idle-101 | Repo | idle and exceeds minimumRunningTimeInMinutes
75+
// i-idle-102 | Org | idle and exceeds minimumRunningTimeInMinutes
76+
// i-oldest-idle-103 | Repo | idle and exceeds minimumRunningTimeInMinutes
77+
// i-oldest-idle-104 | Org | idle and exceeds minimumRunningTimeInMinutes
78+
// i-running-cannot-delete-105 | Repo | unable to delete
79+
// i-running-cannot-delete-106 | Org | unable to delete
80+
// i-orphan-107 | Repo | orphaned no GitHub registration and exceeds minimumRunningTimeInMinutes
81+
// i-orphan-108 | Org | orphaned no GitHub registration and exceeds minimumRunningTimeInMinutes
82+
// i-not-registered-108 | Org | not registered and not exceeding minimumRunningTimeInMinutes
83+
// i-not-registered-109 | Repo | not registered and not exceeding minimumRunningTimeInMinutes
84+
// i-running-110 | Org | running and not exceeding minimumRunningTimeInMinutes
85+
// i-running-111 | Repo | running and not exceeding minimumRunningTimeInMinutes
86+
// i-running-112 | Org | busy
87+
// i-running-113 | Repo | busy
7188
const DEFAULT_RUNNERS_ORIGINAL = [
7289
{
7390
instanceId: 'i-idle-101',
@@ -102,13 +119,13 @@ const DEFAULT_RUNNERS_ORIGINAL = [
102119
owner: TEST_DATA.repositoryOwner,
103120
},
104121
{
105-
instanceId: 'i-running-105',
122+
instanceId: 'i-running-cannot-delete-105',
106123
launchTime: moment(new Date()).subtract(25, 'minutes').toDate(),
107124
type: 'Repo',
108125
owner: `doe/another-repo`,
109126
},
110127
{
111-
instanceId: 'i-running-106',
128+
instanceId: 'i-running-cannot-delete-106',
112129
launchTime: moment(new Date()).subtract(25, 'minutes').toDate(),
113130
type: 'Org',
114131
owner: TEST_DATA.repositoryOwner,
@@ -122,41 +139,42 @@ const DEFAULT_RUNNERS_ORIGINAL = [
122139
owner: `doe/another-repo`,
123140
},
124141
{
125-
instanceId: 'i-not-registered-108',
142+
instanceId: 'i-orphan-108',
126143
launchTime: moment(new Date())
127-
.subtract(minimumRunningTimeInMinutes - 1, 'minutes')
144+
.subtract(minimumRunningTimeInMinutes + 5, 'minutes')
128145
.toDate(),
129-
type: 'Repo',
130-
owner: `doe/another-repo`,
146+
type: 'Org',
147+
owner: TEST_DATA.repositoryOwner,
131148
},
132149
{
133150
instanceId: 'i-not-registered-109',
134151
launchTime: moment(new Date())
135-
.subtract(minimumRunningTimeInMinutes - 2, 'minutes')
152+
.subtract(runnerBootTimeInMinutes - 2, 'minutes')
136153
.toDate(),
137-
type: 'Org',
138-
owner: TEST_DATA.repositoryOwner,
154+
type: 'Repo',
155+
owner: `doe/another-repo`,
139156
},
140157
{
141-
instanceId: 'i-legacy-110',
158+
instanceId: 'i-not-registered-110',
142159
launchTime: moment(new Date())
143-
.subtract(minimumRunningTimeInMinutes + 5, 'minutes')
160+
.subtract(runnerBootTimeInMinutes - 2, 'minutes')
144161
.toDate(),
145-
repo: `${TEST_DATA.repositoryOwner}/${TEST_DATA.repositoryName}`,
162+
type: 'Org',
163+
owner: TEST_DATA.repositoryOwner,
146164
},
147165
{
148166
instanceId: 'i-new-111',
149167
launchTime: moment(new Date()).toDate(),
150168
repo: `${TEST_DATA.repositoryOwner}/${TEST_DATA.repositoryName}`,
151169
},
152170
{
153-
instanceId: 'i-running-112',
171+
instanceId: 'i-running-busy-112',
154172
launchTime: moment(new Date()).subtract(25, 'minutes').toDate(),
155173
type: 'Repo',
156174
owner: `doe/another-repo`,
157175
},
158176
{
159-
instanceId: 'i-running-113',
177+
instanceId: 'i-running-busy-113',
160178
launchTime: moment(new Date()).subtract(25, 'minutes').toDate(),
161179
type: 'Org',
162180
owner: TEST_DATA.repositoryOwner,
@@ -182,27 +200,19 @@ const DEFAULT_REGISTERED_RUNNERS = [
182200
},
183201
{
184202
id: 105,
185-
name: 'i-running-105',
203+
name: 'i-running-cannot-delete-105',
186204
},
187205
{
188206
id: 106,
189-
name: 'i-running-106',
190-
},
191-
{
192-
id: 1121,
193-
name: 'i-running-112-1',
194-
},
195-
{
196-
id: 1122,
197-
name: 'i-running-112-2',
207+
name: 'i-running-cannot-delete-106',
198208
},
199209
{
200-
id: 1131,
201-
name: 'i-running-113-1',
210+
id: 112,
211+
name: 'i-running-busy-112',
202212
},
203213
{
204-
id: 1132,
205-
name: 'i-running-113-2',
214+
id: 113,
215+
name: 'i-running-busy-113',
206216
},
207217
];
208218

@@ -251,7 +261,7 @@ describe('scaleDown', () => {
251261
});
252262

253263
mockOctokit.actions.getSelfHostedRunnerForRepo.mockImplementation((repo) => {
254-
if (repo.runner_id === 1121) {
264+
if (repo.runner_id === 112) {
255265
return {
256266
data: { busy: true },
257267
};
@@ -262,7 +272,7 @@ describe('scaleDown', () => {
262272
}
263273
});
264274
mockOctokit.actions.getSelfHostedRunnerForOrg.mockImplementation((repo) => {
265-
if (repo.runner_id === 1131) {
275+
if (repo.runner_id === 113) {
266276
return {
267277
data: { busy: true },
268278
};
@@ -347,7 +357,7 @@ describe('scaleDown', () => {
347357
});
348358
});
349359

350-
it('Terminates 3 of 5 runners owned by repos and all orphaned', async () => {
360+
it('Terminates 3 of 5 runners owned by repos and one orphaned', async () => {
351361
mockListRunners.mockResolvedValue(DEFAULT_RUNNERS_REPO);
352362
await scaleDown();
353363
expect(listEC2Runners).toBeCalledWith({
@@ -356,7 +366,7 @@ describe('scaleDown', () => {
356366

357367
expect(mockOctokit.apps.getRepoInstallation).toBeCalled();
358368

359-
expect(terminateRunner).toBeCalledTimes(4);
369+
expect(terminateRunner).toBeCalledTimes(3);
360370
for (const toTerminate of DEFAULT_RUNNERS_REPO_TO_BE_REMOVED) {
361371
expect(terminateRunner).toHaveBeenCalledWith(toTerminate.instanceId);
362372
}
@@ -365,7 +375,7 @@ describe('scaleDown', () => {
365375
}
366376
});
367377

368-
it('Terminates 2 of 3 runners owned by orgs and all orphaned', async () => {
378+
it('Terminates 2 of 3 runners owned by orgs and one orphaned', async () => {
369379
mockListRunners.mockResolvedValue(DEFAULT_RUNNERS_ORG);
370380
await scaleDown();
371381
expect(listEC2Runners).toBeCalledWith({
@@ -438,7 +448,7 @@ describe('scaleDown', () => {
438448
expect(terminateRunner).not.toBeCalled;
439449
});
440450

441-
it('Terminates 6 runners amongst all owners and all orphaned', async () => {
451+
it('Terminates 4 runners amongst all owners and two orphaned', async () => {
442452
mockListRunners.mockResolvedValue(DEFAULT_RUNNERS);
443453
await scaleDown();
444454

@@ -448,7 +458,7 @@ describe('scaleDown', () => {
448458

449459
expect(mockOctokit.apps.getRepoInstallation).toBeCalledTimes(2);
450460
expect(mockOctokit.apps.getOrgInstallation).toBeCalledTimes(1);
451-
expect(terminateRunner).toBeCalledTimes(7);
461+
expect(terminateRunner).toBeCalledTimes(6);
452462
for (const toTerminate of RUNNERS_ALL_REMOVED) {
453463
expect(terminateRunner).toHaveBeenCalledWith(toTerminate.instanceId);
454464
}
@@ -478,15 +488,15 @@ describe('scaleDown', () => {
478488
});
479489
});
480490

481-
it('Terminates 3 of 5 runners owned by repos and all orphaned', async () => {
491+
it('Terminates 3 of 5 runners owned by repos and one orphaned', async () => {
482492
mockListRunners.mockResolvedValue(DEFAULT_RUNNERS_REPO);
483493
await scaleDown();
484494
expect(listEC2Runners).toBeCalledWith({
485495
environment: environment,
486496
});
487497

488498
expect(mockOctokit.apps.getRepoInstallation).toBeCalled();
489-
expect(terminateRunner).toBeCalledTimes(4);
499+
expect(terminateRunner).toBeCalledTimes(3);
490500
for (const toTerminate of DEFAULT_RUNNERS_REPO_TO_BE_REMOVED) {
491501
expect(terminateRunner).toHaveBeenCalledWith(toTerminate.instanceId);
492502
}
@@ -495,7 +505,7 @@ describe('scaleDown', () => {
495505
}
496506
});
497507

498-
it('Terminates 2 of 3 runners owned by orgs and all orphaned', async () => {
508+
it('Terminates 2 of 3 runners owned by orgs and one orphaned', async () => {
499509
mockListRunners.mockResolvedValue(DEFAULT_RUNNERS_ORG);
500510
await scaleDown();
501511
expect(listEC2Runners).toBeCalledWith({
@@ -552,7 +562,7 @@ describe('scaleDown', () => {
552562
});
553563
});
554564

555-
it('Terminates 6 runners amongst all owners and all orphaned', async () => {
565+
it('Terminates 4 runners amongst all owners and two orphaned', async () => {
556566
mockListRunners.mockResolvedValue(DEFAULT_RUNNERS);
557567
await scaleDown();
558568

@@ -562,7 +572,7 @@ describe('scaleDown', () => {
562572

563573
expect(mockOctokit.apps.getRepoInstallation).toBeCalledTimes(2);
564574
expect(mockOctokit.apps.getOrgInstallation).toBeCalledTimes(1);
565-
expect(terminateRunner).toBeCalledTimes(7);
575+
expect(terminateRunner).toBeCalledTimes(6);
566576
for (const toTerminate of RUNNERS_ALL_REMOVED) {
567577
expect(terminateRunner).toHaveBeenCalledWith(toTerminate.instanceId);
568578
}

0 commit comments

Comments
 (0)