REGRESSION(r230960): Browser tests under TimeSeriesChart fetchMeasurementSets all...
[WebKit-https.git] / Websites / perf.webkit.org / unit-tests / analysis-task-tests.js
1 'use strict';
2
3 const assert = require('assert');
4
5 require('../tools/js/v3-models.js');
6 const MockModels = require('./resources/mock-v3-models.js').MockModels;
7 const MockRemoteAPI = require('./resources/mock-remote-api.js').MockRemoteAPI;
8 const BrowserPrivilegedAPI = require('../public/v3/privileged-api.js').PrivilegedAPI;
9 const NodePrivilegedAPI = require('../tools/js/privileged-api').PrivilegedAPI;
10
11 function sampleAnalysisTask()
12 {
13     return {
14         'analysisTasks': [
15             {
16                 'author': null,
17                 'bugs': [],
18                 'buildRequestCount': '14',
19                 'finishedBuildRequestCount': '6',
20                 'category': 'identified',
21                 'causes': [
22                     '105975'
23                 ],
24                 'createdAt': 1454594330000,
25                 'endRun': '37253448',
26                 'endRunTime': 1454515020303,
27                 'fixes': [],
28                 'id': '1082',
29                 'metric': '2884',
30                 'name': 'Potential 1.2% regression between 2016-02-02 20:20 and 02-03 15:57',
31                 'needed': null,
32                 'platform': '65',
33                 'result': 'regression',
34                 'segmentationStrategy': '1',
35                 'startRun': '37117949',
36                 'startRunTime': 1454444458791,
37                 'testRangeStragegy': '2'
38             }
39         ],
40         'bugs': [],
41         'commits': [
42             {
43                 'authorEmail': 'commit-queue@webkit.org',
44                 'authorName': 'Commit Queue',
45                 'id': '105975',
46                 'message': 'Commit message',
47                 'order': null,
48                 'previousCommit': null,
49                 'repository': '11',
50                 'revision': '196051',
51                 'time': 1454481246108
52             }
53         ],
54         'status': 'OK'
55     };
56 }
57
58 function measurementCluster()
59 {
60     return {
61         'clusterSize': 5184000000,
62         'clusterStart': 946684800000,
63         'configurations': {
64             'current': [
65                 [
66                     37188161,
67                     124.15015662116,
68                     25,
69                     3103.7539155291,
70                     385398.06003414,
71                     false,
72                     [
73                         [
74                             105978,
75                             10,
76                             '791451',
77                             null,
78                             1454481204649
79                         ],
80                         [
81                             105975,
82                             11,
83                             '196051',
84                             null,
85                             1454481246108
86                         ],
87                         [
88                             105502,
89                             9,
90                             '10.11 15D21',
91                             1504021,
92                             0
93                         ]
94                     ],
95                     1454481246108,
96                     227020,
97                     1454492139496,
98                     '3151',
99                     179
100                 ],
101             ],
102         },
103         'endTime': 1454716800000,
104         'formatMap': [
105             'id',
106             'mean',
107             'iterationCount',
108             'sum',
109             'squareSum',
110             'markedOutlier',
111             'revisions',
112             'commitTime',
113             'build',
114             'buildTime',
115             'buildNumber',
116             'builder'
117         ],
118         'lastModified': 1455236216153,
119         'startTime': 1449532800000,
120         'status': 'OK'
121     };
122 }
123
124 describe('AnalysisTask', () => {
125     MockModels.inject();
126     function makeMockPoints(id, commitSet) {
127         return {
128             id,
129             commitSet: () => commitSet
130         }
131     }
132
133     describe('fetchAll', () => {
134         const requests = MockRemoteAPI.inject(null, BrowserPrivilegedAPI);
135         it('should request all analysis tasks', () => {
136             let callCount = 0;
137             AnalysisTask.fetchAll().then(() => { callCount++; });
138             assert.equal(callCount, 0);
139             assert.equal(requests.length, 1);
140             assert.equal(requests[0].url, '/api/analysis-tasks');
141         });
142
143         it('should not request all analysis tasks multiple times', () => {
144             let callCount = 0;
145             AnalysisTask.fetchAll().then(() => { callCount++; });
146             assert.equal(callCount, 0);
147             assert.equal(requests.length, 1);
148             assert.equal(requests[0].url, '/api/analysis-tasks');
149
150             AnalysisTask.fetchAll().then(() => { callCount++; });
151             assert.equal(callCount, 0);
152             assert.equal(requests.length, 1);
153         });
154
155         it('should resolve the promise when the request is fullfilled', () => {
156             let callCount = 0;
157             const promise = AnalysisTask.fetchAll().then(() => { callCount++; });
158             assert.equal(callCount, 0);
159             assert.equal(requests.length, 1);
160             assert.equal(requests[0].url, '/api/analysis-tasks');
161
162             requests[0].resolve(sampleAnalysisTask());
163
164             let anotherCallCount = 0;
165             return promise.then(() => {
166                 assert.equal(callCount, 1);
167                 AnalysisTask.fetchAll().then(() => { anotherCallCount++; });
168             }).then(() => {
169                 assert.equal(callCount, 1);
170                 assert.equal(anotherCallCount, 1);
171                 assert.equal(requests.length, 1);
172             });
173         });
174
175         it('should create AnalysisTask objects', () => {
176             const promise = AnalysisTask.fetchAll();
177             requests[0].resolve(sampleAnalysisTask());
178
179             return promise.then(() => {
180                 assert.equal(AnalysisTask.all().length, 1);
181                 var task = AnalysisTask.all()[0];
182                 assert.equal(task.id(), 1082);
183                 assert.equal(task.metric(), MockModels.someMetric);
184                 assert.equal(task.platform(), MockModels.somePlatform);
185                 assert.ok(task.hasResults());
186                 assert.ok(task.hasPendingRequests());
187                 assert.equal(task.requestLabel(), '6 of 14');
188                 assert.equal(task.category(), 'investigated');
189                 assert.equal(task.changeType(), 'regression');
190                 assert.equal(task.startMeasurementId(), 37117949);
191                 assert.equal(task.startTime(), 1454444458791);
192                 assert.equal(task.endMeasurementId(), 37253448);
193                 assert.equal(task.endTime(), 1454515020303);
194             });
195         });
196
197         it('should create CommitLog objects for `causes`', () => {
198             const promise = AnalysisTask.fetchAll();
199             requests[0].resolve(sampleAnalysisTask());
200
201             return promise.then(() => {
202                 assert.equal(AnalysisTask.all().length, 1);
203                 var task = AnalysisTask.all()[0];
204
205                 assert.equal(task.causes().length, 1);
206                 var commit = task.causes()[0];
207
208                 assert.equal(commit.revision(), '196051');
209                 assert.equal(commit.repository(), MockModels.webkit);
210                 assert.equal(+commit.time(), 1454481246108);
211             });
212         });
213
214         it('should find CommitLog objects for `causes` when MeasurementAdaptor created matching objects', () => {
215             const adaptor = new MeasurementAdaptor(measurementCluster().formatMap);
216             const adaptedMeasurement = adaptor.applyTo(measurementCluster().configurations.current[0]);
217             assert.equal(adaptedMeasurement.id, 37188161);
218             assert.equal(adaptedMeasurement.commitSet().commitForRepository(MockModels.webkit).revision(), '196051');
219
220             const promise = AnalysisTask.fetchAll();
221             requests[0].resolve(sampleAnalysisTask());
222
223             return promise.then(() => {
224                 assert.equal(AnalysisTask.all().length, 1);
225                 var task = AnalysisTask.all()[0];
226
227                 assert.equal(task.causes().length, 1);
228                 var commit = task.causes()[0];
229                 assert.equal(commit.revision(), '196051');
230                 assert.equal(commit.repository(), MockModels.webkit);
231                 assert.equal(+commit.time(), 1454481246108);
232             });
233         });
234     });
235
236
237     function mockStartAndEndPoints() {
238         const startPoint = makeMockPoints(1, new MeasurementCommitSet(1, [
239             [1, MockModels.ios.id(), 'ios-revision-1', null, 0],
240             [3, MockModels.webkit.id(), 'webkit-revision-1', null, 0]
241         ]));
242         const endPoint = makeMockPoints(2, new MeasurementCommitSet(2, [
243             [2, MockModels.ios.id(), 'ios-revision-2', null, 0],
244             [4, MockModels.webkit.id(), 'webkit-revision-2', null, 0]
245         ]));
246         return [startPoint, endPoint];
247     }
248
249     describe('create with browser privilege api', () => {
250         const requests = MockRemoteAPI.inject(null, BrowserPrivilegedAPI);
251
252         it('should create analysis task with confirming repetition count zero as default with browser privilege api', async () => {
253             const [startPoint, endPoint] = mockStartAndEndPoints();
254             AnalysisTask.create('confirm', startPoint, endPoint);
255             assert.equal(requests.length, 1);
256             assert.equal(requests[0].url, '/privileged-api/generate-csrf-token');
257             requests[0].resolve({
258                 token: 'abc',
259                 expiration: Date.now() + 3600 * 1000,
260             });
261
262             await MockRemoteAPI.waitForRequest();
263             assert.equal(requests[1].url, '/privileged-api/create-analysis-task');
264             assert.equal(requests.length, 2);
265             assert.deepEqual(requests[1].data, {name: 'confirm', startRun: 1, endRun: 2, token: 'abc'});
266         });
267
268         it('should create analysis task with confirming repetition count specified', async () => {
269             const [startPoint, endPoint] = mockStartAndEndPoints();
270             AnalysisTask.create('confirm', startPoint, endPoint, 'Confirm', 4);
271             assert.equal(requests.length, 1);
272             assert.equal(requests[0].url, '/privileged-api/generate-csrf-token');
273             requests[0].resolve({
274                 token: 'abc',
275                 expiration: Date.now() + 3600 * 1000,
276             });
277
278             await MockRemoteAPI.waitForRequest();
279             assert.equal(requests[1].url, '/privileged-api/create-analysis-task');
280             assert.equal(requests.length, 2);
281             assert.deepEqual(requests[1].data, {name: 'confirm', repetitionCount: 4,
282                 startRun: 1, endRun: 2, testGroupName: 'Confirm', token: 'abc', revisionSets: [
283                     {'11': {revision: 'webkit-revision-1', ownerRevision: null, patch: null},
284                         '22': {revision: 'ios-revision-1', ownerRevision: null, patch: null}},
285                     {'11': {revision: 'webkit-revision-2', ownerRevision: null, patch: null},
286                         '22': { revision: 'ios-revision-2', ownerRevision: null, patch: null}}]}
287             );
288         });
289     });
290
291     describe('create with node privilege api', () => {
292         const requests = MockRemoteAPI.inject(null, NodePrivilegedAPI);
293         beforeEach(() => {
294             PrivilegedAPI.configure('worker', 'password');
295         });
296
297         it('should create analysis task with confirming repetition count zero as default with browser privilege api', () => {
298             const [startPoint, endPoint] = mockStartAndEndPoints();
299             AnalysisTask.create('confirm', startPoint, endPoint);
300             assert.equal(requests[0].url, '/privileged-api/create-analysis-task');
301             assert.equal(requests.length, 1);
302             assert.deepEqual(requests[0].data, {name: 'confirm', startRun: 1, endRun: 2, slaveName: 'worker', slavePassword: 'password'});
303         });
304
305         it('should create analysis task with confirming repetition count specified', () => {
306             const [startPoint, endPoint] = mockStartAndEndPoints();
307             AnalysisTask.create('confirm', startPoint, endPoint, 'Confirm', 4);
308             assert.equal(requests[0].url, '/privileged-api/create-analysis-task');
309             assert.equal(requests.length, 1);
310             assert.deepEqual(requests[0].data, {name: 'confirm', repetitionCount: 4,
311                 startRun: 1, endRun: 2, slaveName: 'worker', slavePassword: 'password',
312                 testGroupName: 'Confirm', revisionSets: [
313                     {'11': {revision: 'webkit-revision-1', ownerRevision: null, patch: null},
314                         '22': {revision: 'ios-revision-1', ownerRevision: null, patch: null}},
315                     {'11': {revision: 'webkit-revision-2', ownerRevision: null, patch: null},
316                         '22': { revision: 'ios-revision-2', ownerRevision: null, patch: null}}]}
317             );
318         });
319     });
320 });