-
Notifications
You must be signed in to change notification settings - Fork 95
Expand file tree
/
Copy patherror-serialization-test.ts
More file actions
200 lines (171 loc) · 5.76 KB
/
error-serialization-test.ts
File metadata and controls
200 lines (171 loc) · 5.76 KB
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
import { WritableStreamBuffer } from 'stream-buffers'
const mockExec = jest.fn()
const mockReadNamespacedPod = jest.fn()
const mockReadNamespacedJob = jest.fn()
jest.mock('@kubernetes/client-node', () => {
return {
KubeConfig: jest.fn().mockImplementation(() => ({
loadFromDefault: jest.fn(),
makeApiClient: jest.fn().mockImplementation(ApiClass => {
const name = ApiClass?.name || ApiClass?.toString() || ''
if (name.includes('Batch')) {
return { readNamespacedJob: mockReadNamespacedJob }
}
if (name.includes('Authorization')) {
return { createSelfSubjectAccessReview: jest.fn() }
}
return { readNamespacedPod: mockReadNamespacedPod }
}),
getContexts: jest
.fn()
.mockReturnValue([{ namespace: 'test-namespace' }])
})),
Exec: jest.fn().mockImplementation(() => ({ exec: mockExec })),
CoreV1Api: class CoreV1Api {},
BatchV1Api: class BatchV1Api {},
AuthorizationV1Api: class AuthorizationV1Api {},
Log: jest.fn()
}
})
jest.mock('tar-fs', () => ({
default: {
pack: jest.fn().mockReturnValue({ pipe: jest.fn() }),
extract: jest.fn().mockReturnValue({
on: jest.fn(),
pipe: jest.fn()
})
},
__esModule: true
}))
jest.mock('../src/k8s/utils', () => {
const actual = jest.requireActual('../src/k8s/utils')
return {
...actual,
sleep: jest.fn().mockResolvedValue(undefined)
}
})
import {
execCpToPod,
execCpFromPod,
waitForJobToComplete,
waitForPodPhases
} from '../src/k8s'
import { PodPhase } from '../src/k8s/utils'
describe('error serialization', () => {
beforeEach(() => {
jest.clearAllMocks()
process.env['ACTIONS_RUNNER_KUBERNETES_NAMESPACE'] = 'test-namespace'
})
afterEach(() => {
delete process.env['ACTIONS_RUNNER_KUBERNETES_NAMESPACE']
})
describe('execCpToPod', () => {
it('should include Error.message in thrown error after retries', async () => {
mockExec.mockRejectedValue(new Error('connection refused'))
await expect(
execCpToPod('test-pod', '/tmp/src', '/workspace')
).rejects.toThrow('cpToPod failed after 30 attempts: connection refused')
})
it('should use String() for non-Error throwables', async () => {
mockExec.mockRejectedValue('raw string error')
await expect(
execCpToPod('test-pod', '/tmp/src', '/workspace')
).rejects.toThrow('cpToPod failed after 30 attempts: raw string error')
})
it('should not produce empty braces in error message', async () => {
mockExec.mockRejectedValue(new Error('ETIMEOUT'))
await expect(
execCpToPod('test-pod', '/tmp/src', '/workspace')
).rejects.toThrow(
expect.not.objectContaining({ message: expect.stringContaining('{}') })
)
})
})
describe('execCpFromPod', () => {
it('should include Error.message in thrown error after retries', async () => {
mockExec.mockRejectedValue(new Error('container not found'))
await expect(
execCpFromPod('test-pod', '/workspace/output', '/tmp/dst')
).rejects.toThrow(
'execCpFromPod failed after 30 attempts: container not found'
)
})
it('should use String() for non-Error throwables', async () => {
mockExec.mockRejectedValue(42)
await expect(
execCpFromPod('test-pod', '/workspace/output', '/tmp/dst')
).rejects.toThrow('execCpFromPod failed after 30 attempts: 42')
})
})
describe('waitForJobToComplete', () => {
it('should include Error.message when job fails', async () => {
mockReadNamespacedJob.mockResolvedValue({
status: { failed: 1 }
})
await expect(waitForJobToComplete('my-job')).rejects.toThrow(
'job my-job has failed: job my-job has failed'
)
})
it('should include Error.message when API call throws', async () => {
mockReadNamespacedJob.mockRejectedValue(
new Error('403 Forbidden')
)
await expect(waitForJobToComplete('my-job')).rejects.toThrow(
'job my-job has failed: 403 Forbidden'
)
})
it('should use String() for non-Error throwables from API', async () => {
mockReadNamespacedJob.mockRejectedValue('unexpected API failure')
await expect(waitForJobToComplete('my-job')).rejects.toThrow(
'job my-job has failed: unexpected API failure'
)
})
})
describe('waitForPodPhases', () => {
it('should include error message when pod enters unhealthy phase', async () => {
mockReadNamespacedPod.mockResolvedValue({
status: { phase: 'Failed' }
})
await expect(
waitForPodPhases(
'test-pod',
new Set([PodPhase.RUNNING]),
new Set([PodPhase.PENDING])
)
).rejects.toThrow(
/Pod test-pod is unhealthy with phase status Failed/
)
})
it('should include Error.message when API call throws', async () => {
mockReadNamespacedPod.mockRejectedValue(
new Error('network timeout')
)
await expect(
waitForPodPhases(
'test-pod',
new Set([PodPhase.RUNNING]),
new Set([PodPhase.PENDING])
)
).rejects.toThrow(
'Pod test-pod is unhealthy with phase status Unknown: network timeout'
)
})
it('should not produce empty braces from Error objects', async () => {
mockReadNamespacedPod.mockRejectedValue(
new Error('socket hang up')
)
try {
await waitForPodPhases(
'test-pod',
new Set([PodPhase.RUNNING]),
new Set([PodPhase.PENDING])
)
fail('Expected waitForPodPhases to throw')
} catch (error) {
const msg = (error as Error).message
expect(msg).not.toContain('{}')
expect(msg).toContain('socket hang up')
}
})
})
})