-
Notifications
You must be signed in to change notification settings - Fork 344
/
Copy pathcml.e2e.test.js
296 lines (238 loc) · 8.73 KB
/
cml.e2e.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
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
const CML = require('../src/cml').default;
describe('Github tests', () => {
const OLD_ENV = process.env;
const {
TEST_GITHUB_TOKEN: TOKEN,
TEST_GITHUB_REPOSITORY: REPO,
TEST_GITHUB_COMMIT: SHA
} = process.env;
beforeEach(() => {
jest.resetModules();
process.env = {};
process.env.REPO_TOKEN = TOKEN;
});
afterAll(() => {
process.env = OLD_ENV;
});
test('driver has to be github', async () => {
const cml = new CML({ repo: REPO, token: TOKEN });
expect(cml.driver).toBe('github');
});
test('Publish image without markdown returns an url', async () => {
const path = `${__dirname}/../assets/logo.png`;
const output = await new CML().publish({ path });
expect(output.startsWith('https://')).toBe(true);
expect(output.includes('cml=png')).toBe(true);
});
test('Publish image with markdown', async () => {
const path = `${__dirname}/../assets/logo.png`;
const title = 'my title';
const output = await new CML().publish({ path, md: true, title });
expect(output.startsWith(').toBe(true);
expect(output.endsWith(` "${title}")`)).toBe(true);
expect(output.includes('cml=png')).toBe(true);
});
test('Publish image embedded in markdown', async () => {
const path = `${__dirname}/../assets/test.md`;
const title = 'my title';
const output = await new CML().publish({ path, md: true, title });
expect(output.startsWith(`[${title}](https://`)).toBe(true);
expect(output.endsWith(')')).toBe(true);
expect(output.includes('cml=plain')).toBe(true);
});
test('Publish a non image file in markdown', async () => {
const path = `${__dirname}/../assets/logo.pdf`;
const title = 'my title';
const output = await new CML().publish({ path, md: true, title });
expect(output.startsWith(`[${title}](https://`)).toBe(true);
expect(output.endsWith(')')).toBe(true);
expect(output.includes('cml=pdf')).toBe(true);
});
test('Comment should succeed with a valid sha', async () => {
const report = '## Test comment';
await new CML({ repo: REPO }).commentCreate({ report, commitSha: SHA });
});
test('Comment should fail with a invalid sha', async () => {
let caughtErr;
try {
const report = '## Test comment';
const commitSha = 'invalid_sha';
await new CML({ repo: REPO }).commentCreate({ report, commitSha });
} catch (err) {
caughtErr = err.message;
}
expect(caughtErr).toBe('No commit found for SHA: invalid_sha');
});
test('Runner logs', async () => {
const cml = new CML();
cml.driver = 'github';
let logs = await cml.parseRunnerLog({
data: `
2022-06-05 16:25:56Z: Listening for Jobs
2022-06-05 16:26:35Z: Running job: train
2022-06-05 16:28:03Z: Job train completed with result: Failed
`
});
expect(logs.length).toBe(3);
expect(logs[0].status).toBe('ready');
expect(logs[1].status).toBe('job_started');
expect(logs[2].status).toBe('job_ended');
expect(logs[2].success).toBe(false);
logs = await cml.parseRunnerLog({
data: '2022-06-05 16:28:03Z: Job train completed with result: Succeeded'
});
expect(logs[0].status).toBe('job_ended');
expect(logs[0].success).toBe(true);
});
});
describe('Gitlab tests', () => {
const OLD_ENV = process.env;
const {
TEST_GITLAB_TOKEN: TOKEN,
TEST_GITLAB_REPOSITORY: REPO,
TEST_GITLAB_COMMIT: SHA
} = process.env;
beforeEach(() => {
jest.resetModules();
process.env = {};
process.env.REPO_TOKEN = TOKEN;
});
afterAll(() => {
process.env = OLD_ENV;
});
test('driver has to be gitlab', async () => {
const cml = new CML({ repo: REPO, token: TOKEN, driver: 'gitlab' });
expect(cml.driver).toBe('gitlab');
});
test('Publish image using gl without markdown returns an url', async () => {
const path = `${__dirname}/../assets/logo.png`;
const output = await new CML({ repo: REPO }).publish({
path,
native: true
});
expect(output.startsWith('/uploads/')).toBe(true);
expect(output.includes('cml=png')).toBe(false);
});
test('Publish image using gl with markdown', async () => {
const path = `${__dirname}/../assets/logo.png`;
const title = 'my title';
const output = await new CML({ repo: REPO }).publish({
path,
md: true,
title,
native: true
});
expect(output.startsWith(').toBe(true);
expect(output.endsWith(` "${title}")`)).toBe(true);
expect(output.includes('cml=png')).toBe(false);
});
test('Publish a non image file using gl in markdown', async () => {
const path = `${__dirname}/../assets/logo.pdf`;
const title = 'my title';
const output = await new CML({ repo: REPO }).publish({
path,
md: true,
title,
native: true
});
expect(output.startsWith(`[${title}](/uploads/`)).toBe(true);
expect(output.endsWith(')')).toBe(true);
expect(output.includes('cml=pdf')).toBe(false);
});
test('Publish a non image file using native', async () => {
const path = `${__dirname}/../assets/logo.pdf`;
const title = 'my title';
const output = await new CML({ repo: REPO }).publish({
path,
md: true,
title,
native: true
});
expect(output.startsWith(`[${title}](/uploads/`)).toBe(true);
expect(output.endsWith(')')).toBe(true);
expect(output.includes('cml=pdf')).toBe(false);
});
test('Publish should fail with an invalid driver', async () => {
let caughtErr;
try {
const path = `${__dirname}/../assets/logo.pdf`;
await new CML({ repo: REPO, driver: 'invalid' }).publish({
path,
md: true,
native: true
});
} catch (err) {
caughtErr = err.message;
}
expect(caughtErr).not.toBeUndefined();
});
test('Comment should succeed with a valid sha', async () => {
const report = '## Test comment';
await new CML({ repo: REPO }).commentCreate({ report, commitSha: SHA });
});
test('Comment should fail with a invalid sha', async () => {
let caughtErr;
try {
const report = '## Test comment';
const commitSha = 'invalid_sha';
await new CML({ repo: REPO }).commentCreate({ report, commitSha });
} catch (err) {
caughtErr = err.message;
}
expect(caughtErr).toBe('Not Found');
});
test('Runner logs', async () => {
const cml = new CML();
cml.driver = 'gitlab';
let logs = await cml.parseRunnerLog({
data: `
{"level":"info","msg":"Starting runner for https://gitlab.com with token 2SGFrnGt ...","time":"2021-07-02T16:45:05Z"}
{"job":1396213069,"level":"info","msg":"Checking for jobs... received","repo_url":"https://gitlab.com/iterative.ai/fashion_mnist.git","runner":"2SGFrnGt","time":"2021-07-02T16:45:47Z"}
{"duration_s":120.0120526,"job":1396213069,"level":"warning","msg":"Job failed: execution took longer than 2m0s seconds","project":27856642,"runner":"2SGFrnGt","time":"2021-07-02T16:47:47Z"}
`
});
expect(logs.length).toBe(3);
expect(logs[0].status).toBe('ready');
expect(logs[1].status).toBe('job_started');
expect(logs[2].status).toBe('job_ended');
expect(logs[2].success).toBe(false);
logs = await cml.parseRunnerLog({
data: '{"duration_s":7.706165838,"job":2177867438,"level":"info","msg":"Job succeeded","project":27939020,"runner":"fe36krFK","time":"2022-03-08T18:12:57+01:00"}'
});
expect(logs[0].status).toBe('job_ended');
expect(logs[0].success).toBe(true);
});
});
describe('Bitbucket tests', () => {
const OLD_ENV = process.env;
const { TEST_BITBUCKET_TOKEN: TOKEN = 'DUMMY' } = process.env;
beforeEach(() => {
jest.resetModules();
process.env = {};
process.env.REPO_TOKEN = TOKEN;
});
afterAll(() => {
process.env = OLD_ENV;
});
test('Runner logs', async () => {
const cml = new CML();
cml.driver = 'bitbucket';
let logs = await cml.parseRunnerLog({
data: `
[2022-06-05 17:23:41,945] Updating runner status to "ONLINE" and checking for new steps assigned to the runner after 0 seconds and then every 30 seconds.
[2022-06-05 17:24:12,246] Getting step StepId{accountUuid={XXXXX-XXX-XXX-XXXXXXXX}, repositoryUuid={XXXXX-XXX-XXX-XXXXXXXX}, pipelineUuid={XXXXX-XXX-XXX-XXXXXXXX}, stepUuid={XXXXX-XXX-XXX-XXXXXXXX}}.
[2022-06-05 17:24:53,466] Completing step with result Result{status=FAILED, error=None}.
`
});
expect(logs.length).toBe(3);
expect(logs[0].status).toBe('ready');
expect(logs[1].status).toBe('job_started');
expect(logs[2].status).toBe('job_ended');
expect(logs[2].success).toBe(false);
logs = await cml.parseRunnerLog({
data: '[2022-06-05 17:24:53,466] Completing step with result Result{status=PASSED, error=None}.'
});
expect(logs[0].status).toBe('job_ended');
expect(logs[0].success).toBe(true);
});
});