From 4b2f504712d7d5f575063c0c9c4e34c6681cc859 Mon Sep 17 00:00:00 2001 From: Fedor Dikarev Date: Thu, 23 Jan 2025 15:46:10 +0100 Subject: [PATCH] pre-checkin Signed-off-by: Fedor Dikarev --- __tests__/retries_fail.test.ts | 47 +++++++++++-------------------- __tests__/retries_success.test.ts | 34 ++++++++++------------ 2 files changed, 32 insertions(+), 49 deletions(-) diff --git a/__tests__/retries_fail.test.ts b/__tests__/retries_fail.test.ts index 12e2804..29ea9c9 100644 --- a/__tests__/retries_fail.test.ts +++ b/__tests__/retries_fail.test.ts @@ -1,60 +1,47 @@ -import { expect, jest, test, } from '@jest/globals'; +import {expect, jest, test} from '@jest/globals'; // import * as dockerModule from '../src/docker'; -import { login } from '../src/docker'; -import { Docker } from '@docker/actions-toolkit/lib/docker/docker'; +import {login} from '../src/docker'; +import {Docker} from '@docker/actions-toolkit/lib/docker/docker'; test('login retries function', async () => { - let stderr_strings: string[] = [] - let call_count: number = -1 + let stderr_strings: string[] = []; + let call_count: number = -1; // const execSpy = jest.spyOn(Docker, 'getExecOutput').mockImplementation(async () => { Docker.getExecOutput = jest.fn(async () => { - call_count++ - console.log(`Mock: ${call_count}, ${stderr_strings}`) + call_count++; + console.log(`Mock: ${call_count}, ${stderr_strings}`); if (call_count >= stderr_strings.length) { return { exitCode: 0, stdout: 'Mock success', stderr: '' - } + }; } return { exitCode: 1, stdout: '', - stderr: stderr_strings[(call_count) % stderr_strings.length] - } - }) + stderr: stderr_strings[call_count % stderr_strings.length] + }; + }); const username = 'dbowie'; const password = 'groundcontrol'; const registry = 'https://ghcr.io'; - stderr_strings = [ - 'mock error, failed with status: 408 Request Timeout', - 'mock error, failed with status: 502 Request Timeout', - 'mock error, failed with status: 400 Request Timeout', - ] - call_count = -1 + stderr_strings = ['mock error, failed with status: 408 Request Timeout', 'mock error, failed with status: 502 Request Timeout', 'mock error, failed with status: 400 Request Timeout']; + call_count = -1; await expect(async () => { await login(registry, username, password, 'false', ['408', '400'], 5, 0.1); - }) - .rejects - .toThrow("mock error, failed with status: 502 Request Timeout"); + }).rejects.toThrow('mock error, failed with status: 502 Request Timeout'); expect(Docker.getExecOutput).toHaveBeenCalledTimes(2); - stderr_strings = [ - 'not matching error', - 'mock error, failed with status: 502 Request Timeout', - 'mock error, failed with status: 400 Request Timeout', - ] - call_count = -1 + stderr_strings = ['not matching error', 'mock error, failed with status: 502 Request Timeout', 'mock error, failed with status: 400 Request Timeout']; + call_count = -1; await expect(async () => { await login(registry, username, password, 'false', ['408', '400'], 5, 0.1); - }) - .rejects - .toThrow('not matching error'); + }).rejects.toThrow('not matching error'); expect(Docker.getExecOutput).toHaveBeenCalledTimes(2 + 1); - }); diff --git a/__tests__/retries_success.test.ts b/__tests__/retries_success.test.ts index 9450429..98102a2 100644 --- a/__tests__/retries_success.test.ts +++ b/__tests__/retries_success.test.ts @@ -1,44 +1,40 @@ -import { expect, jest, test, } from '@jest/globals'; +import {expect, jest, test} from '@jest/globals'; -import { login } from '../src/docker'; -import { Docker } from '@docker/actions-toolkit/lib/docker/docker'; +import {login} from '../src/docker'; +import {Docker} from '@docker/actions-toolkit/lib/docker/docker'; test('login retries success function', async () => { - let stderr_strings: string[] = [] - let call_count: number = -1 + let stderr_strings: string[] = []; + let call_count: number = -1; Docker.getExecOutput = jest.fn(async () => { - call_count++ - console.log(`Mock: ${call_count}, ${stderr_strings}`) + call_count++; + console.log(`Mock: ${call_count}, ${stderr_strings}`); if (call_count >= stderr_strings.length) { return { exitCode: 0, stdout: 'Mock success', stderr: '' - } + }; } return { exitCode: 1, stdout: '', - stderr: stderr_strings[(call_count) % stderr_strings.length] - } - }) + stderr: stderr_strings[call_count % stderr_strings.length] + }; + }); const username = 'dbowie'; const password = 'groundcontrol'; const registry = 'https://ghcr.io'; - stderr_strings = [] - call_count = -1 + stderr_strings = []; + call_count = -1; await login(registry, username, password, 'false', ['408', '502', '400'], 5, 0.1); expect(Docker.getExecOutput).toHaveBeenCalledTimes(1); - stderr_strings = [ - 'mock error, failed with status: 408 Request Timeout', - 'mock error, failed with status: 502 Request Timeout', - 'mock error, failed with status: 400 Request Timeout', - ] - call_count = -1 + stderr_strings = ['mock error, failed with status: 408 Request Timeout', 'mock error, failed with status: 502 Request Timeout', 'mock error, failed with status: 400 Request Timeout']; + call_count = -1; await login(registry, username, password, 'false', ['408', '502', '400'], 5, 0.1); expect(Docker.getExecOutput).toHaveBeenCalledTimes(1 + 4); });