diff --git a/modules/runner-binaries-syncer/lambdas/runner-binaries-syncer/jest.config.js b/modules/runner-binaries-syncer/lambdas/runner-binaries-syncer/jest.config.js index 07ccfd28a8..9a46cb29ff 100644 --- a/modules/runner-binaries-syncer/lambdas/runner-binaries-syncer/jest.config.js +++ b/modules/runner-binaries-syncer/lambdas/runner-binaries-syncer/jest.config.js @@ -6,7 +6,7 @@ module.exports = { coverageThreshold: { global: { branches: 80, - functions: 60, + functions: 80, lines: 80, statements: 80 } diff --git a/modules/runner-binaries-syncer/lambdas/runner-binaries-syncer/src/lambda.test.ts b/modules/runner-binaries-syncer/lambdas/runner-binaries-syncer/src/lambda.test.ts index 09086385d8..ccc3277f2d 100644 --- a/modules/runner-binaries-syncer/lambdas/runner-binaries-syncer/src/lambda.test.ts +++ b/modules/runner-binaries-syncer/lambdas/runner-binaries-syncer/src/lambda.test.ts @@ -4,8 +4,8 @@ import { mocked } from 'ts-jest/utils'; jest.mock('./syncer/syncer'); -describe('Test scale up lambda wrapper.', () => { - it('Scale without error should resolve.', async () => { +describe('Test download sync wrapper.', () => { + it('Test successful download.', async () => { const mock = mocked(sync); mock.mockImplementation(() => { return new Promise((resolve) => { @@ -15,7 +15,7 @@ describe('Test scale up lambda wrapper.', () => { await expect(handler({}, {})).resolves; }); - it('Scale without error should resolve2 . ', async () => { + it('Test wrapper with returning an error. ', async () => { const mock = mocked(sync); mock.mockRejectedValue(new Error('')); diff --git a/modules/runner-binaries-syncer/lambdas/runner-binaries-syncer/src/lambda.ts b/modules/runner-binaries-syncer/lambdas/runner-binaries-syncer/src/lambda.ts index 4f38cdfa72..12d53f26e5 100644 --- a/modules/runner-binaries-syncer/lambdas/runner-binaries-syncer/src/lambda.ts +++ b/modules/runner-binaries-syncer/lambdas/runner-binaries-syncer/src/lambda.ts @@ -2,16 +2,13 @@ import { sync } from './syncer/syncer'; import { logger } from './syncer/logger'; // eslint-disable-next-line -export const handler = async (event: any, context: any): Promise => { +export async function handler(event: any, context: any): Promise { logger.setSettings({ requestId: context.awsRequestId }); logger.debug(JSON.stringify(event)); - return new Promise((resolve) => { - sync() - .then(() => resolve()) - .catch((e: Error) => { - logger.warn('Ignoring error:', e); - resolve(); - }); - }); -}; + try { + await sync(); + } catch (e) { + logger.warn('Ignoring error:', e); + } +} diff --git a/modules/runner-binaries-syncer/lambdas/runner-binaries-syncer/src/syncer/syncer.test.ts b/modules/runner-binaries-syncer/lambdas/runner-binaries-syncer/src/syncer/syncer.test.ts index 33851f5123..fc17f1fef5 100644 --- a/modules/runner-binaries-syncer/lambdas/runner-binaries-syncer/src/syncer/syncer.test.ts +++ b/modules/runner-binaries-syncer/lambdas/runner-binaries-syncer/src/syncer/syncer.test.ts @@ -3,6 +3,10 @@ import listReleases from '../../test/resources/github-list-releases.json'; import listReleasesEmpty from '../../test/resources/github-list-releases-empty-assets.json'; import listReleasesNoLinux from '../../test/resources/github-list-releases-no-linux.json'; import listReleasesNoArm64 from '../../test/resources/github-list-releases-no-arm64.json'; +import { S3 } from 'aws-sdk'; +import axios from 'axios'; +import { request } from 'http'; +import { EventEmitter, PassThrough, Readable } from 'stream'; const mockOctokit = { repos: { @@ -13,9 +17,23 @@ jest.mock('@octokit/rest', () => ({ Octokit: jest.fn().mockImplementation(() => mockOctokit), })); +// mock stream for Axios +const mockResponse = `{"data": 123}`; +const mockStream = new PassThrough(); +mockStream.push(mockResponse); +mockStream.end(); + +jest.mock('axios'); +const mockedAxios = axios as jest.Mocked; +mockedAxios.request.mockResolvedValue({ + data: mockStream, +}); + const mockS3 = { getObjectTagging: jest.fn(), - upload: jest.fn(), + upload: jest.fn().mockImplementation(() => { + return { promise: jest.fn(() => Promise.resolve()) }; + }), }; jest.mock('aws-sdk', () => ({ S3: jest.fn().mockImplementation(() => mockS3), @@ -27,6 +45,8 @@ beforeEach(() => { jest.clearAllMocks(); }); +jest.setTimeout(60 * 1000); + describe('Synchronize action distribution.', () => { beforeEach(() => { process.env.S3_BUCKET_NAME = bucketName; diff --git a/modules/runner-binaries-syncer/lambdas/runner-binaries-syncer/src/syncer/syncer.ts b/modules/runner-binaries-syncer/lambdas/runner-binaries-syncer/src/syncer/syncer.ts index d7478cf5d9..4787bf7602 100644 --- a/modules/runner-binaries-syncer/lambdas/runner-binaries-syncer/src/syncer/syncer.ts +++ b/modules/runner-binaries-syncer/lambdas/runner-binaries-syncer/src/syncer/syncer.ts @@ -125,7 +125,7 @@ export async function sync(): Promise { const currentVersion = await getCachedVersion(s3, cacheObject); logger.debug('latest: ' + currentVersion); if (currentVersion === undefined || currentVersion != actionRunnerReleaseAsset.name) { - uploadToS3(s3, cacheObject, actionRunnerReleaseAsset); + await uploadToS3(s3, cacheObject, actionRunnerReleaseAsset); } else { logger.debug('Distribution is up-to-date, no action.'); }