2022-02-28 15:28:55 +08:00
|
|
|
import {expect, jest, test} from '@jest/globals';
|
2020-10-09 18:30:45 +08:00
|
|
|
import * as path from 'path';
|
2023-09-08 15:35:48 +08:00
|
|
|
|
|
|
|
import {loginStandard, logout} from '../src/docker';
|
2024-07-22 16:41:37 +08:00
|
|
|
|
|
|
|
import {Docker} from '@docker/actions-toolkit/lib/docker/docker';
|
2020-10-09 18:30:45 +08:00
|
|
|
|
|
|
|
process.env['RUNNER_TEMP'] = path.join(__dirname, 'runner');
|
|
|
|
|
|
|
|
test('loginStandard calls exec', async () => {
|
2022-03-21 17:57:36 +08:00
|
|
|
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
2022-02-28 15:28:55 +08:00
|
|
|
// @ts-ignore
|
2024-07-22 16:41:37 +08:00
|
|
|
const execSpy = jest.spyOn(Docker, 'getExecOutput').mockImplementation(async () => {
|
2022-02-28 15:28:55 +08:00
|
|
|
return {
|
2021-06-22 17:09:26 +08:00
|
|
|
exitCode: expect.any(Number),
|
|
|
|
stdout: expect.any(Function),
|
|
|
|
stderr: expect.any(Function)
|
2022-02-28 15:28:55 +08:00
|
|
|
};
|
|
|
|
});
|
2020-10-09 18:30:45 +08:00
|
|
|
|
2022-03-21 17:57:36 +08:00
|
|
|
const username = 'dbowie';
|
|
|
|
const password = 'groundcontrol';
|
|
|
|
const registry = 'https://ghcr.io';
|
2020-10-09 18:30:45 +08:00
|
|
|
|
|
|
|
await loginStandard(registry, username, password);
|
|
|
|
|
2024-07-22 16:41:37 +08:00
|
|
|
expect(execSpy).toHaveBeenCalledTimes(1);
|
|
|
|
const callfunc = execSpy.mock.calls[0];
|
|
|
|
if (callfunc && callfunc[1]) {
|
|
|
|
// we don't want to check env opt
|
|
|
|
callfunc[1].env = undefined;
|
|
|
|
}
|
|
|
|
expect(execSpy).toHaveBeenCalledWith(['login', '--password-stdin', '--username', username, registry], {
|
2020-10-09 18:30:45 +08:00
|
|
|
input: Buffer.from(password),
|
|
|
|
silent: true,
|
2021-06-22 17:09:26 +08:00
|
|
|
ignoreReturnCode: true
|
2020-10-09 18:30:45 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
test('logout calls exec', async () => {
|
2022-03-21 17:57:36 +08:00
|
|
|
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
2022-02-28 15:28:55 +08:00
|
|
|
// @ts-ignore
|
2024-07-22 16:41:37 +08:00
|
|
|
const execSpy = jest.spyOn(Docker, 'getExecOutput').mockImplementation(async () => {
|
2022-02-28 15:28:55 +08:00
|
|
|
return {
|
2021-06-22 17:09:26 +08:00
|
|
|
exitCode: expect.any(Number),
|
|
|
|
stdout: expect.any(Function),
|
|
|
|
stderr: expect.any(Function)
|
2022-02-28 15:28:55 +08:00
|
|
|
};
|
|
|
|
});
|
2020-10-09 18:30:45 +08:00
|
|
|
|
2022-03-21 17:57:36 +08:00
|
|
|
const registry = 'https://ghcr.io';
|
2020-10-09 18:30:45 +08:00
|
|
|
|
|
|
|
await logout(registry);
|
|
|
|
|
2024-07-22 16:41:37 +08:00
|
|
|
expect(execSpy).toHaveBeenCalledTimes(1);
|
|
|
|
const callfunc = execSpy.mock.calls[0];
|
|
|
|
if (callfunc && callfunc[1]) {
|
|
|
|
// we don't want to check env opt
|
|
|
|
callfunc[1].env = undefined;
|
|
|
|
}
|
|
|
|
expect(execSpy).toHaveBeenCalledWith(['logout', registry], {
|
2021-06-22 17:09:26 +08:00
|
|
|
ignoreReturnCode: true
|
2020-10-09 18:30:45 +08:00
|
|
|
});
|
|
|
|
});
|