blob: 4256ef22ab93d371eaca9052ac84f6c2f635e71c [file] [log] [blame]
Yunlian Jiangfbda0492015-12-21 15:32:44 -08001#!/usr/bin/python2
Yunlian Jiang4578ec12013-05-22 14:54:39 -07002
3# Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
4# Use of this source code is governed by a BSD-style license that can be
5# found in the LICENSE file.
6
Yunlian Jiangfbda0492015-12-21 15:32:44 -08007"""Module of result cache unittest."""
8
9from __future__ import print_function
10
cmticec490e072014-06-13 15:38:45 -070011import mock
cmticec490e072014-06-13 15:38:45 -070012import os
13import tempfile
Yunlian Jiang4578ec12013-05-22 14:54:39 -070014import unittest
15
cmticec490e072014-06-13 15:38:45 -070016import image_checksummer
17import machine_manager
Caroline Ticee627fd62015-12-11 12:07:59 -080018import test_flag
Yunlian Jiang4578ec12013-05-22 14:54:39 -070019
cmticec490e072014-06-13 15:38:45 -070020from label import MockLabel
21from results_cache import CacheConditions
Yunlian Jiang4578ec12013-05-22 14:54:39 -070022from results_cache import Result
23from results_cache import ResultsCache
cmticec490e072014-06-13 15:38:45 -070024from results_cache import TelemetryResult
Yunlian Jiang0d1a9f32015-12-09 10:47:11 -080025from cros_utils import command_executer
26from cros_utils import logger
27from cros_utils import misc
Yunlian Jiang4578ec12013-05-22 14:54:39 -070028
cmticed96e4572015-05-19 16:19:25 -070029OUTPUT = """CMD (True): ./test_that.sh --remote=172.17.128.241 --board=lumpy LibCBench
Yunlian Jiang4578ec12013-05-22 14:54:39 -070030CMD (None): cd /usr/local/google/home/yunlian/gd/src/build/images/lumpy/latest/../../../../..; cros_sdk -- ./in_chroot_cmd6X7Cxu.sh
cmticed96e4572015-05-19 16:19:25 -070031Identity added: /tmp/test_that.PO1234567/autotest_key (/tmp/test_that.PO1234567/autotest_key)
Yunlian Jiang4578ec12013-05-22 14:54:39 -070032INFO : Using emerged autotests already installed at /build/lumpy/usr/local/autotest.
33
34INFO : Running the following control files 1 times:
35INFO : * 'client/site_tests/platform_LibCBench/control'
36
37INFO : Running client test client/site_tests/platform_LibCBench/control
cmticed96e4572015-05-19 16:19:25 -070038./server/autoserv -m 172.17.128.241 --ssh-port 22 -c client/site_tests/platform_LibCBench/control -r /tmp/test_that.PO1234567/platform_LibCBench --test-retry=0 --args
Yunlian Jiang4578ec12013-05-22 14:54:39 -070039ERROR:root:import statsd failed, no stats will be reported.
cmticed96e4572015-05-19 16:19:25 -07004014:20:22 INFO | Results placed in /tmp/test_that.PO1234567/platform_LibCBench
Yunlian Jiang4578ec12013-05-22 14:54:39 -07004114:20:22 INFO | Processing control file
4214:20:23 INFO | Starting master ssh connection '/usr/bin/ssh -a -x -N -o ControlMaster=yes -o ControlPath=/tmp/_autotmp_VIIP67ssh-master/socket -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -o BatchMode=yes -o ConnectTimeout=30 -o ServerAliveInterval=180 -o ServerAliveCountMax=3 -o ConnectionAttempts=4 -o Protocol=2 -l root -p 22 172.17.128.241'
4314:20:23 ERROR| [stderr] Warning: Permanently added '172.17.128.241' (RSA) to the list of known hosts.
4414:20:23 INFO | INFO ---- ---- kernel=3.8.11 localtime=May 22 14:20:23 timestamp=1369257623
4514:20:23 INFO | Installing autotest on 172.17.128.241
4614:20:23 INFO | Using installation dir /usr/local/autotest
4714:20:23 WARNI| No job_repo_url for <remote host: 172.17.128.241>
4814:20:23 INFO | Could not install autotest using the packaging system: No repos to install an autotest client from. Trying other methods
4914:20:23 INFO | Installation of autotest completed
5014:20:24 WARNI| No job_repo_url for <remote host: 172.17.128.241>
5114:20:24 INFO | Executing /usr/local/autotest/bin/autotest /usr/local/autotest/control phase 0
5214:20:24 INFO | Entered autotestd_monitor.
5314:20:24 INFO | Finished launching tail subprocesses.
5414:20:24 INFO | Finished waiting on autotestd to start.
5514:20:26 INFO | START ---- ---- timestamp=1369257625 localtime=May 22 14:20:25
5614:20:26 INFO | START platform_LibCBench platform_LibCBench timestamp=1369257625 localtime=May 22 14:20:25
5714:20:30 INFO | GOOD platform_LibCBench platform_LibCBench timestamp=1369257630 localtime=May 22 14:20:30 completed successfully
5814:20:30 INFO | END GOOD platform_LibCBench platform_LibCBench timestamp=1369257630 localtime=May 22 14:20:30
5914:20:31 INFO | END GOOD ---- ---- timestamp=1369257630 localtime=May 22 14:20:30
6014:20:31 INFO | Got lock of exit_code_file.
6114:20:31 INFO | Released lock of exit_code_file and closed it.
62OUTPUT: ==============================
63OUTPUT: Current time: 2013-05-22 14:20:32.818831 Elapsed: 0:01:30 ETA: Unknown
64Done: 0% [ ]
65OUTPUT: Thread Status:
66RUNNING: 1 ('ttt: LibCBench (1)' 0:01:21)
67Machine Status:
68Machine Thread Lock Status Checksum
69172.17.128.241 ttt: LibCBench (1) True RUNNING 3ba9f2ecbb222f20887daea5583d86ba
70
71OUTPUT: ==============================
7214:20:33 INFO | Killing child processes.
7314:20:33 INFO | Client complete
7414:20:33 INFO | Finished processing control file
7514:20:33 INFO | Starting master ssh connection '/usr/bin/ssh -a -x -N -o ControlMaster=yes -o ControlPath=/tmp/_autotmp_aVJUgmssh-master/socket -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -o BatchMode=yes -o ConnectTimeout=30 -o ServerAliveInterval=180 -o ServerAliveCountMax=3 -o ConnectionAttempts=4 -o Protocol=2 -l root -p 22 172.17.128.241'
7614:20:33 ERROR| [stderr] Warning: Permanently added '172.17.128.241' (RSA) to the list of known hosts.
77
78INFO : Test results:
79-------------------------------------------------------------------
80platform_LibCBench [ PASSED ]
81platform_LibCBench/platform_LibCBench [ PASSED ]
82platform_LibCBench/platform_LibCBench b_malloc_big1__0_ 0.00375231466667
83platform_LibCBench/platform_LibCBench b_malloc_big2__0_ 0.002951359
84platform_LibCBench/platform_LibCBench b_malloc_bubble__0_ 0.015066374
85platform_LibCBench/platform_LibCBench b_malloc_sparse__0_ 0.015053784
86platform_LibCBench/platform_LibCBench b_malloc_thread_local__0_ 0.01138439
87platform_LibCBench/platform_LibCBench b_malloc_thread_stress__0_ 0.0367894733333
88platform_LibCBench/platform_LibCBench b_malloc_tiny1__0_ 0.000768474333333
89platform_LibCBench/platform_LibCBench b_malloc_tiny2__0_ 0.000581407333333
90platform_LibCBench/platform_LibCBench b_pthread_create_serial1__0_ 0.0291785246667
91platform_LibCBench/platform_LibCBench b_pthread_createjoin_serial1__0_ 0.031907936
92platform_LibCBench/platform_LibCBench b_pthread_createjoin_serial2__0_ 0.043485347
93platform_LibCBench/platform_LibCBench b_pthread_uselesslock__0_ 0.0294113346667
94platform_LibCBench/platform_LibCBench b_regex_compile____a_b_c__d_b__ 0.00529833933333
95platform_LibCBench/platform_LibCBench b_regex_search____a_b_c__d_b__ 0.00165455066667
96platform_LibCBench/platform_LibCBench b_regex_search___a_25_b__ 0.0496191923333
97platform_LibCBench/platform_LibCBench b_stdio_putcgetc__0_ 0.100005711667
98platform_LibCBench/platform_LibCBench b_stdio_putcgetc_unlocked__0_ 0.0371443833333
99platform_LibCBench/platform_LibCBench b_string_memset__0_ 0.00275405066667
100platform_LibCBench/platform_LibCBench b_string_strchr__0_ 0.00456903
101platform_LibCBench/platform_LibCBench b_string_strlen__0_ 0.044893587
102platform_LibCBench/platform_LibCBench b_string_strstr___aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaac__ 0.118360778
103platform_LibCBench/platform_LibCBench b_string_strstr___aaaaaaaaaaaaaaaaaaaaaaaaac__ 0.068957325
104platform_LibCBench/platform_LibCBench b_string_strstr___aaaaaaaaaaaaaacccccccccccc__ 0.0135694476667
105platform_LibCBench/platform_LibCBench b_string_strstr___abcdefghijklmnopqrstuvwxyz__ 0.0134553343333
106platform_LibCBench/platform_LibCBench b_string_strstr___azbycxdwevfugthsirjqkplomn__ 0.0133123556667
107platform_LibCBench/platform_LibCBench b_utf8_bigbuf__0_ 0.0473772253333
108platform_LibCBench/platform_LibCBench b_utf8_onebyone__0_ 0.130938538333
109-------------------------------------------------------------------
110Total PASS: 2/2 (100%)
111
112INFO : Elapsed time: 0m16s
113"""
114
115error = """
cmticed96e4572015-05-19 16:19:25 -0700116ERROR: Identity added: /tmp/test_that.Z4Ld/autotest_key (/tmp/test_that.Z4Ld/autotest_key)
Yunlian Jiang4578ec12013-05-22 14:54:39 -0700117INFO : Using emerged autotests already installed at /build/lumpy/usr/local/autotest.
118INFO : Running the following control files 1 times:
119INFO : * 'client/site_tests/platform_LibCBench/control'
120INFO : Running client test client/site_tests/platform_LibCBench/control
121INFO : Test results:
122INFO : Elapsed time: 0m18s
123"""
124
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800125keyvals = {'': 'PASS',
126 'b_stdio_putcgetc__0_': '0.100005711667',
127 'b_string_strstr___azbycxdwevfugthsirjqkplomn__': '0.0133123556667',
128 'b_malloc_thread_local__0_': '0.01138439',
129 'b_string_strlen__0_': '0.044893587',
130 'b_malloc_sparse__0_': '0.015053784',
131 'b_string_memset__0_': '0.00275405066667',
132 'platform_LibCBench': 'PASS',
133 'b_pthread_uselesslock__0_': '0.0294113346667',
134 'b_string_strchr__0_': '0.00456903',
135 'b_pthread_create_serial1__0_': '0.0291785246667',
136 'b_string_strstr___aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaac__':
137 '0.118360778',
138 'b_string_strstr___aaaaaaaaaaaaaacccccccccccc__': '0.0135694476667',
139 'b_pthread_createjoin_serial1__0_': '0.031907936',
140 'b_malloc_thread_stress__0_': '0.0367894733333',
141 'b_regex_search____a_b_c__d_b__': '0.00165455066667',
142 'b_malloc_bubble__0_': '0.015066374',
143 'b_malloc_big2__0_': '0.002951359',
144 'b_stdio_putcgetc_unlocked__0_': '0.0371443833333',
145 'b_pthread_createjoin_serial2__0_': '0.043485347',
146 'b_regex_search___a_25_b__': '0.0496191923333',
147 'b_utf8_bigbuf__0_': '0.0473772253333',
148 'b_malloc_big1__0_': '0.00375231466667',
149 'b_regex_compile____a_b_c__d_b__': '0.00529833933333',
150 'b_string_strstr___aaaaaaaaaaaaaaaaaaaaaaaaac__': '0.068957325',
151 'b_malloc_tiny2__0_': '0.000581407333333',
152 'b_utf8_onebyone__0_': '0.130938538333',
153 'b_malloc_tiny1__0_': '0.000768474333333',
154 'b_string_strstr___abcdefghijklmnopqrstuvwxyz__': '0.0134553343333'}
cmticec490e072014-06-13 15:38:45 -0700155
156TMP_DIR1 = '/tmp/tmpAbcXyz'
157
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800158
Yunlian Jiang4578ec12013-05-22 14:54:39 -0700159class MockResult(Result):
Yunlian Jiangaefb76c2015-12-29 08:59:52 -0800160 """Mock result class."""
Yunlian Jiangfbda0492015-12-21 15:32:44 -0800161 def __init__(self, mylogger, label, logging_level, machine):
162 super(MockResult, self).__init__(mylogger, label, logging_level, machine)
Yunlian Jiang4578ec12013-05-22 14:54:39 -0700163
164 def _FindFilesInResultsDir(self, find_args):
cmticec490e072014-06-13 15:38:45 -0700165 return ''
Yunlian Jiang4578ec12013-05-22 14:54:39 -0700166
cmticec454cee2014-04-09 10:58:51 -0700167 def _GetKeyvals(self, show_all_results):
Yunlian Jiangaefb76c2015-12-29 08:59:52 -0800168 if show_all_results:
169 pass
Yunlian Jiang4578ec12013-05-22 14:54:39 -0700170 return keyvals
171
172
173class ResultTest(unittest.TestCase):
Yunlian Jiangaefb76c2015-12-29 08:59:52 -0800174 """Result test class."""
cmticec490e072014-06-13 15:38:45 -0700175 mock_label = MockLabel('mock_label', 'chromeos_image', '/tmp', 'lumpy',
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800176 'remote', 'image_args', 'cache_dir', 'average', 'gcc',
177 None)
cmticec490e072014-06-13 15:38:45 -0700178 mock_logger = mock.Mock(spec=logger.Logger)
179 mock_cmd_exec = mock.Mock(spec=command_executer.CommandExecuter)
180
Yunlian Jiang4578ec12013-05-22 14:54:39 -0700181 def testCreateFromRun(self):
cmticec490e072014-06-13 15:38:45 -0700182 result = MockResult.CreateFromRun(logger.GetLogger(), 'average',
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800183 self.mock_label, 'remote1', OUTPUT, error,
184 0, True, 0)
Yunlian Jiang4578ec12013-05-22 14:54:39 -0700185 self.assertEqual(result.keyvals, keyvals)
Yunlian Jiang9e533332013-09-30 15:45:46 -0700186 self.assertEqual(result.chroot_results_dir,
cmticed96e4572015-05-19 16:19:25 -0700187 '/tmp/test_that.PO1234567/platform_LibCBench')
Yunlian Jiang9e533332013-09-30 15:45:46 -0700188 self.assertEqual(result.results_dir,
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800189 '/tmp/chroot/tmp/test_that.PO1234567/platform_LibCBench')
Yunlian Jiang4578ec12013-05-22 14:54:39 -0700190 self.assertEqual(result.retval, 0)
191
cmticec490e072014-06-13 15:38:45 -0700192 def setUp(self):
193 self.result = Result(self.mock_logger, self.mock_label, 'average',
194 self.mock_cmd_exec)
195
196 @mock.patch.object(os.path, 'isdir')
197 @mock.patch.object(command_executer.CommandExecuter, 'RunCommand')
198 @mock.patch.object(command_executer.CommandExecuter, 'CopyFiles')
199 def test_copy_files_to(self, mock_copyfiles, mock_runcmd, mock_isdir):
200
201 files = ['src_file_1', 'src_file_2', 'src_file_3']
202 dest_dir = '/tmp/test'
203 self.mock_cmd_exec.RunCommand = mock_runcmd
204 self.mock_cmd_exec.CopyFiles = mock_copyfiles
205
206 mock_copyfiles.return_value = 0
207
208 #test 1. dest_dir exists; CopyFiles returns 0.
209 mock_isdir.return_value = True
210 self.result._CopyFilesTo(dest_dir, files)
211 self.assertEqual(mock_runcmd.call_count, 0)
212 self.assertEqual(mock_copyfiles.call_count, 3)
213 first_args = mock_copyfiles.call_args_list[0][0]
214 second_args = mock_copyfiles.call_args_list[1][0]
215 third_args = mock_copyfiles.call_args_list[2][0]
216 self.assertEqual(first_args, ('src_file_1', '/tmp/test/src_file_1.0'))
217 self.assertEqual(second_args, ('src_file_2', '/tmp/test/src_file_2.0'))
218 self.assertEqual(third_args, ('src_file_3', '/tmp/test/src_file_3.0'))
219
220 mock_runcmd.reset_mock()
221 mock_copyfiles.reset_mock()
222 #test 2. dest_dir does not exist; CopyFiles returns 0.
223 mock_isdir.return_value = False
224 self.result._CopyFilesTo(dest_dir, files)
225 self.assertEqual(mock_runcmd.call_count, 3)
226 self.assertEqual(mock_copyfiles.call_count, 3)
227 self.assertEqual(mock_runcmd.call_args_list[0],
228 mock_runcmd.call_args_list[1])
229 self.assertEqual(mock_runcmd.call_args_list[0],
230 mock_runcmd.call_args_list[2])
231 self.assertEqual(mock_runcmd.call_args_list[0][0], ('mkdir -p /tmp/test',))
232
233 #test 3. CopyFiles returns 1 (fails).
234 mock_copyfiles.return_value = 1
235 self.assertRaises(Exception, self.result._CopyFilesTo, dest_dir, files)
236
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800237 @mock.patch.object(Result, '_CopyFilesTo')
cmticec490e072014-06-13 15:38:45 -0700238 def test_copy_results_to(self, mock_CopyFilesTo):
239 perf_data_files = ['/tmp/perf.data.0', '/tmp/perf.data.1',
240 '/tmp/perf.data.2']
241 perf_report_files = ['/tmp/perf.report.0', '/tmp/perf.report.1',
242 '/tmp/perf.report.2']
243
244 self.result.perf_data_files = perf_data_files
245 self.result.perf_report_files = perf_report_files
246
247 self.result._CopyFilesTo = mock_CopyFilesTo
248 self.result.CopyResultsTo('/tmp/results/')
249 self.assertEqual(mock_CopyFilesTo.call_count, 2)
250 self.assertEqual(len(mock_CopyFilesTo.call_args_list), 2)
251 self.assertEqual(mock_CopyFilesTo.call_args_list[0][0],
252 ('/tmp/results/', perf_data_files))
253 self.assertEqual(mock_CopyFilesTo.call_args_list[1][0],
254 ('/tmp/results/', perf_report_files))
255
cmticec490e072014-06-13 15:38:45 -0700256 def test_get_new_keyvals(self):
257 kv_dict = {}
258
259 def FakeGetDataMeasurementsFiles():
260 filename = os.path.join(os.getcwd(), 'unittest_keyval_file.txt')
261 return [filename]
262
263 self.result._GetDataMeasurementsFiles = FakeGetDataMeasurementsFiles
264 kv_dict2, udict = self.result._GetNewKeyvals(kv_dict)
265 self.assertEqual(kv_dict2,
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800266 {u'Box2D__Box2D': 4775,
267 u'Mandreel__Mandreel': 6620,
268 u'Gameboy__Gameboy': 9901,
269 u'Crypto__Crypto': 8737,
270 u'telemetry_page_measurement_results__num_errored': 0,
271 u'telemetry_page_measurement_results__num_failed': 0,
272 u'PdfJS__PdfJS': 6455,
273 u'Total__Score': 7918,
274 u'EarleyBoyer__EarleyBoyer': 14340,
275 u'MandreelLatency__MandreelLatency': 5188,
276 u'CodeLoad__CodeLoad': 6271,
277 u'DeltaBlue__DeltaBlue': 14401,
278 u'Typescript__Typescript': 9815,
279 u'SplayLatency__SplayLatency': 7653,
280 u'zlib__zlib': 16094,
281 u'Richards__Richards': 10358,
282 u'RegExp__RegExp': 1765,
283 u'NavierStokes__NavierStokes': 9815,
284 u'Splay__Splay': 4425,
285 u'RayTrace__RayTrace': 16600})
286 self.assertEqual(
287 udict, {u'Box2D__Box2D': u'score',
288 u'Mandreel__Mandreel': u'score',
289 u'Gameboy__Gameboy': u'score',
290 u'Crypto__Crypto': u'score',
291 u'telemetry_page_measurement_results__num_errored': u'count',
292 u'telemetry_page_measurement_results__num_failed': u'count',
293 u'PdfJS__PdfJS': u'score',
294 u'Total__Score': u'score',
295 u'EarleyBoyer__EarleyBoyer': u'score',
296 u'MandreelLatency__MandreelLatency': u'score',
297 u'CodeLoad__CodeLoad': u'score',
298 u'DeltaBlue__DeltaBlue': u'score',
299 u'Typescript__Typescript': u'score',
300 u'SplayLatency__SplayLatency': u'score',
301 u'zlib__zlib': u'score',
302 u'Richards__Richards': u'score',
303 u'RegExp__RegExp': u'score',
304 u'NavierStokes__NavierStokes': u'score',
305 u'Splay__Splay': u'score',
306 u'RayTrace__RayTrace': u'score'})
cmticec490e072014-06-13 15:38:45 -0700307
308 def test_append_telemetry_units(self):
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800309 kv_dict = {u'Box2D__Box2D': 4775,
310 u'Mandreel__Mandreel': 6620,
311 u'Gameboy__Gameboy': 9901,
312 u'Crypto__Crypto': 8737,
313 u'PdfJS__PdfJS': 6455,
314 u'Total__Score': 7918,
cmticec490e072014-06-13 15:38:45 -0700315 u'EarleyBoyer__EarleyBoyer': 14340,
316 u'MandreelLatency__MandreelLatency': 5188,
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800317 u'CodeLoad__CodeLoad': 6271,
318 u'DeltaBlue__DeltaBlue': 14401,
cmticec490e072014-06-13 15:38:45 -0700319 u'Typescript__Typescript': 9815,
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800320 u'SplayLatency__SplayLatency': 7653,
321 u'zlib__zlib': 16094,
322 u'Richards__Richards': 10358,
323 u'RegExp__RegExp': 1765,
324 u'NavierStokes__NavierStokes': 9815,
325 u'Splay__Splay': 4425,
cmticec490e072014-06-13 15:38:45 -0700326 u'RayTrace__RayTrace': 16600}
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800327 units_dict = {u'Box2D__Box2D': u'score',
328 u'Mandreel__Mandreel': u'score',
329 u'Gameboy__Gameboy': u'score',
330 u'Crypto__Crypto': u'score',
331 u'PdfJS__PdfJS': u'score',
332 u'Total__Score': u'score',
cmticec490e072014-06-13 15:38:45 -0700333 u'EarleyBoyer__EarleyBoyer': u'score',
334 u'MandreelLatency__MandreelLatency': u'score',
335 u'CodeLoad__CodeLoad': u'score',
336 u'DeltaBlue__DeltaBlue': u'score',
337 u'Typescript__Typescript': u'score',
338 u'SplayLatency__SplayLatency': u'score',
339 u'zlib__zlib': u'score',
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800340 u'Richards__Richards': u'score',
341 u'RegExp__RegExp': u'score',
cmticec490e072014-06-13 15:38:45 -0700342 u'NavierStokes__NavierStokes': u'score',
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800343 u'Splay__Splay': u'score',
344 u'RayTrace__RayTrace': u'score'}
cmticec490e072014-06-13 15:38:45 -0700345
346 results_dict = self.result._AppendTelemetryUnits(kv_dict, units_dict)
347 self.assertEqual(results_dict,
348 {u'Box2D__Box2D': [4775, u'score'],
349 u'Splay__Splay': [4425, u'score'],
350 u'Gameboy__Gameboy': [9901, u'score'],
351 u'Crypto__Crypto': [8737, u'score'],
352 u'PdfJS__PdfJS': [6455, u'score'],
353 u'Total__Score': [7918, u'score'],
354 u'EarleyBoyer__EarleyBoyer': [14340, u'score'],
355 u'MandreelLatency__MandreelLatency': [5188, u'score'],
356 u'DeltaBlue__DeltaBlue': [14401, u'score'],
357 u'SplayLatency__SplayLatency': [7653, u'score'],
358 u'Mandreel__Mandreel': [6620, u'score'],
359 u'Richards__Richards': [10358, u'score'],
360 u'zlib__zlib': [16094, u'score'],
361 u'CodeLoad__CodeLoad': [6271, u'score'],
362 u'Typescript__Typescript': [9815, u'score'],
363 u'RegExp__RegExp': [1765, u'score'],
364 u'RayTrace__RayTrace': [16600, u'score'],
365 u'NavierStokes__NavierStokes': [9815, u'score']})
366
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800367 @mock.patch.object(misc, 'GetInsideChrootPath')
368 @mock.patch.object(tempfile, 'mkdtemp')
369 @mock.patch.object(command_executer.CommandExecuter, 'RunCommand')
Yunlian Jiangfbda0492015-12-21 15:32:44 -0800370 @mock.patch.object(command_executer.CommandExecuter,
371 'ChrootRunCommandWOutput')
cmticec490e072014-06-13 15:38:45 -0700372 def test_get_keyvals(self, mock_chrootruncmd, mock_runcmd, mock_mkdtemp,
373 mock_getpath):
374
375 self.kv_dict = {}
376 self.call_GetNewKeyvals = False
377
378 def reset():
379 self.kv_dict = {}
380 self.call_GetNewKeyvals = False
381 mock_chrootruncmd.reset_mock()
382 mock_runcmd.reset_mock()
383 mock_mkdtemp.reset_mock()
384 mock_getpath.reset_mock()
385
386 def FakeGetNewKeyvals(kv_dict):
387 self.kv_dict = kv_dict
388 self.call_GetNewKeyvals = True
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800389 return_kvdict = {'first_time': 680, 'Total': 10}
390 return_udict = {'first_time': 'ms', 'Total': 'score'}
cmticec490e072014-06-13 15:38:45 -0700391 return return_kvdict, return_udict
392
cmticec490e072014-06-13 15:38:45 -0700393 mock_mkdtemp.return_value = TMP_DIR1
394 mock_chrootruncmd.return_value = ['',
395 ('%s,PASS\n%s/telemetry_Crosperf,PASS\n')
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800396 % (TMP_DIR1, TMP_DIR1), '']
cmticec490e072014-06-13 15:38:45 -0700397 mock_getpath.return_value = TMP_DIR1
Caroline Tice8d818fb2015-12-16 16:42:54 -0800398 self.result._ce.ChrootRunCommandWOutput = mock_chrootruncmd
cmticec490e072014-06-13 15:38:45 -0700399 self.result._ce.RunCommand = mock_runcmd
400 self.result._GetNewKeyvals = FakeGetNewKeyvals
401 self.result.suite = 'telemetry_Crosperf'
402 self.result.results_dir = '/tmp/test_that_resultsNmq'
403
404 # Test 1. no self._temp_dir.
405 res = self.result._GetKeyvals(True)
406 self.assertTrue(self.call_GetNewKeyvals)
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800407 self.assertEqual(self.kv_dict, {'': 'PASS', 'telemetry_Crosperf': 'PASS'})
cmticec490e072014-06-13 15:38:45 -0700408 self.assertEqual(mock_runcmd.call_count, 1)
409 self.assertEqual(mock_runcmd.call_args_list[0][0],
410 ('cp -r /tmp/test_that_resultsNmq/* %s' % TMP_DIR1,))
411 self.assertEqual(mock_chrootruncmd.call_count, 1)
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800412 self.assertEqual(mock_chrootruncmd.call_args_list[0][0], (
413 '/tmp', ('python generate_test_report --no-color --csv %s') % TMP_DIR1))
cmticec490e072014-06-13 15:38:45 -0700414 self.assertEqual(mock_getpath.call_count, 1)
415 self.assertEqual(mock_mkdtemp.call_count, 1)
416 self.assertEqual(res, {'Total': [10, 'score'], 'first_time': [680, 'ms']})
417
cmticec490e072014-06-13 15:38:45 -0700418 # Test 2. self._temp_dir
419 reset()
420 mock_chrootruncmd.return_value = ['',
421 ('/tmp/tmpJCajRG,PASS\n/tmp/tmpJCajRG/'
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800422 'telemetry_Crosperf,PASS\n'), '']
cmticec490e072014-06-13 15:38:45 -0700423 mock_getpath.return_value = '/tmp/tmpJCajRG'
424 self.result._temp_dir = '/tmp/tmpJCajRG'
425 res = self.result._GetKeyvals(True)
426 self.assertEqual(mock_runcmd.call_count, 0)
427 self.assertEqual(mock_mkdtemp.call_count, 0)
428 self.assertEqual(mock_chrootruncmd.call_count, 1)
429 self.assertTrue(self.call_GetNewKeyvals)
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800430 self.assertEqual(self.kv_dict, {'': 'PASS', 'telemetry_Crosperf': 'PASS'})
cmticec490e072014-06-13 15:38:45 -0700431 self.assertEqual(res, {'Total': [10, 'score'], 'first_time': [680, 'ms']})
432
433 # Test 3. suite != telemetry_Crosperf. Normally this would be for
434 # running non-Telemetry autotests, such as BootPerfServer. In this test
435 # case, the keyvals we have set up were returned from a Telemetry test run;
436 # so this pass is basically testing that we don't append the units to the
437 # test results (which we do for Telemetry autotest runs).
438 reset()
439 self.result.suite = ''
440 res = self.result._GetKeyvals(True)
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800441 self.assertEqual(res, {'Total': 10, 'first_time': 680})
cmticec490e072014-06-13 15:38:45 -0700442
443 def test_get_results_dir(self):
444
445 self.result.out = ''
446 self.assertRaises(Exception, self.result._GetResultsDir)
447
448 self.result.out = OUTPUT
449 resdir = self.result._GetResultsDir()
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800450 self.assertEqual(resdir, '/tmp/test_that.PO1234567/platform_LibCBench')
cmticec490e072014-06-13 15:38:45 -0700451
Caroline Tice8d818fb2015-12-16 16:42:54 -0800452 @mock.patch.object(command_executer.CommandExecuter, 'RunCommandGeneric')
cmticec490e072014-06-13 15:38:45 -0700453 def test_find_files_in_results_dir(self, mock_runcmd):
454
455 self.result.results_dir = None
456 res = self.result._FindFilesInResultsDir('-name perf.data')
457 self.assertIsNone(res)
458
459 self.result._ce.RunCommand = mock_runcmd
460 self.result.results_dir = '/tmp/test_results'
461 mock_runcmd.return_value = [0, '/tmp/test_results/perf.data', '']
462 res = self.result._FindFilesInResultsDir('-name perf.data')
463 self.assertEqual(mock_runcmd.call_count, 1)
464 self.assertEqual(mock_runcmd.call_args_list[0][0],
465 ('find /tmp/test_results -name perf.data',))
466 self.assertEqual(res, '/tmp/test_results/perf.data')
467
468 mock_runcmd.reset_mock()
469 mock_runcmd.return_value = [1, '', '']
470 self.assertRaises(Exception, self.result._FindFilesInResultsDir,
471 '-name perf.data')
472
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800473 @mock.patch.object(Result, '_FindFilesInResultsDir')
cmticec490e072014-06-13 15:38:45 -0700474 def test_get_perf_data_files(self, mock_findfiles):
475 self.args = None
476
477 mock_findfiles.return_value = 'line1\nline1\n'
478 self.result._FindFilesInResultsDir = mock_findfiles
479 res = self.result._GetPerfDataFiles()
480 self.assertEqual(res, ['line1', 'line1'])
481 self.assertEqual(mock_findfiles.call_args_list[0][0], ('-name perf.data',))
482
cmticec490e072014-06-13 15:38:45 -0700483 def test_get_perf_report_files(self):
484 self.args = None
485
486 def FakeFindFiles(find_args):
487 self.args = find_args
488 return 'line1\nline1\n'
489
490 self.result._FindFilesInResultsDir = FakeFindFiles
491 res = self.result._GetPerfReportFiles()
492 self.assertEqual(res, ['line1', 'line1'])
493 self.assertEqual(self.args, '-name perf.data.report')
494
cmticec490e072014-06-13 15:38:45 -0700495 def test_get_data_measurement_files(self):
496 self.args = None
497
498 def FakeFindFiles(find_args):
499 self.args = find_args
500 return 'line1\nline1\n'
501
502 self.result._FindFilesInResultsDir = FakeFindFiles
503 res = self.result._GetDataMeasurementsFiles()
504 self.assertEqual(res, ['line1', 'line1'])
505 self.assertEqual(self.args, '-name perf_measurements')
506
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800507 @mock.patch.object(misc, 'GetInsideChrootPath')
508 @mock.patch.object(command_executer.CommandExecuter, 'ChrootRunCommand')
cmticec490e072014-06-13 15:38:45 -0700509 def test_generate_perf_report_files(self, mock_chrootruncmd, mock_getpath):
510 fake_file = '/usr/chromeos/chroot/tmp/results/fake_file'
511 self.result.perf_data_files = ['/tmp/results/perf.data']
512 self.result._board = 'lumpy'
513 mock_getpath.return_value = fake_file
514 self.result._ce.ChrootRunCommand = mock_chrootruncmd
515 tmp = self.result._GeneratePerfReportFiles()
516 self.assertEqual(tmp, ['/tmp/chroot%s' % fake_file])
517 self.assertEqual(mock_chrootruncmd.call_args_list[0][0],
518 ('/tmp',
519 ('/tmp/perf.static report -n --symfs /build/lumpy '
520 '--vmlinux /build/lumpy/usr/lib/debug/boot/vmlinux '
521 '--kallsyms /build/lumpy/boot/System.map-* -i '
522 '%s --stdio > %s') % (fake_file, fake_file)))
523
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800524 @mock.patch.object(misc, 'GetOutsideChrootPath')
cmticec490e072014-06-13 15:38:45 -0700525 def test_populate_from_run(self, mock_getpath):
526
527 def FakeGetResultsDir():
528 self.callGetResultsDir = True
529 return '/tmp/results_dir'
530
531 def FakeGetPerfDataFiles():
532 self.callGetPerfDataFiles = True
533 return []
534
535 def FakeGetPerfReportFiles():
536 self.callGetPerfReportFiles = True
537 return []
538
539 def FakeProcessResults(show_results):
Yunlian Jiangaefb76c2015-12-29 08:59:52 -0800540 if show_results:
541 pass
cmticec490e072014-06-13 15:38:45 -0700542 self.callProcessResults = True
543
Yunlian Jiangaefb76c2015-12-29 08:59:52 -0800544 if mock_getpath:
545 pass
cmticec490e072014-06-13 15:38:45 -0700546 mock.get_path = '/tmp/chromeos/tmp/results_dir'
547 self.result._chromeos_root = '/tmp/chromeos'
548
549 self.callGetResultsDir = False
550 self.callGetPerfDataFiles = False
551 self.callGetPerfReportFiles = False
552 self.callProcessResults = False
553
554 self.result._GetResultsDir = FakeGetResultsDir
555 self.result._GetPerfDataFiles = FakeGetPerfDataFiles
556 self.result._GeneratePerfReportFiles = FakeGetPerfReportFiles
557 self.result._ProcessResults = FakeProcessResults
558
559 self.result._PopulateFromRun(OUTPUT, '', 0, True, 'test',
560 'telemetry_Crosperf')
561 self.assertTrue(self.callGetResultsDir)
562 self.assertTrue(self.callGetPerfDataFiles)
563 self.assertTrue(self.callGetPerfReportFiles)
564 self.assertTrue(self.callProcessResults)
565
566 def test_process_results(self):
567
568 def FakeGetKeyvals(show_all):
569 if show_all:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800570 return {'first_time': 680, 'Total': 10}
cmticec490e072014-06-13 15:38:45 -0700571 else:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800572 return {'Total': 10}
cmticec490e072014-06-13 15:38:45 -0700573
574 def FakeGatherPerfResults():
575 self.callGatherPerfResults = True
576
577 self.callGatherPerfResults = False
578
579 self.result._GetKeyvals = FakeGetKeyvals
580 self.result._GatherPerfResults = FakeGatherPerfResults
581
582 self.result.retval = 0
583 self.result._ProcessResults(True)
584 self.assertTrue(self.callGatherPerfResults)
585 self.assertEqual(len(self.result.keyvals), 3)
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800586 self.assertEqual(self.result.keyvals, {'first_time': 680,
587 'Total': 10,
588 'retval': 0})
cmticec490e072014-06-13 15:38:45 -0700589
590 self.result.retval = 1
591 self.result._ProcessResults(False)
592 self.assertEqual(len(self.result.keyvals), 2)
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800593 self.assertEqual(self.result.keyvals, {'Total': 10, 'retval': 1})
cmticec490e072014-06-13 15:38:45 -0700594
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800595 @mock.patch.object(misc, 'GetInsideChrootPath')
Yunlian Jiangfbda0492015-12-21 15:32:44 -0800596 @mock.patch.object(command_executer.CommandExecuter,
597 'ChrootRunCommandWOutput')
cmticec490e072014-06-13 15:38:45 -0700598 def test_populate_from_cache_dir(self, mock_runchrootcmd, mock_getpath):
599
600 def FakeMkdtemp(dir=''):
Yunlian Jiangaefb76c2015-12-29 08:59:52 -0800601 if dir:
602 pass
cmticec490e072014-06-13 15:38:45 -0700603 return self.tmpdir
604
605 current_path = os.getcwd()
606 cache_dir = os.path.join(current_path, 'test_cache/test_input')
607 self.result._ce = command_executer.GetCommandExecuter(log_level='average')
Caroline Tice8d818fb2015-12-16 16:42:54 -0800608 self.result._ce.ChrootRunCommandWOutput = mock_runchrootcmd
cmticec490e072014-06-13 15:38:45 -0700609 mock_runchrootcmd.return_value = ['',
610 ('%s,PASS\n%s/\telemetry_Crosperf,PASS\n')
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800611 % (TMP_DIR1, TMP_DIR1), '']
cmticec490e072014-06-13 15:38:45 -0700612 mock_getpath.return_value = TMP_DIR1
613 self.tmpdir = tempfile.mkdtemp()
614 save_real_mkdtemp = tempfile.mkdtemp
615 tempfile.mkdtemp = FakeMkdtemp
616
617 self.result._PopulateFromCacheDir(cache_dir, True, 'sunspider',
618 'telemetry_Crosperf')
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800619 self.assertEqual(
620 self.result.keyvals,
621 {u'Total__Total': [444.0, u'ms'],
622 u'regexp-dna__regexp-dna': [16.2, u'ms'],
623 u'telemetry_page_measurement_results__num_failed': [0, u'count'],
624 u'telemetry_page_measurement_results__num_errored': [0, u'count'],
625 u'string-fasta__string-fasta': [23.2, u'ms'],
626 u'crypto-sha1__crypto-sha1': [11.6, u'ms'],
627 u'bitops-3bit-bits-in-byte__bitops-3bit-bits-in-byte': [3.2, u'ms'],
628 u'access-nsieve__access-nsieve': [7.9, u'ms'],
629 u'bitops-nsieve-bits__bitops-nsieve-bits': [9.4, u'ms'],
630 u'string-validate-input__string-validate-input': [19.3, u'ms'],
631 u'3d-raytrace__3d-raytrace': [24.7, u'ms'],
632 u'3d-cube__3d-cube': [28.0, u'ms'],
633 u'string-unpack-code__string-unpack-code': [46.7, u'ms'],
634 u'date-format-tofte__date-format-tofte': [26.3, u'ms'],
635 u'math-partial-sums__math-partial-sums': [22.0, u'ms'],
636 '\telemetry_Crosperf': ['PASS', ''],
637 u'crypto-aes__crypto-aes': [15.2, u'ms'],
638 u'bitops-bitwise-and__bitops-bitwise-and': [8.4, u'ms'],
639 u'crypto-md5__crypto-md5': [10.5, u'ms'],
640 u'string-tagcloud__string-tagcloud': [52.8, u'ms'],
641 u'access-nbody__access-nbody': [8.5, u'ms'],
642 'retval': 0,
643 u'math-spectral-norm__math-spectral-norm': [6.6, u'ms'],
644 u'math-cordic__math-cordic': [8.7, u'ms'],
645 u'access-binary-trees__access-binary-trees': [4.5, u'ms'],
646 u'controlflow-recursive__controlflow-recursive': [4.4, u'ms'],
647 u'access-fannkuch__access-fannkuch': [17.8, u'ms'],
648 u'string-base64__string-base64': [16.0, u'ms'],
649 u'date-format-xparb__date-format-xparb': [20.9, u'ms'],
650 u'3d-morph__3d-morph': [22.1, u'ms'],
651 u'bitops-bits-in-byte__bitops-bits-in-byte': [9.1, u'ms']})
cmticec490e072014-06-13 15:38:45 -0700652
653 # Clean up after test.
654 tempfile.mkdtemp = save_real_mkdtemp
655 command = 'rm -Rf %s' % self.tmpdir
656 self.result._ce.RunCommand(command)
657
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800658 @mock.patch.object(misc, 'GetRoot')
659 @mock.patch.object(command_executer.CommandExecuter, 'RunCommand')
cmticec490e072014-06-13 15:38:45 -0700660 def test_cleanup(self, mock_runcmd, mock_getroot):
661
662 # Test 1. 'rm_chroot_tmp' is True; self.results_dir exists;
663 # self._temp_dir exists; results_dir name contains 'test_that_results_'.
664 mock_getroot.return_value = ['/tmp/tmp_AbcXyz', 'test_that_results_fake']
665 self.result._ce.RunCommand = mock_runcmd
666 self.result.results_dir = 'test_results_dir'
667 self.result._temp_dir = 'test_temp_dir'
668 self.result.CleanUp(True)
669 self.assertEqual(mock_getroot.call_count, 1)
670 self.assertEqual(mock_runcmd.call_count, 2)
671 self.assertEqual(mock_runcmd.call_args_list[0][0],
672 ('rm -rf test_results_dir',))
673 self.assertEqual(mock_runcmd.call_args_list[1][0],
674 ('rm -rf test_temp_dir',))
675
676 # Test 2. Same, except ath results_dir name does not contain
677 # 'test_that_results_'
678 mock_getroot.reset_mock()
679 mock_runcmd.reset_mock()
680 mock_getroot.return_value = ['/tmp/tmp_AbcXyz', 'other_results_fake']
681 self.result._ce.RunCommand = mock_runcmd
682 self.result.results_dir = 'test_results_dir'
683 self.result._temp_dir = 'test_temp_dir'
684 self.result.CleanUp(True)
685 self.assertEqual(mock_getroot.call_count, 1)
686 self.assertEqual(mock_runcmd.call_count, 2)
687 self.assertEqual(mock_runcmd.call_args_list[0][0],
688 ('rm -rf /tmp/tmp_AbcXyz',))
689 self.assertEqual(mock_runcmd.call_args_list[1][0],
690 ('rm -rf test_temp_dir',))
691
692 # Test 3. mock_getroot returns nothing; 'rm_chroot_tmp' is False.
693 mock_getroot.reset_mock()
694 mock_runcmd.reset_mock()
695 self.result.CleanUp(False)
696 self.assertEqual(mock_getroot.call_count, 0)
697 self.assertEqual(mock_runcmd.call_count, 1)
698 self.assertEqual(mock_runcmd.call_args_list[0][0],
699 ('rm -rf test_temp_dir',))
700
701 # Test 4. 'rm_chroot_tmp' is True, but result_dir & _temp_dir are None.
702 mock_getroot.reset_mock()
703 mock_runcmd.reset_mock()
704 self.result.results_dir = None
705 self.result._temp_dir = None
706 self.result.CleanUp(True)
707 self.assertEqual(mock_getroot.call_count, 0)
708 self.assertEqual(mock_runcmd.call_count, 0)
709
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800710 @mock.patch.object(misc, 'GetInsideChrootPath')
711 @mock.patch.object(command_executer.CommandExecuter, 'ChrootRunCommand')
cmticec490e072014-06-13 15:38:45 -0700712 def test_store_to_cache_dir(self, mock_chrootruncmd, mock_getpath):
713
Yunlian Jiangaefb76c2015-12-29 08:59:52 -0800714 def FakeMkdtemp(directory=''):
715 if directory:
716 pass
cmticec490e072014-06-13 15:38:45 -0700717 return self.tmpdir
718
Yunlian Jiangaefb76c2015-12-29 08:59:52 -0800719 if mock_chrootruncmd or mock_getpath:
720 pass
cmticec490e072014-06-13 15:38:45 -0700721 current_path = os.getcwd()
722 cache_dir = os.path.join(current_path, 'test_cache/test_output')
723
724 self.result._ce = command_executer.GetCommandExecuter(log_level='average')
725 self.result.out = OUTPUT
726 self.result.err = error
727 self.result.retval = 0
728 self.tmpdir = tempfile.mkdtemp()
729 if not os.path.exists(self.tmpdir):
730 os.makedirs(self.tmpdir)
731 self.result.results_dir = os.path.join(os.getcwd(), 'test_cache')
732 save_real_mkdtemp = tempfile.mkdtemp
733 tempfile.mkdtemp = FakeMkdtemp
734
735 mock_mm = machine_manager.MockMachineManager('/tmp/chromeos_root', 0,
Caroline Ticee627fd62015-12-11 12:07:59 -0800736 'average')
cmticec490e072014-06-13 15:38:45 -0700737 mock_mm.machine_checksum_string['mock_label'] = 'fake_machine_checksum123'
738
Caroline Ticee627fd62015-12-11 12:07:59 -0800739 mock_keylist = ['key1', 'key2', 'key3']
740 test_flag.SetTestMode(True)
741 self.result.StoreToCacheDir(cache_dir, mock_mm, mock_keylist)
cmticec490e072014-06-13 15:38:45 -0700742
743 # Check that the correct things were written to the 'cache'.
744 test_dir = os.path.join(os.getcwd(), 'test_cache/test_output')
745 base_dir = os.path.join(os.getcwd(), 'test_cache/compare_output')
746 self.assertTrue(os.path.exists(os.path.join(test_dir, 'autotest.tbz2')))
747 self.assertTrue(os.path.exists(os.path.join(test_dir, 'machine.txt')))
748 self.assertTrue(os.path.exists(os.path.join(test_dir, 'results.txt')))
749
750 f1 = os.path.join(test_dir, 'machine.txt')
751 f2 = os.path.join(base_dir, 'machine.txt')
752 cmd = 'diff %s %s' % (f1, f2)
Luis Lozano036c9232015-12-10 10:47:01 -0800753 [_, out, _] = self.result._ce.RunCommandWOutput(cmd)
cmticec490e072014-06-13 15:38:45 -0700754 self.assertEqual(len(out), 0)
755
756 f1 = os.path.join(test_dir, 'results.txt')
757 f2 = os.path.join(base_dir, 'results.txt')
758 cmd = 'diff %s %s' % (f1, f2)
Luis Lozano036c9232015-12-10 10:47:01 -0800759 [_, out, _] = self.result._ce.RunCommandWOutput(cmd)
cmticec490e072014-06-13 15:38:45 -0700760 self.assertEqual(len(out), 0)
761
762 # Clean up after test.
763 tempfile.mkdtemp = save_real_mkdtemp
764 command = 'rm %s/*' % test_dir
765 self.result._ce.RunCommand(command)
766
767
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800768TELEMETRY_RESULT_KEYVALS = {
769 'http://www.webkit.org/perf/sunspider-1.0.2/sunspider-1.0.2/driver.html '
770 'math-cordic (ms)':
771 '11.4',
772 'http://www.webkit.org/perf/sunspider-1.0.2/sunspider-1.0.2/driver.html '
773 'access-nbody (ms)':
774 '6.9',
775 'http://www.webkit.org/perf/sunspider-1.0.2/sunspider-1.0.2/driver.html '
776 'access-fannkuch (ms)':
777 '26.3',
778 'http://www.webkit.org/perf/sunspider-1.0.2/sunspider-1.0.2/driver.html '
779 'math-spectral-norm (ms)':
780 '6.3',
781 'http://www.webkit.org/perf/sunspider-1.0.2/sunspider-1.0.2/driver.html '
782 'bitops-nsieve-bits (ms)':
783 '9.3',
784 'http://www.webkit.org/perf/sunspider-1.0.2/sunspider-1.0.2/driver.html '
785 'math-partial-sums (ms)':
786 '32.8',
787 'http://www.webkit.org/perf/sunspider-1.0.2/sunspider-1.0.2/driver.html '
788 'regexp-dna (ms)':
789 '16.1',
790 'http://www.webkit.org/perf/sunspider-1.0.2/sunspider-1.0.2/driver.html '
791 '3d-cube (ms)':
792 '42.7',
793 'http://www.webkit.org/perf/sunspider-1.0.2/sunspider-1.0.2/driver.html '
794 'crypto-md5 (ms)':
795 '10.8',
796 'http://www.webkit.org/perf/sunspider-1.0.2/sunspider-1.0.2/driver.html '
797 'crypto-sha1 (ms)':
798 '12.4',
799 'http://www.webkit.org/perf/sunspider-1.0.2/sunspider-1.0.2/driver.html '
800 'string-tagcloud (ms)':
801 '47.2',
802 'http://www.webkit.org/perf/sunspider-1.0.2/sunspider-1.0.2/driver.html '
803 'string-fasta (ms)':
804 '36.3',
805 'http://www.webkit.org/perf/sunspider-1.0.2/sunspider-1.0.2/driver.html '
806 'access-binary-trees (ms)':
807 '7.3',
808 'http://www.webkit.org/perf/sunspider-1.0.2/sunspider-1.0.2/driver.html '
809 'date-format-xparb (ms)':
810 '138.1',
811 'http://www.webkit.org/perf/sunspider-1.0.2/sunspider-1.0.2/driver.html '
812 'crypto-aes (ms)':
813 '19.2',
814 'http://www.webkit.org/perf/sunspider-1.0.2/sunspider-1.0.2/driver.html '
815 'Total (ms)':
816 '656.5',
817 'http://www.webkit.org/perf/sunspider-1.0.2/sunspider-1.0.2/driver.html '
818 'string-base64 (ms)':
819 '17.5',
820 'http://www.webkit.org/perf/sunspider-1.0.2/sunspider-1.0.2/driver.html '
821 'string-validate-input (ms)':
822 '24.8',
823 'http://www.webkit.org/perf/sunspider-1.0.2/sunspider-1.0.2/driver.html '
824 '3d-raytrace (ms)':
825 '28.7',
826 'http://www.webkit.org/perf/sunspider-1.0.2/sunspider-1.0.2/driver.html '
827 'controlflow-recursive (ms)':
828 '5.3',
829 'http://www.webkit.org/perf/sunspider-1.0.2/sunspider-1.0.2/driver.html '
830 'bitops-bits-in-byte (ms)':
831 '9.8',
832 'http://www.webkit.org/perf/sunspider-1.0.2/sunspider-1.0.2/driver.html '
833 '3d-morph (ms)':
834 '50.2',
835 'http://www.webkit.org/perf/sunspider-1.0.2/sunspider-1.0.2/driver.html '
836 'bitops-bitwise-and (ms)':
837 '8.8',
838 'http://www.webkit.org/perf/sunspider-1.0.2/sunspider-1.0.2/driver.html '
839 'access-nsieve (ms)':
840 '8.6',
841 'http://www.webkit.org/perf/sunspider-1.0.2/sunspider-1.0.2/driver.html '
842 'date-format-tofte (ms)':
843 '31.2',
844 'http://www.webkit.org/perf/sunspider-1.0.2/sunspider-1.0.2/driver.html '
845 'bitops-3bit-bits-in-byte (ms)':
846 '3.5',
847 'retval': 0,
848 'http://www.webkit.org/perf/sunspider-1.0.2/sunspider-1.0.2/driver.html '
849 'string-unpack-code (ms)':
850 '45.0'
851}
cmticec490e072014-06-13 15:38:45 -0700852
Yunlian Jiangfbda0492015-12-21 15:32:44 -0800853PURE_TELEMETRY_OUTPUT = """
854page_name,3d-cube (ms),3d-morph (ms),3d-raytrace (ms),Total (ms),access-binary-trees (ms),access-fannkuch (ms),access-nbody (ms),access-nsieve (ms),bitops-3bit-bits-in-byte (ms),bitops-bits-in-byte (ms),bitops-bitwise-and (ms),bitops-nsieve-bits (ms),controlflow-recursive (ms),crypto-aes (ms),crypto-md5 (ms),crypto-sha1 (ms),date-format-tofte (ms),date-format-xparb (ms),math-cordic (ms),math-partial-sums (ms),math-spectral-norm (ms),regexp-dna (ms),string-base64 (ms),string-fasta (ms),string-tagcloud (ms),string-unpack-code (ms),string-validate-input (ms)\r\nhttp://www.webkit.org/perf/sunspider-1.0.2/sunspider-1.0.2/driver.html,42.7,50.2,28.7,656.5,7.3,26.3,6.9,8.6,3.5,9.8,8.8,9.3,5.3,19.2,10.8,12.4,31.2,138.1,11.4,32.8,6.3,16.1,17.5,36.3,47.2,45.0,24.8\r
855"""
cmticec490e072014-06-13 15:38:45 -0700856
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800857
cmticec490e072014-06-13 15:38:45 -0700858class TelemetryResultTest(unittest.TestCase):
Yunlian Jiangaefb76c2015-12-29 08:59:52 -0800859 """Telemetry result test."""
cmticec490e072014-06-13 15:38:45 -0700860 mock_logger = mock.Mock(spec=logger.Logger)
861 mock_cmd_exec = mock.Mock(spec=command_executer.CommandExecuter)
862 mock_label = MockLabel('mock_label', 'chromeos_image', '/tmp', 'lumpy',
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800863 'remote', 'image_args', 'cache_dir', 'average', 'gcc',
864 None)
865 mock_machine = machine_manager.MockCrosMachine('falco.cros', '/tmp/chromeos',
Caroline Ticee627fd62015-12-11 12:07:59 -0800866 'average')
cmticec490e072014-06-13 15:38:45 -0700867
868 def test_populate_from_run(self):
869
870 def FakeProcessResults():
871 self.callFakeProcessResults = True
872
873 self.callFakeProcessResults = False
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800874 self.result = TelemetryResult(self.mock_logger, self.mock_label, 'average',
875 self.mock_cmd_exec)
cmticec490e072014-06-13 15:38:45 -0700876 self.result._ProcessResults = FakeProcessResults
877 self.result._PopulateFromRun(OUTPUT, error, 3, False, 'fake_test',
878 'telemetry_Crosperf')
879 self.assertTrue(self.callFakeProcessResults)
880 self.assertEqual(self.result.out, OUTPUT)
881 self.assertEqual(self.result.err, error)
882 self.assertEqual(self.result.retval, 3)
883
cmticec490e072014-06-13 15:38:45 -0700884 def test_populate_from_cache_dir_and_process_results(self):
885
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800886 self.result = TelemetryResult(self.mock_logger, self.mock_label, 'average',
887 self.mock_machine)
cmticec490e072014-06-13 15:38:45 -0700888 current_path = os.getcwd()
889 cache_dir = os.path.join(current_path,
890 'test_cache/test_puretelemetry_input')
891 self.result._PopulateFromCacheDir(cache_dir)
Yunlian Jiangfbda0492015-12-21 15:32:44 -0800892 self.assertEqual(self.result.out.strip(), PURE_TELEMETRY_OUTPUT.strip())
cmticec490e072014-06-13 15:38:45 -0700893 self.assertEqual(self.result.err, '')
894 self.assertEqual(self.result.retval, 0)
895 self.assertEqual(self.result.keyvals, TELEMETRY_RESULT_KEYVALS)
896
897
898class ResultsCacheTest(unittest.TestCase):
Yunlian Jiangaefb76c2015-12-29 08:59:52 -0800899 """Resultcache test class."""
cmticec490e072014-06-13 15:38:45 -0700900 mock_logger = mock.Mock(spec=logger.Logger)
901 mock_label = MockLabel('mock_label', 'chromeos_image', '/tmp', 'lumpy',
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800902 'remote', 'image_args', 'cache_dir', 'average', 'gcc',
903 None)
904
cmticec490e072014-06-13 15:38:45 -0700905 def setUp(self):
906 self.results_cache = ResultsCache()
907
Caroline Ticee627fd62015-12-11 12:07:59 -0800908 mock_machine = machine_manager.MockCrosMachine('falco.cros',
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800909 '/tmp/chromeos', 'average')
cmticec490e072014-06-13 15:38:45 -0700910
911 mock_mm = machine_manager.MockMachineManager('/tmp/chromeos_root', 0,
Caroline Ticee627fd62015-12-11 12:07:59 -0800912 'average')
cmticec490e072014-06-13 15:38:45 -0700913 mock_mm.machine_checksum_string['mock_label'] = 'fake_machine_checksum123'
914
915 self.results_cache.Init(self.mock_label.chromeos_image,
916 self.mock_label.chromeos_root,
917 'sunspider',
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800918 1, # benchmark_run.iteration,
919 '', # benchmark_run.test_args,
920 '', # benchmark_run.profiler_args,
cmticec490e072014-06-13 15:38:45 -0700921 mock_mm,
Caroline Ticee627fd62015-12-11 12:07:59 -0800922 mock_machine,
cmticec490e072014-06-13 15:38:45 -0700923 self.mock_label.board,
924 [CacheConditions.CACHE_FILE_EXISTS,
925 CacheConditions.CHECKSUMS_MATCH],
926 self.mock_logger,
927 'average',
928 self.mock_label,
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800929 '', # benchmark_run.share_cache
cmticec490e072014-06-13 15:38:45 -0700930 'telemetry_Crosperf',
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800931 True, # benchmark_run.show_all_results
932 False) # benchmark_run.run_local
cmticec490e072014-06-13 15:38:45 -0700933
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800934 @mock.patch.object(image_checksummer.ImageChecksummer, 'Checksum')
cmticec490e072014-06-13 15:38:45 -0700935 def test_get_cache_dir_for_write(self, mock_checksum):
936
937 def FakeGetMachines(label):
Yunlian Jiangaefb76c2015-12-29 08:59:52 -0800938 if label:
939 pass
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800940 m1 = machine_manager.MockCrosMachine(
941 'lumpy1.cros', self.results_cache.chromeos_root, 'average')
942 m2 = machine_manager.MockCrosMachine(
943 'lumpy2.cros', self.results_cache.chromeos_root, 'average')
944 return [m1, m2]
cmticec490e072014-06-13 15:38:45 -0700945
946 mock_checksum.return_value = 'FakeImageChecksumabc123'
947 self.results_cache.machine_manager.GetMachines = FakeGetMachines
948 self.results_cache.machine_manager.machine_checksum['mock_label'] = \
949 'FakeMachineChecksumabc987'
950 # Based on the label, benchmark and machines, get the directory in which
951 # to store the cache information for this test run.
952 result_path = self.results_cache._GetCacheDirForWrite()
953 # Verify that the returned directory is correct (since the label
954 # contained a cache_dir, named 'cache_dir', that's what is expected in
955 # the result, rather than '~/cros_scratch').
956 comp_path = os.path.join(os.getcwd(),
957 'cache_dir/54524606abaae4fdf7b02f49f7ae7127_'
Caroline Ticee627fd62015-12-11 12:07:59 -0800958 'sunspider_1_fda29412ceccb72977516c4785d08e2c_'
cmticec490e072014-06-13 15:38:45 -0700959 'FakeImageChecksumabc123_FakeMachineChecksum'
960 'abc987__6')
961 self.assertEqual(result_path, comp_path)
962
cmticec490e072014-06-13 15:38:45 -0700963 def test_form_cache_dir(self):
964 # This is very similar to the previous test (_FormCacheDir is called
965 # from _GetCacheDirForWrite).
966 cache_key_list = ('54524606abaae4fdf7b02f49f7ae7127', 'sunspider', '1',
967 '7215ee9c7d9dc229d2921a40e899ec5f',
968 'FakeImageChecksumabc123', '*', '*', '6')
969 path = self.results_cache._FormCacheDir(cache_key_list)
970 self.assertEqual(len(path), 1)
971 path1 = path[0]
972 test_dirname = ('54524606abaae4fdf7b02f49f7ae7127_sunspider_1_7215ee9'
973 'c7d9dc229d2921a40e899ec5f_FakeImageChecksumabc123_*_*_6')
974 comp_path = os.path.join(os.getcwd(), 'cache_dir', test_dirname)
975 self.assertEqual(path1, comp_path)
976
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800977 @mock.patch.object(image_checksummer.ImageChecksummer, 'Checksum')
cmticec490e072014-06-13 15:38:45 -0700978 def test_get_cache_key_list(self, mock_checksum):
979 # This tests the mechanism that generates the various pieces of the
980 # cache directory name, based on various conditions.
981
982 def FakeGetMachines(label):
Yunlian Jiangaefb76c2015-12-29 08:59:52 -0800983 if label:
984 pass
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800985 m1 = machine_manager.MockCrosMachine(
986 'lumpy1.cros', self.results_cache.chromeos_root, 'average')
987 m2 = machine_manager.MockCrosMachine(
988 'lumpy2.cros', self.results_cache.chromeos_root, 'average')
989 return [m1, m2]
cmticec490e072014-06-13 15:38:45 -0700990
991 mock_checksum.return_value = 'FakeImageChecksumabc123'
992 self.results_cache.machine_manager.GetMachines = FakeGetMachines
993 self.results_cache.machine_manager.machine_checksum['mock_label'] = \
994 'FakeMachineChecksumabc987'
995
996 # Test 1. Generating cache name for reading (not writing).
997 key_list = self.results_cache._GetCacheKeyList(True)
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800998 self.assertEqual(key_list[0], '*') # Machine checksum value, for read.
cmticec490e072014-06-13 15:38:45 -0700999 self.assertEqual(key_list[1], 'sunspider')
1000 self.assertEqual(key_list[2], '1')
Caroline Ticee627fd62015-12-11 12:07:59 -08001001 self.assertEqual(key_list[3], 'fda29412ceccb72977516c4785d08e2c')
cmticec490e072014-06-13 15:38:45 -07001002 self.assertEqual(key_list[4], 'FakeImageChecksumabc123')
1003 self.assertEqual(key_list[5], '*')
1004 self.assertEqual(key_list[6], '*')
1005 self.assertEqual(key_list[7], '6')
1006
1007 # Test 2. Generating cache name for writing, with local image type.
1008 key_list = self.results_cache._GetCacheKeyList(False)
1009 self.assertEqual(key_list[0], '54524606abaae4fdf7b02f49f7ae7127')
1010 self.assertEqual(key_list[1], 'sunspider')
1011 self.assertEqual(key_list[2], '1')
Caroline Ticee627fd62015-12-11 12:07:59 -08001012 self.assertEqual(key_list[3], 'fda29412ceccb72977516c4785d08e2c')
cmticec490e072014-06-13 15:38:45 -07001013 self.assertEqual(key_list[4], 'FakeImageChecksumabc123')
1014 self.assertEqual(key_list[5], 'FakeMachineChecksumabc987')
1015 self.assertEqual(key_list[6], '')
1016 self.assertEqual(key_list[7], '6')
1017
1018 # Test 3. Generating cache name for writing, with trybot image type.
1019 self.results_cache.label.image_type = 'trybot'
1020 key_list = self.results_cache._GetCacheKeyList(False)
1021 self.assertEqual(key_list[0], '54524606abaae4fdf7b02f49f7ae7127')
Caroline Ticee627fd62015-12-11 12:07:59 -08001022 self.assertEqual(key_list[3], 'fda29412ceccb72977516c4785d08e2c')
cmticec490e072014-06-13 15:38:45 -07001023 self.assertEqual(key_list[4], '54524606abaae4fdf7b02f49f7ae7127')
1024 self.assertEqual(key_list[5], 'FakeMachineChecksumabc987')
1025
1026 # Test 4. Generating cache name for writing, with official image type.
1027 self.results_cache.label.image_type = 'official'
1028 key_list = self.results_cache._GetCacheKeyList(False)
1029 self.assertEqual(key_list[0], '54524606abaae4fdf7b02f49f7ae7127')
1030 self.assertEqual(key_list[1], 'sunspider')
1031 self.assertEqual(key_list[2], '1')
Caroline Ticee627fd62015-12-11 12:07:59 -08001032 self.assertEqual(key_list[3], 'fda29412ceccb72977516c4785d08e2c')
cmticec490e072014-06-13 15:38:45 -07001033 self.assertEqual(key_list[4], '*')
1034 self.assertEqual(key_list[5], 'FakeMachineChecksumabc987')
1035 self.assertEqual(key_list[6], '')
1036 self.assertEqual(key_list[7], '6')
1037
1038 # Test 5. Generating cache name for writing, with local image type, and
1039 # specifying that the image path must match the cached image path.
1040 self.results_cache.label.image_type = 'local'
1041 self.results_cache.cache_conditions.append(CacheConditions.IMAGE_PATH_MATCH)
1042 key_list = self.results_cache._GetCacheKeyList(False)
1043 self.assertEqual(key_list[0], '54524606abaae4fdf7b02f49f7ae7127')
Caroline Ticee627fd62015-12-11 12:07:59 -08001044 self.assertEqual(key_list[3], 'fda29412ceccb72977516c4785d08e2c')
cmticec490e072014-06-13 15:38:45 -07001045 self.assertEqual(key_list[4], 'FakeImageChecksumabc123')
1046 self.assertEqual(key_list[5], 'FakeMachineChecksumabc987')
1047
Luis Lozanof2a3ef42015-12-15 13:49:30 -08001048 @mock.patch.object(command_executer.CommandExecuter, 'RunCommand')
1049 @mock.patch.object(os.path, 'isdir')
1050 @mock.patch.object(Result, 'CreateFromCacheHit')
cmticec490e072014-06-13 15:38:45 -07001051 def test_read_result(self, mock_create, mock_isdir, mock_runcmd):
1052
1053 self.fakeCacheReturnResult = None
Luis Lozanof2a3ef42015-12-15 13:49:30 -08001054
cmticec490e072014-06-13 15:38:45 -07001055 def FakeGetCacheDirForRead():
1056 return self.fakeCacheReturnResult
1057
1058 def FakeGetCacheDirForWrite():
1059 return self.fakeCacheReturnResult
1060
1061 mock_cmd_exec = mock.Mock(spec=command_executer.CommandExecuter)
1062 fake_result = Result(self.mock_logger, self.mock_label, 'average',
1063 mock_cmd_exec)
1064 fake_result.retval = 0
1065
1066 # Set up results_cache _GetCacheDirFor{Read,Write} to return
1067 # self.fakeCacheReturnResult, which is initially None (see above).
1068 # So initially, no cache dir is returned.
Luis Lozanof2a3ef42015-12-15 13:49:30 -08001069 self.results_cache._GetCacheDirForRead = FakeGetCacheDirForRead
cmticec490e072014-06-13 15:38:45 -07001070 self.results_cache._GetCacheDirForWrite = FakeGetCacheDirForWrite
1071
1072 mock_isdir.return_value = True
1073 save_cc = [CacheConditions.CACHE_FILE_EXISTS,
1074 CacheConditions.CHECKSUMS_MATCH]
1075 self.results_cache.cache_conditions.append(CacheConditions.FALSE)
1076
1077 # Test 1. CacheCondition.FALSE, which means do not read from the cache.
1078 # (force re-running of test). Result should be None.
1079 res = self.results_cache.ReadResult()
1080 self.assertIsNone(res)
1081 self.assertEqual(mock_runcmd.call_count, 1)
1082
1083 # Test 2. Remove CacheCondition.FALSE. Result should still be None,
1084 # because _GetCacheDirForRead is returning None at the moment.
1085 mock_runcmd.reset_mock()
1086 self.results_cache.cache_conditions = save_cc
1087 res = self.results_cache.ReadResult()
1088 self.assertIsNone(res)
1089 self.assertEqual(mock_runcmd.call_count, 0)
1090
1091 # Test 3. Now set up cache dir to be returned by _GetCacheDirForRead.
1092 # Since cache_dir is found, will call Result.CreateFromCacheHit, which
1093 # which will actually all our mock_create and should return fake_result.
1094 self.fakeCacheReturnResult = 'fake/cache/dir'
1095 mock_create.return_value = fake_result
1096 res = self.results_cache.ReadResult()
1097 self.assertEqual(mock_runcmd.call_count, 0)
1098 self.assertEqual(res, fake_result)
1099
1100 # Test 4. os.path.isdir(cache_dir) will now return false, so result
1101 # should be None again (no cache found).
1102 mock_isdir.return_value = False
1103 res = self.results_cache.ReadResult()
1104 self.assertEqual(mock_runcmd.call_count, 0)
1105 self.assertIsNone(res)
1106
1107 # Test 5. os.path.isdir returns true, but mock_create now returns None
1108 # (the call to CreateFromCacheHit returns None), so overal result is None.
1109 mock_isdir.return_value = True
1110 mock_create.return_value = None
1111 res = self.results_cache.ReadResult()
1112 self.assertEqual(mock_runcmd.call_count, 0)
1113 self.assertIsNone(res)
1114
1115 # Test 6. Everything works 'as expected', result should be fake_result.
1116 mock_create.return_value = fake_result
1117 res = self.results_cache.ReadResult()
1118 self.assertEqual(mock_runcmd.call_count, 0)
1119 self.assertEqual(res, fake_result)
1120
1121 # Test 7. The run failed; result should be None.
1122 mock_create.return_value = fake_result
1123 fake_result.retval = 1
1124 self.results_cache.cache_conditions.append(CacheConditions.RUN_SUCCEEDED)
1125 res = self.results_cache.ReadResult()
1126 self.assertEqual(mock_runcmd.call_count, 0)
1127 self.assertIsNone(res)
1128
1129
1130if __name__ == '__main__':
Yunlian Jiang4578ec12013-05-22 14:54:39 -07001131 unittest.main()