Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions lambdas/functions/control-plane/src/pool/ec2-pool.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { bootTimeExceeded } from '../aws/ec2-runners';
import type { RunnerList } from '../aws/ec2-runners.d';
import { calculateEc2PoolSize } from './ec2-pool';
import { beforeEach, describe, expect, it, vi } from 'vitest';

vi.mock('../aws/ec2-runners', () => ({
bootTimeExceeded: vi.fn(),
}));

const mockBootTimeExceeded = vi.mocked(bootTimeExceeded);

describe('calculateEc2PoolSize', () => {
beforeEach(() => {
vi.clearAllMocks();
});

it('counts registered online idle runners', () => {
const runners: RunnerList[] = [{ instanceId: 'i-idle' }];
const runnerStatus = new Map([['i-idle', { busy: false, status: 'online' }]]);

expect(calculateEc2PoolSize(runners, runnerStatus)).toBe(1);
expect(mockBootTimeExceeded).not.toHaveBeenCalled();
});

it('does not count registered busy or offline runners', () => {
const runners: RunnerList[] = [{ instanceId: 'i-busy' }, { instanceId: 'i-offline' }];
const runnerStatus = new Map([
['i-busy', { busy: true, status: 'online' }],
['i-offline', { busy: false, status: 'offline' }],
]);

expect(calculateEc2PoolSize(runners, runnerStatus)).toBe(0);
expect(mockBootTimeExceeded).not.toHaveBeenCalled();
});

it('counts unregistered runners that are still booting', () => {
const runners: RunnerList[] = [{ instanceId: 'i-booting' }];
mockBootTimeExceeded.mockReturnValue(false);

expect(calculateEc2PoolSize(runners, new Map())).toBe(1);
});

it('does not count unregistered runners whose boot time expired', () => {
const runners: RunnerList[] = [{ instanceId: 'i-expired' }];
mockBootTimeExceeded.mockReturnValue(true);

expect(calculateEc2PoolSize(runners, new Map())).toBe(0);
});
});
44 changes: 1 addition & 43 deletions lambdas/functions/control-plane/src/pool/pool.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@ import { Octokit } from '@octokit/rest';
import moment from 'moment-timezone';
import * as nock from 'nock';

import { bootTimeExceeded, listEC2Runners } from '../aws/ec2-runners';
import type { RunnerList } from '../aws/ec2-runners.d';
import { listEC2Runners } from '../aws/ec2-runners';
import * as ghAuth from '../github/auth';
import { createRunners } from '../scale-runners/ec2';
import { getGitHubEnterpriseApiUrl } from '../scale-runners/github-runner';
import { adjust } from './pool';
import { calculateEc2PoolSize } from './ec2-pool';
import { describe, it, expect, beforeEach, vi, MockedClass } from 'vitest';

const mockOctokit = {
Expand Down Expand Up @@ -57,7 +55,6 @@ const mockedAppAuth = vi.mocked(ghAuth.createGithubAppAuth);
const mockedInstallationAuth = vi.mocked(ghAuth.createGithubInstallationAuth);
const mockCreateClient = vi.mocked(ghAuth.createOctokitClient);
const mockListRunners = vi.mocked(listEC2Runners);
const mockBootTimeExceeded = vi.mocked(bootTimeExceeded);

const cleanEnv = process.env;

Expand Down Expand Up @@ -496,42 +493,3 @@ describe('Test simple pool.', () => {
});
});
});

describe('calculateEc2PoolSize', () => {
beforeEach(() => {
vi.clearAllMocks();
});

it('counts registered online idle runners', () => {
const runners: RunnerList[] = [{ instanceId: 'i-idle' }];
const runnerStatus = new Map([['i-idle', { busy: false, status: 'online' }]]);

expect(calculateEc2PoolSize(runners, runnerStatus)).toBe(1);
expect(mockBootTimeExceeded).not.toHaveBeenCalled();
});

it('does not count registered busy or offline runners', () => {
const runners: RunnerList[] = [{ instanceId: 'i-busy' }, { instanceId: 'i-offline' }];
const runnerStatus = new Map([
['i-busy', { busy: true, status: 'online' }],
['i-offline', { busy: false, status: 'offline' }],
]);

expect(calculateEc2PoolSize(runners, runnerStatus)).toBe(0);
expect(mockBootTimeExceeded).not.toHaveBeenCalled();
});

it('counts unregistered runners that are still booting', () => {
const runners: RunnerList[] = [{ instanceId: 'i-booting' }];
mockBootTimeExceeded.mockReturnValue(false);

expect(calculateEc2PoolSize(runners, new Map())).toBe(1);
});

it('does not count unregistered runners whose boot time expired', () => {
const runners: RunnerList[] = [{ instanceId: 'i-expired' }];
mockBootTimeExceeded.mockReturnValue(true);

expect(calculateEc2PoolSize(runners, new Map())).toBe(0);
});
});
Loading
Loading