1# Lint as: python3 2# 3# Copyright (C) 2021 The Android Open Source Project 4# 5# Licensed under the Apache License, Version 2.0 (the 'License'); 6# you may not use this file except in compliance with the License. 7# You may obtain a copy of the License at 8# 9# http://www.apache.org/licenses/LICENSE-2.0 10# 11# Unless required by applicable law or agreed to in writing, software 12# distributed under the License is distributed on an 'AS IS' BASIS, 13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14# See the License for the specific language governing permissions and 15# limitations under the License. 16 17"""Tests for incremental_deqp.""" 18 19import incremental_deqp 20import os 21import unittest 22from unittest.mock import MagicMock 23from unittest.mock import patch 24 25@patch('incremental_deqp.AdbHelper', MagicMock()) 26class IncrementalDeqpTest(unittest.TestCase): 27 28 def setUp(self): 29 testfile_dir = os.path.dirname(os.path.abspath(__file__)) 30 self.testdata_dir = testfile_dir +'/testdata' 31 self.dependency_collector = incremental_deqp.DeqpDependencyCollector(None, None, None) 32 self.parser = incremental_deqp._get_parser() 33 34 def test_update_dependency(self): 35 """Test update_dependency gets correct dEQP dependency from dump file.""" 36 dump_file = 'testdata/perf_dump.txt' 37 deps = set() 38 self.dependency_collector.update_dependency(deps, dump_file) 39 self.assertEqual(len(deps),2) 40 self.assertIn('file_2', deps) 41 self.assertIn('file_3', deps) 42 43 def test_check_test_log_all_test_executed(self): 44 """Test check_test_log returns true if all tests are executed.""" 45 test_file = 'testdata/test_list.txt' 46 log_file = 'testdata/log_1.qpa' 47 self.assertTrue(self.dependency_collector.check_test_log(test_file, log_file)) 48 49 def test_check_test_log_test_crashed(self): 50 """Test check_test_log returns false if tests are crashed.""" 51 test_file = 'testdata/test_list.txt' 52 log_file = 'testdata/log_2.qpa' 53 self.assertFalse(self.dependency_collector.check_test_log(test_file, log_file)) 54 55 def test_get_test_binary_name(self): 56 """Test get_test_binary_name gets dEQP binary name based on test name.""" 57 self.assertEqual(self.dependency_collector.get_test_binary_name('vk-32'), 'deqp-binary') 58 self.assertEqual(self.dependency_collector.get_test_binary_name('vk-64'), 'deqp-binary64') 59 with self.assertRaises(incremental_deqp.TestError): 60 self.dependency_collector.get_test_binary_name('test') 61 62 def test_get_test_log_name(self): 63 """Test get_test_log_name gets correct test log name based on test name.""" 64 self.assertEqual(self.dependency_collector.get_test_log_name('test'), 'test.qpa') 65 66 def test_get_perf_name(self): 67 """Test get_perf_name gets correct perf file name based on test name.""" 68 self.assertEqual(self.dependency_collector.get_test_perf_name('test'), 'test.data') 69 70 def test_get_perf_dump_name(self): 71 """Test get_perf_dump_name gets correct perf dump file name based on test name.""" 72 self.assertEqual(self.dependency_collector.get_perf_dump_name('test'), 'test-perf-dump.txt') 73 74 def test_get_test_list_name(self): 75 """Test get_test_list_name gets test list name based on test name.""" 76 self.assertEqual(self.dependency_collector.get_test_list_name('vk-32'), 'vk-master-subset.txt') 77 self.assertEqual(self.dependency_collector.get_test_list_name('gles3-32'), 78 'gles3-master-subset.txt') 79 with self.assertRaises(incremental_deqp.TestError): 80 self.dependency_collector.get_test_list_name('test') 81 82 def test_valid_dependency(self): 83 """Test if dependency is valid.""" 84 self.assertTrue(incremental_deqp._is_deqp_dependency('/file/a.so')) 85 self.assertFalse(incremental_deqp._is_deqp_dependency('/apex/a.so')) 86 87 def test_build_helper_compare_build_with_device_files_true(self): 88 """Test BuildHelper.compare_base_build_with_device_files returns true.""" 89 build_helper = incremental_deqp.BuildHelper() 90 deqp_deps = ['/system/deqp_dependency_file_a.so', '/vendor/deqp_dependency_file_b.so'] 91 base_build_file = './testdata/base_build_target-files.zip' 92 93 def side_effect(command): 94 if 'file_a.so' in command: 95 return b'placeholder\nplaceholder\n' 96 if 'file_b.so' in command: 97 return b'placeholder\nplaceholder\nplaceholder\n\n' 98 99 adb = incremental_deqp.AdbHelper() 100 adb.run_shell_command = MagicMock(side_effect=side_effect) 101 self.assertTrue(build_helper.compare_base_build_with_device_files( 102 deqp_deps, adb, base_build_file)[0]) 103 104 def test_compare_build_with_device_files_false(self): 105 """Test BuildHelper.compare_base_build_with_device_files returns false.""" 106 deqp_deps = ['/system/deqp_dependency_file_a.so', '/vendor/deqp_dependency_file_b.so'] 107 build_helper = incremental_deqp.BuildHelper() 108 base_build_file = './testdata/base_build_target-files.zip' 109 def side_effect(command): 110 if 'file_a.so' in command: 111 return b'different text' 112 if 'file_b.so' in command: 113 return b'placeholder\nplaceholder\nplaceholder\n\n' 114 115 adb = incremental_deqp.AdbHelper() 116 adb.run_shell_command = MagicMock(side_effect=side_effect) 117 self.assertFalse(build_helper.compare_base_build_with_device_files( 118 deqp_deps, adb, base_build_file)[0]) 119 120 def test_build_helper_compare_build_with_current_build_true(self): 121 """Test BuildHelper.compare_base_build_with_current_build returns true.""" 122 build_helper = incremental_deqp.BuildHelper() 123 deqp_deps = ['/system/deqp_dependency_file_a.so', '/vendor/deqp_dependency_file_b.so'] 124 base_build_file = './testdata/base_build_target-files.zip' 125 126 self.assertTrue(build_helper.compare_base_build_with_current_build( 127 deqp_deps, base_build_file, base_build_file)[0]) 128 129 def test_build_helper_compare_build_with_current_build_false(self): 130 """Test BuildHelper.compare_base_build_with_current_build returns false.""" 131 build_helper = incremental_deqp.BuildHelper() 132 deqp_deps = ['/system/deqp_dependency_file_a.so', '/vendor/deqp_dependency_file_b.so'] 133 base_build_file = './testdata/base_build_target-files.zip' 134 current_build_file = './testdata/current_build_target-files.zip' 135 136 self.assertFalse(build_helper.compare_base_build_with_current_build( 137 deqp_deps, current_build_file, base_build_file)[0]) 138 139 def test_build_helper_get_system_fingerprint(self): 140 """Test BuildHelper gets system fingerprint.""" 141 build_helper = incremental_deqp.BuildHelper() 142 build_file = './testdata/base_build_target-files.zip' 143 144 self.assertEqual(('generic/aosp_cf_x86_64_phone/vsoc_x86_64:S/AOSP.MASTER/7363308:' 145 'userdebug/test-keys'), build_helper.get_system_fingerprint(build_file)) 146 147 148 @patch('incremental_deqp.BuildHelper', autospec=True) 149 @patch('incremental_deqp._save_deqp_deps', autospec=True) 150 @patch('incremental_deqp.DeqpDependencyCollector', autospec=True) 151 @patch('incremental_deqp.AdbHelper', autospec=True) 152 def test_local_run_generate_deps_only(self, adb_helper_mock, dependency_collector_mock, 153 save_deps_mock, build_helper_mock): 154 """Test generate_deps_only option in local_run.""" 155 dependency_collector_mock.return_value.get_deqp_dependency.return_value = {'a.so'} 156 args = self.parser.parse_args(['--generate_deps_only']) 157 incremental_deqp._local_run(args, '') 158 save_deps_mock.assert_called_once_with({'a.so'}, 'dEQP-dependency.txt') 159 build_helper_mock.assert_not_called() 160 161 def test_local_run_missing_extra_deps(self): 162 """Test local_run throws exception if extra_deqp_dependency.txt is missing.""" 163 args = self.parser.parse_args(['-t ./testdata']) 164 with self.assertRaises(incremental_deqp.TestResourceError): 165 incremental_deqp._local_run(args, '') 166 167 @patch('incremental_deqp._generate_report', autospec=True) 168 @patch('incremental_deqp.BuildHelper', autospec=True) 169 @patch('incremental_deqp._save_deqp_deps', autospec=True) 170 @patch('incremental_deqp.DeqpDependencyCollector', autospec=True) 171 @patch('incremental_deqp.AdbHelper', autospec=True) 172 def test_local_run_compare_build(self, adb_helper_mock, dependency_collector_mock, 173 save_deps_mock, build_helper_mock, generate_report_mock): 174 """Test local_run could compare build based on dependency.""" 175 dependency_collector_mock.return_value.get_deqp_dependency.return_value = {'a.so'} 176 build_helper_mock.return_value.compare_base_build_with_device_files.return_value = [False, {}] 177 args = self.parser.parse_args(['-b', 'base_build', '-t', self.testdata_dir]) 178 179 incremental_deqp._local_run(args, '') 180 181 save_deps_mock.assert_called_once_with({'a.so', 'extra_a.so'}, 'dEQP-dependency.txt') 182 build_helper_mock.assert_called_once_with(False) 183 184if __name__ == '__main__': 185 unittest.main() 186