New syncing script sometimes schedules a build request on a wrong builder
[WebKit.git] / Websites / perf.webkit.org / server-tests / api-build-requests-tests.js
1 'use strict';
2
3 let assert = require('assert');
4
5 let MockData = require('./resources/mock-data.js');
6 let TestServer = require('./resources/test-server.js');
7
8 describe('/api/build-requests', function () {
9     this.timeout(1000);
10     TestServer.inject();
11
12     beforeEach(function () {
13         MockData.resetV3Models();
14     });
15
16     it('should return "TriggerableNotFound" when the database is empty', function (done) {
17         TestServer.remoteAPI().getJSON('/api/build-requests/build-webkit').then(function (content) {
18             assert.equal(content['status'], 'TriggerableNotFound');
19             done();
20         }).catch(done);
21     });
22
23     it('should return an empty list when there are no build requests', function (done) {
24         TestServer.database().connect().then(function () {
25             return TestServer.database().insert('build_triggerables', {name: 'build-webkit'});
26         }).then(function () {
27             return TestServer.remoteAPI().getJSON('/api/build-requests/build-webkit');
28         }).then(function (content) {
29             assert.equal(content['status'], 'OK');
30             assert.deepEqual(content['buildRequests'], []);
31             assert.deepEqual(content['rootSets'], []);
32             assert.deepEqual(content['roots'], []);
33             assert.deepEqual(Object.keys(content).sort(), ['buildRequests', 'rootSets', 'roots', 'status']);
34             done();
35         }).catch(done);
36     });
37
38     it('should return build requets associated with a given triggerable with appropriate roots and rootSets', function (done) {
39         let db = TestServer.database();
40         db.connect().then(function () {
41             return MockData.addMockData(db);
42         }).then(function () {
43             return TestServer.remoteAPI().getJSONWithStatus('/api/build-requests/build-webkit');
44         }).then(function (content) {
45             assert.deepEqual(Object.keys(content).sort(), ['buildRequests', 'rootSets', 'roots', 'status']);
46
47             assert.equal(content['rootSets'].length, 2);
48             assert.equal(content['rootSets'][0].id, 401);
49             assert.deepEqual(content['rootSets'][0].roots, ['87832', '93116']);
50             assert.equal(content['rootSets'][1].id, 402);
51             assert.deepEqual(content['rootSets'][1].roots, ['87832', '96336']);
52
53             assert.equal(content['roots'].length, 3);
54             assert.equal(content['roots'][0].id, 87832);
55             assert.equal(content['roots'][0].repository, '9');
56             assert.equal(content['roots'][0].revision, '10.11 15A284');
57             assert.equal(content['roots'][1].id, 93116);
58             assert.equal(content['roots'][1].repository, '11');
59             assert.equal(content['roots'][1].revision, '191622');
60             assert.equal(content['roots'][2].id, 96336);
61             assert.equal(content['roots'][2].repository, '11');
62             assert.equal(content['roots'][2].revision, '192736');
63
64             assert.equal(content['buildRequests'].length, 4);
65             assert.deepEqual(content['buildRequests'][0].id, 700);
66             assert.deepEqual(content['buildRequests'][0].order, 0);
67             assert.deepEqual(content['buildRequests'][0].platform, '65');
68             assert.deepEqual(content['buildRequests'][0].rootSet, 401);
69             assert.deepEqual(content['buildRequests'][0].status, 'pending');
70             assert.deepEqual(content['buildRequests'][0].test, '200');
71
72             assert.deepEqual(content['buildRequests'][1].id, 701);
73             assert.deepEqual(content['buildRequests'][1].order, 1);
74             assert.deepEqual(content['buildRequests'][1].platform, '65');
75             assert.deepEqual(content['buildRequests'][1].rootSet, 402);
76             assert.deepEqual(content['buildRequests'][1].status, 'pending');
77             assert.deepEqual(content['buildRequests'][1].test, '200');
78
79             assert.deepEqual(content['buildRequests'][2].id, 702);
80             assert.deepEqual(content['buildRequests'][2].order, 2);
81             assert.deepEqual(content['buildRequests'][2].platform, '65');
82             assert.deepEqual(content['buildRequests'][2].rootSet, 401);
83             assert.deepEqual(content['buildRequests'][2].status, 'pending');
84             assert.deepEqual(content['buildRequests'][2].test, '200');
85
86             assert.deepEqual(content['buildRequests'][3].id, 703);
87             assert.deepEqual(content['buildRequests'][3].order, 3);
88             assert.deepEqual(content['buildRequests'][3].platform, '65');
89             assert.deepEqual(content['buildRequests'][3].rootSet, 402);
90             assert.deepEqual(content['buildRequests'][3].status, 'pending');
91             assert.deepEqual(content['buildRequests'][3].test, '200');
92             done();
93         }).catch(done);
94     });
95
96     it('should support useLegacyIdResolution option', function (done) {
97         let db = TestServer.database();
98         db.connect().then(function () {
99             return MockData.addMockData(db);
100         }).then(function () {
101             return TestServer.remoteAPI().getJSONWithStatus('/api/build-requests/build-webkit?useLegacyIdResolution=true');
102         }).then(function (content) {
103             assert.deepEqual(Object.keys(content).sort(), ['buildRequests', 'rootSets', 'roots', 'status']);
104
105             assert.equal(content['rootSets'].length, 2);
106             assert.equal(content['rootSets'][0].id, 401);
107             assert.deepEqual(content['rootSets'][0].roots, ['87832', '93116']);
108             assert.equal(content['rootSets'][1].id, 402);
109             assert.deepEqual(content['rootSets'][1].roots, ['87832', '96336']);
110
111             assert.equal(content['roots'].length, 3);
112             assert.equal(content['roots'][0].id, 87832);
113             assert.equal(content['roots'][0].repository, 'OS X');
114             assert.equal(content['roots'][0].revision, '10.11 15A284');
115             assert.equal(content['roots'][1].id, 93116);
116             assert.equal(content['roots'][1].repository, 'WebKit');
117             assert.equal(content['roots'][1].revision, '191622');
118             assert.equal(content['roots'][2].id, 96336);
119             assert.equal(content['roots'][2].repository, 'WebKit');
120             assert.equal(content['roots'][2].revision, '192736');
121
122             assert.equal(content['buildRequests'].length, 4);
123             assert.deepEqual(content['buildRequests'][0].id, 700);
124             assert.deepEqual(content['buildRequests'][0].order, 0);
125             assert.deepEqual(content['buildRequests'][0].platform, 'some platform');
126             assert.deepEqual(content['buildRequests'][0].rootSet, 401);
127             assert.deepEqual(content['buildRequests'][0].status, 'pending');
128             assert.deepEqual(content['buildRequests'][0].test, ['some test']);
129
130             assert.deepEqual(content['buildRequests'][1].id, 701);
131             assert.deepEqual(content['buildRequests'][1].order, 1);
132             assert.deepEqual(content['buildRequests'][1].platform, 'some platform');
133             assert.deepEqual(content['buildRequests'][1].rootSet, 402);
134             assert.deepEqual(content['buildRequests'][1].status, 'pending');
135             assert.deepEqual(content['buildRequests'][1].test, ['some test']);
136
137             assert.deepEqual(content['buildRequests'][2].id, 702);
138             assert.deepEqual(content['buildRequests'][2].order, 2);
139             assert.deepEqual(content['buildRequests'][2].platform, 'some platform');
140             assert.deepEqual(content['buildRequests'][2].rootSet, 401);
141             assert.deepEqual(content['buildRequests'][2].status, 'pending');
142             assert.deepEqual(content['buildRequests'][2].test, ['some test']);
143
144             assert.deepEqual(content['buildRequests'][3].id, 703);
145             assert.deepEqual(content['buildRequests'][3].order, 3);
146             assert.deepEqual(content['buildRequests'][3].platform, 'some platform');
147             assert.deepEqual(content['buildRequests'][3].rootSet, 402);
148             assert.deepEqual(content['buildRequests'][3].status, 'pending');
149             assert.deepEqual(content['buildRequests'][3].test, ['some test']);
150             done();
151         }).catch(done);
152     });
153
154     it('should be fetchable by BuildRequest.fetchForTriggerable', function (done) {
155         let db = TestServer.database();
156         db.connect().then(function () {
157             return MockData.addMockData(db);
158         }).then(function () {
159             return Manifest.fetch();
160         }).then(function () {
161             return BuildRequest.fetchForTriggerable('build-webkit');
162         }).then(function (buildRequests) {
163             assert.equal(buildRequests.length, 4);
164
165             let test = Test.findById(200);
166             assert(test);
167
168             let platform = Platform.findById(65);
169             assert(platform);
170
171             assert.equal(buildRequests[0].id(), 700);
172             assert.equal(buildRequests[0].testGroupId(), 600);
173             assert.equal(buildRequests[0].test(), test);
174             assert.equal(buildRequests[0].platform(), platform);
175             assert.equal(buildRequests[0].order(), 0);
176             assert.ok(buildRequests[0].rootSet() instanceof RootSet);
177             assert.ok(!buildRequests[0].hasFinished());
178             assert.ok(!buildRequests[0].hasStarted());
179             assert.ok(buildRequests[0].isPending());
180             assert.equal(buildRequests[0].statusLabel(), 'Waiting to be scheduled');
181
182             assert.equal(buildRequests[1].id(), 701);
183             assert.equal(buildRequests[1].testGroupId(), 600);
184             assert.equal(buildRequests[1].test(), test);
185             assert.equal(buildRequests[1].platform(), platform);
186             assert.equal(buildRequests[1].order(), 1);
187             assert.ok(buildRequests[1].rootSet() instanceof RootSet);
188             assert.ok(!buildRequests[1].hasFinished());
189             assert.ok(!buildRequests[1].hasStarted());
190             assert.ok(buildRequests[1].isPending());
191             assert.equal(buildRequests[1].statusLabel(), 'Waiting to be scheduled');
192
193             assert.equal(buildRequests[2].id(), 702);
194             assert.equal(buildRequests[2].testGroupId(), 600);
195             assert.equal(buildRequests[2].test(), test);
196             assert.equal(buildRequests[2].platform(), platform);
197             assert.equal(buildRequests[2].order(), 2);
198             assert.ok(buildRequests[2].rootSet() instanceof RootSet);
199             assert.ok(!buildRequests[2].hasFinished());
200             assert.ok(!buildRequests[2].hasStarted());
201             assert.ok(buildRequests[2].isPending());
202             assert.equal(buildRequests[2].statusLabel(), 'Waiting to be scheduled');
203
204             assert.equal(buildRequests[3].id(), 703);
205             assert.equal(buildRequests[3].testGroupId(), 600);
206             assert.equal(buildRequests[3].test(), test);
207             assert.equal(buildRequests[3].platform(), platform);
208             assert.equal(buildRequests[3].order(), 3);
209             assert.ok(buildRequests[3].rootSet() instanceof RootSet);
210             assert.ok(!buildRequests[3].hasFinished());
211             assert.ok(!buildRequests[3].hasStarted());
212             assert.ok(buildRequests[3].isPending());
213             assert.equal(buildRequests[3].statusLabel(), 'Waiting to be scheduled');
214
215             let osx = Repository.findById(9);
216             assert.equal(osx.name(), 'OS X');
217
218             let webkit = Repository.findById(11);
219             assert.equal(webkit.name(), 'WebKit');
220
221             let firstRootSet = buildRequests[0].rootSet();
222             assert.equal(buildRequests[2].rootSet(), firstRootSet);
223
224             let secondRootSet = buildRequests[1].rootSet();
225             assert.equal(buildRequests[3].rootSet(), secondRootSet);
226
227             assert.equal(firstRootSet.revisionForRepository(osx), '10.11 15A284');
228             assert.equal(firstRootSet.revisionForRepository(webkit), '191622');
229
230             assert.equal(secondRootSet.revisionForRepository(osx), '10.11 15A284');
231             assert.equal(secondRootSet.revisionForRepository(webkit), '192736');
232
233             let osxCommit = firstRootSet.commitForRepository(osx);
234             assert.equal(osxCommit.revision(), '10.11 15A284');
235             assert.equal(osxCommit, secondRootSet.commitForRepository(osx));
236
237             let firstWebKitCommit = firstRootSet.commitForRepository(webkit);
238             assert.equal(firstWebKitCommit.revision(), '191622');
239             assert.equal(+firstWebKitCommit.time(), 1445945816878);
240
241             let secondWebKitCommit = secondRootSet.commitForRepository(webkit);
242             assert.equal(secondWebKitCommit.revision(), '192736');
243             assert.equal(+secondWebKitCommit.time(), 1448225325650);
244
245             done();
246         }).catch(done);
247     });
248
249     it('should not include a build request if all requests in the same group had been completed', function (done) {
250         let db = TestServer.database();
251         db.connect().then(function () {
252             return MockData.addMockData(db, ['completed', 'completed', 'completed', 'completed']);
253         }).then(function () {
254             return Manifest.fetch();
255         }).then(function () {
256             return BuildRequest.fetchForTriggerable('build-webkit');
257         }).then(function (buildRequests) {
258             assert.equal(buildRequests.length, 0);
259             done();
260         }).catch(done);
261     });
262
263     it('should not include a build request if all requests in the same group had been failed or cancled', function (done) {
264         let db = TestServer.database();
265         db.connect().then(function () {
266             return MockData.addMockData(db, ['failed', 'failed', 'canceled', 'canceled']);
267         }).then(function () {
268             return Manifest.fetch();
269         }).then(function () {
270             return BuildRequest.fetchForTriggerable('build-webkit');
271         }).then(function (buildRequests) {
272             assert.equal(buildRequests.length, 0);
273             done();
274         }).catch(done);
275     });
276
277     it('should include all build requests of a test group if one of the reqeusts in the group had not been finished', function (done) {
278         let db = TestServer.database();
279         db.connect().then(function () {
280             return MockData.addMockData(db, ['completed', 'completed', 'scheduled', 'pending']);
281         }).then(function () {
282             return Manifest.fetch();
283         }).then(function () {
284             return BuildRequest.fetchForTriggerable('build-webkit');
285         }).then(function (buildRequests) {
286             assert.equal(buildRequests.length, 4);
287             assert.ok(buildRequests[0].hasFinished());
288             assert.ok(buildRequests[0].hasStarted());
289             assert.ok(!buildRequests[0].isPending());
290             assert.ok(buildRequests[1].hasFinished());
291             assert.ok(buildRequests[1].hasStarted());
292             assert.ok(!buildRequests[1].isPending());
293             assert.ok(!buildRequests[2].hasFinished());
294             assert.ok(buildRequests[2].hasStarted());
295             assert.ok(!buildRequests[2].isPending());
296             assert.ok(!buildRequests[3].hasFinished());
297             assert.ok(!buildRequests[3].hasStarted());
298             assert.ok(buildRequests[3].isPending());
299             done();
300         }).catch(done);
301     });
302
303     it('should include all build requests of a test group if one of the reqeusts in the group is still running', function (done) {
304         let db = TestServer.database();
305         db.connect().then(function () {
306             return MockData.addMockData(db, ['completed', 'completed', 'completed', 'running']);
307         }).then(function () {
308             return Manifest.fetch();
309         }).then(function () {
310             return BuildRequest.fetchForTriggerable('build-webkit');
311         }).then(function (buildRequests) {
312             assert.equal(buildRequests.length, 4);
313             assert.ok(buildRequests[0].hasFinished());
314             assert.ok(buildRequests[0].hasStarted());
315             assert.ok(!buildRequests[0].isPending());
316             assert.ok(buildRequests[1].hasFinished());
317             assert.ok(buildRequests[1].hasStarted());
318             assert.ok(!buildRequests[1].isPending());
319             assert.ok(buildRequests[2].hasFinished());
320             assert.ok(buildRequests[2].hasStarted());
321             assert.ok(!buildRequests[2].isPending());
322             assert.ok(!buildRequests[3].hasFinished());
323             assert.ok(buildRequests[3].hasStarted());
324             assert.ok(!buildRequests[3].isPending());
325             done();
326         }).catch(done);
327     });
328
329     it('should order build requests based on test group creation time and order', function (done) {
330         let db = TestServer.database();
331         db.connect().then(function () {
332             return Promise.all([MockData.addMockData(db), MockData.addAnotherMockTestGroup(db)]);
333         }).then(function () {
334             return Manifest.fetch();
335         }).then(function () {
336             return BuildRequest.fetchForTriggerable('build-webkit');
337         }).then(function (buildRequests) {
338             assert.equal(buildRequests.length, 8);
339             assert.equal(buildRequests[0].id(), 700);
340             assert.equal(buildRequests[0].testGroupId(), 600);
341             assert.strictEqual(buildRequests[0].order(), 0);
342             assert.equal(buildRequests[1].id(), 701);
343             assert.equal(buildRequests[1].testGroupId(), 600);
344             assert.strictEqual(buildRequests[1].order(), 1);
345             assert.equal(buildRequests[2].id(), 702);
346             assert.equal(buildRequests[2].testGroupId(), 600);
347             assert.strictEqual(buildRequests[2].order(), 2);
348             assert.equal(buildRequests[3].id(), 703);
349             assert.equal(buildRequests[3].testGroupId(), 600);
350             assert.strictEqual(buildRequests[3].order(), 3);
351
352             assert.equal(buildRequests[4].id(), 710);
353             assert.equal(buildRequests[4].testGroupId(), 601);
354             assert.strictEqual(buildRequests[4].order(), 0);
355             assert.equal(buildRequests[5].id(), 711);
356             assert.equal(buildRequests[5].testGroupId(), 601);
357             assert.strictEqual(buildRequests[5].order(), 1);
358             assert.equal(buildRequests[6].id(), 712);
359             assert.equal(buildRequests[6].testGroupId(), 601);
360             assert.strictEqual(buildRequests[6].order(), 2);
361             assert.equal(buildRequests[7].id(), 713);
362             assert.equal(buildRequests[7].testGroupId(), 601);
363             assert.strictEqual(buildRequests[7].order(), 3);
364             done();
365         }).catch(done);
366     });
367
368     it('should place build requests created by user before automatically created ones', function (done) {
369         let db = TestServer.database();
370         db.connect().then(function () {
371             return Promise.all([MockData.addMockData(db), MockData.addAnotherMockTestGroup(db, null, 'rniwa')]);
372         }).then(function () {
373             return Manifest.fetch();
374         }).then(function () {
375             return BuildRequest.fetchForTriggerable('build-webkit');
376         }).then(function (buildRequests) {
377             assert.equal(buildRequests.length, 8);
378             assert.equal(buildRequests[0].id(), 710);
379             assert.equal(buildRequests[0].testGroupId(), 601);
380             assert.strictEqual(buildRequests[0].order(), 0);
381             assert.equal(buildRequests[1].id(), 711);
382             assert.equal(buildRequests[1].testGroupId(), 601);
383             assert.strictEqual(buildRequests[1].order(), 1);
384             assert.equal(buildRequests[2].id(), 712);
385             assert.equal(buildRequests[2].testGroupId(), 601);
386             assert.strictEqual(buildRequests[2].order(), 2);
387             assert.equal(buildRequests[3].id(), 713);
388             assert.equal(buildRequests[3].testGroupId(), 601);
389             assert.strictEqual(buildRequests[3].order(), 3);
390
391             assert.equal(buildRequests[4].id(), 700);
392             assert.equal(buildRequests[4].testGroupId(), 600);
393             assert.strictEqual(buildRequests[4].order(), 0);
394             assert.equal(buildRequests[5].id(), 701);
395             assert.equal(buildRequests[5].testGroupId(), 600);
396             assert.strictEqual(buildRequests[5].order(), 1);
397             assert.equal(buildRequests[6].id(), 702);
398             assert.equal(buildRequests[6].testGroupId(), 600);
399             assert.strictEqual(buildRequests[6].order(), 2);
400             assert.equal(buildRequests[7].id(), 703);
401             assert.equal(buildRequests[7].testGroupId(), 600);
402             assert.strictEqual(buildRequests[7].order(), 3);
403             done();
404         }).catch(done);
405     });
406 });