blob: 44f088f09ac900f7184087b45cba278a91b5227b [file] [log] [blame]
jadmanskia9894d02009-08-21 16:49:48 +00001#!/usr/bin/python
Aviv Keshet39164ca2013-03-27 15:08:33 -07002#pylint: disable-msg=C0111
mbligh234a84f2008-11-20 19:57:43 +00003"""Unit Tests for autotest.client.common_lib.test"""
4
5__author__ = 'gps@google.com (Gregory P. Smith)'
6
Keith Haddowd804bc82016-03-15 17:16:03 -07007import json
8import tempfile
mbligh234a84f2008-11-20 19:57:43 +00009import unittest
mbligh234a84f2008-11-20 19:57:43 +000010import common
Scott Zawalski91493c82013-01-25 16:15:20 -050011from autotest_lib.client.common_lib import test
mbligh234a84f2008-11-20 19:57:43 +000012from autotest_lib.client.common_lib.test_utils import mock
Aviv Keshet39164ca2013-03-27 15:08:33 -070013from autotest_lib.client.common_lib import error as common_lib_error
mbligh234a84f2008-11-20 19:57:43 +000014
mbligh4b835b82009-02-11 01:26:13 +000015class TestTestCase(unittest.TestCase):
mbligh234a84f2008-11-20 19:57:43 +000016 class _neutered_base_test(test.base_test):
17 """A child class of base_test to avoid calling the constructor."""
18 def __init__(self, *args, **kwargs):
mbligh4b835b82009-02-11 01:26:13 +000019 class MockJob(object):
20 pass
21 class MockProfilerManager(object):
22 def active(self):
23 return False
mblighc6bf6012009-10-02 00:02:15 +000024 def present(self):
25 return True
mbligh4b835b82009-02-11 01:26:13 +000026 self.job = MockJob()
showarda6082ef2009-10-12 20:25:44 +000027 self.job.default_profile_only = False
mbligh4b835b82009-02-11 01:26:13 +000028 self.job.profilers = MockProfilerManager()
Scott Zawalski91493c82013-01-25 16:15:20 -050029 self.job.test_retry = 0
jadmanskia93fbca2009-08-21 15:34:04 +000030 self._new_keyval = False
mbligh5e703a22009-06-15 22:00:12 +000031 self.iteration = 0
mbligh742ae422009-05-13 20:46:41 +000032 self.before_iteration_hooks = []
33 self.after_iteration_hooks = []
34
mbligh234a84f2008-11-20 19:57:43 +000035
36 def setUp(self):
37 self.god = mock.mock_god()
38 self.test = self._neutered_base_test()
mbligh234a84f2008-11-20 19:57:43 +000039
40
41 def tearDown(self):
42 self.god.unstub_all()
43
44
mbligh4b835b82009-02-11 01:26:13 +000045
mbligh4b835b82009-02-11 01:26:13 +000046class Test_base_test_execute(TestTestCase):
47 # Test the various behaviors of the base_test.execute() method.
48 def setUp(self):
49 TestTestCase.setUp(self)
mbligh4b835b82009-02-11 01:26:13 +000050 self.god.stub_function(self.test, 'run_once_profiling')
51 self.god.stub_function(self.test, 'postprocess')
mbligh32cb5b42009-05-01 23:05:09 +000052 self.god.stub_function(self.test, 'process_failed_constraints')
mbligh4b835b82009-02-11 01:26:13 +000053
mbligh4b835b82009-02-11 01:26:13 +000054
mbligh4395bbd2009-03-25 19:34:17 +000055 def test_call_run_once(self):
56 # setup
57 self.god.stub_function(self.test, 'drop_caches_between_iterations')
58 self.god.stub_function(self.test, 'run_once')
59 self.god.stub_function(self.test, 'postprocess_iteration')
mbligh7af09972009-04-17 22:17:08 +000060 self.god.stub_function(self.test, 'analyze_perf_constraints')
mbligh4395bbd2009-03-25 19:34:17 +000061 before_hook = self.god.create_mock_function('before_hook')
62 after_hook = self.god.create_mock_function('after_hook')
mbligh742ae422009-05-13 20:46:41 +000063 self.test.register_before_iteration_hook(before_hook)
64 self.test.register_after_iteration_hook(after_hook)
mbligh4395bbd2009-03-25 19:34:17 +000065
66 # tests the test._call_run_once implementation
67 self.test.drop_caches_between_iterations.expect_call()
showardd4ead172009-05-01 00:08:56 +000068 before_hook.expect_call(self.test)
mbligh4395bbd2009-03-25 19:34:17 +000069 self.test.run_once.expect_call(1, 2, arg='val')
mbligh4395bbd2009-03-25 19:34:17 +000070 self.test.postprocess_iteration.expect_call()
mbligh7af09972009-04-17 22:17:08 +000071 self.test.analyze_perf_constraints.expect_call([])
Eric Lidaf6ff02011-03-01 15:31:31 -080072 after_hook.expect_call(self.test)
jadmanskia93fbca2009-08-21 15:34:04 +000073 self.test._call_run_once([], False, None, (1, 2), {'arg': 'val'})
showardd4ead172009-05-01 00:08:56 +000074 self.god.check_playback()
mbligh4395bbd2009-03-25 19:34:17 +000075
76
Eric Lidaf6ff02011-03-01 15:31:31 -080077 def test_call_run_once_with_exception(self):
78 # setup
79 self.god.stub_function(self.test, 'drop_caches_between_iterations')
80 self.god.stub_function(self.test, 'run_once')
81 before_hook = self.god.create_mock_function('before_hook')
82 after_hook = self.god.create_mock_function('after_hook')
83 self.test.register_before_iteration_hook(before_hook)
84 self.test.register_after_iteration_hook(after_hook)
85 error = Exception('fail')
86
87 # tests the test._call_run_once implementation
88 self.test.drop_caches_between_iterations.expect_call()
89 before_hook.expect_call(self.test)
90 self.test.run_once.expect_call(1, 2, arg='val').and_raises(error)
91 after_hook.expect_call(self.test)
92 try:
93 self.test._call_run_once([], False, None, (1, 2), {'arg': 'val'})
94 except:
95 pass
96 self.god.check_playback()
97
98
Scott Zawalski91493c82013-01-25 16:15:20 -050099 def _setup_failed_test_calls(self, fail_count, error):
100 """
101 Set up failed test calls for use with call_run_once_with_retry.
102
103 @param fail_count: The amount of times to mock a failure.
104 @param error: The error to raise while failing.
105 """
106 self.god.stub_function(self.test.job, 'record')
107 self.god.stub_function(self.test, '_call_run_once')
108 # tests the test._call_run_once implementation
109 for run in xrange(0, fail_count):
110 self.test._call_run_once.expect_call([], False, None, (1, 2),
111 {'arg': 'val'}).and_raises(
112 error)
113 info_str = 'Run %s failed with %s' % (run, error)
114 # On the final run we do not emit this message.
Aviv Keshet39164ca2013-03-27 15:08:33 -0700115 if run != self.test.job.test_retry and isinstance(error,
116 common_lib_error.TestFailRetry):
Scott Zawalski91493c82013-01-25 16:15:20 -0500117 self.test.job.record.expect_call('INFO', None, None, info_str)
118
119
120 def test_call_run_once_with_retry_exception(self):
121 """
122 Test call_run_once_with_retry duplicating a test that will always fail.
123 """
124 self.test.job.test_retry = 5
125 self.god.stub_function(self.test, 'drop_caches_between_iterations')
126 self.god.stub_function(self.test, 'run_once')
127 before_hook = self.god.create_mock_function('before_hook')
128 after_hook = self.god.create_mock_function('after_hook')
129 self.test.register_before_iteration_hook(before_hook)
130 self.test.register_after_iteration_hook(after_hook)
Aviv Keshet39164ca2013-03-27 15:08:33 -0700131 error = common_lib_error.TestFailRetry('fail')
Scott Zawalski91493c82013-01-25 16:15:20 -0500132 self._setup_failed_test_calls(self.test.job.test_retry+1, error)
133 try:
134 self.test._call_run_once_with_retry([], False, None, (1, 2),
135 {'arg': 'val'})
136 except Exception as err:
137 if err != error:
138 raise
139 self.god.check_playback()
140
141
Aviv Keshet39164ca2013-03-27 15:08:33 -0700142 def test_call_run_once_with_retry_exception_unretryable(self):
143 """
144 Test call_run_once_with_retry duplicating a test that will always fail
145 with a non-retryable exception.
146 """
147 self.test.job.test_retry = 5
148 self.god.stub_function(self.test, 'drop_caches_between_iterations')
149 self.god.stub_function(self.test, 'run_once')
150 before_hook = self.god.create_mock_function('before_hook')
151 after_hook = self.god.create_mock_function('after_hook')
152 self.test.register_before_iteration_hook(before_hook)
153 self.test.register_after_iteration_hook(after_hook)
154 error = common_lib_error.TestFail('fail')
155 self._setup_failed_test_calls(1, error)
156 try:
157 self.test._call_run_once_with_retry([], False, None, (1, 2),
158 {'arg': 'val'})
159 except Exception as err:
160 if err != error:
161 raise
162 self.god.check_playback()
163
164
Scott Zawalski91493c82013-01-25 16:15:20 -0500165 def test_call_run_once_with_retry_exception_and_pass(self):
166 """
167 Test call_run_once_with_retry duplicating a test that fails at first
168 and later passes.
169 """
170 # Stubbed out for the write_keyval call.
171 self.test.outputdir = '/tmp'
Scott Zawalski91493c82013-01-25 16:15:20 -0500172
173 num_to_fail = 2
174 self.test.job.test_retry = 5
175 self.god.stub_function(self.test, 'drop_caches_between_iterations')
176 self.god.stub_function(self.test, 'run_once')
177 before_hook = self.god.create_mock_function('before_hook')
178 after_hook = self.god.create_mock_function('after_hook')
179 self.god.stub_function(self.test, '_call_run_once')
180 self.test.register_before_iteration_hook(before_hook)
181 self.test.register_after_iteration_hook(after_hook)
182 self.god.stub_function(self.test.job, 'record')
183 # tests the test._call_run_once implementation
Aviv Keshet39164ca2013-03-27 15:08:33 -0700184 error = common_lib_error.TestFailRetry('fail')
Scott Zawalski91493c82013-01-25 16:15:20 -0500185 self._setup_failed_test_calls(num_to_fail, error)
186 # Passing call
187 self.test._call_run_once.expect_call([], False, None, (1, 2),
188 {'arg': 'val'})
189 self.test._call_run_once_with_retry([], False, None, (1, 2),
190 {'arg': 'val'})
191 self.god.check_playback()
192
193
mbligh4395bbd2009-03-25 19:34:17 +0000194 def _expect_call_run_once(self):
jadmanskia93fbca2009-08-21 15:34:04 +0000195 self.test._call_run_once.expect_call((), False, None, (), {})
mbligh4395bbd2009-03-25 19:34:17 +0000196
197
mbligh4b835b82009-02-11 01:26:13 +0000198 def test_execute_test_length(self):
199 # test that test_length overrides iterations and works.
mbligh4395bbd2009-03-25 19:34:17 +0000200 self.god.stub_function(self.test, '_call_run_once')
201
202 self._expect_call_run_once()
203 self._expect_call_run_once()
204 self._expect_call_run_once()
mbligha49c5cb2009-02-26 01:01:09 +0000205 self.test.run_once_profiling.expect_call(None)
mbligh4b835b82009-02-11 01:26:13 +0000206 self.test.postprocess.expect_call()
mbligh32cb5b42009-05-01 23:05:09 +0000207 self.test.process_failed_constraints.expect_call()
mbligh4b835b82009-02-11 01:26:13 +0000208
209 fake_time = iter(xrange(4)).next
210 self.test.execute(iterations=1, test_length=3, _get_time=fake_time)
211 self.god.check_playback()
212
213
214 def test_execute_iterations(self):
215 # test that iterations works.
mbligh4395bbd2009-03-25 19:34:17 +0000216 self.god.stub_function(self.test, '_call_run_once')
217
mbligh4b835b82009-02-11 01:26:13 +0000218 iterations = 2
219 for _ in range(iterations):
mbligh4395bbd2009-03-25 19:34:17 +0000220 self._expect_call_run_once()
mbligha49c5cb2009-02-26 01:01:09 +0000221 self.test.run_once_profiling.expect_call(None)
mbligh4b835b82009-02-11 01:26:13 +0000222 self.test.postprocess.expect_call()
mbligh32cb5b42009-05-01 23:05:09 +0000223 self.test.process_failed_constraints.expect_call()
mbligh4b835b82009-02-11 01:26:13 +0000224
225 self.test.execute(iterations=iterations)
226 self.god.check_playback()
227
228
229 def _mock_calls_for_execute_no_iterations(self):
mbligha49c5cb2009-02-26 01:01:09 +0000230 self.test.run_once_profiling.expect_call(None)
mbligh4b835b82009-02-11 01:26:13 +0000231 self.test.postprocess.expect_call()
mbligh32cb5b42009-05-01 23:05:09 +0000232 self.test.process_failed_constraints.expect_call()
mbligh4b835b82009-02-11 01:26:13 +0000233
234
235 def test_execute_iteration_zero(self):
236 # test that iterations=0 works.
237 self._mock_calls_for_execute_no_iterations()
238
239 self.test.execute(iterations=0)
240 self.god.check_playback()
241
242
243 def test_execute_profile_only(self):
mblighc6bf6012009-10-02 00:02:15 +0000244 # test that profile_only=True works.
jadmanskia93fbca2009-08-21 15:34:04 +0000245 self.god.stub_function(self.test, 'drop_caches_between_iterations')
246 self.test.drop_caches_between_iterations.expect_call()
247 self.test.run_once_profiling.expect_call(None)
248 self.test.drop_caches_between_iterations.expect_call()
249 self.test.run_once_profiling.expect_call(None)
250 self.test.postprocess.expect_call()
251 self.test.process_failed_constraints.expect_call()
mbligh4b835b82009-02-11 01:26:13 +0000252 self.test.execute(profile_only=True, iterations=2)
253 self.god.check_playback()
254
255
showarda6082ef2009-10-12 20:25:44 +0000256 def test_execute_default_profile_only(self):
257 # test that profile_only=True works.
258 self.god.stub_function(self.test, 'drop_caches_between_iterations')
259 for _ in xrange(3):
260 self.test.drop_caches_between_iterations.expect_call()
261 self.test.run_once_profiling.expect_call(None)
262 self.test.postprocess.expect_call()
263 self.test.process_failed_constraints.expect_call()
264 self.test.job.default_profile_only = True
265 self.test.execute(iterations=3)
266 self.god.check_playback()
267
268
mbligha49c5cb2009-02-26 01:01:09 +0000269 def test_execute_postprocess_profiled_false(self):
270 # test that postprocess_profiled_run=False works
mbligh4395bbd2009-03-25 19:34:17 +0000271 self.god.stub_function(self.test, '_call_run_once')
272
jadmanskia93fbca2009-08-21 15:34:04 +0000273 self.test._call_run_once.expect_call((), False, False, (), {})
mbligha49c5cb2009-02-26 01:01:09 +0000274 self.test.run_once_profiling.expect_call(False)
275 self.test.postprocess.expect_call()
mbligh32cb5b42009-05-01 23:05:09 +0000276 self.test.process_failed_constraints.expect_call()
mbligha49c5cb2009-02-26 01:01:09 +0000277
278 self.test.execute(postprocess_profiled_run=False, iterations=1)
279 self.god.check_playback()
280
281
282 def test_execute_postprocess_profiled_true(self):
283 # test that postprocess_profiled_run=True works
mbligh4395bbd2009-03-25 19:34:17 +0000284 self.god.stub_function(self.test, '_call_run_once')
285
jadmanskia93fbca2009-08-21 15:34:04 +0000286 self.test._call_run_once.expect_call((), False, True, (), {})
mbligha49c5cb2009-02-26 01:01:09 +0000287 self.test.run_once_profiling.expect_call(True)
288 self.test.postprocess.expect_call()
mbligh32cb5b42009-05-01 23:05:09 +0000289 self.test.process_failed_constraints.expect_call()
mbligha49c5cb2009-02-26 01:01:09 +0000290
291 self.test.execute(postprocess_profiled_run=True, iterations=1)
292 self.god.check_playback()
293
294
Keith Haddowd804bc82016-03-15 17:16:03 -0700295 def test_output_single_perf_value(self):
296 self.test.resultsdir = tempfile.mkdtemp()
297
298 self.test.output_perf_value("Test", 1, units="ms", higher_is_better=True)
299
300 f = open(self.test.resultsdir + "/results-chart.json")
301 expected_result = {"Test": {"summary": {"units": "ms", "type": "scalar",
302 "value": 1, "improvement_direction": "up"}}}
303 self.assertDictEqual(expected_result, json.loads(f.read()))
304
305
306 def test_output_single_perf_value_twice(self):
307 self.test.resultsdir = tempfile.mkdtemp()
308
309 self.test.output_perf_value("Test", 1, units="ms", higher_is_better=True)
310 self.test.output_perf_value("Test", 2, units="ms", higher_is_better=True)
311
312 f = open(self.test.resultsdir + "/results-chart.json")
Keith Haddow4fc24652016-03-16 10:08:17 -0700313 expected_result = {"Test": {"summary": {"units": "ms",
314 "type": "list_of_scalar_values", "values": [1, 2],
315 "improvement_direction": "up"}}}
Keith Haddowd804bc82016-03-15 17:16:03 -0700316 self.assertDictEqual(expected_result, json.loads(f.read()))
317
318
319 def test_output_single_perf_value_three_times(self):
320 self.test.resultsdir = tempfile.mkdtemp()
321
Keith Haddow4fc24652016-03-16 10:08:17 -0700322 self.test.output_perf_value("Test", 1, units="ms",
323 higher_is_better=True)
Keith Haddowd804bc82016-03-15 17:16:03 -0700324 self.test.output_perf_value("Test", 2, units="ms", higher_is_better=True)
325 self.test.output_perf_value("Test", 3, units="ms", higher_is_better=True)
326
327 f = open(self.test.resultsdir + "/results-chart.json")
Keith Haddow4fc24652016-03-16 10:08:17 -0700328 expected_result = {"Test": {"summary": {"units": "ms",
329 "type": "list_of_scalar_values", "values": [1, 2, 3],
330 "improvement_direction": "up"}}}
Keith Haddowd804bc82016-03-15 17:16:03 -0700331 self.assertDictEqual(expected_result, json.loads(f.read()))
332
333
334 def test_output_list_perf_value(self):
335 self.test.resultsdir = tempfile.mkdtemp()
336
Keith Haddow4fc24652016-03-16 10:08:17 -0700337 self.test.output_perf_value("Test", [1, 2, 3], units="ms",
338 higher_is_better=False)
Keith Haddowd804bc82016-03-15 17:16:03 -0700339
340 f = open(self.test.resultsdir + "/results-chart.json")
Keith Haddow4fc24652016-03-16 10:08:17 -0700341 expected_result = {"Test": {"summary": {"units": "ms",
342 "type": "list_of_scalar_values", "values": [1, 2, 3],
343 "improvement_direction": "down"}}}
Keith Haddowd804bc82016-03-15 17:16:03 -0700344 self.assertDictEqual(expected_result, json.loads(f.read()))
345
346
347 def test_output_single_then_list_perf_value(self):
348 self.test.resultsdir = tempfile.mkdtemp()
Keith Haddow4fc24652016-03-16 10:08:17 -0700349 self.test.output_perf_value("Test", 1, units="ms",
350 higher_is_better=False)
351 self.test.output_perf_value("Test", [4, 3, 2], units="ms",
352 higher_is_better=False)
Keith Haddowd804bc82016-03-15 17:16:03 -0700353 f = open(self.test.resultsdir + "/results-chart.json")
Keith Haddow4fc24652016-03-16 10:08:17 -0700354 expected_result = {"Test": {"summary": {"units": "ms",
355 "type": "list_of_scalar_values",
356 "values": [1, 4, 3, 2],
357 "improvement_direction": "down"}}}
Keith Haddowd804bc82016-03-15 17:16:03 -0700358 self.assertDictEqual(expected_result, json.loads(f.read()))
359
360
361 def test_output_list_then_list_perf_value(self):
362 self.test.resultsdir = tempfile.mkdtemp()
Keith Haddow4fc24652016-03-16 10:08:17 -0700363 self.test.output_perf_value("Test", [1, 2, 3], units="ms",
364 higher_is_better=False)
365 self.test.output_perf_value("Test", [4, 3, 2], units="ms",
366 higher_is_better=False)
Keith Haddowd804bc82016-03-15 17:16:03 -0700367 f = open(self.test.resultsdir + "/results-chart.json")
Keith Haddow4fc24652016-03-16 10:08:17 -0700368 expected_result = {"Test": {"summary": {"units": "ms",
369 "type": "list_of_scalar_values",
370 "values": [1, 2, 3, 4, 3, 2],
371 "improvement_direction": "down"}}}
Keith Haddowd804bc82016-03-15 17:16:03 -0700372 self.assertDictEqual(expected_result, json.loads(f.read()))
373
374
Keith Haddow4fc24652016-03-16 10:08:17 -0700375 def test_output_single_perf_value_input_string(self):
376 self.test.resultsdir = tempfile.mkdtemp()
377
378 self.test.output_perf_value("Test", u'-0.34', units="ms",
379 higher_is_better=True)
380
381 f = open(self.test.resultsdir + "/results-chart.json")
382 expected_result = {"Test": {"summary": {"units": "ms", "type": "scalar",
383 "value": -0.34, "improvement_direction": "up"}}}
384 self.assertDictEqual(expected_result, json.loads(f.read()))
385
386
387 def test_output_single_perf_value_input_list_of_string(self):
388 self.test.resultsdir = tempfile.mkdtemp()
389
390 self.test.output_perf_value("Test", [0, u'-0.34', 1], units="ms",
391 higher_is_better=True)
392
393 f = open(self.test.resultsdir + "/results-chart.json")
394 expected_result = {"Test": {"summary": {"units": "ms",
395 "type": "list_of_scalar_values",
396 "values": [0, -0.34, 1],
397 "improvement_direction": "up"}}}
398 self.assertDictEqual(expected_result, json.loads(f.read()))
399
Po-Hsien Wang3c4fc6f2017-05-20 12:11:58 -0700400 def test_output_list_then_replace_list_perf_value(self):
401 self.test.resultsdir = tempfile.mkdtemp()
402 self.test.output_perf_value("Test", [1, 2, 3], units="ms",
403 higher_is_better=False)
404 self.test.output_perf_value("Test", [4, 5, 6], units="ms",
405 higher_is_better=False,
406 replace_existing_values=True)
407 f = open(self.test.resultsdir + "/results-chart.json")
408 expected_result = {"Test": {"summary": {"units": "ms",
409 "type": "list_of_scalar_values",
410 "values": [4, 5, 6],
411 "improvement_direction": "down"}}}
412 self.assertDictEqual(expected_result, json.loads(f.read()))
413
414 def test_output_single_then_replace_list_perf_value(self):
415 self.test.resultsdir = tempfile.mkdtemp()
416 self.test.output_perf_value("Test", 3, units="ms",
417 higher_is_better=False)
418 self.test.output_perf_value("Test", [4, 5, 6], units="ms",
419 higher_is_better=False,
420 replace_existing_values=True)
421 f = open(self.test.resultsdir + "/results-chart.json")
422 expected_result = {"Test": {"summary": {"units": "ms",
423 "type": "list_of_scalar_values",
424 "values": [4, 5, 6],
425 "improvement_direction": "down"}}}
426 self.assertDictEqual(expected_result, json.loads(f.read()))
427
428 def test_output_list_then_replace_single_perf_value(self):
429 self.test.resultsdir = tempfile.mkdtemp()
430 self.test.output_perf_value("Test", [1,2,3], units="ms",
431 higher_is_better=False)
432 self.test.output_perf_value("Test", 4, units="ms",
433 higher_is_better=False,
434 replace_existing_values=True)
435 f = open(self.test.resultsdir + "/results-chart.json")
436 expected_result = {"Test": {"summary": {"units": "ms",
437 "type": "scalar",
438 "value": 4,
439 "improvement_direction": "down"}}}
440 self.assertDictEqual(expected_result, json.loads(f.read()))
441
442 def test_output_single_then_replace_single_perf_value(self):
443 self.test.resultsdir = tempfile.mkdtemp()
444 self.test.output_perf_value("Test", 1, units="ms",
445 higher_is_better=False)
446 self.test.output_perf_value("Test", 2, units="ms",
447 higher_is_better=False,
448 replace_existing_values=True)
449 f = open(self.test.resultsdir + "/results-chart.json")
450 expected_result = {"Test": {"summary": {"units": "ms",
451 "type": "scalar",
452 "value": 2,
453 "improvement_direction": "down"}}}
454 self.assertDictEqual(expected_result, json.loads(f.read()))
455
456 def test_output_perf_then_replace_certain_perf_value(self):
457 self.test.resultsdir = tempfile.mkdtemp()
458 self.test.output_perf_value("Test1", 1, units="ms",
459 higher_is_better=False)
460 self.test.output_perf_value("Test2", 2, units="ms",
461 higher_is_better=False)
462 self.test.output_perf_value("Test3", 3, units="ms",
463 higher_is_better=False)
464 self.test.output_perf_value("Test2", -1, units="ms",
465 higher_is_better=False,
466 replace_existing_values=True)
467 f = open(self.test.resultsdir + "/results-chart.json")
468 expected_result = {"Test1": {"summary":
469 {"units": "ms",
470 "type": "scalar",
471 "value": 1,
472 "improvement_direction": "down"}},
473 "Test2": {"summary":
474 {"units": "ms",
475 "type": "scalar",
476 "value": -1,
477 "improvement_direction": "down"}},
478 "Test3": {"summary":
479 {"units": "ms",
480 "type": "scalar",
481 "value": 3,
482 "improvement_direction": "down"}}}
483 self.assertDictEqual(expected_result, json.loads(f.read()))
484
Keith Haddowdf2730b2016-03-28 21:31:18 -0700485 def test_chart_supplied(self):
486 self.test.resultsdir = tempfile.mkdtemp()
Keith Haddow4fc24652016-03-16 10:08:17 -0700487
Keith Haddowdf2730b2016-03-28 21:31:18 -0700488 test_data = [("tcp_tx", "ch006_mode11B_none", "BT_connected_but_not_streaming", 0),
489 ("tcp_tx", "ch006_mode11B_none", "BT_streaming_audiofile", 5),
490 ("tcp_tx", "ch006_mode11B_none", "BT_disconnected_again", 0),
491 ("tcp_rx", "ch006_mode11B_none", "BT_connected_but_not_streaming", 0),
492 ("tcp_rx", "ch006_mode11B_none", "BT_streaming_audiofile", 8),
493 ("tcp_rx", "ch006_mode11B_none", "BT_disconnected_again", 0),
494 ("udp_tx", "ch006_mode11B_none", "BT_connected_but_not_streaming", 0),
495 ("udp_tx", "ch006_mode11B_none", "BT_streaming_audiofile", 6),
496 ("udp_tx", "ch006_mode11B_none", "BT_disconnected_again", 0),
497 ("udp_rx", "ch006_mode11B_none", "BT_connected_but_not_streaming", 0),
498 ("udp_rx", "ch006_mode11B_none", "BT_streaming_audiofile", 8),
499 ("udp_rx", "ch006_mode11B_none", "BT_streaming_audiofile", 9),
500 ("udp_rx", "ch006_mode11B_none", "BT_disconnected_again", 0)]
501
502
503 for (config_tag, ap_config_tag, bt_tag, drop) in test_data:
504 self.test.output_perf_value(config_tag + '_' + bt_tag + '_drop',
505 drop, units='percent_drop',
506 higher_is_better=False,
507 graph=ap_config_tag + '_drop')
508 f = open(self.test.resultsdir + "/results-chart.json")
509 expected_result = {
510 "ch006_mode11B_none_drop": {
511 "udp_tx_BT_streaming_audiofile_drop": {
512 "units": "percent_drop",
513 "type": "scalar",
514 "value": 6.0,
515 "improvement_direction": "down"
516 },
517 "udp_rx_BT_disconnected_again_drop": {
518 "units": "percent_drop",
519 "type": "scalar",
520 "value": 0.0,
521 "improvement_direction": "down"
522 },
523 "tcp_tx_BT_disconnected_again_drop": {
524 "units": "percent_drop",
525 "type": "scalar",
526 "value": 0.0,
527 "improvement_direction": "down"
528 },
529 "tcp_rx_BT_streaming_audiofile_drop": {
530 "units": "percent_drop",
531 "type": "scalar",
532 "value": 8.0,
533 "improvement_direction": "down"
534 },
535 "udp_tx_BT_connected_but_not_streaming_drop": {
536 "units": "percent_drop",
537 "type": "scalar",
538 "value": 0.0,
539 "improvement_direction": "down"
540 },
541 "tcp_tx_BT_connected_but_not_streaming_drop": {
542 "units": "percent_drop",
543 "type": "scalar",
544 "value": 0.0,
545 "improvement_direction": "down"
546 },
547 "udp_tx_BT_disconnected_again_drop": {
548 "units": "percent_drop",
549 "type": "scalar",
550 "value": 0.0,
551 "improvement_direction": "down"
552 },
553 "tcp_tx_BT_streaming_audiofile_drop": {
554 "units": "percent_drop",
555 "type": "scalar",
556 "value": 5.0,
557 "improvement_direction": "down"
558 },
559 "tcp_rx_BT_connected_but_not_streaming_drop": {
560 "units": "percent_drop",
561 "type": "scalar",
562 "value": 0.0,
563 "improvement_direction": "down"
564 },
565 "udp_rx_BT_connected_but_not_streaming_drop": {
566 "units": "percent_drop",
567 "type": "scalar",
568 "value": 0.0,
569 "improvement_direction": "down"
570 },
571 "udp_rx_BT_streaming_audiofile_drop": {
572 "units": "percent_drop",
573 "type": "list_of_scalar_values",
574 "values": [
575 8.0,
576 9.0
577 ],
578 "improvement_direction": "down"
579 },
580 "tcp_rx_BT_disconnected_again_drop": {
581 "units": "percent_drop",
582 "type": "scalar",
583 "value": 0.0,
584 "improvement_direction": "down"
585 }
586 }
587 }
588 self.maxDiff = None
589 self.assertDictEqual(expected_result, json.loads(f.read()))
Keith Haddow4fc24652016-03-16 10:08:17 -0700590
mbligh234a84f2008-11-20 19:57:43 +0000591if __name__ == '__main__':
592 unittest.main()