2022-02-28 15:28:55 +08:00
|
|
|
import {expect, jest, test} from '@jest/globals';
|
2020-10-09 18:30:45 +08:00
|
|
|
import osm = require('os');
|
|
|
|
|
2023-02-21 17:06:17 +08:00
|
|
|
import {main} from '../src/main';
|
2020-10-09 18:30:45 +08:00
|
|
|
import * as docker from '../src/docker';
|
|
|
|
import * as stateHelper from '../src/state-helper';
|
|
|
|
|
2020-10-20 20:41:56 +08:00
|
|
|
test('errors without username and password', async () => {
|
2022-03-21 17:57:36 +08:00
|
|
|
jest.spyOn(osm, 'platform').mockImplementation(() => 'linux');
|
2021-06-22 16:39:55 +08:00
|
|
|
process.env['INPUT_LOGOUT'] = 'true'; // default value
|
2023-04-17 08:31:57 +08:00
|
|
|
await expect(main()).rejects.toThrow(new Error('Username and password required'));
|
2020-10-09 18:30:45 +08:00
|
|
|
});
|
|
|
|
|
2020-10-17 00:34:48 +08:00
|
|
|
test('successful with username and password', async () => {
|
2022-03-21 17:57:36 +08:00
|
|
|
jest.spyOn(osm, 'platform').mockImplementation(() => 'linux');
|
2022-02-28 15:28:55 +08:00
|
|
|
const setRegistrySpy = jest.spyOn(stateHelper, 'setRegistry');
|
|
|
|
const setLogoutSpy = jest.spyOn(stateHelper, 'setLogout');
|
2023-04-17 08:31:57 +08:00
|
|
|
const dockerSpy = jest.spyOn(docker, 'login').mockImplementation(() => Promise.resolve());
|
2020-10-17 00:24:41 +08:00
|
|
|
|
2022-03-21 17:57:36 +08:00
|
|
|
const username = 'dbowie';
|
2020-10-17 00:34:48 +08:00
|
|
|
process.env[`INPUT_USERNAME`] = username;
|
|
|
|
|
2022-03-21 17:57:36 +08:00
|
|
|
const password = 'groundcontrol';
|
2020-10-17 00:24:41 +08:00
|
|
|
process.env[`INPUT_PASSWORD`] = password;
|
|
|
|
|
2022-03-21 17:57:36 +08:00
|
|
|
const ecr = 'auto';
|
2021-12-20 17:59:11 +08:00
|
|
|
process.env['INPUT_ECR'] = ecr;
|
|
|
|
|
2022-03-21 17:57:36 +08:00
|
|
|
const logout = false;
|
2021-06-22 16:39:55 +08:00
|
|
|
process.env['INPUT_LOGOUT'] = String(logout);
|
|
|
|
|
2023-02-21 17:06:17 +08:00
|
|
|
await main();
|
2020-10-17 00:24:41 +08:00
|
|
|
|
|
|
|
expect(setRegistrySpy).toHaveBeenCalledWith('');
|
2021-06-22 16:39:55 +08:00
|
|
|
expect(setLogoutSpy).toHaveBeenCalledWith(logout);
|
2021-12-20 17:59:11 +08:00
|
|
|
expect(dockerSpy).toHaveBeenCalledWith('', username, password, ecr);
|
2020-10-09 18:30:45 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
test('calls docker login', async () => {
|
2022-03-21 17:57:36 +08:00
|
|
|
jest.spyOn(osm, 'platform').mockImplementation(() => 'linux');
|
2022-02-28 15:28:55 +08:00
|
|
|
const setRegistrySpy = jest.spyOn(stateHelper, 'setRegistry');
|
|
|
|
const setLogoutSpy = jest.spyOn(stateHelper, 'setLogout');
|
2023-04-17 08:31:57 +08:00
|
|
|
const dockerSpy = jest.spyOn(docker, 'login').mockImplementation(() => Promise.resolve());
|
2020-10-09 18:30:45 +08:00
|
|
|
|
2022-03-21 17:57:36 +08:00
|
|
|
const username = 'dbowie';
|
2020-10-09 18:30:45 +08:00
|
|
|
process.env[`INPUT_USERNAME`] = username;
|
|
|
|
|
2022-03-21 17:57:36 +08:00
|
|
|
const password = 'groundcontrol';
|
2020-10-09 18:30:45 +08:00
|
|
|
process.env[`INPUT_PASSWORD`] = password;
|
|
|
|
|
2022-03-21 17:57:36 +08:00
|
|
|
const registry = 'ghcr.io';
|
2020-10-09 18:30:45 +08:00
|
|
|
process.env[`INPUT_REGISTRY`] = registry;
|
|
|
|
|
2022-03-21 17:57:36 +08:00
|
|
|
const ecr = 'auto';
|
2021-12-20 17:59:11 +08:00
|
|
|
process.env['INPUT_ECR'] = ecr;
|
|
|
|
|
2022-03-21 17:57:36 +08:00
|
|
|
const logout = true;
|
2021-06-22 16:39:55 +08:00
|
|
|
process.env['INPUT_LOGOUT'] = String(logout);
|
2020-10-09 18:30:45 +08:00
|
|
|
|
2023-02-21 17:06:17 +08:00
|
|
|
await main();
|
2020-10-09 18:30:45 +08:00
|
|
|
|
|
|
|
expect(setRegistrySpy).toHaveBeenCalledWith(registry);
|
|
|
|
expect(setLogoutSpy).toHaveBeenCalledWith(logout);
|
2021-12-20 17:59:11 +08:00
|
|
|
expect(dockerSpy).toHaveBeenCalledWith(registry, username, password, ecr);
|
2020-10-09 18:30:45 +08:00
|
|
|
});
|