|
| 1 | +import { mocked } from 'ts-jest/utils'; |
| 2 | +import { scaleDown } from './scale-down'; |
| 3 | +import moment from 'moment'; |
| 4 | +import { createAppAuth } from '@octokit/auth-app'; |
| 5 | +import { Octokit } from '@octokit/rest'; |
| 6 | +import { listRunners, terminateRunner } from './runners'; |
| 7 | + |
| 8 | +jest.mock('@octokit/auth-app', () => ({ |
| 9 | + createAppAuth: jest.fn().mockImplementation(() => jest.fn().mockImplementation(() => ({ token: 'Blaat' }))), |
| 10 | +})); |
| 11 | +const mockOctokit = { |
| 12 | + apps: { |
| 13 | + getOrgInstallation: jest.fn(), |
| 14 | + getRepoInstallation: jest.fn(), |
| 15 | + }, |
| 16 | + actions: { |
| 17 | + listSelfHostedRunnersForRepo: jest.fn(), |
| 18 | + listSelfHostedRunnersForOrg: jest.fn(), |
| 19 | + deleteSelfHostedRunnerFromOrg: jest.fn(), |
| 20 | + deleteSelfHostedRunnerFromRepo: jest.fn(), |
| 21 | + }, |
| 22 | +}; |
| 23 | +jest.mock('@octokit/rest', () => ({ |
| 24 | + Octokit: jest.fn().mockImplementation(() => mockOctokit), |
| 25 | +})); |
| 26 | + |
| 27 | +jest.mock('./runners'); |
| 28 | + |
| 29 | +export interface TestData { |
| 30 | + repositoryName: string; |
| 31 | + repositoryOwner: string; |
| 32 | +} |
| 33 | + |
| 34 | +const environment = 'unit-test-environment'; |
| 35 | +const TEST_DATA: TestData = { |
| 36 | + repositoryName: 'hello-world', |
| 37 | + repositoryOwner: 'Codertocat', |
| 38 | +}; |
| 39 | + |
| 40 | +const DEFAULT_RUNNERS = [ |
| 41 | + { |
| 42 | + instanceId: 'i-idle-101', |
| 43 | + launchTime: new Date('2020-05-12T11:32:06.000Z'), |
| 44 | + repo: `${TEST_DATA.repositoryOwner}/${TEST_DATA.repositoryName}`, |
| 45 | + org: undefined, |
| 46 | + }, |
| 47 | + { |
| 48 | + instanceId: 'i-idle-102', |
| 49 | + launchTime: new Date('2020-05-12T10:32:06.000Z'), |
| 50 | + repo: `${TEST_DATA.repositoryOwner}/${TEST_DATA.repositoryName}`, |
| 51 | + org: undefined, |
| 52 | + }, |
| 53 | + { |
| 54 | + instanceId: 'i-running-103', |
| 55 | + launchTime: moment(new Date()).subtract(25, 'minutes').toDate(), |
| 56 | + repo: `doe/another-repo`, |
| 57 | + org: undefined, |
| 58 | + }, |
| 59 | + { |
| 60 | + instanceId: 'i-not-registered-104', |
| 61 | + launchTime: moment(new Date()).subtract(5, 'minutes').toDate(), |
| 62 | + repo: `doe/another-repo`, |
| 63 | + org: undefined, |
| 64 | + }, |
| 65 | +]; |
| 66 | + |
| 67 | +const DEFAULT_REGISTERED_RUNNERS: any = { |
| 68 | + data: { |
| 69 | + runners: [ |
| 70 | + { |
| 71 | + id: 101, |
| 72 | + name: 'i-idle-101', |
| 73 | + }, |
| 74 | + { |
| 75 | + id: 102, |
| 76 | + name: 'i-idle-101', |
| 77 | + }, |
| 78 | + { |
| 79 | + id: 103, |
| 80 | + name: 'i-running-103', |
| 81 | + }, |
| 82 | + ], |
| 83 | + }, |
| 84 | +}; |
| 85 | + |
| 86 | +describe('scaleDown', () => { |
| 87 | + beforeEach(() => { |
| 88 | + process.env.GITHUB_APP_KEY_BASE64 = 'TEST_CERTIFICATE_DATA'; |
| 89 | + process.env.GITHUB_APP_ID = '1337'; |
| 90 | + process.env.GITHUB_APP_CLIENT_ID = 'TEST_CLIENT_ID'; |
| 91 | + process.env.GITHUB_APP_CLIENT_SECRET = 'TEST_CLIENT_SECRET'; |
| 92 | + process.env.RUNNERS_MAXIMUM_COUNT = '3'; |
| 93 | + process.env.ENVIRONMENT = environment; |
| 94 | + const minimumRunningTimeInMinutes = '15'; |
| 95 | + process.env.MINIMUM_RUNNING_TIME_IN_MINUTES = minimumRunningTimeInMinutes; |
| 96 | + jest.clearAllMocks(); |
| 97 | + mockOctokit.apps.getOrgInstallation.mockImplementation(() => ({ |
| 98 | + data: { |
| 99 | + id: 'ORG', |
| 100 | + }, |
| 101 | + })); |
| 102 | + mockOctokit.apps.getRepoInstallation.mockImplementation(() => ({ |
| 103 | + data: { |
| 104 | + id: 'REPO', |
| 105 | + }, |
| 106 | + })); |
| 107 | + |
| 108 | + mockOctokit.actions.listSelfHostedRunnersForOrg.mockImplementation(() => { |
| 109 | + return DEFAULT_REGISTERED_RUNNERS; |
| 110 | + }); |
| 111 | + mockOctokit.actions.listSelfHostedRunnersForRepo.mockImplementation(() => { |
| 112 | + return DEFAULT_REGISTERED_RUNNERS; |
| 113 | + }); |
| 114 | + |
| 115 | + function deRegisterRunnerGithub(id: number): any {} |
| 116 | + mockOctokit.actions.deleteSelfHostedRunnerFromRepo.mockImplementation((repo) => { |
| 117 | + if (repo.runner_id === 103) { |
| 118 | + throw Error(); |
| 119 | + } else { |
| 120 | + return { status: 204 }; |
| 121 | + } |
| 122 | + }); |
| 123 | + mockOctokit.actions.deleteSelfHostedRunnerFromOrg.mockImplementation((repo) => { |
| 124 | + return repo.runner_id === 103 ? { status: 500 } : { status: 204 }; |
| 125 | + }); |
| 126 | + |
| 127 | + const mockTerminateRunners = mocked(terminateRunner); |
| 128 | + mockTerminateRunners.mockImplementation(async () => { |
| 129 | + return; |
| 130 | + }); |
| 131 | + }); |
| 132 | + |
| 133 | + describe('no runners running', () => { |
| 134 | + beforeAll(() => { |
| 135 | + const mockListRunners = mocked(listRunners); |
| 136 | + mockListRunners.mockImplementation(async () => []); |
| 137 | + }); |
| 138 | + |
| 139 | + it('No runners for repo.', async () => { |
| 140 | + process.env.ENABLE_ORGANIZATION_RUNNERS = 'false'; |
| 141 | + await scaleDown(); |
| 142 | + expect(listRunners).toBeCalledWith({ |
| 143 | + environment: environment, |
| 144 | + }); |
| 145 | + expect(terminateRunner).not; |
| 146 | + expect(mockOctokit.apps.getRepoInstallation).not; |
| 147 | + }); |
| 148 | + |
| 149 | + it('No runners for org.', async () => { |
| 150 | + process.env.ENABLE_ORGANIZATION_RUNNERS = 'true'; |
| 151 | + await scaleDown(); |
| 152 | + expect(listRunners).toBeCalledWith({ |
| 153 | + environment: environment, |
| 154 | + }); |
| 155 | + expect(terminateRunner).not; |
| 156 | + expect(mockOctokit.apps.getRepoInstallation).not; |
| 157 | + }); |
| 158 | + }); |
| 159 | + |
| 160 | + describe('on repo level', () => { |
| 161 | + beforeAll(() => { |
| 162 | + process.env.ENABLE_ORGANIZATION_RUNNERS = 'false'; |
| 163 | + const mockListRunners = mocked(listRunners); |
| 164 | + mockListRunners.mockImplementation(async () => { |
| 165 | + return DEFAULT_RUNNERS; |
| 166 | + }); |
| 167 | + }); |
| 168 | + |
| 169 | + it('Terminate 2 of 4 runners for repo.', async () => { |
| 170 | + await scaleDown(); |
| 171 | + expect(listRunners).toBeCalledWith({ |
| 172 | + environment: environment, |
| 173 | + }); |
| 174 | + |
| 175 | + expect(mockOctokit.apps.getRepoInstallation).toBeCalled(); |
| 176 | + |
| 177 | + expect(terminateRunner).toBeCalledTimes(2); |
| 178 | + }); |
| 179 | + }); |
| 180 | + |
| 181 | + describe('on org level', () => { |
| 182 | + beforeAll(() => { |
| 183 | + process.env.ENABLE_ORGANIZATION_RUNNERS = 'true'; |
| 184 | + const mockListRunners = mocked(listRunners); |
| 185 | + mockListRunners.mockImplementation(async () => { |
| 186 | + return DEFAULT_RUNNERS; |
| 187 | + }); |
| 188 | + }); |
| 189 | + |
| 190 | + it('Terminate 2 of 4 runners for org.', async () => { |
| 191 | + await scaleDown(); |
| 192 | + expect(listRunners).toBeCalledWith({ |
| 193 | + environment: environment, |
| 194 | + }); |
| 195 | + |
| 196 | + expect(mockOctokit.apps.getOrgInstallation).toBeCalled(); |
| 197 | + |
| 198 | + expect(terminateRunner).toBeCalledTimes(2); |
| 199 | + }); |
| 200 | + }); |
| 201 | +}); |
0 commit comments