-
Notifications
You must be signed in to change notification settings - Fork 174
/
cleanup.test.js
122 lines (102 loc) · 4.39 KB
/
cleanup.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
const cleanup = require('./cleanup.js');
const core = require('@actions/core');
const exec = require('@actions/exec');
jest.mock('@actions/core');
jest.mock('@actions/exec');
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 = jest.fn().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 = jest.fn().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 = jest.fn().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 = jest.fn().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);
});
});