forked from philips-labs/terraform-aws-github-runner
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WIP: list runners, create token if needed
- Loading branch information
1 parent
503543c
commit cf7124c
Showing
5 changed files
with
230 additions
and
411 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
92 changes: 86 additions & 6 deletions
92
modules/runners/lambdas/scale-runners/src/scale-runners/runners.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,98 @@ | ||
import { listRunners } from './runners'; | ||
import { handle } from './handler'; | ||
import { listRunners, RunnerInfo } from './runners'; | ||
import { EC2 } from 'aws-sdk'; | ||
|
||
jest.mock('./handler'); | ||
const mockEC2 = { describeInstances: jest.fn() }; | ||
jest.mock('aws-sdk', () => ({ | ||
EC2: jest.fn().mockImplementation(() => mockEC2), | ||
})); | ||
|
||
describe('list instances', () => { | ||
beforeAll(() => { | ||
const mockDescribeInstances = { promise: jest.fn() }; | ||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
mockEC2.describeInstances.mockImplementation(() => mockDescribeInstances); | ||
const mockRunningInstances: AWS.EC2.DescribeInstancesResult = { | ||
Reservations: [ | ||
{ | ||
Instances: [ | ||
{ | ||
LaunchTime: new Date('2020-10-10T14:48:00.000+09:00'), | ||
InstanceId: 'i-1234', | ||
Tags: [ | ||
{ Key: 'Repo', Value: 'CoderToCat/hello-world' }, | ||
{ Key: 'Org', Value: 'CoderToCat' }, | ||
{ Key: 'Application', Value: 'github-action-runner' }, | ||
], | ||
}, | ||
{ | ||
LaunchTime: new Date('2020-10-11T14:48:00.000+09:00'), | ||
InstanceId: 'i-5678', | ||
Tags: [ | ||
{ Key: 'Repo', Value: 'SomeAwesomeCoder/some-amazing-library' }, | ||
{ Key: 'Org', Value: 'SomeAwesomeCoder' }, | ||
{ Key: 'Application', Value: 'github-action-runner' }, | ||
], | ||
}, | ||
], | ||
}, | ||
], | ||
}; | ||
mockDescribeInstances.promise.mockReturnValue(mockRunningInstances); | ||
}); | ||
it('returns a list of instances', () => { | ||
listRunners(); | ||
|
||
it('returns a list of instances', async () => { | ||
const resp = await listRunners(); | ||
expect(resp.length).toBe(2); | ||
expect(resp).toContainEqual({ | ||
instanceId: 'i-1234', | ||
launchTime: new Date('2020-10-10T14:48:00.000+09:00'), | ||
repo: 'CoderToCat/hello-world', | ||
org: 'CoderToCat', | ||
}); | ||
expect(resp).toContainEqual({ | ||
instanceId: 'i-5678', | ||
launchTime: new Date('2020-10-11T14:48:00.000+09:00'), | ||
repo: 'SomeAwesomeCoder/some-amazing-library', | ||
org: 'SomeAwesomeCoder', | ||
}); | ||
}); | ||
|
||
it('calls EC2 describe instances', async () => { | ||
await listRunners(); | ||
expect(mockEC2.describeInstances).toBeCalled(); | ||
}); | ||
|
||
it('filters instances on repo name', async () => { | ||
await listRunners({ repoName: 'SomeAwesomeCoder/some-amazing-library' }); | ||
expect(mockEC2.describeInstances).toBeCalledWith({ | ||
Filters: [ | ||
{ Name: 'tag:Application', Values: ['github-action-runner'] }, | ||
{ Name: 'instance-state-name', Values: ['running', 'pending'] }, | ||
{ Name: 'tag:Repo', Values: ['SomeAwesomeCoder/some-amazing-library'] }, | ||
], | ||
}); | ||
}); | ||
|
||
it('filters instances on org name', async () => { | ||
await listRunners({ orgName: 'SomeAwesomeCoder' }); | ||
expect(mockEC2.describeInstances).toBeCalledWith({ | ||
Filters: [ | ||
{ Name: 'tag:Application', Values: ['github-action-runner'] }, | ||
{ Name: 'instance-state-name', Values: ['running', 'pending'] }, | ||
{ Name: 'tag:Org', Values: ['SomeAwesomeCoder'] }, | ||
], | ||
}); | ||
}); | ||
|
||
it('filters instances on both org name and repo name', async () => { | ||
await listRunners({ orgName: 'SomeAwesomeCoder', repoName: 'SomeAwesomeCoder/some-amazing-library' }); | ||
expect(mockEC2.describeInstances).toBeCalledWith({ | ||
Filters: [ | ||
{ Name: 'tag:Application', Values: ['github-action-runner'] }, | ||
{ Name: 'instance-state-name', Values: ['running', 'pending'] }, | ||
{ Name: 'tag:Repo', Values: ['SomeAwesomeCoder/some-amazing-library'] }, | ||
{ Name: 'tag:Org', Values: ['SomeAwesomeCoder'] }, | ||
], | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.