Files
amazon-ecr-login/cleanup.test.js
T
tmn 4a4464211b
Package / Package distribution file (push) Canceled after 0s
CodeQL / Analyze (javascript) (push) Canceled after 0s
Initial commit from zip
2026-10-27 14:08:25 +08:00

139 lines
4.7 KiB
JavaScript

import {jest} from '@jest/globals';
jest.unstable_mockModule('@actions/core', () => ({
getInput: jest.fn(),
setOutput: jest.fn(),
setFailed: jest.fn(),
setSecret: jest.fn(),
saveState: jest.fn(),
getState: jest.fn(),
debug: jest.fn(),
info: jest.fn(),
warning: jest.fn(),
error: jest.fn(),
}));
jest.unstable_mockModule('@actions/exec', () => ({
exec: jest.fn(),
}));
const core = await import('@actions/core');
const exec = await import('@actions/exec');
const { default: cleanup } = await import('./cleanup.js');
function mockGetState(requestResponse) {
return function (name, options) { // eslint-disable-line no-unused-vars
return requestResponse[name]
}
}
const ECR_STATES = {
'registries': '123456789012.dkr.ecr.aws-region-1.amazonaws.com,111111111111.dkr.ecr.aws-region-1.amazonaws.com'
};
describe('Logout from ECR', () => {
beforeEach(() => {
jest.clearAllMocks();
core.getState.mockImplementation(mockGetState(ECR_STATES));
exec.exec.mockReturnValue(0);
});
test('logs out docker client for registries in action state', async () => {
await cleanup();
expect(core.getState).toHaveBeenCalledWith('registries');
expect(exec.exec).toHaveBeenNthCalledWith(1,
'docker',
['logout', '123456789012.dkr.ecr.aws-region-1.amazonaws.com'],
expect.anything());
expect(exec.exec).toHaveBeenNthCalledWith(2,
'docker',
['logout', '111111111111.dkr.ecr.aws-region-1.amazonaws.com'],
expect.anything());
expect(exec.exec).toHaveBeenCalledTimes(2);
expect(core.setFailed).toHaveBeenCalledTimes(0);
});
test('logs out docker client for public registry in action state', async () => {
const mockStates = {
'registries': 'public.ecr.aws'
};
core.getState.mockImplementation(mockGetState(mockStates));
await cleanup();
expect(core.getState).toHaveBeenCalledWith('registries');
expect(exec.exec).toHaveBeenNthCalledWith(1,
'docker',
['logout', 'public.ecr.aws'],
expect.anything());
expect(exec.exec).toHaveBeenCalledTimes(1);
expect(core.setFailed).toHaveBeenCalledTimes(0);
});
test('handles zero registries', async () => {
const mockStates = {
'registries' : ''
};
core.getState.mockImplementation(mockGetState(mockStates));
await cleanup();
expect(core.getState).toHaveBeenCalledWith('registries');
expect(exec.exec).toHaveBeenCalledTimes(0);
expect(core.setFailed).toHaveBeenCalledTimes(0);
});
test('error is caught by core.setFailed for failed docker logout', async () => {
exec.exec.mockReturnValue(1);
await cleanup();
expect(core.setFailed).toHaveBeenCalled();
});
test('continues to attempt logouts after a failed logout', async () => {
const mockStates = {
'registries' : '123456789012.dkr.ecr.aws-region-1.amazonaws.com,111111111111.dkr.ecr.aws-region-1.amazonaws.com,222222222222.dkr.ecr.aws-region-1.amazonaws.com'
};
core.getState.mockImplementation(mockGetState(mockStates));
exec.exec.mockImplementationOnce((commandLine, args, options) => {
options.listeners.stdout('stdout of ');
options.listeners.stdout('registry 1');
options.listeners.stderr('stderr of ');
options.listeners.stderr('registry 1');
return(1);
}).mockImplementationOnce((commandLine, args, options) => {
options.listeners.stdout('stdout of ');
options.listeners.stdout('registry 2');
options.listeners.stderr('stderr of ');
options.listeners.stderr('registry 2');
return(1);
}).mockReturnValueOnce(0);
await cleanup();
expect(core.getState).toHaveBeenCalledWith('registries');
expect(exec.exec).toHaveBeenNthCalledWith(1,
'docker',
['logout', '123456789012.dkr.ecr.aws-region-1.amazonaws.com'],
expect.anything());
expect(exec.exec).toHaveBeenNthCalledWith(2,
'docker',
['logout', '111111111111.dkr.ecr.aws-region-1.amazonaws.com'],
expect.anything());
expect(exec.exec).toHaveBeenNthCalledWith(3,
'docker',
['logout', '222222222222.dkr.ecr.aws-region-1.amazonaws.com'],
expect.anything());
expect(core.error).toHaveBeenNthCalledWith(1, 'Could not logout of registry 123456789012.dkr.ecr.aws-region-1.amazonaws.com: stderr of registry 1');
expect(core.error).toHaveBeenNthCalledWith(2, 'Could not logout of registry 111111111111.dkr.ecr.aws-region-1.amazonaws.com: stderr of registry 2');
expect(core.setFailed).toHaveBeenCalledWith('Failed to logout: 123456789012.dkr.ecr.aws-region-1.amazonaws.com,111111111111.dkr.ecr.aws-region-1.amazonaws.com');
expect(exec.exec).toHaveBeenCalledTimes(3);
expect(core.error).toHaveBeenCalledTimes(2);
expect(core.setFailed).toHaveBeenCalledTimes(1);
});
});