@@ -86,7 +86,8 @@ let DEFAULT_ORG_RUNNERS_ORPHANED: RunnerInfo[];
86
86
// i-running-111 | Repo | running and not exceeding minimumRunningTimeInMinutes
87
87
// i-running-112 | Org | busy
88
88
// i-running-113 | Repo | busy
89
- const oldest = moment ( new Date ( ) ) . subtract ( 25 , 'minutes' ) . toDate ( ) ;
89
+ const oldest = moment ( new Date ( ) ) . subtract ( 26 , 'minutes' ) . toDate ( ) ;
90
+ const old25minutes = moment ( new Date ( ) ) . subtract ( 25 , 'minutes' ) . toDate ( ) ;
90
91
const DEFAULT_RUNNERS_ORIGINAL = [
91
92
{
92
93
instanceId : 'i-idle-101' ,
@@ -118,13 +119,13 @@ const DEFAULT_RUNNERS_ORIGINAL = [
118
119
} ,
119
120
{
120
121
instanceId : 'i-running-cannot-delete-105' ,
121
- launchTime : moment ( new Date ( ) ) . subtract ( 25 , 'minutes' ) . toDate ( ) ,
122
+ launchTime : old25minutes ,
122
123
type : 'Repo' ,
123
124
owner : `doe/another-repo` ,
124
125
} ,
125
126
{
126
127
instanceId : 'i-running-cannot-delete-106' ,
127
- launchTime : moment ( new Date ( ) ) . subtract ( 25 , 'minutes' ) . toDate ( ) ,
128
+ launchTime : old25minutes ,
128
129
type : 'Org' ,
129
130
owner : TEST_DATA . repositoryOwner ,
130
131
} ,
@@ -167,13 +168,13 @@ const DEFAULT_RUNNERS_ORIGINAL = [
167
168
} ,
168
169
{
169
170
instanceId : 'i-running-busy-112' ,
170
- launchTime : moment ( new Date ( ) ) . subtract ( 25 , 'minutes' ) . toDate ( ) ,
171
+ launchTime : old25minutes ,
171
172
type : 'Repo' ,
172
173
owner : `doe/another-repo` ,
173
174
} ,
174
175
{
175
176
instanceId : 'i-running-busy-113' ,
176
- launchTime : moment ( new Date ( ) ) . subtract ( 25 , 'minutes' ) . toDate ( ) ,
177
+ launchTime : old25minutes ,
177
178
type : 'Org' ,
178
179
owner : TEST_DATA . repositoryOwner ,
179
180
} ,
@@ -214,7 +215,7 @@ const DEFAULT_REGISTERED_RUNNERS = [
214
215
} ,
215
216
] ;
216
217
217
- describe ( 'scaleDown ' , ( ) => {
218
+ describe ( 'Scale down runners ' , ( ) => {
218
219
beforeEach ( ( ) => {
219
220
process . env = { ...cleanEnv } ;
220
221
process . env . GITHUB_APP_KEY_BASE64 = 'TEST_CERTIFICATE_DATA' ;
@@ -339,24 +340,20 @@ describe('scaleDown', () => {
339
340
) ;
340
341
} ) ;
341
342
342
- describe ( 'github.com' , ( ) => {
343
- describe ( 'no runners running' , ( ) => {
344
- beforeEach ( ( ) => {
345
- mockListRunners . mockResolvedValue ( [ ] ) ;
346
- } ) ;
343
+ describe ( 'for github.com' , ( ) => {
344
+ it ( 'Should not call terminate when no runners online.' , async ( ) => {
345
+ mockListRunners . mockResolvedValue ( [ ] ) ;
347
346
348
- it ( 'No runners online' , async ( ) => {
349
- await scaleDown ( ) ;
350
- expect ( listEC2Runners ) . toBeCalledWith ( {
351
- environment : environment ,
352
- } ) ;
353
- expect ( terminateRunner ) . not ;
354
- expect ( mockOctokit . apps . getRepoInstallation ) . not ;
355
- expect ( mockOctokit . apps . getRepoInstallation ) . not ;
347
+ await scaleDown ( ) ;
348
+ expect ( listEC2Runners ) . toBeCalledWith ( {
349
+ environment : environment ,
356
350
} ) ;
351
+ expect ( terminateRunner ) . not ;
352
+ expect ( mockOctokit . apps . getRepoInstallation ) . not ;
353
+ expect ( mockOctokit . apps . getRepoInstallation ) . not ;
357
354
} ) ;
358
355
359
- it ( 'Terminates 3 of 5 runners owned by repos and one orphaned' , async ( ) => {
356
+ it ( 'Should terminates 3 of 5 runners owned by repos and one orphaned' , async ( ) => {
360
357
mockListRunners . mockResolvedValue ( DEFAULT_RUNNERS_REPO ) ;
361
358
await scaleDown ( ) ;
362
359
expect ( listEC2Runners ) . toBeCalledWith ( {
@@ -374,7 +371,7 @@ describe('scaleDown', () => {
374
371
}
375
372
} ) ;
376
373
377
- it ( 'Terminates 2 of 3 runners owned by orgs and one orphaned' , async ( ) => {
374
+ it ( 'Should terminates 2 of 3 runners owned by orgs and one orphaned' , async ( ) => {
378
375
mockListRunners . mockResolvedValue ( DEFAULT_RUNNERS_ORG ) ;
379
376
await scaleDown ( ) ;
380
377
expect ( listEC2Runners ) . toBeCalledWith ( {
@@ -391,35 +388,38 @@ describe('scaleDown', () => {
391
388
}
392
389
} ) ;
393
390
394
- describe ( 'With idle config' , ( ) => {
391
+ describe ( 'When idle config defined ' , ( ) => {
395
392
const defaultConfig = {
396
393
idleCount : 3 ,
397
394
cron : '* * * * * *' ,
398
395
timeZone : 'Europe/Amsterdam' ,
399
396
evictionStrategy : 'oldest_first' ,
400
397
} ;
398
+
401
399
beforeEach ( ( ) => {
402
400
process . env . SCALE_DOWN_CONFIG = JSON . stringify ( [ defaultConfig ] ) ;
403
401
} ) ;
404
402
405
- it ( 'Terminates 1 runner owned by orgs ' , async ( ) => {
403
+ it ( 'Should terminate based on the the idle config ' , async ( ) => {
406
404
mockListRunners . mockResolvedValue ( RUNNERS_ORG_WITH_AUTO_SCALING_CONFIG ) ;
407
405
await scaleDown ( ) ;
408
406
409
- expect ( listEC2Runners ) . toBeCalledWith ( {
410
- environment : environment ,
411
- } ) ;
412
-
413
- expect ( mockOctokit . apps . getOrgInstallation ) . toBeCalled ( ) ;
414
407
expect ( terminateRunner ) . toBeCalledTimes ( 1 ) ;
415
408
for ( const toTerminate of RUNNERS_ORG_TO_BE_REMOVED_WITH_AUTO_SCALING_CONFIG ) {
416
409
expect ( terminateRunner ) . toHaveBeenCalledWith ( toTerminate . instanceId ) ;
417
410
}
411
+
412
+ process . env . SCALE_DOWN_CONFIG = JSON . stringify ( [ ] ) ;
413
+
414
+ // run test again with out idle config
415
+ jest . clearAllMocks ( ) ;
416
+ mockListRunners . mockResolvedValue ( RUNNERS_ORG_WITH_AUTO_SCALING_CONFIG ) ;
417
+ await scaleDown ( ) ;
418
+ expect ( terminateRunner ) . toBeCalledTimes ( 2 ) ;
418
419
} ) ;
419
420
420
- it ( 'Terminates 0 runners owned by org' , async ( ) => {
421
+ it ( 'Should terminates 0 runners owned by org' , async ( ) => {
421
422
mockListRunners . mockResolvedValue ( RUNNERS_REPO_WITH_AUTO_SCALING_CONFIG ) ;
422
- process . env . ENABLE_ORGANIZATION_RUNNERS = 'false' ;
423
423
await scaleDown ( ) ;
424
424
425
425
expect ( listEC2Runners ) . toBeCalledWith ( {
@@ -430,21 +430,17 @@ describe('scaleDown', () => {
430
430
expect ( terminateRunner ) . not . toBeCalled ( ) ;
431
431
} ) ;
432
432
433
- describe ( 'With newest_first eviction strategy' , ( ) => {
434
- beforeEach ( ( ) => {
435
- process . env . SCALE_DOWN_CONFIG = JSON . stringify ( [ { ...defaultConfig , evictionStrategy : 'newest_first' } ] ) ;
436
- } ) ;
433
+ it ( 'Should terminates the newest runner.' , async ( ) => {
434
+ process . env . SCALE_DOWN_CONFIG = JSON . stringify ( [ { ...defaultConfig , evictionStrategy : 'newest_first' } ] ) ;
437
435
438
- it ( 'Terminates the newest org' , async ( ) => {
439
- mockListRunners . mockResolvedValue ( RUNNERS_ORG_WITH_AUTO_SCALING_CONFIG ) ;
440
- await scaleDown ( ) ;
441
- expect ( terminateRunner ) . toBeCalledTimes ( 1 ) ;
442
- expect ( terminateRunner ) . toHaveBeenCalledWith ( 'i-idle-102' ) ;
443
- } ) ;
436
+ mockListRunners . mockResolvedValue ( RUNNERS_ORG_WITH_AUTO_SCALING_CONFIG ) ;
437
+ await scaleDown ( ) ;
438
+ expect ( terminateRunner ) . toBeCalledTimes ( 1 ) ;
439
+ expect ( terminateRunner ) . toHaveBeenCalledWith ( 'i-idle-102' ) ;
444
440
} ) ;
445
441
} ) ;
446
442
447
- it ( 'No instances terminates when delete runner in github results in a non 204 status.' , async ( ) => {
443
+ it ( 'Should terminate no instances when delete runner in github results in a non 204 status.' , async ( ) => {
448
444
mockListRunners . mockResolvedValue ( DEFAULT_RUNNERS ) ;
449
445
mockOctokit . actions . deleteSelfHostedRunnerFromOrg . mockImplementation ( ( ) => {
450
446
return { status : 500 } ;
@@ -460,7 +456,7 @@ describe('scaleDown', () => {
460
456
expect ( terminateRunner ) . not . toBeCalled ;
461
457
} ) ;
462
458
463
- it ( 'Terminates 4 runners amongst all owners and two orphaned' , async ( ) => {
459
+ it ( 'Should terminates 4 runners amongst all owners and two orphaned' , async ( ) => {
464
460
mockListRunners . mockResolvedValue ( DEFAULT_RUNNERS ) ;
465
461
await scaleDown ( ) ;
466
462
@@ -480,27 +476,23 @@ describe('scaleDown', () => {
480
476
} ) ;
481
477
} ) ;
482
478
483
- describe ( 'ghes ' , ( ) => {
479
+ describe ( 'for GHES (GitHub Enterprise) ' , ( ) => {
484
480
beforeEach ( ( ) => {
485
481
process . env . GHES_URL = 'https://github.enterprise.something' ;
486
482
} ) ;
487
- describe ( 'no runners running' , ( ) => {
488
- beforeEach ( ( ) => {
489
- mockListRunners . mockResolvedValue ( [ ] ) ;
490
- } ) ;
491
483
492
- it ( 'No runners online' , async ( ) => {
493
- await scaleDown ( ) ;
494
- expect ( listEC2Runners ) . toBeCalledWith ( {
495
- environment : environment ,
496
- } ) ;
497
- expect ( terminateRunner ) . not ;
498
- expect ( mockOctokit . apps . getRepoInstallation ) . not ;
499
- expect ( mockOctokit . apps . getRepoInstallation ) . not ;
484
+ it ( 'Should not call terminate when no runners online' , async ( ) => {
485
+ mockListRunners . mockResolvedValue ( [ ] ) ;
486
+ await scaleDown ( ) ;
487
+ expect ( listEC2Runners ) . toBeCalledWith ( {
488
+ environment : environment ,
500
489
} ) ;
490
+ expect ( terminateRunner ) . not ;
491
+ expect ( mockOctokit . apps . getRepoInstallation ) . not ;
492
+ expect ( mockOctokit . apps . getRepoInstallation ) . not ;
501
493
} ) ;
502
494
503
- it ( 'Terminates 3 of 5 runners owned by repos and one orphaned' , async ( ) => {
495
+ it ( 'Should terminates 3 of 5 runners owned by repos and one orphaned' , async ( ) => {
504
496
mockListRunners . mockResolvedValue ( DEFAULT_RUNNERS_REPO ) ;
505
497
await scaleDown ( ) ;
506
498
expect ( listEC2Runners ) . toBeCalledWith ( {
@@ -517,7 +509,7 @@ describe('scaleDown', () => {
517
509
}
518
510
} ) ;
519
511
520
- it ( 'Terminates 2 of 3 runners owned by orgs and one orphaned' , async ( ) => {
512
+ it ( 'Should terminates 2 of 3 runners owned by orgs and one orphaned' , async ( ) => {
521
513
mockListRunners . mockResolvedValue ( DEFAULT_RUNNERS_ORG ) ;
522
514
await scaleDown ( ) ;
523
515
expect ( listEC2Runners ) . toBeCalledWith ( {
@@ -534,7 +526,7 @@ describe('scaleDown', () => {
534
526
}
535
527
} ) ;
536
528
537
- describe ( 'With idle config' , ( ) => {
529
+ describe ( 'When idle config defined ' , ( ) => {
538
530
beforeEach ( ( ) => {
539
531
process . env . SCALE_DOWN_CONFIG = JSON . stringify ( [
540
532
{
@@ -546,7 +538,7 @@ describe('scaleDown', () => {
546
538
] ) ;
547
539
} ) ;
548
540
549
- it ( 'Terminates 1 runner owned by orgs' , async ( ) => {
541
+ it ( 'Should terminates 1 runner owned by orgs' , async ( ) => {
550
542
mockListRunners . mockResolvedValue ( RUNNERS_ORG_WITH_AUTO_SCALING_CONFIG ) ;
551
543
await scaleDown ( ) ;
552
544
@@ -561,7 +553,7 @@ describe('scaleDown', () => {
561
553
}
562
554
} ) ;
563
555
564
- it ( 'Terminates 0 runners owned by repos' , async ( ) => {
556
+ it ( 'Should terminates 0 runners owned by repos' , async ( ) => {
565
557
mockListRunners . mockResolvedValue ( RUNNERS_REPO_WITH_AUTO_SCALING_CONFIG ) ;
566
558
process . env . ENABLE_ORGANIZATION_RUNNERS = 'false' ;
567
559
await scaleDown ( ) ;
@@ -575,7 +567,7 @@ describe('scaleDown', () => {
575
567
} ) ;
576
568
} ) ;
577
569
578
- it ( 'Terminates 4 runners amongst all owners and two orphaned' , async ( ) => {
570
+ it ( 'Should terminates 4 runners amongst all owners and two orphaned' , async ( ) => {
579
571
mockListRunners . mockResolvedValue ( DEFAULT_RUNNERS ) ;
580
572
await scaleDown ( ) ;
581
573
0 commit comments