|
1 |
| -import { listRunners, RunnerInfo } from './runners'; |
2 |
| -import { EC2 } from 'aws-sdk'; |
| 1 | +import { listRunners, RunnerInfo, createRunner } from './runners'; |
| 2 | +import { EC2, SSM } from 'aws-sdk'; |
3 | 3 |
|
4 |
| -const mockEC2 = { describeInstances: jest.fn() }; |
| 4 | +const mockEC2 = { describeInstances: jest.fn(), runInstances: jest.fn() }; |
| 5 | +const mockSSM = { putParameter: jest.fn() }; |
5 | 6 | jest.mock('aws-sdk', () => ({
|
6 | 7 | EC2: jest.fn().mockImplementation(() => mockEC2),
|
| 8 | + SSM: jest.fn().mockImplementation(() => mockSSM), |
7 | 9 | }));
|
8 | 10 |
|
9 | 11 | describe('list instances', () => {
|
@@ -96,3 +98,98 @@ describe('list instances', () => {
|
96 | 98 | });
|
97 | 99 | });
|
98 | 100 | });
|
| 101 | + |
| 102 | +describe('create runner', () => { |
| 103 | + const mockRunInstances = { promise: jest.fn() }; |
| 104 | + const mockPutParameter = { promise: jest.fn() }; |
| 105 | + beforeEach(() => { |
| 106 | + jest.clearAllMocks(); |
| 107 | + mockEC2.runInstances.mockImplementation(() => mockRunInstances); |
| 108 | + mockRunInstances.promise.mockReturnValue({ |
| 109 | + Instances: [ |
| 110 | + { |
| 111 | + InstanceId: 'i-1234', |
| 112 | + }, |
| 113 | + ], |
| 114 | + }); |
| 115 | + mockSSM.putParameter.mockImplementation(() => mockPutParameter); |
| 116 | + process.env.LAUNCH_TEMPLATE_NAME = 'launch-template-name'; |
| 117 | + process.env.LAUNCH_TEMPLATE_VERSION = '1'; |
| 118 | + process.env.SUBNET_IDS = 'sub-1234'; |
| 119 | + }); |
| 120 | + |
| 121 | + it('calls run instances with the correct config for repo', async () => { |
| 122 | + await createRunner({ |
| 123 | + runnerConfig: 'bla', |
| 124 | + environment: 'unit-test-env', |
| 125 | + repoName: 'SomeAwesomeCoder/some-amazing-library', |
| 126 | + orgName: undefined, |
| 127 | + }); |
| 128 | + expect(mockEC2.runInstances).toBeCalledWith({ |
| 129 | + MaxCount: 1, |
| 130 | + MinCount: 1, |
| 131 | + LaunchTemplate: { LaunchTemplateName: 'launch-template-name', Version: '1' }, |
| 132 | + SubnetId: 'sub-1234', |
| 133 | + TagSpecifications: [ |
| 134 | + { |
| 135 | + ResourceType: 'instance', |
| 136 | + Tags: [ |
| 137 | + { Key: 'Application', Value: 'github-action-runner' }, |
| 138 | + { Key: 'Repo', Value: 'SomeAwesomeCoder/some-amazing-library' }, |
| 139 | + ], |
| 140 | + }, |
| 141 | + ], |
| 142 | + }); |
| 143 | + }); |
| 144 | + |
| 145 | + it('calls run instances with the correct config for org', async () => { |
| 146 | + await createRunner({ |
| 147 | + runnerConfig: 'bla', |
| 148 | + environment: 'unit-test-env', |
| 149 | + repoName: undefined, |
| 150 | + orgName: 'SomeAwesomeCoder', |
| 151 | + }); |
| 152 | + expect(mockEC2.runInstances).toBeCalledWith({ |
| 153 | + MaxCount: 1, |
| 154 | + MinCount: 1, |
| 155 | + LaunchTemplate: { LaunchTemplateName: 'launch-template-name', Version: '1' }, |
| 156 | + SubnetId: 'sub-1234', |
| 157 | + TagSpecifications: [ |
| 158 | + { |
| 159 | + ResourceType: 'instance', |
| 160 | + Tags: [ |
| 161 | + { Key: 'Application', Value: 'github-action-runner' }, |
| 162 | + { Key: 'Org', Value: 'SomeAwesomeCoder' }, |
| 163 | + ], |
| 164 | + }, |
| 165 | + ], |
| 166 | + }); |
| 167 | + }); |
| 168 | + |
| 169 | + it('creates ssm parameters for each created instance', async () => { |
| 170 | + await createRunner({ |
| 171 | + runnerConfig: 'bla', |
| 172 | + environment: 'unit-test-env', |
| 173 | + repoName: undefined, |
| 174 | + orgName: 'SomeAwesomeCoder', |
| 175 | + }); |
| 176 | + expect(mockSSM.putParameter).toBeCalledWith({ |
| 177 | + Name: 'unit-test-env-i-1234', |
| 178 | + Value: 'bla', |
| 179 | + Type: 'SecureString', |
| 180 | + }); |
| 181 | + }); |
| 182 | + |
| 183 | + it('does not create ssm parameters when no instance is created', async () => { |
| 184 | + mockRunInstances.promise.mockReturnValue({ |
| 185 | + Instances: [], |
| 186 | + }); |
| 187 | + await createRunner({ |
| 188 | + runnerConfig: 'bla', |
| 189 | + environment: 'unit-test-env', |
| 190 | + repoName: undefined, |
| 191 | + orgName: 'SomeAwesomeCoder', |
| 192 | + }); |
| 193 | + expect(mockSSM.putParameter).not.toBeCalled(); |
| 194 | + }); |
| 195 | +}); |
0 commit comments