Skip to content

Commit 69068bf

Browse files
committed
add unit tests for creating runners
1 parent 20bed7f commit 69068bf

File tree

2 files changed

+102
-6
lines changed

2 files changed

+102
-6
lines changed

modules/runners/lambdas/scale-runners/src/scale-runners/runners.test.ts

Lines changed: 100 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
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';
33

4-
const mockEC2 = { describeInstances: jest.fn() };
4+
const mockEC2 = { describeInstances: jest.fn(), runInstances: jest.fn() };
5+
const mockSSM = { putParameter: jest.fn() };
56
jest.mock('aws-sdk', () => ({
67
EC2: jest.fn().mockImplementation(() => mockEC2),
8+
SSM: jest.fn().mockImplementation(() => mockSSM),
79
}));
810

911
describe('list instances', () => {
@@ -96,3 +98,98 @@ describe('list instances', () => {
9698
});
9799
});
98100
});
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+
});

modules/runners/lambdas/scale-runners/src/scale-runners/runners.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,14 +91,13 @@ export async function createRunner(runnerParameters: RunnerInputParameters): Pro
9191
);
9292

9393
const ssm = new SSM();
94-
runInstancesResponse.Instances?.forEach((i: EC2.Instance) => {
95-
const r = ssm
94+
runInstancesResponse.Instances?.forEach(async (i: EC2.Instance) => {
95+
await ssm
9696
.putParameter({
9797
Name: runnerParameters.environment + '-' + (i.InstanceId as string),
9898
Value: runnerParameters.runnerConfig,
9999
Type: 'SecureString',
100100
})
101101
.promise();
102-
console.log(r);
103102
});
104103
}

0 commit comments

Comments
 (0)