build.webkit.org/dashboard: Move layoutTestResultsDirectoryURLForIteration implementa...
[WebKit-https.git] / Websites / perf.webkit.org / tests / api-report-commits.js
1 describe("/api/report-commits/", function () {
2     var emptyReport = {
3         "slaveName": "someSlave",
4         "slavePassword": "somePassword",
5     };
6     var subversionCommit = {
7         "slaveName": "someSlave",
8         "slavePassword": "somePassword",
9         "commits": [
10             {
11                 "repository": "WebKit",
12                 "revision": "141977",
13                 "time": "2013-02-06T08:55:20.9Z",
14                 "author": {"name": "Commit Queue", "account": "commit-queue@webkit.org"},
15                 "message": "some message",
16             }
17         ],
18     };
19     var subversionInvalidCommit = {
20         "slaveName": "someSlave",
21         "slavePassword": "somePassword",
22         "commits": [
23             {
24                 "repository": "WebKit",
25                 "revision": "_141977",
26                 "time": "2013-02-06T08:55:20.9Z",
27                 "author": {"name": "Commit Queue", "account": "commit-queue@webkit.org"},
28                 "message": "some message",
29             }
30         ],
31     };
32     var subversionTwoCommits = {
33         "slaveName": "someSlave",
34         "slavePassword": "somePassword",
35         "commits": [
36             {
37                 "repository": "WebKit",
38                 "revision": "141977",
39                 "time": "2013-02-06T08:55:20.9Z",
40                 "author": {"name": "Commit Queue", "account": "commit-queue@webkit.org"},
41                 "message": "some message",
42             },
43             {
44                 "repository": "WebKit",
45                 "parent": "141977",
46                 "revision": "141978",
47                 "time": "2013-02-06T09:54:56.0Z",
48                 "author": {"name": "Mikhail Pozdnyakov", "account": "mikhail.pozdnyakov@intel.com"},
49                 "message": "another message",
50             }
51         ]
52     }
53
54     function addSlave(report, callback) {
55         queryAndFetchAll('INSERT INTO build_slaves (slave_name, slave_password_hash) values ($1, $2)',
56             [report.slaveName, sha256(report.slavePassword)], callback);
57     }
58
59     it("should reject error when slave name is missing", function () {
60         postJSON('/api/report-commits/', {}, function (response) {
61             assert.equal(response.statusCode, 200);
62             assert.equal(JSON.parse(response.responseText)['status'], 'MissingSlaveName');
63             notifyDone();
64         });
65     });
66
67     it("should reject when there are no slaves", function () {
68         postJSON('/api/report-commits/', emptyReport, function (response) {
69             assert.equal(response.statusCode, 200);
70             assert.notEqual(JSON.parse(response.responseText)['status'], 'OK');
71
72             queryAndFetchAll('SELECT COUNT(*) from commits', [], function (rows) {
73                 assert.equal(rows[0].count, 0);
74                 notifyDone();
75             });
76         });
77     });
78
79     it("should accept an empty report", function () {
80         addSlave(emptyReport, function () {
81             postJSON('/api/report-commits/', emptyReport, function (response) {
82                 assert.equal(response.statusCode, 200);
83                 assert.equal(JSON.parse(response.responseText)['status'], 'OK');
84                 notifyDone();
85             });
86         });
87     });
88
89     it("should add a missing repository", function () {
90         addSlave(subversionCommit, function () {
91             postJSON('/api/report-commits/', subversionCommit, function (response) {
92                 assert.equal(response.statusCode, 200);
93                 assert.equal(JSON.parse(response.responseText)['status'], 'OK');
94                 queryAndFetchAll('SELECT * FROM repositories', [], function (rows) {
95                     assert.equal(rows.length, 1);
96                     assert.equal(rows[0]['repository_name'], subversionCommit.commits[0]['repository']);
97                     notifyDone();
98                 });
99             });
100         });
101     });
102
103     it("should store a commit from a valid slave", function () {
104         addSlave(subversionCommit, function () {
105             postJSON('/api/report-commits/', subversionCommit, function (response) {
106                 assert.equal(response.statusCode, 200);
107                 assert.equal(JSON.parse(response.responseText)['status'], 'OK');
108                 queryAndFetchAll('SELECT * FROM commits JOIN committers ON commit_committer = committer_id', [], function (rows) {
109                     assert.equal(rows.length, 1);
110                     var reportedData = subversionCommit.commits[0];
111                     assert.equal(rows[0]['commit_revision'], reportedData['revision']);
112                     assert.equal(rows[0]['commit_time'].toString(), new Date('2013-02-06 08:55:20.9').toString());
113                     assert.equal(rows[0]['committer_name'], reportedData['author']['name']);
114                     assert.equal(rows[0]['committer_account'], reportedData['author']['account']);
115                     assert.equal(rows[0]['commit_message'], reportedData['message']);
116                     notifyDone();
117                 });
118             });
119         });
120     });
121
122     it("should reject an invalid revision number", function () {
123         addSlave(subversionCommit, function () {
124             subversionCommit
125             postJSON('/api/report-commits/', subversionInvalidCommit, function (response) {
126                 assert.equal(response.statusCode, 200);
127                 assert.notEqual(JSON.parse(response.responseText)['status'], 'OK');
128                 queryAndFetchAll('SELECT * FROM commits', [], function (rows) {
129                     assert.equal(rows.length, 0);
130                     notifyDone();
131                 });
132             });
133         });
134     });
135
136     it("should store two commits from a valid slave", function () {
137         addSlave(subversionTwoCommits, function () {
138             postJSON('/api/report-commits/', subversionTwoCommits, function (response) {
139                 assert.equal(response.statusCode, 200);
140                 assert.equal(JSON.parse(response.responseText)['status'], 'OK');
141                 queryAndFetchAll('SELECT * FROM commits JOIN committers ON commit_committer = committer_id ORDER BY commit_time', [], function (rows) {
142                     assert.equal(rows.length, 2);
143                     var reportedData = subversionTwoCommits.commits[0];
144                     assert.equal(rows[0]['commit_revision'], reportedData['revision']);
145                     assert.equal(rows[0]['commit_time'].toString(), new Date('2013-02-06 08:55:20.9').toString());
146                     assert.equal(rows[0]['committer_name'], reportedData['author']['name']);
147                     assert.equal(rows[0]['committer_account'], reportedData['author']['account']);
148                     assert.equal(rows[0]['commit_message'], reportedData['message']);
149                     var reportedData = subversionTwoCommits.commits[1];
150                     assert.equal(rows[1]['commit_revision'], reportedData['revision']);
151                     assert.equal(rows[1]['commit_time'].toString(), new Date('2013-02-06 09:54:56.0').toString());
152                     assert.equal(rows[1]['committer_name'], reportedData['author']['name']);
153                     assert.equal(rows[1]['committer_account'], reportedData['author']['account']);
154                     assert.equal(rows[1]['commit_message'], reportedData['message']);
155                     notifyDone();
156                 });
157             });
158         });
159     });
160
161     it("should update an existing commit if there is one", function () {
162         queryAndFetchAll('INSERT INTO repositories (repository_name) VALUES ($1) RETURNING *', ['WebKit'], function (repositories) {
163             var repositoryId = repositories[0]['repository_id'];
164             var reportedData = subversionCommit.commits[0];
165             queryAndFetchAll('INSERT INTO commits (commit_repository, commit_revision, commit_time) VALUES ($1, $2, $3) RETURNING *',
166                 [repositoryId, reportedData['revision'], reportedData['time']], function (existingCommits) {
167                 var commitId = existingCommits[0]['commit_id'];
168                 assert.equal(existingCommits[0]['commit_message'], null);
169                 addSlave(subversionCommit, function () {
170                     postJSON('/api/report-commits/', subversionCommit, function (response) {
171                         assert.equal(response.statusCode, 200);
172                         assert.equal(JSON.parse(response.responseText)['status'], 'OK');
173                         queryAndFetchAll('SELECT * FROM commits JOIN committers ON commit_committer = committer_id', [], function (rows) {
174                             assert.equal(rows.length, 1);
175                             var reportedData = subversionCommit.commits[0];
176                             assert.equal(rows[0]['committer_name'], reportedData['author']['name']);
177                             assert.equal(rows[0]['committer_account'], reportedData['author']['account']);
178                             assert.equal(rows[0]['commit_message'], reportedData['message']);
179                             notifyDone();
180                         });
181                     });
182                 });
183             });
184         });
185     });
186
187     it("should not update an unrelated commit", function () {
188         queryAndFetchAll('INSERT INTO repositories (repository_name) VALUES ($1) RETURNING *', ['WebKit'], function (repositories) {
189             var repositoryId = repositories[0]['repository_id'];
190             var reportedData = subversionTwoCommits.commits[1];
191             queryAndFetchAll('INSERT INTO commits (commit_repository, commit_revision, commit_time) VALUES ($1, $2, $3) RETURNING *',
192                 [repositoryId, reportedData['revision'], reportedData['time']], function (existingCommits) {
193                 reportedData = subversionTwoCommits.commits[0];
194                 queryAndFetchAll('INSERT INTO commits (commit_repository, commit_revision, commit_time) VALUES ($1, $2, $3) RETURNING *',
195                     [repositoryId, reportedData['revision'], reportedData['time']], function () {
196                         addSlave(subversionCommit, function () {
197                             postJSON('/api/report-commits/', subversionCommit, function (response) {
198                                 assert.equal(response.statusCode, 200);
199                                 assert.equal(JSON.parse(response.responseText)['status'], 'OK');
200                                 queryAndFetchAll('SELECT * FROM commits LEFT OUTER JOIN committers ON commit_committer = committer_id ORDER BY commit_time', [], function (rows) {
201                                     assert.equal(rows.length, 2);
202                                     assert.equal(rows[0]['committer_name'], reportedData['author']['name']);
203                                     assert.equal(rows[0]['committer_account'], reportedData['author']['account']);
204                                     assert.equal(rows[0]['commit_message'], reportedData['message']);
205                                     assert.equal(rows[1]['committer_name'], null);
206                                     assert.equal(rows[1]['committer_account'], null);
207                                     assert.equal(rows[1]['commit_message'], null);
208                                     notifyDone();
209                                 });
210                             });
211                         });
212                 });
213             });
214         });
215     });
216
217     it("should update an existing committer if there is one", function () {
218         queryAndFetchAll('INSERT INTO repositories (repository_id, repository_name) VALUES (1, \'WebKit\')', [], function () {
219             var author = subversionCommit.commits[0]['author'];
220             queryAndFetchAll('INSERT INTO committers (committer_repository, committer_account) VALUES (1, $1)', [author['account']], function () {
221                 addSlave(subversionCommit, function () {
222                     postJSON('/api/report-commits/', subversionCommit, function (response) {
223                         assert.equal(response.statusCode, 200);
224                         assert.equal(JSON.parse(response.responseText)['status'], 'OK');
225                         queryAndFetchAll('SELECT * FROM committers', [], function (rows) {
226                             assert.equal(rows.length, 1);
227                             assert.equal(rows[0]['committer_name'], author['name']);
228                             assert.equal(rows[0]['committer_account'], author['account']);
229                             notifyDone();
230                         });
231                     });
232                 });
233             });
234         });
235     });
236
237 });