blob: 5a824b14169f0e384a0afb95b4d63ed852f7ba90 [file] [log] [blame]
George Burgess IVed696762016-09-12 18:24:54 -07001#!/usr/bin/env 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
Yunlian Jiang4ef13522015-12-29 13:20:45 -080029OUTPUT = """CMD (True): ./test_that.sh\
30 --remote=172.17.128.241 --board=lumpy LibCBench
Yunlian Jiang4578ec12013-05-22 14:54:39 -070031CMD (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 -070032Identity added: /tmp/test_that.PO1234567/autotest_key (/tmp/test_that.PO1234567/autotest_key)
Yunlian Jiang4578ec12013-05-22 14:54:39 -070033INFO : Using emerged autotests already installed at /build/lumpy/usr/local/autotest.
34
35INFO : Running the following control files 1 times:
36INFO : * 'client/site_tests/platform_LibCBench/control'
37
38INFO : Running client test client/site_tests/platform_LibCBench/control
cmticed96e4572015-05-19 16:19:25 -070039./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 -070040ERROR:root:import statsd failed, no stats will be reported.
cmticed96e4572015-05-19 16:19:25 -07004114:20:22 INFO | Results placed in /tmp/test_that.PO1234567/platform_LibCBench
Yunlian Jiang4578ec12013-05-22 14:54:39 -07004214:20:22 INFO | Processing control file
4314: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'
4414:20:23 ERROR| [stderr] Warning: Permanently added '172.17.128.241' (RSA) to the list of known hosts.
4514:20:23 INFO | INFO ---- ---- kernel=3.8.11 localtime=May 22 14:20:23 timestamp=1369257623
4614:20:23 INFO | Installing autotest on 172.17.128.241
4714:20:23 INFO | Using installation dir /usr/local/autotest
4814:20:23 WARNI| No job_repo_url for <remote host: 172.17.128.241>
4914:20:23 INFO | Could not install autotest using the packaging system: No repos to install an autotest client from. Trying other methods
5014:20:23 INFO | Installation of autotest completed
5114:20:24 WARNI| No job_repo_url for <remote host: 172.17.128.241>
5214:20:24 INFO | Executing /usr/local/autotest/bin/autotest /usr/local/autotest/control phase 0
5314:20:24 INFO | Entered autotestd_monitor.
5414:20:24 INFO | Finished launching tail subprocesses.
5514:20:24 INFO | Finished waiting on autotestd to start.
5614:20:26 INFO | START ---- ---- timestamp=1369257625 localtime=May 22 14:20:25
5714:20:26 INFO | START platform_LibCBench platform_LibCBench timestamp=1369257625 localtime=May 22 14:20:25
5814:20:30 INFO | GOOD platform_LibCBench platform_LibCBench timestamp=1369257630 localtime=May 22 14:20:30 completed successfully
5914:20:30 INFO | END GOOD platform_LibCBench platform_LibCBench timestamp=1369257630 localtime=May 22 14:20:30
6014:20:31 INFO | END GOOD ---- ---- timestamp=1369257630 localtime=May 22 14:20:30
6114:20:31 INFO | Got lock of exit_code_file.
6214:20:31 INFO | Released lock of exit_code_file and closed it.
63OUTPUT: ==============================
64OUTPUT: Current time: 2013-05-22 14:20:32.818831 Elapsed: 0:01:30 ETA: Unknown
65Done: 0% [ ]
66OUTPUT: Thread Status:
67RUNNING: 1 ('ttt: LibCBench (1)' 0:01:21)
68Machine Status:
69Machine Thread Lock Status Checksum
70172.17.128.241 ttt: LibCBench (1) True RUNNING 3ba9f2ecbb222f20887daea5583d86ba
71
72OUTPUT: ==============================
7314:20:33 INFO | Killing child processes.
7414:20:33 INFO | Client complete
7514:20:33 INFO | Finished processing control file
7614: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'
7714:20:33 ERROR| [stderr] Warning: Permanently added '172.17.128.241' (RSA) to the list of known hosts.
78
79INFO : Test results:
80-------------------------------------------------------------------
81platform_LibCBench [ PASSED ]
82platform_LibCBench/platform_LibCBench [ PASSED ]
83platform_LibCBench/platform_LibCBench b_malloc_big1__0_ 0.00375231466667
84platform_LibCBench/platform_LibCBench b_malloc_big2__0_ 0.002951359
85platform_LibCBench/platform_LibCBench b_malloc_bubble__0_ 0.015066374
86platform_LibCBench/platform_LibCBench b_malloc_sparse__0_ 0.015053784
87platform_LibCBench/platform_LibCBench b_malloc_thread_local__0_ 0.01138439
88platform_LibCBench/platform_LibCBench b_malloc_thread_stress__0_ 0.0367894733333
89platform_LibCBench/platform_LibCBench b_malloc_tiny1__0_ 0.000768474333333
90platform_LibCBench/platform_LibCBench b_malloc_tiny2__0_ 0.000581407333333
91platform_LibCBench/platform_LibCBench b_pthread_create_serial1__0_ 0.0291785246667
92platform_LibCBench/platform_LibCBench b_pthread_createjoin_serial1__0_ 0.031907936
93platform_LibCBench/platform_LibCBench b_pthread_createjoin_serial2__0_ 0.043485347
94platform_LibCBench/platform_LibCBench b_pthread_uselesslock__0_ 0.0294113346667
95platform_LibCBench/platform_LibCBench b_regex_compile____a_b_c__d_b__ 0.00529833933333
96platform_LibCBench/platform_LibCBench b_regex_search____a_b_c__d_b__ 0.00165455066667
97platform_LibCBench/platform_LibCBench b_regex_search___a_25_b__ 0.0496191923333
98platform_LibCBench/platform_LibCBench b_stdio_putcgetc__0_ 0.100005711667
99platform_LibCBench/platform_LibCBench b_stdio_putcgetc_unlocked__0_ 0.0371443833333
100platform_LibCBench/platform_LibCBench b_string_memset__0_ 0.00275405066667
101platform_LibCBench/platform_LibCBench b_string_strchr__0_ 0.00456903
102platform_LibCBench/platform_LibCBench b_string_strlen__0_ 0.044893587
103platform_LibCBench/platform_LibCBench b_string_strstr___aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaac__ 0.118360778
104platform_LibCBench/platform_LibCBench b_string_strstr___aaaaaaaaaaaaaaaaaaaaaaaaac__ 0.068957325
105platform_LibCBench/platform_LibCBench b_string_strstr___aaaaaaaaaaaaaacccccccccccc__ 0.0135694476667
106platform_LibCBench/platform_LibCBench b_string_strstr___abcdefghijklmnopqrstuvwxyz__ 0.0134553343333
107platform_LibCBench/platform_LibCBench b_string_strstr___azbycxdwevfugthsirjqkplomn__ 0.0133123556667
108platform_LibCBench/platform_LibCBench b_utf8_bigbuf__0_ 0.0473772253333
109platform_LibCBench/platform_LibCBench b_utf8_onebyone__0_ 0.130938538333
110-------------------------------------------------------------------
111Total PASS: 2/2 (100%)
112
113INFO : Elapsed time: 0m16s
114"""
115
116error = """
cmticed96e4572015-05-19 16:19:25 -0700117ERROR: Identity added: /tmp/test_that.Z4Ld/autotest_key (/tmp/test_that.Z4Ld/autotest_key)
Yunlian Jiang4578ec12013-05-22 14:54:39 -0700118INFO : Using emerged autotests already installed at /build/lumpy/usr/local/autotest.
119INFO : Running the following control files 1 times:
120INFO : * 'client/site_tests/platform_LibCBench/control'
121INFO : Running client test client/site_tests/platform_LibCBench/control
122INFO : Test results:
123INFO : Elapsed time: 0m18s
124"""
125
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800126keyvals = {'': 'PASS',
127 'b_stdio_putcgetc__0_': '0.100005711667',
128 'b_string_strstr___azbycxdwevfugthsirjqkplomn__': '0.0133123556667',
129 'b_malloc_thread_local__0_': '0.01138439',
130 'b_string_strlen__0_': '0.044893587',
131 'b_malloc_sparse__0_': '0.015053784',
132 'b_string_memset__0_': '0.00275405066667',
133 'platform_LibCBench': 'PASS',
134 'b_pthread_uselesslock__0_': '0.0294113346667',
135 'b_string_strchr__0_': '0.00456903',
136 'b_pthread_create_serial1__0_': '0.0291785246667',
137 'b_string_strstr___aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaac__':
138 '0.118360778',
139 'b_string_strstr___aaaaaaaaaaaaaacccccccccccc__': '0.0135694476667',
140 'b_pthread_createjoin_serial1__0_': '0.031907936',
141 'b_malloc_thread_stress__0_': '0.0367894733333',
142 'b_regex_search____a_b_c__d_b__': '0.00165455066667',
143 'b_malloc_bubble__0_': '0.015066374',
144 'b_malloc_big2__0_': '0.002951359',
145 'b_stdio_putcgetc_unlocked__0_': '0.0371443833333',
146 'b_pthread_createjoin_serial2__0_': '0.043485347',
147 'b_regex_search___a_25_b__': '0.0496191923333',
148 'b_utf8_bigbuf__0_': '0.0473772253333',
149 'b_malloc_big1__0_': '0.00375231466667',
150 'b_regex_compile____a_b_c__d_b__': '0.00529833933333',
151 'b_string_strstr___aaaaaaaaaaaaaaaaaaaaaaaaac__': '0.068957325',
152 'b_malloc_tiny2__0_': '0.000581407333333',
153 'b_utf8_onebyone__0_': '0.130938538333',
154 'b_malloc_tiny1__0_': '0.000768474333333',
155 'b_string_strstr___abcdefghijklmnopqrstuvwxyz__': '0.0134553343333'}
cmticec490e072014-06-13 15:38:45 -0700156
157TMP_DIR1 = '/tmp/tmpAbcXyz'
158
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800159
Yunlian Jiang4578ec12013-05-22 14:54:39 -0700160class MockResult(Result):
Yunlian Jiangaefb76c2015-12-29 08:59:52 -0800161 """Mock result class."""
Yunlian Jiangfbda0492015-12-21 15:32:44 -0800162 def __init__(self, mylogger, label, logging_level, machine):
163 super(MockResult, self).__init__(mylogger, label, logging_level, machine)
Yunlian Jiang4578ec12013-05-22 14:54:39 -0700164
Yunlian Jiang655dfa32015-12-30 09:12:09 -0800165 def FindFilesInResultsDir(self, find_args):
cmticec490e072014-06-13 15:38:45 -0700166 return ''
Yunlian Jiang4578ec12013-05-22 14:54:39 -0700167
Caroline Ticeeddb0632016-04-14 09:19:02 -0700168 # pylint: disable=arguments-differ
169 def GetKeyvals(self, temp=False):
170 if temp:
Yunlian Jiangaefb76c2015-12-29 08:59:52 -0800171 pass
Yunlian Jiang4578ec12013-05-22 14:54:39 -0700172 return keyvals
173
174
175class ResultTest(unittest.TestCase):
Yunlian Jiangaefb76c2015-12-29 08:59:52 -0800176 """Result test class."""
Yunlian Jiang4ef13522015-12-29 13:20:45 -0800177
178 def __init__(self, *args, **kwargs):
179 super(ResultTest, self).__init__(*args, **kwargs)
180 self.callFakeProcessResults = False
181 self.fakeCacheReturnResult = None
182 self.callGetResultsDir = False
183 self.callProcessResults = False
184 self.callGetPerfReportFiles = False
185 self.kv_dict = None
186 self.tmpdir = ''
Yunlian Jiang655dfa32015-12-30 09:12:09 -0800187 self.callGetNewKeyvals = False
Rahul Chaudhryb45d1fa2016-07-29 14:24:07 -0700188 self.callGetResultsFile = False
Yunlian Jiang4ef13522015-12-29 13:20:45 -0800189 self.callGetPerfDataFiles = False
190 self.args = None
191 self.callGatherPerfResults = False
192 self.mock_logger = mock.Mock(spec=logger.Logger)
193 self.mock_cmd_exec = mock.Mock(spec=command_executer.CommandExecuter)
194 self.mock_label = MockLabel('mock_label', 'chromeos_image', '/tmp', 'lumpy',
195 'remote', 'image_args', 'cache_dir', 'average',
196 'gcc', None)
cmticec490e072014-06-13 15:38:45 -0700197
Yunlian Jiang4578ec12013-05-22 14:54:39 -0700198 def testCreateFromRun(self):
cmticec490e072014-06-13 15:38:45 -0700199 result = MockResult.CreateFromRun(logger.GetLogger(), 'average',
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800200 self.mock_label, 'remote1', OUTPUT, error,
201 0, True, 0)
Yunlian Jiang4578ec12013-05-22 14:54:39 -0700202 self.assertEqual(result.keyvals, keyvals)
Yunlian Jiang9e533332013-09-30 15:45:46 -0700203 self.assertEqual(result.chroot_results_dir,
cmticed96e4572015-05-19 16:19:25 -0700204 '/tmp/test_that.PO1234567/platform_LibCBench')
Yunlian Jiang9e533332013-09-30 15:45:46 -0700205 self.assertEqual(result.results_dir,
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800206 '/tmp/chroot/tmp/test_that.PO1234567/platform_LibCBench')
Yunlian Jiang4578ec12013-05-22 14:54:39 -0700207 self.assertEqual(result.retval, 0)
208
cmticec490e072014-06-13 15:38:45 -0700209 def setUp(self):
210 self.result = Result(self.mock_logger, self.mock_label, 'average',
211 self.mock_cmd_exec)
212
213 @mock.patch.object(os.path, 'isdir')
214 @mock.patch.object(command_executer.CommandExecuter, 'RunCommand')
215 @mock.patch.object(command_executer.CommandExecuter, 'CopyFiles')
216 def test_copy_files_to(self, mock_copyfiles, mock_runcmd, mock_isdir):
217
218 files = ['src_file_1', 'src_file_2', 'src_file_3']
219 dest_dir = '/tmp/test'
220 self.mock_cmd_exec.RunCommand = mock_runcmd
221 self.mock_cmd_exec.CopyFiles = mock_copyfiles
222
223 mock_copyfiles.return_value = 0
224
225 #test 1. dest_dir exists; CopyFiles returns 0.
226 mock_isdir.return_value = True
Yunlian Jiang655dfa32015-12-30 09:12:09 -0800227 self.result.CopyFilesTo(dest_dir, files)
cmticec490e072014-06-13 15:38:45 -0700228 self.assertEqual(mock_runcmd.call_count, 0)
229 self.assertEqual(mock_copyfiles.call_count, 3)
230 first_args = mock_copyfiles.call_args_list[0][0]
231 second_args = mock_copyfiles.call_args_list[1][0]
232 third_args = mock_copyfiles.call_args_list[2][0]
233 self.assertEqual(first_args, ('src_file_1', '/tmp/test/src_file_1.0'))
234 self.assertEqual(second_args, ('src_file_2', '/tmp/test/src_file_2.0'))
235 self.assertEqual(third_args, ('src_file_3', '/tmp/test/src_file_3.0'))
236
237 mock_runcmd.reset_mock()
238 mock_copyfiles.reset_mock()
239 #test 2. dest_dir does not exist; CopyFiles returns 0.
240 mock_isdir.return_value = False
Yunlian Jiang655dfa32015-12-30 09:12:09 -0800241 self.result.CopyFilesTo(dest_dir, files)
cmticec490e072014-06-13 15:38:45 -0700242 self.assertEqual(mock_runcmd.call_count, 3)
243 self.assertEqual(mock_copyfiles.call_count, 3)
244 self.assertEqual(mock_runcmd.call_args_list[0],
245 mock_runcmd.call_args_list[1])
246 self.assertEqual(mock_runcmd.call_args_list[0],
247 mock_runcmd.call_args_list[2])
248 self.assertEqual(mock_runcmd.call_args_list[0][0], ('mkdir -p /tmp/test',))
249
250 #test 3. CopyFiles returns 1 (fails).
251 mock_copyfiles.return_value = 1
Yunlian Jiang655dfa32015-12-30 09:12:09 -0800252 self.assertRaises(Exception, self.result.CopyFilesTo, dest_dir, files)
cmticec490e072014-06-13 15:38:45 -0700253
Yunlian Jiang655dfa32015-12-30 09:12:09 -0800254 @mock.patch.object(Result, 'CopyFilesTo')
255 def test_copy_results_to(self, mockCopyFilesTo):
cmticec490e072014-06-13 15:38:45 -0700256 perf_data_files = ['/tmp/perf.data.0', '/tmp/perf.data.1',
257 '/tmp/perf.data.2']
258 perf_report_files = ['/tmp/perf.report.0', '/tmp/perf.report.1',
259 '/tmp/perf.report.2']
260
261 self.result.perf_data_files = perf_data_files
262 self.result.perf_report_files = perf_report_files
263
Yunlian Jiang655dfa32015-12-30 09:12:09 -0800264 self.result.CopyFilesTo = mockCopyFilesTo
cmticec490e072014-06-13 15:38:45 -0700265 self.result.CopyResultsTo('/tmp/results/')
Yunlian Jiang655dfa32015-12-30 09:12:09 -0800266 self.assertEqual(mockCopyFilesTo.call_count, 2)
267 self.assertEqual(len(mockCopyFilesTo.call_args_list), 2)
268 self.assertEqual(mockCopyFilesTo.call_args_list[0][0],
cmticec490e072014-06-13 15:38:45 -0700269 ('/tmp/results/', perf_data_files))
Yunlian Jiang655dfa32015-12-30 09:12:09 -0800270 self.assertEqual(mockCopyFilesTo.call_args_list[1][0],
cmticec490e072014-06-13 15:38:45 -0700271 ('/tmp/results/', perf_report_files))
272
cmticec490e072014-06-13 15:38:45 -0700273 def test_get_new_keyvals(self):
274 kv_dict = {}
275
276 def FakeGetDataMeasurementsFiles():
277 filename = os.path.join(os.getcwd(), 'unittest_keyval_file.txt')
278 return [filename]
279
Yunlian Jiang655dfa32015-12-30 09:12:09 -0800280 self.result.GetDataMeasurementsFiles = FakeGetDataMeasurementsFiles
281 kv_dict2, udict = self.result.GetNewKeyvals(kv_dict)
cmticec490e072014-06-13 15:38:45 -0700282 self.assertEqual(kv_dict2,
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800283 {u'Box2D__Box2D': 4775,
284 u'Mandreel__Mandreel': 6620,
285 u'Gameboy__Gameboy': 9901,
286 u'Crypto__Crypto': 8737,
287 u'telemetry_page_measurement_results__num_errored': 0,
288 u'telemetry_page_measurement_results__num_failed': 0,
289 u'PdfJS__PdfJS': 6455,
290 u'Total__Score': 7918,
291 u'EarleyBoyer__EarleyBoyer': 14340,
292 u'MandreelLatency__MandreelLatency': 5188,
293 u'CodeLoad__CodeLoad': 6271,
294 u'DeltaBlue__DeltaBlue': 14401,
295 u'Typescript__Typescript': 9815,
296 u'SplayLatency__SplayLatency': 7653,
297 u'zlib__zlib': 16094,
298 u'Richards__Richards': 10358,
299 u'RegExp__RegExp': 1765,
300 u'NavierStokes__NavierStokes': 9815,
301 u'Splay__Splay': 4425,
302 u'RayTrace__RayTrace': 16600})
303 self.assertEqual(
304 udict, {u'Box2D__Box2D': u'score',
305 u'Mandreel__Mandreel': u'score',
306 u'Gameboy__Gameboy': u'score',
307 u'Crypto__Crypto': u'score',
308 u'telemetry_page_measurement_results__num_errored': u'count',
309 u'telemetry_page_measurement_results__num_failed': u'count',
310 u'PdfJS__PdfJS': u'score',
311 u'Total__Score': u'score',
312 u'EarleyBoyer__EarleyBoyer': u'score',
313 u'MandreelLatency__MandreelLatency': u'score',
314 u'CodeLoad__CodeLoad': u'score',
315 u'DeltaBlue__DeltaBlue': u'score',
316 u'Typescript__Typescript': u'score',
317 u'SplayLatency__SplayLatency': u'score',
318 u'zlib__zlib': u'score',
319 u'Richards__Richards': u'score',
320 u'RegExp__RegExp': u'score',
321 u'NavierStokes__NavierStokes': u'score',
322 u'Splay__Splay': u'score',
323 u'RayTrace__RayTrace': u'score'})
cmticec490e072014-06-13 15:38:45 -0700324
325 def test_append_telemetry_units(self):
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800326 kv_dict = {u'Box2D__Box2D': 4775,
327 u'Mandreel__Mandreel': 6620,
328 u'Gameboy__Gameboy': 9901,
329 u'Crypto__Crypto': 8737,
330 u'PdfJS__PdfJS': 6455,
331 u'Total__Score': 7918,
cmticec490e072014-06-13 15:38:45 -0700332 u'EarleyBoyer__EarleyBoyer': 14340,
333 u'MandreelLatency__MandreelLatency': 5188,
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800334 u'CodeLoad__CodeLoad': 6271,
335 u'DeltaBlue__DeltaBlue': 14401,
cmticec490e072014-06-13 15:38:45 -0700336 u'Typescript__Typescript': 9815,
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800337 u'SplayLatency__SplayLatency': 7653,
338 u'zlib__zlib': 16094,
339 u'Richards__Richards': 10358,
340 u'RegExp__RegExp': 1765,
341 u'NavierStokes__NavierStokes': 9815,
342 u'Splay__Splay': 4425,
cmticec490e072014-06-13 15:38:45 -0700343 u'RayTrace__RayTrace': 16600}
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800344 units_dict = {u'Box2D__Box2D': u'score',
345 u'Mandreel__Mandreel': u'score',
346 u'Gameboy__Gameboy': u'score',
347 u'Crypto__Crypto': u'score',
348 u'PdfJS__PdfJS': u'score',
349 u'Total__Score': u'score',
cmticec490e072014-06-13 15:38:45 -0700350 u'EarleyBoyer__EarleyBoyer': u'score',
351 u'MandreelLatency__MandreelLatency': u'score',
352 u'CodeLoad__CodeLoad': u'score',
353 u'DeltaBlue__DeltaBlue': u'score',
354 u'Typescript__Typescript': u'score',
355 u'SplayLatency__SplayLatency': u'score',
356 u'zlib__zlib': u'score',
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800357 u'Richards__Richards': u'score',
358 u'RegExp__RegExp': u'score',
cmticec490e072014-06-13 15:38:45 -0700359 u'NavierStokes__NavierStokes': u'score',
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800360 u'Splay__Splay': u'score',
361 u'RayTrace__RayTrace': u'score'}
cmticec490e072014-06-13 15:38:45 -0700362
Yunlian Jiang655dfa32015-12-30 09:12:09 -0800363 results_dict = self.result.AppendTelemetryUnits(kv_dict, units_dict)
cmticec490e072014-06-13 15:38:45 -0700364 self.assertEqual(results_dict,
365 {u'Box2D__Box2D': [4775, u'score'],
366 u'Splay__Splay': [4425, u'score'],
367 u'Gameboy__Gameboy': [9901, u'score'],
368 u'Crypto__Crypto': [8737, u'score'],
369 u'PdfJS__PdfJS': [6455, u'score'],
370 u'Total__Score': [7918, u'score'],
371 u'EarleyBoyer__EarleyBoyer': [14340, u'score'],
372 u'MandreelLatency__MandreelLatency': [5188, u'score'],
373 u'DeltaBlue__DeltaBlue': [14401, u'score'],
374 u'SplayLatency__SplayLatency': [7653, u'score'],
375 u'Mandreel__Mandreel': [6620, u'score'],
376 u'Richards__Richards': [10358, u'score'],
377 u'zlib__zlib': [16094, u'score'],
378 u'CodeLoad__CodeLoad': [6271, u'score'],
379 u'Typescript__Typescript': [9815, u'score'],
380 u'RegExp__RegExp': [1765, u'score'],
381 u'RayTrace__RayTrace': [16600, u'score'],
382 u'NavierStokes__NavierStokes': [9815, u'score']})
383
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800384 @mock.patch.object(misc, 'GetInsideChrootPath')
385 @mock.patch.object(tempfile, 'mkdtemp')
386 @mock.patch.object(command_executer.CommandExecuter, 'RunCommand')
Yunlian Jiangfbda0492015-12-21 15:32:44 -0800387 @mock.patch.object(command_executer.CommandExecuter,
388 'ChrootRunCommandWOutput')
cmticec490e072014-06-13 15:38:45 -0700389 def test_get_keyvals(self, mock_chrootruncmd, mock_runcmd, mock_mkdtemp,
390 mock_getpath):
391
392 self.kv_dict = {}
Yunlian Jiang655dfa32015-12-30 09:12:09 -0800393 self.callGetNewKeyvals = False
cmticec490e072014-06-13 15:38:45 -0700394
395 def reset():
396 self.kv_dict = {}
Yunlian Jiang655dfa32015-12-30 09:12:09 -0800397 self.callGetNewKeyvals = False
cmticec490e072014-06-13 15:38:45 -0700398 mock_chrootruncmd.reset_mock()
399 mock_runcmd.reset_mock()
400 mock_mkdtemp.reset_mock()
401 mock_getpath.reset_mock()
402
403 def FakeGetNewKeyvals(kv_dict):
404 self.kv_dict = kv_dict
Yunlian Jiang655dfa32015-12-30 09:12:09 -0800405 self.callGetNewKeyvals = True
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800406 return_kvdict = {'first_time': 680, 'Total': 10}
407 return_udict = {'first_time': 'ms', 'Total': 'score'}
cmticec490e072014-06-13 15:38:45 -0700408 return return_kvdict, return_udict
409
cmticec490e072014-06-13 15:38:45 -0700410 mock_mkdtemp.return_value = TMP_DIR1
411 mock_chrootruncmd.return_value = ['',
412 ('%s,PASS\n%s/telemetry_Crosperf,PASS\n')
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800413 % (TMP_DIR1, TMP_DIR1), '']
cmticec490e072014-06-13 15:38:45 -0700414 mock_getpath.return_value = TMP_DIR1
Yunlian Jiang655dfa32015-12-30 09:12:09 -0800415 self.result.ce.ChrootRunCommandWOutput = mock_chrootruncmd
416 self.result.ce.RunCommand = mock_runcmd
417 self.result.GetNewKeyvals = FakeGetNewKeyvals
cmticec490e072014-06-13 15:38:45 -0700418 self.result.suite = 'telemetry_Crosperf'
419 self.result.results_dir = '/tmp/test_that_resultsNmq'
420
Yunlian Jiang655dfa32015-12-30 09:12:09 -0800421 # Test 1. no self.temp_dir.
Caroline Ticeeddb0632016-04-14 09:19:02 -0700422 res = self.result.GetKeyvals()
Yunlian Jiang655dfa32015-12-30 09:12:09 -0800423 self.assertTrue(self.callGetNewKeyvals)
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800424 self.assertEqual(self.kv_dict, {'': 'PASS', 'telemetry_Crosperf': 'PASS'})
cmticec490e072014-06-13 15:38:45 -0700425 self.assertEqual(mock_runcmd.call_count, 1)
426 self.assertEqual(mock_runcmd.call_args_list[0][0],
427 ('cp -r /tmp/test_that_resultsNmq/* %s' % TMP_DIR1,))
428 self.assertEqual(mock_chrootruncmd.call_count, 1)
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800429 self.assertEqual(mock_chrootruncmd.call_args_list[0][0], (
430 '/tmp', ('python generate_test_report --no-color --csv %s') % TMP_DIR1))
cmticec490e072014-06-13 15:38:45 -0700431 self.assertEqual(mock_getpath.call_count, 1)
432 self.assertEqual(mock_mkdtemp.call_count, 1)
433 self.assertEqual(res, {'Total': [10, 'score'], 'first_time': [680, 'ms']})
434
Yunlian Jiang655dfa32015-12-30 09:12:09 -0800435 # Test 2. self.temp_dir
cmticec490e072014-06-13 15:38:45 -0700436 reset()
437 mock_chrootruncmd.return_value = ['',
438 ('/tmp/tmpJCajRG,PASS\n/tmp/tmpJCajRG/'
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800439 'telemetry_Crosperf,PASS\n'), '']
cmticec490e072014-06-13 15:38:45 -0700440 mock_getpath.return_value = '/tmp/tmpJCajRG'
Yunlian Jiang655dfa32015-12-30 09:12:09 -0800441 self.result.temp_dir = '/tmp/tmpJCajRG'
Caroline Ticeeddb0632016-04-14 09:19:02 -0700442 res = self.result.GetKeyvals()
cmticec490e072014-06-13 15:38:45 -0700443 self.assertEqual(mock_runcmd.call_count, 0)
444 self.assertEqual(mock_mkdtemp.call_count, 0)
445 self.assertEqual(mock_chrootruncmd.call_count, 1)
Yunlian Jiang655dfa32015-12-30 09:12:09 -0800446 self.assertTrue(self.callGetNewKeyvals)
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800447 self.assertEqual(self.kv_dict, {'': 'PASS', 'telemetry_Crosperf': 'PASS'})
cmticec490e072014-06-13 15:38:45 -0700448 self.assertEqual(res, {'Total': [10, 'score'], 'first_time': [680, 'ms']})
449
450 # Test 3. suite != telemetry_Crosperf. Normally this would be for
451 # running non-Telemetry autotests, such as BootPerfServer. In this test
452 # case, the keyvals we have set up were returned from a Telemetry test run;
453 # so this pass is basically testing that we don't append the units to the
454 # test results (which we do for Telemetry autotest runs).
455 reset()
456 self.result.suite = ''
Caroline Ticeeddb0632016-04-14 09:19:02 -0700457 res = self.result.GetKeyvals()
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800458 self.assertEqual(res, {'Total': 10, 'first_time': 680})
cmticec490e072014-06-13 15:38:45 -0700459
460 def test_get_results_dir(self):
461
462 self.result.out = ''
Yunlian Jiang655dfa32015-12-30 09:12:09 -0800463 self.assertRaises(Exception, self.result.GetResultsDir)
cmticec490e072014-06-13 15:38:45 -0700464
465 self.result.out = OUTPUT
Yunlian Jiang655dfa32015-12-30 09:12:09 -0800466 resdir = self.result.GetResultsDir()
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800467 self.assertEqual(resdir, '/tmp/test_that.PO1234567/platform_LibCBench')
cmticec490e072014-06-13 15:38:45 -0700468
Caroline Tice8d818fb2015-12-16 16:42:54 -0800469 @mock.patch.object(command_executer.CommandExecuter, 'RunCommandGeneric')
cmticec490e072014-06-13 15:38:45 -0700470 def test_find_files_in_results_dir(self, mock_runcmd):
471
472 self.result.results_dir = None
Yunlian Jiang655dfa32015-12-30 09:12:09 -0800473 res = self.result.FindFilesInResultsDir('-name perf.data')
cmticec490e072014-06-13 15:38:45 -0700474 self.assertIsNone(res)
475
Yunlian Jiang655dfa32015-12-30 09:12:09 -0800476 self.result.ce.RunCommand = mock_runcmd
cmticec490e072014-06-13 15:38:45 -0700477 self.result.results_dir = '/tmp/test_results'
478 mock_runcmd.return_value = [0, '/tmp/test_results/perf.data', '']
Yunlian Jiang655dfa32015-12-30 09:12:09 -0800479 res = self.result.FindFilesInResultsDir('-name perf.data')
cmticec490e072014-06-13 15:38:45 -0700480 self.assertEqual(mock_runcmd.call_count, 1)
481 self.assertEqual(mock_runcmd.call_args_list[0][0],
482 ('find /tmp/test_results -name perf.data',))
483 self.assertEqual(res, '/tmp/test_results/perf.data')
484
485 mock_runcmd.reset_mock()
486 mock_runcmd.return_value = [1, '', '']
Yunlian Jiang655dfa32015-12-30 09:12:09 -0800487 self.assertRaises(Exception, self.result.FindFilesInResultsDir,
cmticec490e072014-06-13 15:38:45 -0700488 '-name perf.data')
489
Yunlian Jiang655dfa32015-12-30 09:12:09 -0800490 @mock.patch.object(Result, 'FindFilesInResultsDir')
cmticec490e072014-06-13 15:38:45 -0700491 def test_get_perf_data_files(self, mock_findfiles):
492 self.args = None
493
494 mock_findfiles.return_value = 'line1\nline1\n'
Yunlian Jiang655dfa32015-12-30 09:12:09 -0800495 self.result.FindFilesInResultsDir = mock_findfiles
496 res = self.result.GetPerfDataFiles()
cmticec490e072014-06-13 15:38:45 -0700497 self.assertEqual(res, ['line1', 'line1'])
498 self.assertEqual(mock_findfiles.call_args_list[0][0], ('-name perf.data',))
499
cmticec490e072014-06-13 15:38:45 -0700500 def test_get_perf_report_files(self):
501 self.args = None
502
503 def FakeFindFiles(find_args):
504 self.args = find_args
505 return 'line1\nline1\n'
506
Yunlian Jiang655dfa32015-12-30 09:12:09 -0800507 self.result.FindFilesInResultsDir = FakeFindFiles
508 res = self.result.GetPerfReportFiles()
cmticec490e072014-06-13 15:38:45 -0700509 self.assertEqual(res, ['line1', 'line1'])
510 self.assertEqual(self.args, '-name perf.data.report')
511
cmticec490e072014-06-13 15:38:45 -0700512 def test_get_data_measurement_files(self):
513 self.args = None
514
515 def FakeFindFiles(find_args):
516 self.args = find_args
517 return 'line1\nline1\n'
518
Yunlian Jiang655dfa32015-12-30 09:12:09 -0800519 self.result.FindFilesInResultsDir = FakeFindFiles
520 res = self.result.GetDataMeasurementsFiles()
cmticec490e072014-06-13 15:38:45 -0700521 self.assertEqual(res, ['line1', 'line1'])
522 self.assertEqual(self.args, '-name perf_measurements')
523
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800524 @mock.patch.object(misc, 'GetInsideChrootPath')
525 @mock.patch.object(command_executer.CommandExecuter, 'ChrootRunCommand')
cmticec490e072014-06-13 15:38:45 -0700526 def test_generate_perf_report_files(self, mock_chrootruncmd, mock_getpath):
527 fake_file = '/usr/chromeos/chroot/tmp/results/fake_file'
528 self.result.perf_data_files = ['/tmp/results/perf.data']
Yunlian Jiang655dfa32015-12-30 09:12:09 -0800529 self.result.board = 'lumpy'
cmticec490e072014-06-13 15:38:45 -0700530 mock_getpath.return_value = fake_file
Yunlian Jiang655dfa32015-12-30 09:12:09 -0800531 self.result.ce.ChrootRunCommand = mock_chrootruncmd
532 tmp = self.result.GeneratePerfReportFiles()
cmticec490e072014-06-13 15:38:45 -0700533 self.assertEqual(tmp, ['/tmp/chroot%s' % fake_file])
534 self.assertEqual(mock_chrootruncmd.call_args_list[0][0],
535 ('/tmp',
536 ('/tmp/perf.static report -n --symfs /build/lumpy '
537 '--vmlinux /build/lumpy/usr/lib/debug/boot/vmlinux '
538 '--kallsyms /build/lumpy/boot/System.map-* -i '
539 '%s --stdio > %s') % (fake_file, fake_file)))
540
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800541 @mock.patch.object(misc, 'GetOutsideChrootPath')
cmticec490e072014-06-13 15:38:45 -0700542 def test_populate_from_run(self, mock_getpath):
543
544 def FakeGetResultsDir():
545 self.callGetResultsDir = True
546 return '/tmp/results_dir'
547
Rahul Chaudhryb45d1fa2016-07-29 14:24:07 -0700548 def FakeGetResultsFile():
549 self.callGetResultsFile = True
550 return []
551
cmticec490e072014-06-13 15:38:45 -0700552 def FakeGetPerfDataFiles():
553 self.callGetPerfDataFiles = True
554 return []
555
556 def FakeGetPerfReportFiles():
557 self.callGetPerfReportFiles = True
558 return []
559
Caroline Ticeeddb0632016-04-14 09:19:02 -0700560 def FakeProcessResults(show_results=False):
Yunlian Jiangaefb76c2015-12-29 08:59:52 -0800561 if show_results:
562 pass
cmticec490e072014-06-13 15:38:45 -0700563 self.callProcessResults = True
564
Yunlian Jiangaefb76c2015-12-29 08:59:52 -0800565 if mock_getpath:
566 pass
cmticec490e072014-06-13 15:38:45 -0700567 mock.get_path = '/tmp/chromeos/tmp/results_dir'
Yunlian Jiang655dfa32015-12-30 09:12:09 -0800568 self.result.chromeos_root = '/tmp/chromeos'
cmticec490e072014-06-13 15:38:45 -0700569
570 self.callGetResultsDir = False
Rahul Chaudhryb45d1fa2016-07-29 14:24:07 -0700571 self.callGetResultsFile = False
cmticec490e072014-06-13 15:38:45 -0700572 self.callGetPerfDataFiles = False
573 self.callGetPerfReportFiles = False
574 self.callProcessResults = False
575
Yunlian Jiang655dfa32015-12-30 09:12:09 -0800576 self.result.GetResultsDir = FakeGetResultsDir
Rahul Chaudhryb45d1fa2016-07-29 14:24:07 -0700577 self.result.GetResultsFile = FakeGetResultsFile
Yunlian Jiang655dfa32015-12-30 09:12:09 -0800578 self.result.GetPerfDataFiles = FakeGetPerfDataFiles
579 self.result.GeneratePerfReportFiles = FakeGetPerfReportFiles
580 self.result.ProcessResults = FakeProcessResults
cmticec490e072014-06-13 15:38:45 -0700581
Caroline Ticeeddb0632016-04-14 09:19:02 -0700582 self.result.PopulateFromRun(OUTPUT, '', 0, 'test',
Yunlian Jiang655dfa32015-12-30 09:12:09 -0800583 'telemetry_Crosperf')
cmticec490e072014-06-13 15:38:45 -0700584 self.assertTrue(self.callGetResultsDir)
Rahul Chaudhryb45d1fa2016-07-29 14:24:07 -0700585 self.assertTrue(self.callGetResultsFile)
cmticec490e072014-06-13 15:38:45 -0700586 self.assertTrue(self.callGetPerfDataFiles)
587 self.assertTrue(self.callGetPerfReportFiles)
588 self.assertTrue(self.callProcessResults)
589
590 def test_process_results(self):
591
Caroline Ticeeddb0632016-04-14 09:19:02 -0700592 def FakeGetKeyvals(show_all=False):
cmticec490e072014-06-13 15:38:45 -0700593 if show_all:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800594 return {'first_time': 680, 'Total': 10}
cmticec490e072014-06-13 15:38:45 -0700595 else:
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800596 return {'Total': 10}
cmticec490e072014-06-13 15:38:45 -0700597
598 def FakeGatherPerfResults():
599 self.callGatherPerfResults = True
600
601 self.callGatherPerfResults = False
602
Yunlian Jiang655dfa32015-12-30 09:12:09 -0800603 self.result.GetKeyvals = FakeGetKeyvals
604 self.result.GatherPerfResults = FakeGatherPerfResults
cmticec490e072014-06-13 15:38:45 -0700605
606 self.result.retval = 0
Caroline Ticeeddb0632016-04-14 09:19:02 -0700607 self.result.ProcessResults()
cmticec490e072014-06-13 15:38:45 -0700608 self.assertTrue(self.callGatherPerfResults)
Caroline Ticeeddb0632016-04-14 09:19:02 -0700609 self.assertEqual(len(self.result.keyvals), 2)
610 self.assertEqual(self.result.keyvals, {'Total': 10,
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800611 'retval': 0})
cmticec490e072014-06-13 15:38:45 -0700612
613 self.result.retval = 1
Caroline Ticeeddb0632016-04-14 09:19:02 -0700614 self.result.ProcessResults()
cmticec490e072014-06-13 15:38:45 -0700615 self.assertEqual(len(self.result.keyvals), 2)
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800616 self.assertEqual(self.result.keyvals, {'Total': 10, 'retval': 1})
cmticec490e072014-06-13 15:38:45 -0700617
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800618 @mock.patch.object(misc, 'GetInsideChrootPath')
Yunlian Jiangfbda0492015-12-21 15:32:44 -0800619 @mock.patch.object(command_executer.CommandExecuter,
620 'ChrootRunCommandWOutput')
cmticec490e072014-06-13 15:38:45 -0700621 def test_populate_from_cache_dir(self, mock_runchrootcmd, mock_getpath):
622
Caroline Ticeeddb0632016-04-14 09:19:02 -0700623 # pylint: disable=redefined-builtin
624 def FakeMkdtemp(dir=None):
Yunlian Jiangaefb76c2015-12-29 08:59:52 -0800625 if dir:
626 pass
cmticec490e072014-06-13 15:38:45 -0700627 return self.tmpdir
628
629 current_path = os.getcwd()
630 cache_dir = os.path.join(current_path, 'test_cache/test_input')
Yunlian Jiang655dfa32015-12-30 09:12:09 -0800631 self.result.ce = command_executer.GetCommandExecuter(log_level='average')
632 self.result.ce.ChrootRunCommandWOutput = mock_runchrootcmd
cmticec490e072014-06-13 15:38:45 -0700633 mock_runchrootcmd.return_value = ['',
634 ('%s,PASS\n%s/\telemetry_Crosperf,PASS\n')
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800635 % (TMP_DIR1, TMP_DIR1), '']
cmticec490e072014-06-13 15:38:45 -0700636 mock_getpath.return_value = TMP_DIR1
637 self.tmpdir = tempfile.mkdtemp()
638 save_real_mkdtemp = tempfile.mkdtemp
639 tempfile.mkdtemp = FakeMkdtemp
640
Caroline Ticeeddb0632016-04-14 09:19:02 -0700641 self.result.PopulateFromCacheDir(cache_dir, 'sunspider',
Yunlian Jiang655dfa32015-12-30 09:12:09 -0800642 'telemetry_Crosperf')
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800643 self.assertEqual(
644 self.result.keyvals,
645 {u'Total__Total': [444.0, u'ms'],
646 u'regexp-dna__regexp-dna': [16.2, u'ms'],
647 u'telemetry_page_measurement_results__num_failed': [0, u'count'],
648 u'telemetry_page_measurement_results__num_errored': [0, u'count'],
649 u'string-fasta__string-fasta': [23.2, u'ms'],
650 u'crypto-sha1__crypto-sha1': [11.6, u'ms'],
651 u'bitops-3bit-bits-in-byte__bitops-3bit-bits-in-byte': [3.2, u'ms'],
652 u'access-nsieve__access-nsieve': [7.9, u'ms'],
653 u'bitops-nsieve-bits__bitops-nsieve-bits': [9.4, u'ms'],
654 u'string-validate-input__string-validate-input': [19.3, u'ms'],
655 u'3d-raytrace__3d-raytrace': [24.7, u'ms'],
656 u'3d-cube__3d-cube': [28.0, u'ms'],
657 u'string-unpack-code__string-unpack-code': [46.7, u'ms'],
658 u'date-format-tofte__date-format-tofte': [26.3, u'ms'],
659 u'math-partial-sums__math-partial-sums': [22.0, u'ms'],
660 '\telemetry_Crosperf': ['PASS', ''],
661 u'crypto-aes__crypto-aes': [15.2, u'ms'],
662 u'bitops-bitwise-and__bitops-bitwise-and': [8.4, u'ms'],
663 u'crypto-md5__crypto-md5': [10.5, u'ms'],
664 u'string-tagcloud__string-tagcloud': [52.8, u'ms'],
665 u'access-nbody__access-nbody': [8.5, u'ms'],
666 'retval': 0,
667 u'math-spectral-norm__math-spectral-norm': [6.6, u'ms'],
668 u'math-cordic__math-cordic': [8.7, u'ms'],
669 u'access-binary-trees__access-binary-trees': [4.5, u'ms'],
670 u'controlflow-recursive__controlflow-recursive': [4.4, u'ms'],
671 u'access-fannkuch__access-fannkuch': [17.8, u'ms'],
672 u'string-base64__string-base64': [16.0, u'ms'],
673 u'date-format-xparb__date-format-xparb': [20.9, u'ms'],
674 u'3d-morph__3d-morph': [22.1, u'ms'],
675 u'bitops-bits-in-byte__bitops-bits-in-byte': [9.1, u'ms']})
cmticec490e072014-06-13 15:38:45 -0700676
677 # Clean up after test.
678 tempfile.mkdtemp = save_real_mkdtemp
679 command = 'rm -Rf %s' % self.tmpdir
Yunlian Jiang655dfa32015-12-30 09:12:09 -0800680 self.result.ce.RunCommand(command)
cmticec490e072014-06-13 15:38:45 -0700681
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800682 @mock.patch.object(misc, 'GetRoot')
683 @mock.patch.object(command_executer.CommandExecuter, 'RunCommand')
cmticec490e072014-06-13 15:38:45 -0700684 def test_cleanup(self, mock_runcmd, mock_getroot):
685
686 # Test 1. 'rm_chroot_tmp' is True; self.results_dir exists;
Yunlian Jiang655dfa32015-12-30 09:12:09 -0800687 # self.temp_dir exists; results_dir name contains 'test_that_results_'.
cmticec490e072014-06-13 15:38:45 -0700688 mock_getroot.return_value = ['/tmp/tmp_AbcXyz', 'test_that_results_fake']
Yunlian Jiang655dfa32015-12-30 09:12:09 -0800689 self.result.ce.RunCommand = mock_runcmd
cmticec490e072014-06-13 15:38:45 -0700690 self.result.results_dir = 'test_results_dir'
Yunlian Jiang655dfa32015-12-30 09:12:09 -0800691 self.result.temp_dir = 'testtemp_dir'
cmticec490e072014-06-13 15:38:45 -0700692 self.result.CleanUp(True)
693 self.assertEqual(mock_getroot.call_count, 1)
694 self.assertEqual(mock_runcmd.call_count, 2)
695 self.assertEqual(mock_runcmd.call_args_list[0][0],
696 ('rm -rf test_results_dir',))
697 self.assertEqual(mock_runcmd.call_args_list[1][0],
Yunlian Jiang655dfa32015-12-30 09:12:09 -0800698 ('rm -rf testtemp_dir',))
cmticec490e072014-06-13 15:38:45 -0700699
700 # Test 2. Same, except ath results_dir name does not contain
701 # 'test_that_results_'
702 mock_getroot.reset_mock()
703 mock_runcmd.reset_mock()
704 mock_getroot.return_value = ['/tmp/tmp_AbcXyz', 'other_results_fake']
Yunlian Jiang655dfa32015-12-30 09:12:09 -0800705 self.result.ce.RunCommand = mock_runcmd
cmticec490e072014-06-13 15:38:45 -0700706 self.result.results_dir = 'test_results_dir'
Yunlian Jiang655dfa32015-12-30 09:12:09 -0800707 self.result.temp_dir = 'testtemp_dir'
cmticec490e072014-06-13 15:38:45 -0700708 self.result.CleanUp(True)
709 self.assertEqual(mock_getroot.call_count, 1)
710 self.assertEqual(mock_runcmd.call_count, 2)
711 self.assertEqual(mock_runcmd.call_args_list[0][0],
712 ('rm -rf /tmp/tmp_AbcXyz',))
713 self.assertEqual(mock_runcmd.call_args_list[1][0],
Yunlian Jiang655dfa32015-12-30 09:12:09 -0800714 ('rm -rf testtemp_dir',))
cmticec490e072014-06-13 15:38:45 -0700715
716 # Test 3. mock_getroot returns nothing; 'rm_chroot_tmp' is False.
717 mock_getroot.reset_mock()
718 mock_runcmd.reset_mock()
719 self.result.CleanUp(False)
720 self.assertEqual(mock_getroot.call_count, 0)
721 self.assertEqual(mock_runcmd.call_count, 1)
722 self.assertEqual(mock_runcmd.call_args_list[0][0],
Yunlian Jiang655dfa32015-12-30 09:12:09 -0800723 ('rm -rf testtemp_dir',))
cmticec490e072014-06-13 15:38:45 -0700724
Yunlian Jiang655dfa32015-12-30 09:12:09 -0800725 # Test 4. 'rm_chroot_tmp' is True, but result_dir & temp_dir are None.
cmticec490e072014-06-13 15:38:45 -0700726 mock_getroot.reset_mock()
727 mock_runcmd.reset_mock()
728 self.result.results_dir = None
Yunlian Jiang655dfa32015-12-30 09:12:09 -0800729 self.result.temp_dir = None
cmticec490e072014-06-13 15:38:45 -0700730 self.result.CleanUp(True)
731 self.assertEqual(mock_getroot.call_count, 0)
732 self.assertEqual(mock_runcmd.call_count, 0)
733
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800734 @mock.patch.object(misc, 'GetInsideChrootPath')
735 @mock.patch.object(command_executer.CommandExecuter, 'ChrootRunCommand')
cmticec490e072014-06-13 15:38:45 -0700736 def test_store_to_cache_dir(self, mock_chrootruncmd, mock_getpath):
737
Yunlian Jiangaefb76c2015-12-29 08:59:52 -0800738 def FakeMkdtemp(directory=''):
739 if directory:
740 pass
cmticec490e072014-06-13 15:38:45 -0700741 return self.tmpdir
742
Yunlian Jiangaefb76c2015-12-29 08:59:52 -0800743 if mock_chrootruncmd or mock_getpath:
744 pass
cmticec490e072014-06-13 15:38:45 -0700745 current_path = os.getcwd()
746 cache_dir = os.path.join(current_path, 'test_cache/test_output')
747
Yunlian Jiang655dfa32015-12-30 09:12:09 -0800748 self.result.ce = command_executer.GetCommandExecuter(log_level='average')
cmticec490e072014-06-13 15:38:45 -0700749 self.result.out = OUTPUT
750 self.result.err = error
751 self.result.retval = 0
752 self.tmpdir = tempfile.mkdtemp()
753 if not os.path.exists(self.tmpdir):
754 os.makedirs(self.tmpdir)
755 self.result.results_dir = os.path.join(os.getcwd(), 'test_cache')
756 save_real_mkdtemp = tempfile.mkdtemp
757 tempfile.mkdtemp = FakeMkdtemp
758
759 mock_mm = machine_manager.MockMachineManager('/tmp/chromeos_root', 0,
David Sharpa9368342016-01-22 17:52:01 -0800760 'average', '')
cmticec490e072014-06-13 15:38:45 -0700761 mock_mm.machine_checksum_string['mock_label'] = 'fake_machine_checksum123'
762
Caroline Ticee627fd62015-12-11 12:07:59 -0800763 mock_keylist = ['key1', 'key2', 'key3']
764 test_flag.SetTestMode(True)
765 self.result.StoreToCacheDir(cache_dir, mock_mm, mock_keylist)
cmticec490e072014-06-13 15:38:45 -0700766
767 # Check that the correct things were written to the 'cache'.
768 test_dir = os.path.join(os.getcwd(), 'test_cache/test_output')
769 base_dir = os.path.join(os.getcwd(), 'test_cache/compare_output')
770 self.assertTrue(os.path.exists(os.path.join(test_dir, 'autotest.tbz2')))
771 self.assertTrue(os.path.exists(os.path.join(test_dir, 'machine.txt')))
772 self.assertTrue(os.path.exists(os.path.join(test_dir, 'results.txt')))
773
774 f1 = os.path.join(test_dir, 'machine.txt')
775 f2 = os.path.join(base_dir, 'machine.txt')
776 cmd = 'diff %s %s' % (f1, f2)
Yunlian Jiang655dfa32015-12-30 09:12:09 -0800777 [_, out, _] = self.result.ce.RunCommandWOutput(cmd)
cmticec490e072014-06-13 15:38:45 -0700778 self.assertEqual(len(out), 0)
779
780 f1 = os.path.join(test_dir, 'results.txt')
781 f2 = os.path.join(base_dir, 'results.txt')
782 cmd = 'diff %s %s' % (f1, f2)
Yunlian Jiang655dfa32015-12-30 09:12:09 -0800783 [_, out, _] = self.result.ce.RunCommandWOutput(cmd)
cmticec490e072014-06-13 15:38:45 -0700784 self.assertEqual(len(out), 0)
785
786 # Clean up after test.
787 tempfile.mkdtemp = save_real_mkdtemp
788 command = 'rm %s/*' % test_dir
Yunlian Jiang655dfa32015-12-30 09:12:09 -0800789 self.result.ce.RunCommand(command)
cmticec490e072014-06-13 15:38:45 -0700790
791
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800792TELEMETRY_RESULT_KEYVALS = {
793 'http://www.webkit.org/perf/sunspider-1.0.2/sunspider-1.0.2/driver.html '
794 'math-cordic (ms)':
795 '11.4',
796 'http://www.webkit.org/perf/sunspider-1.0.2/sunspider-1.0.2/driver.html '
797 'access-nbody (ms)':
798 '6.9',
799 'http://www.webkit.org/perf/sunspider-1.0.2/sunspider-1.0.2/driver.html '
800 'access-fannkuch (ms)':
801 '26.3',
802 'http://www.webkit.org/perf/sunspider-1.0.2/sunspider-1.0.2/driver.html '
803 'math-spectral-norm (ms)':
804 '6.3',
805 'http://www.webkit.org/perf/sunspider-1.0.2/sunspider-1.0.2/driver.html '
806 'bitops-nsieve-bits (ms)':
807 '9.3',
808 'http://www.webkit.org/perf/sunspider-1.0.2/sunspider-1.0.2/driver.html '
809 'math-partial-sums (ms)':
810 '32.8',
811 'http://www.webkit.org/perf/sunspider-1.0.2/sunspider-1.0.2/driver.html '
812 'regexp-dna (ms)':
813 '16.1',
814 'http://www.webkit.org/perf/sunspider-1.0.2/sunspider-1.0.2/driver.html '
815 '3d-cube (ms)':
816 '42.7',
817 'http://www.webkit.org/perf/sunspider-1.0.2/sunspider-1.0.2/driver.html '
818 'crypto-md5 (ms)':
819 '10.8',
820 'http://www.webkit.org/perf/sunspider-1.0.2/sunspider-1.0.2/driver.html '
821 'crypto-sha1 (ms)':
822 '12.4',
823 'http://www.webkit.org/perf/sunspider-1.0.2/sunspider-1.0.2/driver.html '
824 'string-tagcloud (ms)':
825 '47.2',
826 'http://www.webkit.org/perf/sunspider-1.0.2/sunspider-1.0.2/driver.html '
827 'string-fasta (ms)':
828 '36.3',
829 'http://www.webkit.org/perf/sunspider-1.0.2/sunspider-1.0.2/driver.html '
830 'access-binary-trees (ms)':
831 '7.3',
832 'http://www.webkit.org/perf/sunspider-1.0.2/sunspider-1.0.2/driver.html '
833 'date-format-xparb (ms)':
834 '138.1',
835 'http://www.webkit.org/perf/sunspider-1.0.2/sunspider-1.0.2/driver.html '
836 'crypto-aes (ms)':
837 '19.2',
838 'http://www.webkit.org/perf/sunspider-1.0.2/sunspider-1.0.2/driver.html '
839 'Total (ms)':
840 '656.5',
841 'http://www.webkit.org/perf/sunspider-1.0.2/sunspider-1.0.2/driver.html '
842 'string-base64 (ms)':
843 '17.5',
844 'http://www.webkit.org/perf/sunspider-1.0.2/sunspider-1.0.2/driver.html '
845 'string-validate-input (ms)':
846 '24.8',
847 'http://www.webkit.org/perf/sunspider-1.0.2/sunspider-1.0.2/driver.html '
848 '3d-raytrace (ms)':
849 '28.7',
850 'http://www.webkit.org/perf/sunspider-1.0.2/sunspider-1.0.2/driver.html '
851 'controlflow-recursive (ms)':
852 '5.3',
853 'http://www.webkit.org/perf/sunspider-1.0.2/sunspider-1.0.2/driver.html '
854 'bitops-bits-in-byte (ms)':
855 '9.8',
856 'http://www.webkit.org/perf/sunspider-1.0.2/sunspider-1.0.2/driver.html '
857 '3d-morph (ms)':
858 '50.2',
859 'http://www.webkit.org/perf/sunspider-1.0.2/sunspider-1.0.2/driver.html '
860 'bitops-bitwise-and (ms)':
861 '8.8',
862 'http://www.webkit.org/perf/sunspider-1.0.2/sunspider-1.0.2/driver.html '
863 'access-nsieve (ms)':
864 '8.6',
865 'http://www.webkit.org/perf/sunspider-1.0.2/sunspider-1.0.2/driver.html '
866 'date-format-tofte (ms)':
867 '31.2',
868 'http://www.webkit.org/perf/sunspider-1.0.2/sunspider-1.0.2/driver.html '
869 'bitops-3bit-bits-in-byte (ms)':
870 '3.5',
871 'retval': 0,
872 'http://www.webkit.org/perf/sunspider-1.0.2/sunspider-1.0.2/driver.html '
873 'string-unpack-code (ms)':
874 '45.0'
875}
cmticec490e072014-06-13 15:38:45 -0700876
Yunlian Jiangfbda0492015-12-21 15:32:44 -0800877PURE_TELEMETRY_OUTPUT = """
878page_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
879"""
cmticec490e072014-06-13 15:38:45 -0700880
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800881
cmticec490e072014-06-13 15:38:45 -0700882class TelemetryResultTest(unittest.TestCase):
Yunlian Jiangaefb76c2015-12-29 08:59:52 -0800883 """Telemetry result test."""
Yunlian Jiang4ef13522015-12-29 13:20:45 -0800884
885 def __init__(self, *args, **kwargs):
886 super(TelemetryResultTest, self).__init__(*args, **kwargs)
887 self.callFakeProcessResults = False
888 self.result = None
889 self.mock_logger = mock.Mock(spec=logger.Logger)
890 self.mock_cmd_exec = mock.Mock(spec=command_executer.CommandExecuter)
891 self.mock_label = MockLabel('mock_label', 'chromeos_image', '/tmp',
892 'lumpy', 'remote', 'image_args', 'cache_dir',
893 'average', 'gcc', None)
894 self.mock_machine = machine_manager.MockCrosMachine('falco.cros',
895 '/tmp/chromeos',
896 'average')
cmticec490e072014-06-13 15:38:45 -0700897
898 def test_populate_from_run(self):
899
900 def FakeProcessResults():
901 self.callFakeProcessResults = True
902
903 self.callFakeProcessResults = False
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800904 self.result = TelemetryResult(self.mock_logger, self.mock_label, 'average',
905 self.mock_cmd_exec)
Yunlian Jiang655dfa32015-12-30 09:12:09 -0800906 self.result.ProcessResults = FakeProcessResults
Caroline Ticeeddb0632016-04-14 09:19:02 -0700907 self.result.PopulateFromRun(OUTPUT, error, 3, 'fake_test',
Yunlian Jiang655dfa32015-12-30 09:12:09 -0800908 'telemetry_Crosperf')
cmticec490e072014-06-13 15:38:45 -0700909 self.assertTrue(self.callFakeProcessResults)
910 self.assertEqual(self.result.out, OUTPUT)
911 self.assertEqual(self.result.err, error)
912 self.assertEqual(self.result.retval, 3)
913
cmticec490e072014-06-13 15:38:45 -0700914 def test_populate_from_cache_dir_and_process_results(self):
915
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800916 self.result = TelemetryResult(self.mock_logger, self.mock_label, 'average',
917 self.mock_machine)
cmticec490e072014-06-13 15:38:45 -0700918 current_path = os.getcwd()
919 cache_dir = os.path.join(current_path,
920 'test_cache/test_puretelemetry_input')
Caroline Ticeeddb0632016-04-14 09:19:02 -0700921 self.result.PopulateFromCacheDir(cache_dir, '', '')
Yunlian Jiangfbda0492015-12-21 15:32:44 -0800922 self.assertEqual(self.result.out.strip(), PURE_TELEMETRY_OUTPUT.strip())
cmticec490e072014-06-13 15:38:45 -0700923 self.assertEqual(self.result.err, '')
924 self.assertEqual(self.result.retval, 0)
925 self.assertEqual(self.result.keyvals, TELEMETRY_RESULT_KEYVALS)
926
927
928class ResultsCacheTest(unittest.TestCase):
Yunlian Jiangaefb76c2015-12-29 08:59:52 -0800929 """Resultcache test class."""
Yunlian Jiang4ef13522015-12-29 13:20:45 -0800930
931 def __init__(self, *args, **kwargs):
932 super(ResultsCacheTest, self).__init__(*args, **kwargs)
933 self.fakeCacheReturnResult = None
934 self.mock_logger = mock.Mock(spec=logger.Logger)
935 self.mock_label = MockLabel('mock_label', 'chromeos_image', '/tmp', 'lumpy',
936 'remote', 'image_args', 'cache_dir', 'average',
937 'gcc', None)
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800938
cmticec490e072014-06-13 15:38:45 -0700939 def setUp(self):
940 self.results_cache = ResultsCache()
941
Caroline Ticee627fd62015-12-11 12:07:59 -0800942 mock_machine = machine_manager.MockCrosMachine('falco.cros',
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800943 '/tmp/chromeos', 'average')
cmticec490e072014-06-13 15:38:45 -0700944
945 mock_mm = machine_manager.MockMachineManager('/tmp/chromeos_root', 0,
David Sharpa9368342016-01-22 17:52:01 -0800946 'average', '')
cmticec490e072014-06-13 15:38:45 -0700947 mock_mm.machine_checksum_string['mock_label'] = 'fake_machine_checksum123'
948
949 self.results_cache.Init(self.mock_label.chromeos_image,
950 self.mock_label.chromeos_root,
951 'sunspider',
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800952 1, # benchmark_run.iteration,
953 '', # benchmark_run.test_args,
954 '', # benchmark_run.profiler_args,
cmticec490e072014-06-13 15:38:45 -0700955 mock_mm,
Caroline Ticee627fd62015-12-11 12:07:59 -0800956 mock_machine,
cmticec490e072014-06-13 15:38:45 -0700957 self.mock_label.board,
958 [CacheConditions.CACHE_FILE_EXISTS,
959 CacheConditions.CHECKSUMS_MATCH],
960 self.mock_logger,
961 'average',
962 self.mock_label,
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800963 '', # benchmark_run.share_cache
cmticec490e072014-06-13 15:38:45 -0700964 'telemetry_Crosperf',
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800965 True, # benchmark_run.show_all_results
966 False) # benchmark_run.run_local
cmticec490e072014-06-13 15:38:45 -0700967
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800968 @mock.patch.object(image_checksummer.ImageChecksummer, 'Checksum')
cmticec490e072014-06-13 15:38:45 -0700969 def test_get_cache_dir_for_write(self, mock_checksum):
970
971 def FakeGetMachines(label):
Yunlian Jiangaefb76c2015-12-29 08:59:52 -0800972 if label:
973 pass
Luis Lozanof2a3ef42015-12-15 13:49:30 -0800974 m1 = machine_manager.MockCrosMachine(
975 'lumpy1.cros', self.results_cache.chromeos_root, 'average')
976 m2 = machine_manager.MockCrosMachine(
977 'lumpy2.cros', self.results_cache.chromeos_root, 'average')
978 return [m1, m2]
cmticec490e072014-06-13 15:38:45 -0700979
980 mock_checksum.return_value = 'FakeImageChecksumabc123'
981 self.results_cache.machine_manager.GetMachines = FakeGetMachines
982 self.results_cache.machine_manager.machine_checksum['mock_label'] = \
983 'FakeMachineChecksumabc987'
984 # Based on the label, benchmark and machines, get the directory in which
985 # to store the cache information for this test run.
Yunlian Jiang655dfa32015-12-30 09:12:09 -0800986 result_path = self.results_cache.GetCacheDirForWrite()
cmticec490e072014-06-13 15:38:45 -0700987 # Verify that the returned directory is correct (since the label
988 # contained a cache_dir, named 'cache_dir', that's what is expected in
989 # the result, rather than '~/cros_scratch').
990 comp_path = os.path.join(os.getcwd(),
991 'cache_dir/54524606abaae4fdf7b02f49f7ae7127_'
Caroline Ticee627fd62015-12-11 12:07:59 -0800992 'sunspider_1_fda29412ceccb72977516c4785d08e2c_'
cmticec490e072014-06-13 15:38:45 -0700993 'FakeImageChecksumabc123_FakeMachineChecksum'
994 'abc987__6')
995 self.assertEqual(result_path, comp_path)
996
cmticec490e072014-06-13 15:38:45 -0700997 def test_form_cache_dir(self):
Yunlian Jiang655dfa32015-12-30 09:12:09 -0800998 # This is very similar to the previous test (FormCacheDir is called
999 # from GetCacheDirForWrite).
cmticec490e072014-06-13 15:38:45 -07001000 cache_key_list = ('54524606abaae4fdf7b02f49f7ae7127', 'sunspider', '1',
1001 '7215ee9c7d9dc229d2921a40e899ec5f',
1002 'FakeImageChecksumabc123', '*', '*', '6')
Yunlian Jiang655dfa32015-12-30 09:12:09 -08001003 path = self.results_cache.FormCacheDir(cache_key_list)
cmticec490e072014-06-13 15:38:45 -07001004 self.assertEqual(len(path), 1)
1005 path1 = path[0]
1006 test_dirname = ('54524606abaae4fdf7b02f49f7ae7127_sunspider_1_7215ee9'
1007 'c7d9dc229d2921a40e899ec5f_FakeImageChecksumabc123_*_*_6')
1008 comp_path = os.path.join(os.getcwd(), 'cache_dir', test_dirname)
1009 self.assertEqual(path1, comp_path)
1010
Luis Lozanof2a3ef42015-12-15 13:49:30 -08001011 @mock.patch.object(image_checksummer.ImageChecksummer, 'Checksum')
cmticec490e072014-06-13 15:38:45 -07001012 def test_get_cache_key_list(self, mock_checksum):
1013 # This tests the mechanism that generates the various pieces of the
1014 # cache directory name, based on various conditions.
1015
1016 def FakeGetMachines(label):
Yunlian Jiangaefb76c2015-12-29 08:59:52 -08001017 if label:
1018 pass
Luis Lozanof2a3ef42015-12-15 13:49:30 -08001019 m1 = machine_manager.MockCrosMachine(
1020 'lumpy1.cros', self.results_cache.chromeos_root, 'average')
1021 m2 = machine_manager.MockCrosMachine(
1022 'lumpy2.cros', self.results_cache.chromeos_root, 'average')
1023 return [m1, m2]
cmticec490e072014-06-13 15:38:45 -07001024
1025 mock_checksum.return_value = 'FakeImageChecksumabc123'
1026 self.results_cache.machine_manager.GetMachines = FakeGetMachines
1027 self.results_cache.machine_manager.machine_checksum['mock_label'] = \
1028 'FakeMachineChecksumabc987'
1029
1030 # Test 1. Generating cache name for reading (not writing).
Yunlian Jiang655dfa32015-12-30 09:12:09 -08001031 key_list = self.results_cache.GetCacheKeyList(True)
Luis Lozanof2a3ef42015-12-15 13:49:30 -08001032 self.assertEqual(key_list[0], '*') # Machine checksum value, for read.
cmticec490e072014-06-13 15:38:45 -07001033 self.assertEqual(key_list[1], 'sunspider')
1034 self.assertEqual(key_list[2], '1')
Caroline Ticee627fd62015-12-11 12:07:59 -08001035 self.assertEqual(key_list[3], 'fda29412ceccb72977516c4785d08e2c')
cmticec490e072014-06-13 15:38:45 -07001036 self.assertEqual(key_list[4], 'FakeImageChecksumabc123')
1037 self.assertEqual(key_list[5], '*')
1038 self.assertEqual(key_list[6], '*')
1039 self.assertEqual(key_list[7], '6')
1040
1041 # Test 2. Generating cache name for writing, with local image type.
Yunlian Jiang655dfa32015-12-30 09:12:09 -08001042 key_list = self.results_cache.GetCacheKeyList(False)
cmticec490e072014-06-13 15:38:45 -07001043 self.assertEqual(key_list[0], '54524606abaae4fdf7b02f49f7ae7127')
1044 self.assertEqual(key_list[1], 'sunspider')
1045 self.assertEqual(key_list[2], '1')
Caroline Ticee627fd62015-12-11 12:07:59 -08001046 self.assertEqual(key_list[3], 'fda29412ceccb72977516c4785d08e2c')
cmticec490e072014-06-13 15:38:45 -07001047 self.assertEqual(key_list[4], 'FakeImageChecksumabc123')
1048 self.assertEqual(key_list[5], 'FakeMachineChecksumabc987')
1049 self.assertEqual(key_list[6], '')
1050 self.assertEqual(key_list[7], '6')
1051
1052 # Test 3. Generating cache name for writing, with trybot image type.
1053 self.results_cache.label.image_type = 'trybot'
Yunlian Jiang655dfa32015-12-30 09:12:09 -08001054 key_list = self.results_cache.GetCacheKeyList(False)
cmticec490e072014-06-13 15:38:45 -07001055 self.assertEqual(key_list[0], '54524606abaae4fdf7b02f49f7ae7127')
Caroline Ticee627fd62015-12-11 12:07:59 -08001056 self.assertEqual(key_list[3], 'fda29412ceccb72977516c4785d08e2c')
cmticec490e072014-06-13 15:38:45 -07001057 self.assertEqual(key_list[4], '54524606abaae4fdf7b02f49f7ae7127')
1058 self.assertEqual(key_list[5], 'FakeMachineChecksumabc987')
1059
1060 # Test 4. Generating cache name for writing, with official image type.
1061 self.results_cache.label.image_type = 'official'
Yunlian Jiang655dfa32015-12-30 09:12:09 -08001062 key_list = self.results_cache.GetCacheKeyList(False)
cmticec490e072014-06-13 15:38:45 -07001063 self.assertEqual(key_list[0], '54524606abaae4fdf7b02f49f7ae7127')
1064 self.assertEqual(key_list[1], 'sunspider')
1065 self.assertEqual(key_list[2], '1')
Caroline Ticee627fd62015-12-11 12:07:59 -08001066 self.assertEqual(key_list[3], 'fda29412ceccb72977516c4785d08e2c')
cmticec490e072014-06-13 15:38:45 -07001067 self.assertEqual(key_list[4], '*')
1068 self.assertEqual(key_list[5], 'FakeMachineChecksumabc987')
1069 self.assertEqual(key_list[6], '')
1070 self.assertEqual(key_list[7], '6')
1071
1072 # Test 5. Generating cache name for writing, with local image type, and
1073 # specifying that the image path must match the cached image path.
1074 self.results_cache.label.image_type = 'local'
1075 self.results_cache.cache_conditions.append(CacheConditions.IMAGE_PATH_MATCH)
Yunlian Jiang655dfa32015-12-30 09:12:09 -08001076 key_list = self.results_cache.GetCacheKeyList(False)
cmticec490e072014-06-13 15:38:45 -07001077 self.assertEqual(key_list[0], '54524606abaae4fdf7b02f49f7ae7127')
Caroline Ticee627fd62015-12-11 12:07:59 -08001078 self.assertEqual(key_list[3], 'fda29412ceccb72977516c4785d08e2c')
cmticec490e072014-06-13 15:38:45 -07001079 self.assertEqual(key_list[4], 'FakeImageChecksumabc123')
1080 self.assertEqual(key_list[5], 'FakeMachineChecksumabc987')
1081
Luis Lozanof2a3ef42015-12-15 13:49:30 -08001082 @mock.patch.object(command_executer.CommandExecuter, 'RunCommand')
1083 @mock.patch.object(os.path, 'isdir')
1084 @mock.patch.object(Result, 'CreateFromCacheHit')
cmticec490e072014-06-13 15:38:45 -07001085 def test_read_result(self, mock_create, mock_isdir, mock_runcmd):
1086
1087 self.fakeCacheReturnResult = None
Luis Lozanof2a3ef42015-12-15 13:49:30 -08001088
cmticec490e072014-06-13 15:38:45 -07001089 def FakeGetCacheDirForRead():
1090 return self.fakeCacheReturnResult
1091
1092 def FakeGetCacheDirForWrite():
1093 return self.fakeCacheReturnResult
1094
1095 mock_cmd_exec = mock.Mock(spec=command_executer.CommandExecuter)
1096 fake_result = Result(self.mock_logger, self.mock_label, 'average',
1097 mock_cmd_exec)
1098 fake_result.retval = 0
1099
1100 # Set up results_cache _GetCacheDirFor{Read,Write} to return
1101 # self.fakeCacheReturnResult, which is initially None (see above).
1102 # So initially, no cache dir is returned.
Yunlian Jiang655dfa32015-12-30 09:12:09 -08001103 self.results_cache.GetCacheDirForRead = FakeGetCacheDirForRead
1104 self.results_cache.GetCacheDirForWrite = FakeGetCacheDirForWrite
cmticec490e072014-06-13 15:38:45 -07001105
1106 mock_isdir.return_value = True
1107 save_cc = [CacheConditions.CACHE_FILE_EXISTS,
1108 CacheConditions.CHECKSUMS_MATCH]
1109 self.results_cache.cache_conditions.append(CacheConditions.FALSE)
1110
1111 # Test 1. CacheCondition.FALSE, which means do not read from the cache.
1112 # (force re-running of test). Result should be None.
1113 res = self.results_cache.ReadResult()
1114 self.assertIsNone(res)
1115 self.assertEqual(mock_runcmd.call_count, 1)
1116
1117 # Test 2. Remove CacheCondition.FALSE. Result should still be None,
Yunlian Jiang655dfa32015-12-30 09:12:09 -08001118 # because GetCacheDirForRead is returning None at the moment.
cmticec490e072014-06-13 15:38:45 -07001119 mock_runcmd.reset_mock()
1120 self.results_cache.cache_conditions = save_cc
1121 res = self.results_cache.ReadResult()
1122 self.assertIsNone(res)
1123 self.assertEqual(mock_runcmd.call_count, 0)
1124
Yunlian Jiang655dfa32015-12-30 09:12:09 -08001125 # Test 3. Now set up cache dir to be returned by GetCacheDirForRead.
cmticec490e072014-06-13 15:38:45 -07001126 # Since cache_dir is found, will call Result.CreateFromCacheHit, which
1127 # which will actually all our mock_create and should return fake_result.
1128 self.fakeCacheReturnResult = 'fake/cache/dir'
1129 mock_create.return_value = fake_result
1130 res = self.results_cache.ReadResult()
1131 self.assertEqual(mock_runcmd.call_count, 0)
1132 self.assertEqual(res, fake_result)
1133
1134 # Test 4. os.path.isdir(cache_dir) will now return false, so result
1135 # should be None again (no cache found).
1136 mock_isdir.return_value = False
1137 res = self.results_cache.ReadResult()
1138 self.assertEqual(mock_runcmd.call_count, 0)
1139 self.assertIsNone(res)
1140
1141 # Test 5. os.path.isdir returns true, but mock_create now returns None
1142 # (the call to CreateFromCacheHit returns None), so overal result is None.
1143 mock_isdir.return_value = True
1144 mock_create.return_value = None
1145 res = self.results_cache.ReadResult()
1146 self.assertEqual(mock_runcmd.call_count, 0)
1147 self.assertIsNone(res)
1148
1149 # Test 6. Everything works 'as expected', result should be fake_result.
1150 mock_create.return_value = fake_result
1151 res = self.results_cache.ReadResult()
1152 self.assertEqual(mock_runcmd.call_count, 0)
1153 self.assertEqual(res, fake_result)
1154
1155 # Test 7. The run failed; result should be None.
1156 mock_create.return_value = fake_result
1157 fake_result.retval = 1
1158 self.results_cache.cache_conditions.append(CacheConditions.RUN_SUCCEEDED)
1159 res = self.results_cache.ReadResult()
1160 self.assertEqual(mock_runcmd.call_count, 0)
1161 self.assertIsNone(res)
1162
1163
1164if __name__ == '__main__':
Yunlian Jiang4578ec12013-05-22 14:54:39 -07001165 unittest.main()