Skip to content

Commit

Permalink
fix(syncer): Add tests, coverage report, and refactor lambda / naming (
Browse files Browse the repository at this point in the history
…philips-labs#1478)

* fix: Add tests, coverage report, and refactor lambda / naming

* fix: Add tests, coverage report, and refactor lambda / naming
  • Loading branch information
npalm authored Dec 6, 2021
1 parent de5b93f commit 8266442
Show file tree
Hide file tree
Showing 9 changed files with 360 additions and 430 deletions.
6 changes: 2 additions & 4 deletions .github/workflows/lambda-runner-binaries-syncer.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
name: Lambda Syncer
env:
lambda_name: runner-binaries-syncer
lambda_path: modules/runner-binaries-syncer/lambdas/runner-binaries-syncer
on:
push:
branches:
Expand All @@ -17,7 +14,8 @@ jobs:
container: node:14
defaults:
run:
working-directory: ${{ env.lambda_path }}
working-directory: modules/runner-binaries-syncer/lambdas/runner-binaries-syncer

steps:
- uses: actions/checkout@v2
- name: Install dependencies
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
collectCoverage: true,
collectCoverageFrom: ['src/**/*.{ts,js,jsx}', '!src/**/*local*.ts'],
coverageThreshold: {
global: {
branches: 80,
functions: 60,
lines: 80,
statements: 80
}
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"typescript": "^4.5.2"
},
"dependencies": {
"tslog": "^3.3.0",
"axios": "^0.24.0"
"axios": "^0.24.0",
"tslog": "^3.3.0"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { sync } from './syncer/syncer';
import { handler } from './lambda';
import { mocked } from 'ts-jest/utils';

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

describe('Test scale up lambda wrapper.', () => {
it('Scale without error should resolve.', async () => {
const mock = mocked(sync);
mock.mockImplementation(() => {
return new Promise((resolve) => {
resolve();
});
});
await expect(handler({}, {})).resolves;
});

it('Scale without error should resolve2 . ', async () => {
const mock = mocked(sync);
mock.mockRejectedValue(new Error(''));

await expect(handler({}, {})).resolves;
});
});
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
import { handle } from './syncer/handler';
import { sync } from './syncer/syncer';
import { logger } from './syncer/logger';

// eslint-disable-next-line
export const handler = async (event: any, context: any, callback: any): Promise<void> => {
export const handler = async (event: any, context: any): Promise<void> => {
logger.setSettings({ requestId: context.awsRequestId });
logger.debug(JSON.stringify(event));
try {
await handle();
callback(null);
} catch (e) {
callback(e);
}

return new Promise((resolve) => {
sync()
.then(() => resolve())
.catch((e: Error) => {
logger.warn('Ignoring error:', e);
resolve();
});
});
};
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { handle } from './syncer/handler';
import { sync } from './syncer/syncer';

handle()
sync()
.then()
.catch((e) => {
console.log(e);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { handle } from './handler';
import { sync } from './syncer';
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';
Expand Down Expand Up @@ -47,7 +47,7 @@ describe('Synchronize action distribution.', () => {
};
});

await handle();
await sync();
expect(mockOctokit.repos.listReleases).toBeCalledTimes(1);
expect(mockS3.getObjectTagging).toBeCalledWith({
Bucket: bucketName,
Expand All @@ -71,7 +71,7 @@ describe('Synchronize action distribution.', () => {
};
});

await handle();
await sync();
expect(mockOctokit.repos.listReleases).toBeCalledTimes(1);
expect(mockS3.getObjectTagging).toBeCalledWith({
Bucket: bucketName,
Expand All @@ -90,7 +90,7 @@ describe('Synchronize action distribution.', () => {
};
});

await handle();
await sync();
expect(mockOctokit.repos.listReleases).toBeCalledTimes(1);
expect(mockS3.getObjectTagging).toBeCalledWith({
Bucket: bucketName,
Expand All @@ -108,7 +108,7 @@ describe('Synchronize action distribution.', () => {
};
});

await handle();
await sync();
expect(mockOctokit.repos.listReleases).toBeCalledTimes(1);
expect(mockS3.getObjectTagging).toBeCalledWith({
Bucket: bucketName,
Expand All @@ -134,7 +134,7 @@ describe('Synchronize action distribution.', () => {
};
});

await handle();
await sync();
expect(mockOctokit.repos.listReleases).toBeCalledTimes(1);
expect(mockS3.getObjectTagging).toBeCalledWith({
Bucket: bucketName,
Expand All @@ -155,7 +155,7 @@ describe('Synchronize action distribution.', () => {
};
});

await handle();
await sync();
expect(mockOctokit.repos.listReleases).toBeCalledTimes(1);
expect(mockS3.getObjectTagging).toBeCalledWith({
Bucket: bucketName,
Expand Down Expand Up @@ -183,7 +183,7 @@ describe('Synchronize action distribution.', () => {
};
});

await handle();
await sync();
expect(mockOctokit.repos.listReleases).toBeCalledTimes(1);
expect(mockS3.getObjectTagging).toBeCalledWith({
Bucket: bucketName,
Expand All @@ -203,7 +203,7 @@ describe('Synchronize action distribution.', () => {
};
});

await handle();
await sync();
expect(mockOctokit.repos.listReleases).toBeCalledTimes(1);
expect(mockS3.getObjectTagging).toBeCalledWith({
Bucket: bucketName,
Expand All @@ -221,7 +221,7 @@ describe('Synchronize action distribution.', () => {
};
});

await handle();
await sync();
expect(mockOctokit.repos.listReleases).toBeCalledTimes(1);
expect(mockS3.getObjectTagging).toBeCalledWith({
Bucket: bucketName,
Expand All @@ -243,15 +243,15 @@ describe('No release assets found.', () => {
data: listReleasesEmpty,
}));

await expect(handle()).rejects.toThrow(errorMessage);
await expect(sync()).rejects.toThrow(errorMessage);
});

it('No linux x64 asset.', async () => {
mockOctokit.repos.listReleases.mockImplementation(() => ({
data: [listReleasesNoLinux],
}));

await expect(handle()).rejects.toThrow(errorMessage);
await expect(sync()).rejects.toThrow(errorMessage);
});
});

Expand All @@ -260,17 +260,17 @@ describe('Invalid config', () => {
it('No bucket and object key.', async () => {
delete process.env.S3_OBJECT_KEY;
delete process.env.S3_BUCKET_NAME;
await expect(handle()).rejects.toThrow(errorMessage);
await expect(sync()).rejects.toThrow(errorMessage);
});
it('No bucket.', async () => {
delete process.env.S3_BUCKET_NAME;
process.env.S3_OBJECT_KEY = bucketObjectKey;
await expect(handle()).rejects.toThrow(errorMessage);
await expect(sync()).rejects.toThrow(errorMessage);
});
it('No object key.', async () => {
delete process.env.S3_OBJECT_KEY;
process.env.S3_BUCKET_NAME = bucketName;
await expect(handle()).rejects.toThrow(errorMessage);
await expect(sync()).rejects.toThrow(errorMessage);
});
});

Expand All @@ -287,6 +287,6 @@ describe('Synchronize action distribution for arm64.', () => {
data: [listReleasesNoArm64],
}));

await expect(handle()).rejects.toThrow(errorMessage);
await expect(sync()).rejects.toThrow(errorMessage);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ async function uploadToS3(s3: S3, cacheObject: CacheObject, actionRunnerReleaseA
});
}

export const handle = async (): Promise<void> => {
export async function sync(): Promise<void> {
const s3 = new AWS.S3();

const runnerArch = process.env.GITHUB_RUNNER_ARCHITECTURE || 'x64';
Expand All @@ -129,4 +129,4 @@ export const handle = async (): Promise<void> => {
} else {
logger.debug('Distribution is up-to-date, no action.');
}
};
}
Loading

0 comments on commit 8266442

Please sign in to comment.