pre-checkin

Signed-off-by: Fedor Dikarev <fedor.dikarev@gmail.com>
This commit is contained in:
Fedor Dikarev 2025-01-23 15:46:10 +01:00
parent 6cade5565f
commit 4b2f504712
2 changed files with 32 additions and 49 deletions

View file

@ -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 * as dockerModule from '../src/docker';
import { login } from '../src/docker'; import {login} from '../src/docker';
import { Docker } from '@docker/actions-toolkit/lib/docker/docker'; import {Docker} from '@docker/actions-toolkit/lib/docker/docker';
test('login retries function', async () => { test('login retries function', async () => {
let stderr_strings: string[] = [] let stderr_strings: string[] = [];
let call_count: number = -1 let call_count: number = -1;
// const execSpy = jest.spyOn(Docker, 'getExecOutput').mockImplementation(async () => { // const execSpy = jest.spyOn(Docker, 'getExecOutput').mockImplementation(async () => {
Docker.getExecOutput = jest.fn(async () => { Docker.getExecOutput = jest.fn(async () => {
call_count++ call_count++;
console.log(`Mock: ${call_count}, ${stderr_strings}`) console.log(`Mock: ${call_count}, ${stderr_strings}`);
if (call_count >= stderr_strings.length) { if (call_count >= stderr_strings.length) {
return { return {
exitCode: 0, exitCode: 0,
stdout: 'Mock success', stdout: 'Mock success',
stderr: '' stderr: ''
} };
} }
return { return {
exitCode: 1, exitCode: 1,
stdout: '', stdout: '',
stderr: stderr_strings[(call_count) % stderr_strings.length] stderr: stderr_strings[call_count % stderr_strings.length]
} };
}) });
const username = 'dbowie'; const username = 'dbowie';
const password = 'groundcontrol'; const password = 'groundcontrol';
const registry = 'https://ghcr.io'; const registry = 'https://ghcr.io';
stderr_strings = [ 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'];
'mock error, failed with status: 408 Request Timeout', call_count = -1;
'mock error, failed with status: 502 Request Timeout',
'mock error, failed with status: 400 Request Timeout',
]
call_count = -1
await expect(async () => { await expect(async () => {
await login(registry, username, password, 'false', ['408', '400'], 5, 0.1); 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); expect(Docker.getExecOutput).toHaveBeenCalledTimes(2);
stderr_strings = [ stderr_strings = ['not matching error', 'mock error, failed with status: 502 Request Timeout', 'mock error, failed with status: 400 Request Timeout'];
'not matching error', call_count = -1;
'mock error, failed with status: 502 Request Timeout',
'mock error, failed with status: 400 Request Timeout',
]
call_count = -1
await expect(async () => { await expect(async () => {
await login(registry, username, password, 'false', ['408', '400'], 5, 0.1); 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); expect(Docker.getExecOutput).toHaveBeenCalledTimes(2 + 1);
}); });

View file

@ -1,44 +1,40 @@
import { expect, jest, test, } from '@jest/globals'; import {expect, jest, test} from '@jest/globals';
import { login } from '../src/docker'; import {login} from '../src/docker';
import { Docker } from '@docker/actions-toolkit/lib/docker/docker'; import {Docker} from '@docker/actions-toolkit/lib/docker/docker';
test('login retries success function', async () => { test('login retries success function', async () => {
let stderr_strings: string[] = [] let stderr_strings: string[] = [];
let call_count: number = -1 let call_count: number = -1;
Docker.getExecOutput = jest.fn(async () => { Docker.getExecOutput = jest.fn(async () => {
call_count++ call_count++;
console.log(`Mock: ${call_count}, ${stderr_strings}`) console.log(`Mock: ${call_count}, ${stderr_strings}`);
if (call_count >= stderr_strings.length) { if (call_count >= stderr_strings.length) {
return { return {
exitCode: 0, exitCode: 0,
stdout: 'Mock success', stdout: 'Mock success',
stderr: '' stderr: ''
} };
} }
return { return {
exitCode: 1, exitCode: 1,
stdout: '', stdout: '',
stderr: stderr_strings[(call_count) % stderr_strings.length] stderr: stderr_strings[call_count % stderr_strings.length]
} };
}) });
const username = 'dbowie'; const username = 'dbowie';
const password = 'groundcontrol'; const password = 'groundcontrol';
const registry = 'https://ghcr.io'; const registry = 'https://ghcr.io';
stderr_strings = [] stderr_strings = [];
call_count = -1 call_count = -1;
await login(registry, username, password, 'false', ['408', '502', '400'], 5, 0.1); await login(registry, username, password, 'false', ['408', '502', '400'], 5, 0.1);
expect(Docker.getExecOutput).toHaveBeenCalledTimes(1); expect(Docker.getExecOutput).toHaveBeenCalledTimes(1);
stderr_strings = [ 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'];
'mock error, failed with status: 408 Request Timeout', call_count = -1;
'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); await login(registry, username, password, 'false', ['408', '502', '400'], 5, 0.1);
expect(Docker.getExecOutput).toHaveBeenCalledTimes(1 + 4); expect(Docker.getExecOutput).toHaveBeenCalledTimes(1 + 4);
}); });