Skip to content

Commit

Permalink
fix: Download lambda (#1480)
Browse files Browse the repository at this point in the history
* fix: Download lambda

* Add missing await

* fix tests, fix coverage

* process review
  • Loading branch information
npalm committed Dec 7, 2021
1 parent 8266442 commit f1b99d9
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module.exports = {
coverageThreshold: {
global: {
branches: 80,
functions: 60,
functions: 80,
lines: 80,
statements: 80
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand All @@ -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(''));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<void> => {
export async function handler(event: any, context: any): Promise<void> {
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand All @@ -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<typeof axios>;
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),
Expand All @@ -27,6 +45,8 @@ beforeEach(() => {
jest.clearAllMocks();
});

jest.setTimeout(60 * 1000);

describe('Synchronize action distribution.', () => {
beforeEach(() => {
process.env.S3_BUCKET_NAME = bucketName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ export async function sync(): Promise<void> {
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.');
}
Expand Down

0 comments on commit f1b99d9

Please sign in to comment.