1+ import { mocked } from 'ts-jest/utils' ;
12import { ActionRequestMessage , handle } from './handler' ;
23
34import { createAppAuth } from '@octokit/auth-app' ;
45import { Octokit } from '@octokit/rest' ;
6+ import { listRunners } from './runners' ;
57
68jest . mock ( '@octokit/auth-app' , ( ) => ( {
79 createAppAuth : jest . fn ( ) . mockImplementation ( ( ) => jest . fn ( ) . mockImplementation ( ( ) => ( { token : 'Blaat' } ) ) ) ,
@@ -10,14 +12,16 @@ const mockOctokit = {
1012 checks : { get : jest . fn ( ) } ,
1113 actions : {
1214 listRepoWorkflowRuns : jest . fn ( ) ,
13- listSelfHostedRunnersForOrg : jest . fn ( ) ,
14- listSelfHostedRunnersForRepo : jest . fn ( ) ,
15+ createRegistrationTokenForOrg : jest . fn ( ) ,
16+ createRegistrationTokenForRepo : jest . fn ( ) ,
1517 } ,
1618} ;
1719jest . mock ( '@octokit/rest' , ( ) => ( {
1820 Octokit : jest . fn ( ) . mockImplementation ( ( ) => mockOctokit ) ,
1921} ) ) ;
2022
23+ jest . mock ( './runners' ) ;
24+
2125const TEST_DATA : ActionRequestMessage = {
2226 id : 1 ,
2327 eventType : 'check_run' ,
@@ -32,27 +36,29 @@ describe('handler', () => {
3236 process . env . GITHUB_APP_ID = '1337' ;
3337 process . env . GITHUB_APP_CLIENT_ID = 'TEST_CLIENT_ID' ;
3438 process . env . GITHUB_APP_CLIENT_SECRET = 'TEST_CLIENT_SECRET' ;
39+ process . env . RUNNERS_MAXIMUM_COUNT = '3' ;
3540 jest . clearAllMocks ( ) ;
3641 mockOctokit . actions . listRepoWorkflowRuns . mockImplementation ( ( ) => ( {
3742 data : {
3843 total_count : 1 ,
3944 } ,
4045 } ) ) ;
41- const mockRunnersReturnValue = {
46+ const mockTokenReturnValue = {
4247 data : {
43- total_count : 1 ,
44- runners : [
45- {
46- id : 23 ,
47- name : 'Test Runner' ,
48- status : 'online' ,
49- os : 'linux' ,
50- } ,
51- ] ,
48+ token : '1234abcd' ,
5249 } ,
5350 } ;
54- mockOctokit . actions . listSelfHostedRunnersForOrg . mockImplementation ( ( ) => mockRunnersReturnValue ) ;
55- mockOctokit . actions . listSelfHostedRunnersForRepo . mockImplementation ( ( ) => mockRunnersReturnValue ) ;
51+ mockOctokit . actions . createRegistrationTokenForOrg . mockImplementation ( ( ) => mockTokenReturnValue ) ;
52+ mockOctokit . actions . createRegistrationTokenForRepo . mockImplementation ( ( ) => mockTokenReturnValue ) ;
53+ const mockListRunners = mocked ( listRunners ) ;
54+ mockListRunners . mockImplementation ( async ( ) => [
55+ {
56+ instanceId : 'i-1234' ,
57+ launchTime : new Date ( ) ,
58+ repo : `${ TEST_DATA . repositoryOwner } /${ TEST_DATA . repositoryName } ` ,
59+ org : TEST_DATA . repositoryOwner ,
60+ } ,
61+ ] ) ;
5662 } ) ;
5763
5864 it ( 'ignores non-sqs events' , async ( ) => {
@@ -69,30 +75,61 @@ describe('handler', () => {
6975 } ) ;
7076 } ) ;
7177
72- // describe('on org level', () => {
73- // beforeAll(() => {
74- // process.env.ENABLE_ORGANIZATION_RUNNERS = 'true';
75- // });
76-
77- // it('gets the current org level runners', async () => {
78- // await handle('aws:sqs', TEST_DATA);
79- // expect(mockOctokit.actions.listSelfHostedRunnersForOrg).toBeCalledWith({
80- // org: TEST_DATA.repositoryOwner,
81- // });
82- // });
83- // });
84-
85- // describe('on repo level', () => {
86- // beforeAll(() => {
87- // delete process.env.ENABLE_ORGANIZATION_RUNNERS;
88- // });
89-
90- // it('gets the current repo level runners', async () => {
91- // await handle('aws:sqs', TEST_DATA);
92- // expect(mockOctokit.actions.listSelfHostedRunnersForRepo).toBeCalledWith({
93- // owner: TEST_DATA.repositoryOwner,
94- // repo: TEST_DATA.repositoryName,
95- // });
96- // });
97- // });
78+ it ( 'does not list runners when no workflows are queued' , async ( ) => {
79+ mockOctokit . actions . listRepoWorkflowRuns . mockImplementation ( ( ) => ( {
80+ data : { total_count : 0 , runners : [ ] } ,
81+ } ) ) ;
82+ await handle ( 'aws:sqs' , TEST_DATA ) ;
83+ expect ( listRunners ) . not . toBeCalled ( ) ;
84+ } ) ;
85+
86+ describe ( 'on org level' , ( ) => {
87+ beforeAll ( ( ) => {
88+ process . env . ENABLE_ORGANIZATION_RUNNERS = 'true' ;
89+ } ) ;
90+
91+ it ( 'gets the current org level runners' , async ( ) => {
92+ await handle ( 'aws:sqs' , TEST_DATA ) ;
93+ expect ( listRunners ) . toBeCalledWith ( { repoName : undefined } ) ;
94+ } ) ;
95+
96+ it ( 'does not create a token when maximum runners has been reached' , async ( ) => {
97+ process . env . RUNNERS_MAXIMUM_COUNT = '1' ;
98+ await handle ( 'aws:sqs' , TEST_DATA ) ;
99+ expect ( mockOctokit . actions . createRegistrationTokenForOrg ) . not . toBeCalled ( ) ;
100+ } ) ;
101+
102+ it ( 'creates a token when maximum runners has not been reached' , async ( ) => {
103+ await handle ( 'aws:sqs' , TEST_DATA ) ;
104+ expect ( mockOctokit . actions . createRegistrationTokenForOrg ) . toBeCalled ( ) ;
105+ expect ( mockOctokit . actions . createRegistrationTokenForOrg ) . toBeCalledWith ( {
106+ org : TEST_DATA . repositoryOwner ,
107+ } ) ;
108+ } ) ;
109+ } ) ;
110+
111+ describe ( 'on repo level' , ( ) => {
112+ beforeAll ( ( ) => {
113+ process . env . ENABLE_ORGANIZATION_RUNNERS = 'false' ;
114+ } ) ;
115+
116+ it ( 'gets the current repo level runners' , async ( ) => {
117+ await handle ( 'aws:sqs' , TEST_DATA ) ;
118+ expect ( listRunners ) . toBeCalledWith ( { repoName : `${ TEST_DATA . repositoryOwner } /${ TEST_DATA . repositoryName } ` } ) ;
119+ } ) ;
120+
121+ it ( 'does not create a token when maximum runners has been reached' , async ( ) => {
122+ process . env . RUNNERS_MAXIMUM_COUNT = '1' ;
123+ await handle ( 'aws:sqs' , TEST_DATA ) ;
124+ expect ( mockOctokit . actions . createRegistrationTokenForRepo ) . not . toBeCalled ( ) ;
125+ } ) ;
126+
127+ it ( 'creates a token when maximum runners has not been reached' , async ( ) => {
128+ await handle ( 'aws:sqs' , TEST_DATA ) ;
129+ expect ( mockOctokit . actions . createRegistrationTokenForRepo ) . toBeCalledWith ( {
130+ owner : TEST_DATA . repositoryOwner ,
131+ repo : TEST_DATA . repositoryName ,
132+ } ) ;
133+ } ) ;
134+ } ) ;
98135} ) ;
0 commit comments