2025-01-23 15:46:10 +01:00
|
|
|
import {expect, jest, test} from '@jest/globals';
|
2025-01-23 14:24:04 +01:00
|
|
|
|
2025-01-23 15:46:10 +01:00
|
|
|
import {login} from '../src/docker';
|
|
|
|
import {Docker} from '@docker/actions-toolkit/lib/docker/docker';
|
2025-01-23 14:24:04 +01:00
|
|
|
|
|
|
|
test('login retries function', async () => {
|
2025-01-23 17:07:51 +01:00
|
|
|
let stderrStrings: string[] = [];
|
|
|
|
let callCount: number = -1;
|
2025-01-23 14:24:04 +01:00
|
|
|
|
2025-01-23 16:51:59 +01:00
|
|
|
// using spyOn() here isn't enough, as we alter the logic
|
|
|
|
// so use `jest.fn()` here for the `Docker.getExecOutput`
|
2025-01-23 14:24:04 +01:00
|
|
|
Docker.getExecOutput = jest.fn(async () => {
|
2025-01-23 17:07:51 +01:00
|
|
|
callCount++;
|
|
|
|
console.log(`Mock: ${callCount}, ${stderrStrings}`);
|
|
|
|
if (callCount >= stderrStrings.length) {
|
2025-01-23 14:24:04 +01:00
|
|
|
return {
|
|
|
|
exitCode: 0,
|
|
|
|
stdout: 'Mock success',
|
|
|
|
stderr: ''
|
2025-01-23 15:46:10 +01:00
|
|
|
};
|
2025-01-23 14:24:04 +01:00
|
|
|
}
|
|
|
|
return {
|
|
|
|
exitCode: 1,
|
|
|
|
stdout: '',
|
2025-01-23 17:07:51 +01:00
|
|
|
stderr: stderrStrings[callCount % stderrStrings.length]
|
2025-01-23 15:46:10 +01:00
|
|
|
};
|
|
|
|
});
|
2025-01-23 14:24:04 +01:00
|
|
|
|
|
|
|
const username = 'dbowie';
|
|
|
|
const password = 'groundcontrol';
|
|
|
|
const registry = 'https://ghcr.io';
|
|
|
|
|
2025-01-23 17:07:51 +01:00
|
|
|
stderrStrings = ['mock error, failed with status: 408 Request Timeout', 'mock error, failed with status: 502 Request Timeout', 'mock error, failed with status: 400 Request Timeout'];
|
|
|
|
callCount = -1;
|
2025-01-23 14:24:04 +01:00
|
|
|
await expect(async () => {
|
|
|
|
await login(registry, username, password, 'false', ['408', '400'], 5, 0.1);
|
2025-01-23 15:46:10 +01:00
|
|
|
}).rejects.toThrow('mock error, failed with status: 502 Request Timeout');
|
2025-01-23 14:24:04 +01:00
|
|
|
expect(Docker.getExecOutput).toHaveBeenCalledTimes(2);
|
|
|
|
|
2025-01-23 17:07:51 +01:00
|
|
|
stderrStrings = ['not matching error', 'mock error, failed with status: 502 Request Timeout', 'mock error, failed with status: 400 Request Timeout'];
|
|
|
|
callCount = -1;
|
2025-01-23 14:24:04 +01:00
|
|
|
await expect(async () => {
|
|
|
|
await login(registry, username, password, 'false', ['408', '400'], 5, 0.1);
|
2025-01-23 15:46:10 +01:00
|
|
|
}).rejects.toThrow('not matching error');
|
2025-01-23 14:24:04 +01:00
|
|
|
expect(Docker.getExecOutput).toHaveBeenCalledTimes(2 + 1);
|
|
|
|
});
|