Skip to content

Commit f36b4f2

Browse files
committed
cleanup before in scale-up test
1 parent c272393 commit f36b4f2

File tree

2 files changed

+73
-56
lines changed

2 files changed

+73
-56
lines changed

lambdas/functions/control-plane/jest.config.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ const config: Config = {
66
...defaultConfig,
77
coverageThreshold: {
88
global: {
9-
statements: 97.78,
10-
branches: 96.61,
11-
functions: 95.84,
12-
lines: 97.71,
9+
statements: 97.86,
10+
branches: 96.68,
11+
functions: 95.95,
12+
lines: 97.8,
1313
},
1414
},
1515
};

lambdas/functions/control-plane/src/scale-runners/scale-up.test.ts

+69-52
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ const mockOctokit = {
3131
const mockCreateRunner = mocked(createRunner);
3232
const mockListRunners = mocked(listEC2Runners);
3333
const mockSSMClient = mockClient(SSMClient);
34-
const mockSSM = mocked(getParameter);
34+
const mockSSMgetParameter = mocked(getParameter);
3535

3636
jest.mock('@octokit/rest', () => ({
3737
Octokit: jest.fn().mockImplementation(() => mockOctokit),
@@ -85,6 +85,7 @@ let expectedRunnerParams: RunnerInputParameters;
8585

8686
function setDefaults() {
8787
process.env = { ...cleanEnv };
88+
process.env.PARAMETER_GITHUB_APP_ID_NAME = 'github-app-id';
8889
process.env.GITHUB_APP_KEY_BASE64 = 'TEST_CERTIFICATE_DATA';
8990
process.env.GITHUB_APP_ID = '1337';
9091
process.env.GITHUB_APP_CLIENT_ID = 'TEST_CLIENT_ID';
@@ -104,52 +105,9 @@ beforeEach(() => {
104105
jest.clearAllMocks();
105106
setDefaults();
106107

107-
mockOctokit.actions.getJobForWorkflowRun.mockImplementation(() => ({
108-
data: {
109-
status: 'queued',
110-
},
111-
}));
112-
mockOctokit.paginate.mockImplementation(() => [
113-
{
114-
id: 1,
115-
name: 'Default',
116-
},
117-
]);
118-
mockOctokit.actions.generateRunnerJitconfigForOrg.mockImplementation(() => ({
119-
data: {
120-
encoded_jit_config: 'TEST_JIT_CONFIG_ORG',
121-
},
122-
}));
123-
mockOctokit.actions.generateRunnerJitconfigForRepo.mockImplementation(() => ({
124-
data: {
125-
encoded_jit_config: 'TEST_JIT_CONFIG_REPO',
126-
},
127-
}));
128-
mockOctokit.checks.get.mockImplementation(() => ({
129-
data: {
130-
status: 'queued',
131-
},
132-
}));
133-
const mockTokenReturnValue = {
134-
data: {
135-
token: '1234abcd',
136-
},
137-
};
138-
const mockInstallationIdReturnValueOrgs = {
139-
data: {
140-
id: TEST_DATA.installationId,
141-
},
142-
};
143-
const mockInstallationIdReturnValueRepos = {
144-
data: {
145-
id: TEST_DATA.installationId,
146-
},
147-
};
108+
defaultSSMGetParameterMockImpl();
109+
defaultOctokitMockImpl();
148110

149-
mockOctokit.actions.createRegistrationTokenForOrg.mockImplementation(() => mockTokenReturnValue);
150-
mockOctokit.actions.createRegistrationTokenForRepo.mockImplementation(() => mockTokenReturnValue);
151-
mockOctokit.apps.getOrgInstallation.mockImplementation(() => mockInstallationIdReturnValueOrgs);
152-
mockOctokit.apps.getRepoInstallation.mockImplementation(() => mockInstallationIdReturnValueRepos);
153111
mockCreateRunner.mockImplementation(async () => {
154112
return ['i-12345'];
155113
});
@@ -180,10 +138,6 @@ beforeEach(() => {
180138
});
181139

182140
mockCreateClient.mockResolvedValue(new mocktokit());
183-
184-
mockSSM.mockImplementation(async () => {
185-
return '1';
186-
});
187141
});
188142

189143
describe('scaleUp with GHES', () => {
@@ -281,7 +235,7 @@ describe('scaleUp with GHES', () => {
281235

282236
it('Throws an error if runner group doesnt exist for ephemeral runners', async () => {
283237
process.env.RUNNER_GROUP_NAME = 'test-runner-group';
284-
mockSSM.mockImplementation(async () => {
238+
mockSSMgetParameter.mockImplementation(async () => {
285239
throw new Error('ParameterNotFound');
286240
});
287241
await expect(scaleUpModule.scaleUp('aws:sqs', TEST_DATA)).rejects.toBeInstanceOf(Error);
@@ -297,7 +251,7 @@ describe('scaleUp with GHES', () => {
297251
});
298252

299253
it('create SSM parameter for runner group id if it doesnt exist', async () => {
300-
mockSSM.mockImplementation(async () => {
254+
mockSSMgetParameter.mockImplementation(async () => {
301255
throw new Error('ParameterNotFound');
302256
});
303257
await scaleUpModule.scaleUp('aws:sqs', TEST_DATA);
@@ -318,6 +272,7 @@ describe('scaleUp with GHES', () => {
318272

319273
it('create start runner config for ephemeral runners ', async () => {
320274
process.env.RUNNERS_MAXIMUM_COUNT = '2';
275+
321276
await scaleUpModule.scaleUp('aws:sqs', TEST_DATA);
322277
expect(mockOctokit.actions.generateRunnerJitconfigForOrg).toBeCalledWith({
323278
org: TEST_DATA.repositoryOwner,
@@ -719,3 +674,65 @@ describe('scaleUp with public GH', () => {
719674
});
720675
});
721676
});
677+
678+
function defaultOctokitMockImpl() {
679+
mockOctokit.actions.getJobForWorkflowRun.mockImplementation(() => ({
680+
data: {
681+
status: 'queued',
682+
},
683+
}));
684+
mockOctokit.paginate.mockImplementation(() => [
685+
{
686+
id: 1,
687+
name: 'Default',
688+
},
689+
]);
690+
mockOctokit.actions.generateRunnerJitconfigForOrg.mockImplementation(() => ({
691+
data: {
692+
encoded_jit_config: 'TEST_JIT_CONFIG_ORG',
693+
},
694+
}));
695+
mockOctokit.actions.generateRunnerJitconfigForRepo.mockImplementation(() => ({
696+
data: {
697+
encoded_jit_config: 'TEST_JIT_CONFIG_REPO',
698+
},
699+
}));
700+
mockOctokit.checks.get.mockImplementation(() => ({
701+
data: {
702+
status: 'queued',
703+
},
704+
}));
705+
706+
const mockTokenReturnValue = {
707+
data: {
708+
token: '1234abcd',
709+
},
710+
};
711+
const mockInstallationIdReturnValueOrgs = {
712+
data: {
713+
id: TEST_DATA.installationId,
714+
},
715+
};
716+
const mockInstallationIdReturnValueRepos = {
717+
data: {
718+
id: TEST_DATA.installationId,
719+
},
720+
};
721+
722+
mockOctokit.actions.createRegistrationTokenForOrg.mockImplementation(() => mockTokenReturnValue);
723+
mockOctokit.actions.createRegistrationTokenForRepo.mockImplementation(() => mockTokenReturnValue);
724+
mockOctokit.apps.getOrgInstallation.mockImplementation(() => mockInstallationIdReturnValueOrgs);
725+
mockOctokit.apps.getRepoInstallation.mockImplementation(() => mockInstallationIdReturnValueRepos);
726+
}
727+
728+
function defaultSSMGetParameterMockImpl() {
729+
mockSSMgetParameter.mockImplementation(async (name: string) => {
730+
if (name === `${process.env.SSM_CONFIG_PATH}/runner-group/${process.env.RUNNER_GROUP_NAME}`) {
731+
return '1';
732+
} else if (name === `${process.env.PARAMETER_GITHUB_APP_ID_NAME}`) {
733+
return `${process.env.GITHUB_APP_ID}`;
734+
} else {
735+
throw new Error(`ParameterNotFound: ${name}`);
736+
}
737+
});
738+
}

0 commit comments

Comments
 (0)