-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathexit-reasons.test.ts
More file actions
333 lines (284 loc) · 7.93 KB
/
Copy pathexit-reasons.test.ts
File metadata and controls
333 lines (284 loc) · 7.93 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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
import test from 'ava';
import crypto from 'node:crypto';
import path from 'node:path';
import { initLightning, initWorker } from '../src/init';
let lightning;
let worker;
test.before(async () => {
const lightningPort = 4321;
lightning = initLightning(lightningPort);
({ worker } = await initWorker(lightningPort, {
repoDir: path.resolve('tmp/repo/exit-reason'),
}));
});
test.after(async () => {
lightning.destroy();
await worker.destroy();
});
const run = async (attempt) => {
return new Promise<any>(async (done) => {
lightning.once('run:complete', (evt) => {
if (attempt.id === evt.runId) {
done(evt.payload);
}
});
lightning.enqueueRun(attempt);
});
};
test.serial('crash: syntax error', async (t) => {
const attempt = {
id: crypto.randomUUID(),
jobs: [
{
adaptor: '@openfn/language-common@latest',
body: 'fn(() => throw "e")',
},
],
};
const result = await run(attempt);
const { reason, error_type, error_message } = result;
t.is(reason, 'crash');
t.is(error_type, 'CompileError');
t.regex(error_message, /Unexpected token \(1:9\)$/);
});
// https://github.com/OpenFn/kit/issues/1045
test.serial('crash: reference error', async (t) => {
const attempt = {
id: crypto.randomUUID(),
jobs: [
{
name: 'x', // important!
adaptor: '@openfn/language-common@latest',
body: 'fn((s) => s.err.map(s => s))',
},
],
};
const result = await run(attempt);
const { reason, error_type, error_message } = result;
t.is(reason, 'fail');
t.is(error_type, 'RuntimeError');
t.regex(
error_message,
/TypeError: Cannot read properties of undefined \(reading \'map\'\)/
);
});
// https://github.com/OpenFn/kit/issues/758
test.serial('crash: job not found', async (t) => {
lightning.addDataclip('x', {});
const attempt = {
id: crypto.randomUUID(),
jobs: [
{
id: 'x',
adaptor: '@openfn/language-common@latest',
body: 'fn(s => s)',
},
],
dataclip_id: 'x', // having a data clip is important to trigger the crash
starting_node_id: 'y',
};
const result = await run(attempt);
const { reason, error_type, error_message } = result;
t.is(reason, 'crash');
t.is(error_type, 'ValidationError');
t.regex(error_message, /could not find start job: y/i);
});
test.serial('exception: autoinstall error', async (t) => {
const attempt = {
id: crypto.randomUUID(),
jobs: [
{
adaptor: '@openfn/language-worker-integration-tests@9.9.9',
body: 'fn((s) => s)',
},
],
};
const result = await run(attempt);
const { reason, error_type, error_message } = result;
t.is(reason, 'exception');
t.is(error_type, 'AutoinstallError');
t.regex(
error_message,
/Error installing @openfn\/language-worker-integration-tests@9.9.9/
);
});
test.serial('exception: bad credential (not found)', async (t) => {
const attempt = {
id: crypto.randomUUID(),
jobs: [
{
adaptor: '@openfn/language-common@latest',
body: 'fn((s) => s)',
credential: 'been-to-the-mountain', // the mock will return not_found
},
],
};
const expectedLightningErrorResponse = {
errors: { id: ['Credential not found!'] },
};
const result = await run(attempt);
const { reason, error_type, error_message } = result;
t.is(reason, 'exception');
t.is(error_type, 'CredentialLoadError');
t.is(
error_message,
`Failed to load credential been-to-the-mountain: [fetch:credential] ${JSON.stringify(
expectedLightningErrorResponse
)}`
);
});
test.serial('exception: credential timeout', async (t) => {
const attempt = {
id: crypto.randomUUID(),
jobs: [
{
adaptor: '@openfn/language-common@latest',
body: 'fn((s) => s)',
credential: '%TIMEOUT%', // special mock key
},
],
};
const result = await run(attempt);
const { reason, error_type, error_message } = result;
t.is(reason, 'exception');
t.is(error_type, 'CredentialLoadError');
t.is(
error_message,
'Failed to load credential %TIMEOUT%: [fetch:credential] timeout'
);
});
test.serial('kill: oom (small, kill worker)', async (t) => {
const attempt = {
id: crypto.randomUUID(),
jobs: [
{
adaptor: '@openfn/language-common@latest',
body: `fn((s) => {
s.data = [];
while(true) {
s.data.push(new Array(1e6).fill("xyz"))
}
})`,
},
],
};
const result = await run(attempt);
const { reason, error_type, error_message } = result;
t.is(reason, 'kill');
t.is(error_type, 'OOMError');
t.is(error_message, 'Run exceeded maximum memory usage');
});
// TODO this is failing locally... is it OK in CI?
test.serial('kill: oom (large, kill vm)', async (t) => {
const attempt = {
id: crypto.randomUUID(),
jobs: [
{
adaptor: '@openfn/language-common@latest',
body: `fn((s) => {
s.data = [];
while(true) {
s.data.push(new Array(1e9).fill("xyz"))
}
})`,
},
],
};
const result = await run(attempt);
const { reason, error_type, error_message } = result;
t.is(reason, 'kill');
t.is(error_type, 'OOMError');
t.is(error_message, 'Run exceeded maximum memory usage');
});
test.serial('kill: state exceeds the configured state limit', async (t) => {
const attempt = {
id: crypto.randomUUID(),
jobs: [
{
adaptor: '@openfn/language-common@latest',
// ~2mb string for a 1mb limit
body: `fn((s) => {
s.data = new Array(2 * 1024 * 1024).fill('a').join('');
return s;
})`,
},
],
options: {
state_limit_mb: 1,
},
};
const result = await run(attempt);
const { reason, error_type, error_message } = result;
t.is(reason, 'kill');
t.is(error_type, 'StateTooLargeError');
t.regex(error_message, /State exceeds the limit of 1mb/);
});
test.serial(
'kill: state limit is enforced between jobs (downstream job does not run)',
async (t) => {
const jobOne = {
id: crypto.randomUUID(),
adaptor: '@openfn/language-common@latest',
// ~2mb state, over the 1mb limit set below
body: `fn((s) => {
s.data = new Array(2 * 1024 * 1024).fill('a').join('');
return s;
})`,
};
// not expected to run because the first job is expected to trigger state size crash
const jobTwo = {
id: crypto.randomUUID(),
adaptor: '@openfn/language-common@latest',
body: `fn(() => ({ data: 'ok' }))`,
};
const attempt = {
id: crypto.randomUUID(),
jobs: [jobOne, jobTwo],
edges: [
{
id: crypto.randomUUID(),
source_job_id: jobOne.id,
target_job_id: jobTwo.id,
condition: 'always',
},
],
options: {
state_limit_mb: 1,
},
};
const startedJobs: string[] = [];
const unsubscribe = lightning.onSocketEvent(
'step:start',
attempt.id,
(evt) => {
if (evt.runId === attempt.id) {
startedJobs.push(evt.payload.job_id);
}
},
false
);
const result = await run(attempt);
unsubscribe();
const { reason, error_type, error_message } = result;
t.is(reason, 'kill');
t.is(error_type, 'StateTooLargeError');
t.regex(error_message, /State exceeds the limit of 1mb/);
t.deepEqual(startedJobs, [jobOne.id]);
}
);
test.serial('crash: process.exit() triggered by postgres', async (t) => {
const attempt = {
id: crypto.randomUUID(),
jobs: [
{
adaptor: '@openfn/language-postgresql@4.1.8', // version number is important
body: "sql('select * from food_hygiene_interview');",
},
],
};
const result = await run(attempt);
const { reason, error_type, error_message } = result;
t.is(reason, 'crash');
t.is(error_type, 'ExitError');
t.regex(error_message, /Worker thread exited with code: 1/i);
});