Skip to content

Commit

Permalink
add unit tests for creating runners
Browse files Browse the repository at this point in the history
  • Loading branch information
gertjanmaas committed May 11, 2020
1 parent 20bed7f commit 69068bf
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { listRunners, RunnerInfo } from './runners';
import { EC2 } from 'aws-sdk';
import { listRunners, RunnerInfo, createRunner } from './runners';
import { EC2, SSM } from 'aws-sdk';

const mockEC2 = { describeInstances: jest.fn() };
const mockEC2 = { describeInstances: jest.fn(), runInstances: jest.fn() };
const mockSSM = { putParameter: jest.fn() };
jest.mock('aws-sdk', () => ({
EC2: jest.fn().mockImplementation(() => mockEC2),
SSM: jest.fn().mockImplementation(() => mockSSM),
}));

describe('list instances', () => {
Expand Down Expand Up @@ -96,3 +98,98 @@ describe('list instances', () => {
});
});
});

describe('create runner', () => {
const mockRunInstances = { promise: jest.fn() };
const mockPutParameter = { promise: jest.fn() };
beforeEach(() => {
jest.clearAllMocks();
mockEC2.runInstances.mockImplementation(() => mockRunInstances);
mockRunInstances.promise.mockReturnValue({
Instances: [
{
InstanceId: 'i-1234',
},
],
});
mockSSM.putParameter.mockImplementation(() => mockPutParameter);
process.env.LAUNCH_TEMPLATE_NAME = 'launch-template-name';
process.env.LAUNCH_TEMPLATE_VERSION = '1';
process.env.SUBNET_IDS = 'sub-1234';
});

it('calls run instances with the correct config for repo', async () => {
await createRunner({
runnerConfig: 'bla',
environment: 'unit-test-env',
repoName: 'SomeAwesomeCoder/some-amazing-library',
orgName: undefined,
});
expect(mockEC2.runInstances).toBeCalledWith({
MaxCount: 1,
MinCount: 1,
LaunchTemplate: { LaunchTemplateName: 'launch-template-name', Version: '1' },
SubnetId: 'sub-1234',
TagSpecifications: [
{
ResourceType: 'instance',
Tags: [
{ Key: 'Application', Value: 'github-action-runner' },
{ Key: 'Repo', Value: 'SomeAwesomeCoder/some-amazing-library' },
],
},
],
});
});

it('calls run instances with the correct config for org', async () => {
await createRunner({
runnerConfig: 'bla',
environment: 'unit-test-env',
repoName: undefined,
orgName: 'SomeAwesomeCoder',
});
expect(mockEC2.runInstances).toBeCalledWith({
MaxCount: 1,
MinCount: 1,
LaunchTemplate: { LaunchTemplateName: 'launch-template-name', Version: '1' },
SubnetId: 'sub-1234',
TagSpecifications: [
{
ResourceType: 'instance',
Tags: [
{ Key: 'Application', Value: 'github-action-runner' },
{ Key: 'Org', Value: 'SomeAwesomeCoder' },
],
},
],
});
});

it('creates ssm parameters for each created instance', async () => {
await createRunner({
runnerConfig: 'bla',
environment: 'unit-test-env',
repoName: undefined,
orgName: 'SomeAwesomeCoder',
});
expect(mockSSM.putParameter).toBeCalledWith({
Name: 'unit-test-env-i-1234',
Value: 'bla',
Type: 'SecureString',
});
});

it('does not create ssm parameters when no instance is created', async () => {
mockRunInstances.promise.mockReturnValue({
Instances: [],
});
await createRunner({
runnerConfig: 'bla',
environment: 'unit-test-env',
repoName: undefined,
orgName: 'SomeAwesomeCoder',
});
expect(mockSSM.putParameter).not.toBeCalled();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -91,14 +91,13 @@ export async function createRunner(runnerParameters: RunnerInputParameters): Pro
);

const ssm = new SSM();
runInstancesResponse.Instances?.forEach((i: EC2.Instance) => {
const r = ssm
runInstancesResponse.Instances?.forEach(async (i: EC2.Instance) => {
await ssm
.putParameter({
Name: runnerParameters.environment + '-' + (i.InstanceId as string),
Value: runnerParameters.runnerConfig,
Type: 'SecureString',
})
.promise();
console.log(r);
});
}

0 comments on commit 69068bf

Please sign in to comment.