|
| 1 | +import { listRunners, RunnerInfo, createRunner } from './runners'; |
| 2 | +import { EC2, SSM } from 'aws-sdk'; |
| 3 | + |
| 4 | +const mockEC2 = { describeInstances: jest.fn(), runInstances: jest.fn() }; |
| 5 | +const mockSSM = { putParameter: jest.fn() }; |
| 6 | +jest.mock('aws-sdk', () => ({ |
| 7 | + EC2: jest.fn().mockImplementation(() => mockEC2), |
| 8 | + SSM: jest.fn().mockImplementation(() => mockSSM), |
| 9 | +})); |
| 10 | + |
| 11 | +describe('list instances', () => { |
| 12 | + const mockDescribeInstances = { promise: jest.fn() }; |
| 13 | + beforeEach(() => { |
| 14 | + jest.clearAllMocks(); |
| 15 | + mockEC2.describeInstances.mockImplementation(() => mockDescribeInstances); |
| 16 | + const mockRunningInstances: AWS.EC2.DescribeInstancesResult = { |
| 17 | + Reservations: [ |
| 18 | + { |
| 19 | + Instances: [ |
| 20 | + { |
| 21 | + LaunchTime: new Date('2020-10-10T14:48:00.000+09:00'), |
| 22 | + InstanceId: 'i-1234', |
| 23 | + Tags: [ |
| 24 | + { Key: 'Repo', Value: 'CoderToCat/hello-world' }, |
| 25 | + { Key: 'Org', Value: 'CoderToCat' }, |
| 26 | + { Key: 'Application', Value: 'github-action-runner' }, |
| 27 | + ], |
| 28 | + }, |
| 29 | + { |
| 30 | + LaunchTime: new Date('2020-10-11T14:48:00.000+09:00'), |
| 31 | + InstanceId: 'i-5678', |
| 32 | + Tags: [ |
| 33 | + { Key: 'Repo', Value: 'SomeAwesomeCoder/some-amazing-library' }, |
| 34 | + { Key: 'Org', Value: 'SomeAwesomeCoder' }, |
| 35 | + { Key: 'Application', Value: 'github-action-runner' }, |
| 36 | + ], |
| 37 | + }, |
| 38 | + ], |
| 39 | + }, |
| 40 | + ], |
| 41 | + }; |
| 42 | + mockDescribeInstances.promise.mockReturnValue(mockRunningInstances); |
| 43 | + }); |
| 44 | + |
| 45 | + it('returns a list of instances', async () => { |
| 46 | + const resp = await listRunners(); |
| 47 | + expect(resp.length).toBe(2); |
| 48 | + expect(resp).toContainEqual({ |
| 49 | + instanceId: 'i-1234', |
| 50 | + launchTime: new Date('2020-10-10T14:48:00.000+09:00'), |
| 51 | + repo: 'CoderToCat/hello-world', |
| 52 | + org: 'CoderToCat', |
| 53 | + }); |
| 54 | + expect(resp).toContainEqual({ |
| 55 | + instanceId: 'i-5678', |
| 56 | + launchTime: new Date('2020-10-11T14:48:00.000+09:00'), |
| 57 | + repo: 'SomeAwesomeCoder/some-amazing-library', |
| 58 | + org: 'SomeAwesomeCoder', |
| 59 | + }); |
| 60 | + }); |
| 61 | + |
| 62 | + it('calls EC2 describe instances', async () => { |
| 63 | + await listRunners(); |
| 64 | + expect(mockEC2.describeInstances).toBeCalled(); |
| 65 | + }); |
| 66 | + |
| 67 | + it('filters instances on repo name', async () => { |
| 68 | + await listRunners({ repoName: 'SomeAwesomeCoder/some-amazing-library' }); |
| 69 | + expect(mockEC2.describeInstances).toBeCalledWith({ |
| 70 | + Filters: [ |
| 71 | + { Name: 'tag:Application', Values: ['github-action-runner'] }, |
| 72 | + { Name: 'instance-state-name', Values: ['running', 'pending'] }, |
| 73 | + { Name: 'tag:Repo', Values: ['SomeAwesomeCoder/some-amazing-library'] }, |
| 74 | + ], |
| 75 | + }); |
| 76 | + }); |
| 77 | + |
| 78 | + it('filters instances on org name', async () => { |
| 79 | + await listRunners({ orgName: 'SomeAwesomeCoder' }); |
| 80 | + expect(mockEC2.describeInstances).toBeCalledWith({ |
| 81 | + Filters: [ |
| 82 | + { Name: 'tag:Application', Values: ['github-action-runner'] }, |
| 83 | + { Name: 'instance-state-name', Values: ['running', 'pending'] }, |
| 84 | + { Name: 'tag:Org', Values: ['SomeAwesomeCoder'] }, |
| 85 | + ], |
| 86 | + }); |
| 87 | + }); |
| 88 | + |
| 89 | + it('filters instances on org name', async () => { |
| 90 | + await listRunners({ environment: 'unit-test-environment' }); |
| 91 | + expect(mockEC2.describeInstances).toBeCalledWith({ |
| 92 | + Filters: [ |
| 93 | + { Name: 'tag:Application', Values: ['github-action-runner'] }, |
| 94 | + { Name: 'instance-state-name', Values: ['running', 'pending'] }, |
| 95 | + { Name: 'tag:Environment', Values: ['unit-test-environment'] }, |
| 96 | + ], |
| 97 | + }); |
| 98 | + }); |
| 99 | + |
| 100 | + it('filters instances on both org name and repo name', async () => { |
| 101 | + await listRunners({ orgName: 'SomeAwesomeCoder', repoName: 'SomeAwesomeCoder/some-amazing-library' }); |
| 102 | + expect(mockEC2.describeInstances).toBeCalledWith({ |
| 103 | + Filters: [ |
| 104 | + { Name: 'tag:Application', Values: ['github-action-runner'] }, |
| 105 | + { Name: 'instance-state-name', Values: ['running', 'pending'] }, |
| 106 | + { Name: 'tag:Repo', Values: ['SomeAwesomeCoder/some-amazing-library'] }, |
| 107 | + { Name: 'tag:Org', Values: ['SomeAwesomeCoder'] }, |
| 108 | + ], |
| 109 | + }); |
| 110 | + }); |
| 111 | +}); |
| 112 | + |
| 113 | +describe('create runner', () => { |
| 114 | + const mockRunInstances = { promise: jest.fn() }; |
| 115 | + const mockPutParameter = { promise: jest.fn() }; |
| 116 | + beforeEach(() => { |
| 117 | + jest.clearAllMocks(); |
| 118 | + mockEC2.runInstances.mockImplementation(() => mockRunInstances); |
| 119 | + mockRunInstances.promise.mockReturnValue({ |
| 120 | + Instances: [ |
| 121 | + { |
| 122 | + InstanceId: 'i-1234', |
| 123 | + }, |
| 124 | + ], |
| 125 | + }); |
| 126 | + mockSSM.putParameter.mockImplementation(() => mockPutParameter); |
| 127 | + process.env.LAUNCH_TEMPLATE_NAME = 'launch-template-name'; |
| 128 | + process.env.LAUNCH_TEMPLATE_VERSION = '1'; |
| 129 | + process.env.SUBNET_IDS = 'sub-1234'; |
| 130 | + }); |
| 131 | + |
| 132 | + it('calls run instances with the correct config for repo', async () => { |
| 133 | + await createRunner({ |
| 134 | + runnerConfig: 'bla', |
| 135 | + environment: 'unit-test-env', |
| 136 | + repoName: 'SomeAwesomeCoder/some-amazing-library', |
| 137 | + orgName: undefined, |
| 138 | + }); |
| 139 | + expect(mockEC2.runInstances).toBeCalledWith({ |
| 140 | + MaxCount: 1, |
| 141 | + MinCount: 1, |
| 142 | + LaunchTemplate: { LaunchTemplateName: 'launch-template-name', Version: '1' }, |
| 143 | + SubnetId: 'sub-1234', |
| 144 | + TagSpecifications: [ |
| 145 | + { |
| 146 | + ResourceType: 'instance', |
| 147 | + Tags: [ |
| 148 | + { Key: 'Application', Value: 'github-action-runner' }, |
| 149 | + { Key: 'Repo', Value: 'SomeAwesomeCoder/some-amazing-library' }, |
| 150 | + ], |
| 151 | + }, |
| 152 | + ], |
| 153 | + }); |
| 154 | + }); |
| 155 | + |
| 156 | + it('calls run instances with the correct config for org', async () => { |
| 157 | + await createRunner({ |
| 158 | + runnerConfig: 'bla', |
| 159 | + environment: 'unit-test-env', |
| 160 | + repoName: undefined, |
| 161 | + orgName: 'SomeAwesomeCoder', |
| 162 | + }); |
| 163 | + expect(mockEC2.runInstances).toBeCalledWith({ |
| 164 | + MaxCount: 1, |
| 165 | + MinCount: 1, |
| 166 | + LaunchTemplate: { LaunchTemplateName: 'launch-template-name', Version: '1' }, |
| 167 | + SubnetId: 'sub-1234', |
| 168 | + TagSpecifications: [ |
| 169 | + { |
| 170 | + ResourceType: 'instance', |
| 171 | + Tags: [ |
| 172 | + { Key: 'Application', Value: 'github-action-runner' }, |
| 173 | + { Key: 'Org', Value: 'SomeAwesomeCoder' }, |
| 174 | + ], |
| 175 | + }, |
| 176 | + ], |
| 177 | + }); |
| 178 | + }); |
| 179 | + |
| 180 | + it('creates ssm parameters for each created instance', async () => { |
| 181 | + await createRunner({ |
| 182 | + runnerConfig: 'bla', |
| 183 | + environment: 'unit-test-env', |
| 184 | + repoName: undefined, |
| 185 | + orgName: 'SomeAwesomeCoder', |
| 186 | + }); |
| 187 | + expect(mockSSM.putParameter).toBeCalledWith({ |
| 188 | + Name: 'unit-test-env-i-1234', |
| 189 | + Value: 'bla', |
| 190 | + Type: 'SecureString', |
| 191 | + }); |
| 192 | + }); |
| 193 | + |
| 194 | + it('does not create ssm parameters when no instance is created', async () => { |
| 195 | + mockRunInstances.promise.mockReturnValue({ |
| 196 | + Instances: [], |
| 197 | + }); |
| 198 | + await createRunner({ |
| 199 | + runnerConfig: 'bla', |
| 200 | + environment: 'unit-test-env', |
| 201 | + repoName: undefined, |
| 202 | + orgName: 'SomeAwesomeCoder', |
| 203 | + }); |
| 204 | + expect(mockSSM.putParameter).not.toBeCalled(); |
| 205 | + }); |
| 206 | +}); |
0 commit comments