From ba504f8cc5866cd406615fb8448a3ef67b2aeded Mon Sep 17 00:00:00 2001
From: Niek Palm <dev.npalm@gmail.com>
Date: Mon, 6 Dec 2021 17:34:31 +0100
Subject: [PATCH 1/4] fix: Download lambda

---
 .../runner-binaries-syncer/jest.config.js     |  2 +-
 .../runner-binaries-syncer/src/lambda.test.ts |  4 ++--
 .../runner-binaries-syncer/src/lambda.ts      | 19 +++++++++----------
 3 files changed, 12 insertions(+), 13 deletions(-)

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..a8d3c32a4a 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: 56,
       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..ac927ef829 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
@@ -12,13 +12,13 @@ describe('Test scale up lambda wrapper.', () => {
         resolve();
       });
     });
-    await expect(handler({}, {})).resolves;
+    await expect(handler({}, {}, {})).resolves;
   });
 
   it('Scale without error should resolve2 . ', async () => {
     const mock = mocked(sync);
     mock.mockRejectedValue(new Error(''));
 
-    await expect(handler({}, {})).resolves;
+    await expect(handler({}, {}, {})).resolves;
   });
 });
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..f0193ed6d3 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,15 @@ 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, callback: 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();
+    callback(null);
+  } catch (e) {
+    logger.warn('Ignoring error:', e);
+    callback(e);
+  }
+}

From 21bd3506fec5047b77c4d46bd11acca5b0cdac1a Mon Sep 17 00:00:00 2001
From: Niek Palm <dev.npalm@gmail.com>
Date: Tue, 7 Dec 2021 11:06:23 +0100
Subject: [PATCH 2/4] Add missing await

---
 .../lambdas/runner-binaries-syncer/src/lambda.test.ts         | 4 ++--
 .../lambdas/runner-binaries-syncer/src/lambda.ts              | 4 +---
 .../lambdas/runner-binaries-syncer/src/syncer/syncer.ts       | 2 +-
 3 files changed, 4 insertions(+), 6 deletions(-)

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 ac927ef829..09086385d8 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
@@ -12,13 +12,13 @@ describe('Test scale up lambda wrapper.', () => {
         resolve();
       });
     });
-    await expect(handler({}, {}, {})).resolves;
+    await expect(handler({}, {})).resolves;
   });
 
   it('Scale without error should resolve2 . ', async () => {
     const mock = mocked(sync);
     mock.mockRejectedValue(new Error(''));
 
-    await expect(handler({}, {}, {})).resolves;
+    await expect(handler({}, {})).resolves;
   });
 });
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 f0193ed6d3..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,15 +2,13 @@ import { sync } from './syncer/syncer';
 import { logger } from './syncer/logger';
 
 // eslint-disable-next-line
-export async function handler(event: any, context: any, callback: any): Promise<void> {
+export async function handler(event: any, context: any): Promise<void> {
   logger.setSettings({ requestId: context.awsRequestId });
   logger.debug(JSON.stringify(event));
 
   try {
     await sync();
-    callback(null);
   } catch (e) {
     logger.warn('Ignoring error:', e);
-    callback(e);
   }
 }
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<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.');
   }

From eced3745ff8246b4b178f2143b24cb690fee126f Mon Sep 17 00:00:00 2001
From: Niek Palm <dev.npalm@gmail.com>
Date: Tue, 7 Dec 2021 15:13:27 +0100
Subject: [PATCH 3/4] fix tests, fix coverage

---
 .../runner-binaries-syncer/jest.config.js     |  2 +-
 .../runner-binaries-syncer/src/lambda.test.ts |  6 ++--
 .../src/syncer/syncer.test.ts                 | 29 +++++++++++++++++--
 3 files changed, 30 insertions(+), 7 deletions(-)

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 a8d3c32a4a..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: 56,
+      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/syncer/syncer.test.ts b/modules/runner-binaries-syncer/lambdas/runner-binaries-syncer/src/syncer/syncer.test.ts
index 33851f5123..0282c5df0b 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,26 @@ 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(() => {
+  //   promise: jest.fn();
+  // }),
+  upload: jest.fn().mockImplementation(() => {
+    return { promise: jest.fn(() => Promise.resolve()) };
+  }),
 };
 jest.mock('aws-sdk', () => ({
   S3: jest.fn().mockImplementation(() => mockS3),
@@ -27,6 +48,8 @@ beforeEach(() => {
   jest.clearAllMocks();
 });
 
+jest.setTimeout(60 * 1000);
+
 describe('Synchronize action distribution.', () => {
   beforeEach(() => {
     process.env.S3_BUCKET_NAME = bucketName;
@@ -190,8 +213,8 @@ describe('Synchronize action distribution.', () => {
       Key: bucketObjectKey,
     });
     expect(mockS3.upload).toBeCalledTimes(1);
-    const s3JsonBody = mockS3.upload.mock.calls[0][0];
-    expect(s3JsonBody['Tagging']).toEqual('name=actions-runner-linux-x64-2.273.0.tar.gz');
+    //const s3JsonBody = mockS3.upload.mock.calls[0][0];
+    //expect(s3JsonBody['Tagging']).toEqual('name=actions-runner-linux-x64-2.273.0.tar.gz');
   });
 
   it('No tag in S3, distribution should update.', async () => {

From 96aae33775e178aa03ef12b1da51232dec1d434c Mon Sep 17 00:00:00 2001
From: Niek Palm <dev.npalm@gmail.com>
Date: Tue, 7 Dec 2021 15:25:48 +0100
Subject: [PATCH 4/4] process review

---
 .../runner-binaries-syncer/src/syncer/syncer.test.ts       | 7 ++-----
 1 file changed, 2 insertions(+), 5 deletions(-)

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 0282c5df0b..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
@@ -31,9 +31,6 @@ mockedAxios.request.mockResolvedValue({
 
 const mockS3 = {
   getObjectTagging: jest.fn(),
-  // upload: jest.fn(() => {
-  //   promise: jest.fn();
-  // }),
   upload: jest.fn().mockImplementation(() => {
     return { promise: jest.fn(() => Promise.resolve()) };
   }),
@@ -213,8 +210,8 @@ describe('Synchronize action distribution.', () => {
       Key: bucketObjectKey,
     });
     expect(mockS3.upload).toBeCalledTimes(1);
-    //const s3JsonBody = mockS3.upload.mock.calls[0][0];
-    //expect(s3JsonBody['Tagging']).toEqual('name=actions-runner-linux-x64-2.273.0.tar.gz');
+    const s3JsonBody = mockS3.upload.mock.calls[0][0];
+    expect(s3JsonBody['Tagging']).toEqual('name=actions-runner-linux-x64-2.273.0.tar.gz');
   });
 
   it('No tag in S3, distribution should update.', async () => {