Skip to content

Commit cabcb69

Browse files
committed
add tests
1 parent 9c54c68 commit cabcb69

File tree

5 files changed

+211
-85
lines changed

5 files changed

+211
-85
lines changed

lambdas/functions/webhook/src/ConfigLoader.test.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { getParameter } from '@aws-github-runner/aws-ssm-util';
22
import { ConfigWebhook, ConfigWebhookEventBridge, ConfigDispatcher } from './ConfigLoader';
33
import { mocked } from 'jest-mock';
44
import { logger } from '@aws-github-runner/aws-powertools-util';
5-
import { MatcherConfig, RunnerMatcherConfig } from './sqs';
5+
import { RunnerMatcherConfig } from './sqs';
66

77
jest.mock('@aws-github-runner/aws-ssm-util');
88

@@ -78,7 +78,7 @@ describe('ConfigLoader Tests', () => {
7878
setupConfiguration();
7979
const spy = jest.spyOn(logger, 'debug');
8080

81-
const config = await ConfigWebhook.load();
81+
await ConfigWebhook.load();
8282

8383
expect(spy).toHaveBeenCalledWith(
8484
'Config loaded',
@@ -169,7 +169,7 @@ describe('ConfigLoader Tests', () => {
169169
});
170170

171171
await expect(ConfigWebhook.load()).rejects.toThrow(
172-
'Failed to load config: Failed to load parameter for matcherConfig from path /path/to/matcher/config: Failed to load matcher config',
172+
'Failed to load config: Failed to load parameter for matcherConfig from path /path/to/matcher/config: Failed to load matcher config', // eslint-disable-line max-len
173173
);
174174
});
175175
});
@@ -200,7 +200,7 @@ describe('ConfigLoader Tests', () => {
200200
});
201201

202202
await expect(ConfigWebhookEventBridge.load()).rejects.toThrow(
203-
'Failed to load config: Environment variable for eventBusName is not set and no default value provided., Failed to load parameter for webhookSecret from path undefined: Parameter undefined not found',
203+
'Failed to load config: Environment variable for eventBusName is not set and no default value provided., Failed to load parameter for webhookSecret from path undefined: Parameter undefined not found', // eslint-disable-line max-len
204204
);
205205
});
206206
});
@@ -240,7 +240,7 @@ describe('ConfigLoader Tests', () => {
240240
});
241241

242242
await expect(ConfigDispatcher.load()).rejects.toThrow(
243-
'Failed to load config: Failed to load parameter for matcherConfig from path undefined: Parameter undefined not found',
243+
'Failed to load config: Failed to load parameter for matcherConfig from path undefined: Parameter undefined not found', // eslint-disable-line max-len
244244
);
245245
});
246246

@@ -256,7 +256,6 @@ describe('ConfigLoader Tests', () => {
256256
});
257257

258258
await expect(ConfigDispatcher.load()).rejects.toThrow('ailed to load config: Matcher config is empty');
259-
260259
});
261260
});
262261
});

lambdas/functions/webhook/src/ConfigLoader.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { getParameter } from '@aws-github-runner/aws-ssm-util';
2-
import { MatcherConfig, RunnerMatcherConfig } from './sqs';
2+
import { RunnerMatcherConfig } from './sqs';
33
import { logger } from '@aws-github-runner/aws-powertools-util';
44

