OLD | NEW |
---|---|
(Empty) | |
1 #!/usr/bin/env python | |
2 # Copyright (c) 2013 The Chromium Authors. All rights reserved. | |
3 # Use of this source code is governed by a BSD-style license that can be | |
4 # found in the LICENSE file. | |
5 | |
6 """ Source file for results_dashboard testcases.""" | |
7 | |
8 import json | |
9 import os | |
10 import shutil | |
11 import tempfile | |
12 import unittest | |
13 import urllib | |
14 import urllib2 | |
15 | |
16 import test_env # pylint: disable=W0403,W0611 | |
17 | |
18 from slave import results_dashboard | |
19 from slave import slave_utils | |
20 from testing_support.super_mox import mox | |
21 | |
22 | |
23 class IsEncodedJson(mox.Comparator): | |
24 def __init__(self, expected_json): | |
25 self._json = expected_json | |
26 | |
27 def equals(self, rhs): | |
28 rhs_json = urllib.unquote_plus(rhs.data.replace("data=", "")) | |
29 return sorted(json.loads(self._json)) == sorted(json.loads(rhs_json)) | |
30 | |
31 def __repr__(self): | |
32 return "<Is Request JSON %s>" % self._json | |
33 | |
34 | |
35 class ResultsDashboardTest(unittest.TestCase): | |
36 def setUp(self): | |
37 super(ResultsDashboardTest, self).setUp() | |
38 self.mox = mox.Mox() | |
39 self.build_dir = tempfile.mkdtemp() | |
40 os.makedirs(os.path.join(self.build_dir, results_dashboard.CACHE_DIR)) | |
41 self.cache_filename = os.path.join(self.build_dir, | |
42 results_dashboard.CACHE_DIR, | |
43 results_dashboard.CACHE_FILENAME) | |
44 | |
45 def tearDown(self): | |
46 self.mox.UnsetStubs() | |
47 shutil.rmtree(self.build_dir) | |
48 | |
49 def _SendResults(self, send_results_args, expected_new_json, cached_json, | |
50 errors, expected_cache): | |
51 cache_file = open(self.cache_filename, "wb") | |
52 cache_file.write("\n".join(cached_json)) | |
James Simonsen
2013/03/01 21:21:49
I don't think there's anything that tests that the
sullivan
2013/03/04 16:38:36
Done.
| |
53 cache_file.close() | |
54 self.mox.StubOutWithMock(slave_utils, "GetActiveMaster") | |
55 slave_utils.GetActiveMaster().AndReturn("ChromiumPerf") | |
56 self.mox.StubOutWithMock(urllib2, "urlopen") | |
57 for json_line, error in zip(cached_json + [expected_new_json], errors): | |
58 if error: | |
59 urllib2.urlopen(IsEncodedJson(json_line)).AndRaise(error) | |
60 else: | |
61 urllib2.urlopen(IsEncodedJson(json_line)) | |
62 self.mox.ReplayAll() | |
63 send_results_args.append(self.build_dir) | |
64 results_dashboard.SendResults(*send_results_args) | |
65 self.mox.VerifyAll() | |
66 cache_file = open(self.cache_filename, "rb") | |
67 actual_cache = cache_file.read() | |
68 cache_file.close() | |
69 self.assertEqual(expected_cache, actual_cache) | |
James Simonsen
2013/03/01 21:21:49
And then you wouldn't need to test this. Just make
sullivan
2013/03/04 16:38:36
Done.
| |
70 | |
71 def test_SingleLogLine(self): | |
72 args = [ | |
73 "bar-summary.dat", | |
74 ['{"traces": {"baz": ["100.0", "5.0"]},' | |
75 ' "rev": "12345", "webkit_rev": "6789"}'], | |
76 "linux-release", | |
77 "foo", | |
78 "https://perf-dashboard.appspot.com"] | |
79 expected_new_json = json.dumps([{ | |
80 "master": "ChromiumPerf", | |
81 "bot": "linux-release", | |
82 "test": "foo/bar/baz", | |
83 "revision": "12345", | |
84 "value": "100.0", | |
85 "error": "5.0", | |
86 "supplemental_columns": { | |
87 "r_webkit_rev": "6789", | |
88 }}]) | |
89 cached_json = [] | |
90 errors = [None] | |
91 expected_cache = "" | |
92 self._SendResults( | |
93 args, expected_new_json, cached_json, errors, expected_cache) | |
94 | |
95 def test_MultipleLogLines(self): | |
96 args = [ | |
97 "bar-summary.dat", [ | |
98 '{"traces": {"baz": ["100.0", "5.0"]},' | |
99 ' "rev": "12345", "webkit_rev": "6789"}', | |
100 '{"traces": {"box": ["101.0", "4.0"]},' | |
101 ' "rev": "12345", "webkit_rev": "6789"}'], | |
102 "linux-release", | |
103 "foo", | |
104 "https://perf-dashboard.appspot.com"] | |
105 expected_new_json = json.dumps([{ | |
106 "master": "ChromiumPerf", | |
107 "bot": "linux-release", | |
108 "test": "foo/bar/baz", | |
109 "revision": "12345", | |
110 "value": "100.0", | |
111 "error": "5.0", | |
112 "supplemental_columns": { | |
113 "r_webkit_rev": "6789", | |
114 }}, { | |
115 "master": "ChromiumPerf", | |
116 "bot": "linux-release", | |
117 "test": "foo/bar/box", | |
118 "revision": "12345", | |
119 "value": "101.0", | |
120 "error": "4.0", | |
121 "supplemental_columns": { | |
122 "r_webkit_rev": "6789", | |
123 }}]) | |
124 cached_json = [] | |
125 errors = [None] | |
126 expected_cache = "" | |
127 self._SendResults( | |
128 args, expected_new_json, cached_json, errors, expected_cache) | |
129 | |
130 def test_ModifiedTraceNames(self): | |
131 args = [ | |
132 "bar-summary.dat", | |
133 ['{"traces": {"bar": ["100.0", "5.0"], "bar_ref": ["99.0", "2.0"],' | |
134 ' "baz/y": ["101.0", "3.0"], "notchanged": ["102.0", "1.0"]},' | |
135 ' "rev": "12345", "webkit_rev": "6789"}'], | |
136 "linux-release", | |
137 "foo", | |
138 "https://perf-dashboard.appspot.com"] | |
139 expected_new_json = json.dumps([{ | |
140 "master": "ChromiumPerf", | |
141 "bot": "linux-release", | |
142 "test": "foo/bar", | |
143 "revision": "12345", | |
144 "value": "100.0", | |
145 "error": "5.0", | |
146 "supplemental_columns": { | |
147 "r_webkit_rev": "6789", | |
148 }},{ | |
149 "master": "ChromiumPerf", | |
150 "bot": "linux-release", | |
151 "test": "foo/bar/ref", | |
152 "revision": "12345", | |
153 "value": "99.0", | |
154 "error": "2.0", | |
155 "supplemental_columns": { | |
156 "r_webkit_rev": "6789", | |
157 }}, { | |
158 "master": "ChromiumPerf", | |
159 "bot": "linux-release", | |
160 "test": "foo/bar/baz_y", | |
161 "revision": "12345", | |
162 "value": "101.0", | |
163 "error": "3.0", | |
164 "supplemental_columns": { | |
165 "r_webkit_rev": "6789", | |
166 }},{ | |
167 "master": "ChromiumPerf", | |
168 "bot": "linux-release", | |
169 "test": "foo/bar/notchanged", | |
170 "revision": "12345", | |
171 "value": "102.0", | |
172 "error": "1.0", | |
173 "supplemental_columns": { | |
174 "r_webkit_rev": "6789", | |
175 }}]) | |
176 cached_json = [] | |
177 errors = [None] | |
178 expected_cache = "" | |
179 self._SendResults( | |
180 args, expected_new_json, cached_json, errors, expected_cache) | |
181 | |
182 def test_ByUrlGraph(self): | |
183 args = [ | |
184 "bar_by_url-summary.dat", | |
185 ['{"traces": {"baz": ["100.0", "5.0"]},' | |
186 ' "rev": "12345", "webkit_rev": "6789"}'], | |
187 "linux-release", | |
188 "foo", | |
189 "https://perf-dashboard.appspot.com"] | |
190 expected_new_json = json.dumps([{ | |
191 "master": "ChromiumPerf", | |
192 "bot": "linux-release", | |
193 "test": "foo/bar/baz", | |
194 "revision": "12345", | |
195 "value": "100.0", | |
196 "error": "5.0", | |
197 "supplemental_columns": { | |
198 "r_webkit_rev": "6789", | |
199 }}]) | |
200 cached_json = [] | |
201 errors = [None] | |
202 expected_cache = "" | |
203 self._SendResults( | |
204 args, expected_new_json, cached_json, errors, expected_cache) | |
205 | |
206 def test_FailureRetried(self): | |
207 args = [ | |
208 "bar-summary.dat", | |
209 ['{"traces": {"baz": ["100.0", "5.0"]},' | |
210 ' "rev": "12345", "webkit_rev": "6789"}'], | |
211 "linux-release", | |
212 "foo", | |
213 "https://perf-dashboard.appspot.com"] | |
214 expected_new_json = json.dumps([{ | |
215 "master": "ChromiumPerf", | |
216 "bot": "linux-release", | |
217 "test": "foo/bar/baz", | |
218 "revision": "12345", | |
219 "value": "100.0", | |
220 "error": "5.0", | |
221 "supplemental_columns": { | |
222 "r_webkit_rev": "6789", | |
223 }}]) | |
224 cached_json = [json.dumps([{ | |
225 "master": "ChromiumPerf", | |
226 "bot": "linux-release", | |
227 "test": "dromaeo/dom", | |
228 "revision": "78899", | |
229 "value": "200.0", | |
230 "error": "5.0", | |
231 }]), json.dumps([{ | |
232 "master": "ChromiumPerf", | |
233 "bot": "linux-release", | |
234 "test": "dromaeo/jslib", | |
235 "revision": "78899", | |
236 "value": "300.0", | |
237 "error": "5.0", | |
238 }])] | |
239 errors = [None, None, None] | |
240 expected_cache = "" | |
241 self._SendResults( | |
242 args, expected_new_json, cached_json, errors, expected_cache) | |
243 | |
244 def test_FailureCached(self): | |
245 args = [ | |
246 "bar-summary.dat", | |
247 ['{"traces": {"baz": ["100.0", "5.0"]},' | |
248 ' "rev": "12345", "webkit_rev": "6789"}'], | |
249 "linux-release", | |
250 "foo", | |
251 "https://perf-dashboard.appspot.com"] | |
252 expected_new_json = json.dumps([{ | |
253 "master": "ChromiumPerf", | |
254 "bot": "linux-release", | |
255 "test": "foo/bar/baz", | |
256 "revision": "12345", | |
257 "value": "100.0", | |
258 "error": "5.0", | |
259 "supplemental_columns": { | |
260 "r_webkit_rev": "6789", | |
261 }}]) | |
262 cached_json = [] | |
263 errors = [urllib2.URLError("reason")] | |
264 expected_cache = expected_new_json | |
265 self._SendResults( | |
266 args, expected_new_json, cached_json, errors, expected_cache) | |
267 | |
268 def test_MultipleFaliures(self): | |
269 args = [ | |
270 "bar-summary.dat", | |
271 ['{"traces": {"baz": ["100.0", "5.0"]},' | |
272 ' "rev": "12345", "webkit_rev": "6789"}'], | |
273 "linux-release", | |
274 "foo", | |
275 "https://perf-dashboard.appspot.com"] | |
276 expected_new_json = json.dumps([{ | |
277 "master": "ChromiumPerf", | |
278 "bot": "linux-release", | |
279 "test": "foo/bar/baz", | |
280 "revision": "12345", | |
281 "value": "100.0", | |
282 "error": "5.0", | |
283 "supplemental_columns": { | |
284 "r_webkit_rev": "6789", | |
285 }}]) | |
286 cached_json = [json.dumps([{ | |
287 "master": "ChromiumPerf", | |
288 "bot": "linux-release", | |
289 "test": "dromaeo/dom", | |
290 "revision": "78899", | |
291 "value": "200.0", | |
292 "error": "5.0", | |
293 }])] | |
294 errors = [urllib2.URLError("reason"), None] | |
295 expected_cache = cached_json[0] | |
296 self._SendResults( | |
297 args, expected_new_json, cached_json, errors, expected_cache) | |
298 | |
299 | |
300 if __name__ == '__main__': | |
301 unittest.main() | |
OLD | NEW |