Skip to content
This repository was archived by the owner on Jan 16, 2025. It is now read-only.

Commit f1b99d9

Browse files
authored
fix: Download lambda (#1480)
* fix: Download lambda * Add missing await * fix tests, fix coverage * process review
1 parent 8266442 commit f1b99d9

File tree

5 files changed

+33
-16
lines changed

5 files changed

+33
-16
lines changed

Diff for: modules/runner-binaries-syncer/lambdas/runner-binaries-syncer/jest.config.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ module.exports = {
66
coverageThreshold: {
77
global: {
88
branches: 80,
9-
functions: 60,
9+
functions: 80,
1010
lines: 80,
1111
statements: 80
1212
}

Diff for: modules/runner-binaries-syncer/lambdas/runner-binaries-syncer/src/lambda.test.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import { mocked } from 'ts-jest/utils';
44

55
jest.mock('./syncer/syncer');
66

7-
describe('Test scale up lambda wrapper.', () => {
8-
it('Scale without error should resolve.', async () => {
7+
describe('Test download sync wrapper.', () => {
8+
it('Test successful download.', async () => {
99
const mock = mocked(sync);
1010
mock.mockImplementation(() => {
1111
return new Promise((resolve) => {
@@ -15,7 +15,7 @@ describe('Test scale up lambda wrapper.', () => {
1515
await expect(handler({}, {})).resolves;
1616
});
1717

18-
it('Scale without error should resolve2 . ', async () => {
18+
it('Test wrapper with returning an error. ', async () => {
1919
const mock = mocked(sync);
2020
mock.mockRejectedValue(new Error(''));
2121

Diff for: modules/runner-binaries-syncer/lambdas/runner-binaries-syncer/src/lambda.ts

+7-10
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,13 @@ import { sync } from './syncer/syncer';
22
import { logger } from './syncer/logger';
33

44
// eslint-disable-next-line
5-
export const handler = async (event: any, context: any): Promise<void> => {
5+
export async function handler(event: any, context: any): Promise<void> {
66
logger.setSettings({ requestId: context.awsRequestId });
77
logger.debug(JSON.stringify(event));
88

9-
return new Promise((resolve) => {
10-
sync()
11-
.then(() => resolve())
12-
.catch((e: Error) => {
13-
logger.warn('Ignoring error:', e);
14-
resolve();
15-
});
16-
});
17-
};
9+
try {
10+
await sync();
11+
} catch (e) {
12+
logger.warn('Ignoring error:', e);
13+
}
14+
}

Diff for: modules/runner-binaries-syncer/lambdas/runner-binaries-syncer/src/syncer/syncer.test.ts

+21-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ import listReleases from '../../test/resources/github-list-releases.json';
33
import listReleasesEmpty from '../../test/resources/github-list-releases-empty-assets.json';
44
import listReleasesNoLinux from '../../test/resources/github-list-releases-no-linux.json';
55
import listReleasesNoArm64 from '../../test/resources/github-list-releases-no-arm64.json';
6+
import { S3 } from 'aws-sdk';
7+
import axios from 'axios';
8+
import { request } from 'http';
9+
import { EventEmitter, PassThrough, Readable } from 'stream';
610

711
const mockOctokit = {
812
repos: {
@@ -13,9 +17,23 @@ jest.mock('@octokit/rest', () => ({
1317
Octokit: jest.fn().mockImplementation(() => mockOctokit),
1418
}));
1519

20+
// mock stream for Axios
21+
const mockResponse = `{"data": 123}`;
22+
const mockStream = new PassThrough();
23+
mockStream.push(mockResponse);
24+
mockStream.end();
25+
26+
jest.mock('axios');
27+
const mockedAxios = axios as jest.Mocked<typeof axios>;
28+
mockedAxios.request.mockResolvedValue({
29+
data: mockStream,
30+
});
31+
1632
const mockS3 = {
1733
getObjectTagging: jest.fn(),
18-
upload: jest.fn(),
34+
upload: jest.fn().mockImplementation(() => {
35+
return { promise: jest.fn(() => Promise.resolve()) };
36+
}),
1937
};
2038
jest.mock('aws-sdk', () => ({
2139
S3: jest.fn().mockImplementation(() => mockS3),
@@ -27,6 +45,8 @@ beforeEach(() => {
2745
jest.clearAllMocks();
2846
});
2947

48+
jest.setTimeout(60 * 1000);
49+
3050
describe('Synchronize action distribution.', () => {
3151
beforeEach(() => {
3252
process.env.S3_BUCKET_NAME = bucketName;

Diff for: modules/runner-binaries-syncer/lambdas/runner-binaries-syncer/src/syncer/syncer.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ export async function sync(): Promise<void> {
125125
const currentVersion = await getCachedVersion(s3, cacheObject);
126126
logger.debug('latest: ' + currentVersion);
127127
if (currentVersion === undefined || currentVersion != actionRunnerReleaseAsset.name) {
128-
uploadToS3(s3, cacheObject, actionRunnerReleaseAsset);
128+
await uploadToS3(s3, cacheObject, actionRunnerReleaseAsset);
129129
} else {
130130
logger.debug('Distribution is up-to-date, no action.');
131131
}

0 commit comments

Comments
 (0)