55
abstract class BaseConfig {
@@ -36,7 +36,7 @@ abstract class BaseConfig {
3636
protected loadEnvVar<T>(envVar: string, propertyName: keyof this, defaultValue?: T): void {
3737
logger.debug(`Loading env var for ${String(propertyName)}`, { envVar });
3838
if (envVar !== undefined) {
39-
this.loadProperty<T>(propertyName, envVar);
39+
this.loadProperty(propertyName, envVar);
4040
} else if (defaultValue !== undefined) {
4141
this[propertyName] = defaultValue as unknown as this[keyof this];
4242
} else {
@@ -45,22 +45,22 @@ abstract class BaseConfig {
4545
}
4646
}
4747

48-
protected async loadParameter<T>(paramPath: string, propertyName: keyof this): Promise<void> {
48+
protected async loadParameter(paramPath: string, propertyName: keyof this): Promise<void> {
4949
logger.debug(`Loading parameter for ${String(propertyName)} from path ${paramPath}`);
5050
await getParameter(paramPath)
5151
.then((value) => {
52-
this.loadProperty<T>(propertyName, value);
52+
this.loadProperty(propertyName, value);
5353
})
5454
.catch((error) => {
55-
const errorMessage = `Failed to load parameter for ${String(propertyName)} from path ${paramPath}: ${(error as Error).message}`;
55+
const errorMessage = `Failed to load parameter for ${String(propertyName)} from path ${paramPath}: ${(error as Error).message}`; // eslint-disable-line max-len
5656
this.configLoadingErrors.push(errorMessage);
5757
});
5858
}
5959

60-
private loadProperty<T>(propertyName: keyof this, value: string) {
60+
private loadProperty(propertyName: keyof this, value: string) {
6161
try {
6262
this[propertyName] = JSON.parse(value) as unknown as this[keyof this];
63-
} catch (error) {
63+
} catch {
6464
this[propertyName] = value as unknown as this[keyof this];
6565
}
6666
}
@@ -89,7 +89,7 @@ export class ConfigWebhook extends BaseConfig {
8989
this.loadEnvVar(process.env.SQS_WORKFLOW_JOB_QUEUE, 'workflowJobEventSecondaryQueue', '');
9090

9191
await Promise.all([
92-
this.loadParameter<MatcherConfig[]>(process.env.PARAMETER_RUNNER_MATCHER_CONFIG_PATH, 'matcherConfig'),
92+
this.loadParameter(process.env.PARAMETER_RUNNER_MATCHER_CONFIG_PATH, 'matcherConfig'),
9393
this.loadParameter(process.env.PARAMETER_GITHUB_APP_WEBHOOK_SECRET, 'webhookSecret'),
9494
]);
9595
}
@@ -114,7 +114,7 @@ export class ConfigDispatcher extends BaseConfig {
114114

115115
async loadConfig(): Promise<void> {
116116
this.loadEnvVar(process.env.REPOSITORY_ALLOW_LIST, 'repositoryAllowList', []);
117-
await this.loadParameter<MatcherConfig[]>(process.env.PARAMETER_RUNNER_MATCHER_CONFIG_PATH, 'matcherConfig');
117+
await this.loadParameter(process.env.PARAMETER_RUNNER_MATCHER_CONFIG_PATH, 'matcherConfig');
118118

119119
// check matcherConfig object is not empty
120120
if (this.matcherConfig.length === 0) {

lambdas/functions/webhook/src/lambda.test.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import ValidationError from './ValidationError';
99
import { getParameter } from '@aws-github-runner/aws-ssm-util';
1010
import { dispatch } from './runners/dispatch';
1111
import { EventWrapper } from './types';
12-
import { rejects } from 'assert';
1312

1413
const event: APIGatewayEvent = {
1514
body: JSON.stringify(''),
@@ -77,7 +76,6 @@ const context: Context = {
7776
},
7877
};
7978

80-
8179
jest.mock('./runners/dispatch');
8280
jest.mock('./webhook');
8381
jest.mock('@aws-github-runner/aws-ssm-util');
@@ -122,8 +120,6 @@ describe('Test webhook lambda wrapper.', () => {
122120
});
123121
});
124122

125-
126-
127123
describe('Lmmbda eventBridgeWebhook.', () => {
128124
beforeEach(() => {
129125
process.env.EVENT_BUS_NAME = 'test';
@@ -141,7 +137,6 @@ describe('Test webhook lambda wrapper.', () => {
141137
expect(result).toEqual({ body: 'test', statusCode: 200 });
142138
});
143139

144-
145140
it('Reject events .', async () => {
146141
const mock = mocked(publishOnEventBridge);
147142
mock.mockRejectedValue(new Error('some error'));
@@ -190,10 +185,11 @@ describe('Test webhook lambda wrapper.', () => {
190185
'detail-type': 'non_workflow_job',
191186
} as unknown as EventWrapper<WorkflowJobEvent>;
192187

193-
await expect(dispatchToRunners(testEvent, context)).rejects.toThrow('Incorrect Event detail-type only workflow_job is accepted');
188+
await expect(dispatchToRunners(testEvent, context)).rejects.toThrow(
189+
'Incorrect Event detail-type only workflow_job is accepted',
190+
);
194191
});
195192

196-
197193
it('Rejects any event causing an error.', async () => {
198194
const mock = mocked(dispatch);
199195
mock.mockRejectedValue(new Error('some error'));

0 commit comments

Comments
 (0)