1*c2e18aaaSAndroid Build Coastguard Worker#!/usr/bin/env python3 2*c2e18aaaSAndroid Build Coastguard Worker# 3*c2e18aaaSAndroid Build Coastguard Worker# Copyright 2018, The Android Open Source Project 4*c2e18aaaSAndroid Build Coastguard Worker# 5*c2e18aaaSAndroid Build Coastguard Worker# Licensed under the Apache License, Version 2.0 (the "License"); 6*c2e18aaaSAndroid Build Coastguard Worker# you may not use this file except in compliance with the License. 7*c2e18aaaSAndroid Build Coastguard Worker# You may obtain a copy of the License at 8*c2e18aaaSAndroid Build Coastguard Worker# 9*c2e18aaaSAndroid Build Coastguard Worker# http://www.apache.org/licenses/LICENSE-2.0 10*c2e18aaaSAndroid Build Coastguard Worker# 11*c2e18aaaSAndroid Build Coastguard Worker# Unless required by applicable law or agreed to in writing, software 12*c2e18aaaSAndroid Build Coastguard Worker# distributed under the License is distributed on an "AS IS" BASIS, 13*c2e18aaaSAndroid Build Coastguard Worker# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14*c2e18aaaSAndroid Build Coastguard Worker# See the License for the specific language governing permissions and 15*c2e18aaaSAndroid Build Coastguard Worker# limitations under the License. 16*c2e18aaaSAndroid Build Coastguard Worker 17*c2e18aaaSAndroid Build Coastguard Worker"""Unittests for atest_utils.""" 18*c2e18aaaSAndroid Build Coastguard Worker 19*c2e18aaaSAndroid Build Coastguard Worker# pylint: disable=invalid-name 20*c2e18aaaSAndroid Build Coastguard Worker 21*c2e18aaaSAndroid Build Coastguard Workerimport hashlib 22*c2e18aaaSAndroid Build Coastguard Workerfrom io import StringIO 23*c2e18aaaSAndroid Build Coastguard Workerimport os 24*c2e18aaaSAndroid Build Coastguard Workerfrom pathlib import Path 25*c2e18aaaSAndroid Build Coastguard Workerimport subprocess 26*c2e18aaaSAndroid Build Coastguard Workerimport sys 27*c2e18aaaSAndroid Build Coastguard Workerimport tempfile 28*c2e18aaaSAndroid Build Coastguard Workerimport unittest 29*c2e18aaaSAndroid Build Coastguard Workerfrom unittest import mock 30*c2e18aaaSAndroid Build Coastguard Worker 31*c2e18aaaSAndroid Build Coastguard Workerfrom atest import arg_parser 32*c2e18aaaSAndroid Build Coastguard Workerfrom atest import atest_error 33*c2e18aaaSAndroid Build Coastguard Workerfrom atest import atest_utils 34*c2e18aaaSAndroid Build Coastguard Workerfrom atest import constants 35*c2e18aaaSAndroid Build Coastguard Workerfrom atest import unittest_constants 36*c2e18aaaSAndroid Build Coastguard Workerfrom atest import unittest_utils 37*c2e18aaaSAndroid Build Coastguard Workerfrom atest.atest_enum import FilterType 38*c2e18aaaSAndroid Build Coastguard Workerfrom atest.test_finders import test_info 39*c2e18aaaSAndroid Build Coastguard Workerfrom pyfakefs import fake_filesystem_unittest 40*c2e18aaaSAndroid Build Coastguard Worker 41*c2e18aaaSAndroid Build Coastguard WorkerTEST_MODULE_NAME_A = 'ModuleNameA' 42*c2e18aaaSAndroid Build Coastguard WorkerTEST_RUNNER_A = 'FakeTestRunnerA' 43*c2e18aaaSAndroid Build Coastguard WorkerTEST_BUILD_TARGET_A = set(['bt1', 'bt2']) 44*c2e18aaaSAndroid Build Coastguard WorkerTEST_DATA_A = {'test_data_a_1': 'a1', 'test_data_a_2': 'a2'} 45*c2e18aaaSAndroid Build Coastguard WorkerTEST_SUITE_A = 'FakeSuiteA' 46*c2e18aaaSAndroid Build Coastguard WorkerTEST_MODULE_CLASS_A = 'FAKE_MODULE_CLASS_A' 47*c2e18aaaSAndroid Build Coastguard WorkerTEST_INSTALL_LOC_A = set(['host', 'device']) 48*c2e18aaaSAndroid Build Coastguard WorkerTEST_FINDER_A = 'MODULE' 49*c2e18aaaSAndroid Build Coastguard WorkerTEST_INFO_A = test_info.TestInfo( 50*c2e18aaaSAndroid Build Coastguard Worker TEST_MODULE_NAME_A, 51*c2e18aaaSAndroid Build Coastguard Worker TEST_RUNNER_A, 52*c2e18aaaSAndroid Build Coastguard Worker TEST_BUILD_TARGET_A, 53*c2e18aaaSAndroid Build Coastguard Worker TEST_DATA_A, 54*c2e18aaaSAndroid Build Coastguard Worker TEST_SUITE_A, 55*c2e18aaaSAndroid Build Coastguard Worker TEST_MODULE_CLASS_A, 56*c2e18aaaSAndroid Build Coastguard Worker TEST_INSTALL_LOC_A, 57*c2e18aaaSAndroid Build Coastguard Worker) 58*c2e18aaaSAndroid Build Coastguard WorkerTEST_INFO_A.test_finder = TEST_FINDER_A 59*c2e18aaaSAndroid Build Coastguard WorkerTEST_ZIP_DATA_DIR = 'zip_files' 60*c2e18aaaSAndroid Build Coastguard WorkerTEST_SINGLE_ZIP_NAME = 'single_file.zip' 61*c2e18aaaSAndroid Build Coastguard WorkerTEST_MULTI_ZIP_NAME = 'multi_file.zip' 62*c2e18aaaSAndroid Build Coastguard Worker 63*c2e18aaaSAndroid Build Coastguard WorkerREPO_INFO_OUTPUT = """Manifest branch: test_branch 64*c2e18aaaSAndroid Build Coastguard WorkerManifest merge branch: refs/heads/test_branch 65*c2e18aaaSAndroid Build Coastguard WorkerManifest groups: all,-notdefault 66*c2e18aaaSAndroid Build Coastguard Worker---------------------------- 67*c2e18aaaSAndroid Build Coastguard Worker""" 68*c2e18aaaSAndroid Build Coastguard Worker 69*c2e18aaaSAndroid Build Coastguard Worker 70*c2e18aaaSAndroid Build Coastguard Workerclass StreamIoOutputTest(unittest.TestCase): 71*c2e18aaaSAndroid Build Coastguard Worker """Class that tests the _stream_io_output function.""" 72*c2e18aaaSAndroid Build Coastguard Worker 73*c2e18aaaSAndroid Build Coastguard Worker def test_stream_io_output_no_max_lines_no_clear_line_code(self): 74*c2e18aaaSAndroid Build Coastguard Worker """Test when max_lines is None, no clear line code is written to the stream.""" 75*c2e18aaaSAndroid Build Coastguard Worker io_input = StringIO() 76*c2e18aaaSAndroid Build Coastguard Worker io_input.write(f'1\n' * 10) 77*c2e18aaaSAndroid Build Coastguard Worker io_input.seek(0) 78*c2e18aaaSAndroid Build Coastguard Worker io_output = StringIO() 79*c2e18aaaSAndroid Build Coastguard Worker 80*c2e18aaaSAndroid Build Coastguard Worker atest_utils.stream_io_output( 81*c2e18aaaSAndroid Build Coastguard Worker io_input, max_lines=None, io_output=io_output, is_io_output_atty=True 82*c2e18aaaSAndroid Build Coastguard Worker ) 83*c2e18aaaSAndroid Build Coastguard Worker 84*c2e18aaaSAndroid Build Coastguard Worker self.assertNotIn( 85*c2e18aaaSAndroid Build Coastguard Worker atest_utils._BASH_CLEAR_PREVIOUS_LINE_CODE, io_output.getvalue() 86*c2e18aaaSAndroid Build Coastguard Worker ) 87*c2e18aaaSAndroid Build Coastguard Worker 88*c2e18aaaSAndroid Build Coastguard Worker @mock.patch.object(atest_utils, 'get_terminal_size', return_value=(5, -1)) 89*c2e18aaaSAndroid Build Coastguard Worker def test_stream_io_output_wrap_long_lines(self, _): 90*c2e18aaaSAndroid Build Coastguard Worker """Test when max_lines is set, long lines will be wrapped.""" 91*c2e18aaaSAndroid Build Coastguard Worker io_input = StringIO() 92*c2e18aaaSAndroid Build Coastguard Worker io_input.write(f'1' * 10) 93*c2e18aaaSAndroid Build Coastguard Worker io_input.seek(0) 94*c2e18aaaSAndroid Build Coastguard Worker io_output = StringIO() 95*c2e18aaaSAndroid Build Coastguard Worker 96*c2e18aaaSAndroid Build Coastguard Worker atest_utils.stream_io_output( 97*c2e18aaaSAndroid Build Coastguard Worker io_input, max_lines=10, io_output=io_output, is_io_output_atty=True 98*c2e18aaaSAndroid Build Coastguard Worker ) 99*c2e18aaaSAndroid Build Coastguard Worker 100*c2e18aaaSAndroid Build Coastguard Worker self.assertIn('11111\n11111', io_output.getvalue()) 101*c2e18aaaSAndroid Build Coastguard Worker 102*c2e18aaaSAndroid Build Coastguard Worker @mock.patch.object(atest_utils, 'get_terminal_size', return_value=(5, -1)) 103*c2e18aaaSAndroid Build Coastguard Worker def test_stream_io_output_clear_lines_over_max_lines(self, _): 104*c2e18aaaSAndroid Build Coastguard Worker """Test when line exceeds max_lines, the previous lines are cleared.""" 105*c2e18aaaSAndroid Build Coastguard Worker io_input = StringIO() 106*c2e18aaaSAndroid Build Coastguard Worker io_input.write('1\n2\n3\n') 107*c2e18aaaSAndroid Build Coastguard Worker io_input.seek(0) 108*c2e18aaaSAndroid Build Coastguard Worker io_output = StringIO() 109*c2e18aaaSAndroid Build Coastguard Worker 110*c2e18aaaSAndroid Build Coastguard Worker atest_utils.stream_io_output( 111*c2e18aaaSAndroid Build Coastguard Worker io_input, max_lines=2, io_output=io_output, is_io_output_atty=True 112*c2e18aaaSAndroid Build Coastguard Worker ) 113*c2e18aaaSAndroid Build Coastguard Worker 114*c2e18aaaSAndroid Build Coastguard Worker self.assertIn( 115*c2e18aaaSAndroid Build Coastguard Worker '2\n3\n', 116*c2e18aaaSAndroid Build Coastguard Worker io_output.getvalue(), 117*c2e18aaaSAndroid Build Coastguard Worker ) 118*c2e18aaaSAndroid Build Coastguard Worker self.assertNotIn( 119*c2e18aaaSAndroid Build Coastguard Worker '1\n2\n3\n', 120*c2e18aaaSAndroid Build Coastguard Worker io_output.getvalue(), 121*c2e18aaaSAndroid Build Coastguard Worker ) 122*c2e18aaaSAndroid Build Coastguard Worker 123*c2e18aaaSAndroid Build Coastguard Worker @mock.patch.object(atest_utils, 'get_terminal_size', return_value=(5, -1)) 124*c2e18aaaSAndroid Build Coastguard Worker def test_stream_io_output_no_clear_lines_under_max_lines(self, _): 125*c2e18aaaSAndroid Build Coastguard Worker """Test when line is under max_lines, the previous lines are not cleared.""" 126*c2e18aaaSAndroid Build Coastguard Worker io_input = StringIO() 127*c2e18aaaSAndroid Build Coastguard Worker io_input.write('1\n2\n3\n') 128*c2e18aaaSAndroid Build Coastguard Worker io_input.seek(0) 129*c2e18aaaSAndroid Build Coastguard Worker io_output = StringIO() 130*c2e18aaaSAndroid Build Coastguard Worker 131*c2e18aaaSAndroid Build Coastguard Worker atest_utils.stream_io_output( 132*c2e18aaaSAndroid Build Coastguard Worker io_input, max_lines=4, io_output=io_output, is_io_output_atty=True 133*c2e18aaaSAndroid Build Coastguard Worker ) 134*c2e18aaaSAndroid Build Coastguard Worker 135*c2e18aaaSAndroid Build Coastguard Worker self.assertIn( 136*c2e18aaaSAndroid Build Coastguard Worker '1\n2\n3\n', 137*c2e18aaaSAndroid Build Coastguard Worker io_output.getvalue(), 138*c2e18aaaSAndroid Build Coastguard Worker ) 139*c2e18aaaSAndroid Build Coastguard Worker 140*c2e18aaaSAndroid Build Coastguard Worker @mock.patch.object(atest_utils, 'get_terminal_size', return_value=(5, -1)) 141*c2e18aaaSAndroid Build Coastguard Worker def test_stream_io_output_no_lines_written_no_lines_cleared(self, _): 142*c2e18aaaSAndroid Build Coastguard Worker """Test when nothing is written, no lines are cleared.""" 143*c2e18aaaSAndroid Build Coastguard Worker io_input = StringIO() 144*c2e18aaaSAndroid Build Coastguard Worker io_output = StringIO() 145*c2e18aaaSAndroid Build Coastguard Worker 146*c2e18aaaSAndroid Build Coastguard Worker atest_utils.stream_io_output( 147*c2e18aaaSAndroid Build Coastguard Worker io_input, max_lines=2, io_output=io_output, is_io_output_atty=True 148*c2e18aaaSAndroid Build Coastguard Worker ) 149*c2e18aaaSAndroid Build Coastguard Worker 150*c2e18aaaSAndroid Build Coastguard Worker self.assertNotIn( 151*c2e18aaaSAndroid Build Coastguard Worker atest_utils._BASH_CLEAR_PREVIOUS_LINE_CODE, 152*c2e18aaaSAndroid Build Coastguard Worker io_output.getvalue(), 153*c2e18aaaSAndroid Build Coastguard Worker ) 154*c2e18aaaSAndroid Build Coastguard Worker 155*c2e18aaaSAndroid Build Coastguard Worker @mock.patch.object(atest_utils, 'get_terminal_size', return_value=(5, -1)) 156*c2e18aaaSAndroid Build Coastguard Worker def test_stream_io_output_replace_tab_with_spaces(self, _): 157*c2e18aaaSAndroid Build Coastguard Worker """Test when line exceeds max_lines, the previous lines are cleared.""" 158*c2e18aaaSAndroid Build Coastguard Worker io_input = StringIO() 159*c2e18aaaSAndroid Build Coastguard Worker io_input.write('1\t2') 160*c2e18aaaSAndroid Build Coastguard Worker io_input.seek(0) 161*c2e18aaaSAndroid Build Coastguard Worker io_output = StringIO() 162*c2e18aaaSAndroid Build Coastguard Worker 163*c2e18aaaSAndroid Build Coastguard Worker atest_utils.stream_io_output( 164*c2e18aaaSAndroid Build Coastguard Worker io_input, max_lines=2, io_output=io_output, is_io_output_atty=True 165*c2e18aaaSAndroid Build Coastguard Worker ) 166*c2e18aaaSAndroid Build Coastguard Worker 167*c2e18aaaSAndroid Build Coastguard Worker self.assertNotIn( 168*c2e18aaaSAndroid Build Coastguard Worker '\t', 169*c2e18aaaSAndroid Build Coastguard Worker io_output.getvalue(), 170*c2e18aaaSAndroid Build Coastguard Worker ) 171*c2e18aaaSAndroid Build Coastguard Worker 172*c2e18aaaSAndroid Build Coastguard Worker 173*c2e18aaaSAndroid Build Coastguard Workerclass ConcatenatePathTest(unittest.TestCase): 174*c2e18aaaSAndroid Build Coastguard Worker """Class that tests path concatenation.""" 175*c2e18aaaSAndroid Build Coastguard Worker 176*c2e18aaaSAndroid Build Coastguard Worker @classmethod 177*c2e18aaaSAndroid Build Coastguard Worker def setUpClass(cls): 178*c2e18aaaSAndroid Build Coastguard Worker """Mock the environment variables for the entire test class""" 179*c2e18aaaSAndroid Build Coastguard Worker cls.build_top = '/src/build_top' 180*c2e18aaaSAndroid Build Coastguard Worker cls.prod_out = '/src/build_top/product_out' 181*c2e18aaaSAndroid Build Coastguard Worker cls.host_out = '/src/build_top/host_out' 182*c2e18aaaSAndroid Build Coastguard Worker cls.target_out_cases = '/src/build_top/product_out/testcases' 183*c2e18aaaSAndroid Build Coastguard Worker cls.host_out_cases = '/src/build_top/host_out/testcases' 184*c2e18aaaSAndroid Build Coastguard Worker cls.target_product = 'test_target_product' 185*c2e18aaaSAndroid Build Coastguard Worker cls.build_variant = 'test_build_variant' 186*c2e18aaaSAndroid Build Coastguard Worker cls.mock_getenv = mock.patch.dict( 187*c2e18aaaSAndroid Build Coastguard Worker os.environ, 188*c2e18aaaSAndroid Build Coastguard Worker { 189*c2e18aaaSAndroid Build Coastguard Worker 'ANDROID_BUILD_TOP': cls.build_top, 190*c2e18aaaSAndroid Build Coastguard Worker 'ANDROID_PRODUCT_OUT': cls.prod_out, 191*c2e18aaaSAndroid Build Coastguard Worker 'ANDROID_TARGET_OUT_TESTCASES': cls.target_out_cases, 192*c2e18aaaSAndroid Build Coastguard Worker 'ANDROID_HOST_OUT': cls.host_out, 193*c2e18aaaSAndroid Build Coastguard Worker 'ANDROID_HOST_OUT_TESTCASES': cls.host_out_cases, 194*c2e18aaaSAndroid Build Coastguard Worker 'TARGET_PRODUCT': cls.target_product, 195*c2e18aaaSAndroid Build Coastguard Worker 'TARGET_BUILD_VARIANT': cls.build_variant, 196*c2e18aaaSAndroid Build Coastguard Worker }, 197*c2e18aaaSAndroid Build Coastguard Worker ) 198*c2e18aaaSAndroid Build Coastguard Worker cls.mock_getenv.start() 199*c2e18aaaSAndroid Build Coastguard Worker 200*c2e18aaaSAndroid Build Coastguard Worker @classmethod 201*c2e18aaaSAndroid Build Coastguard Worker def tearDownClass(cls): 202*c2e18aaaSAndroid Build Coastguard Worker """Clean up the mocks after the test class finishes""" 203*c2e18aaaSAndroid Build Coastguard Worker cls.mock_getenv.stop() 204*c2e18aaaSAndroid Build Coastguard Worker 205*c2e18aaaSAndroid Build Coastguard Worker def test_get_vars(self): 206*c2e18aaaSAndroid Build Coastguard Worker """Test the values of AndroidVariables are expected""" 207*c2e18aaaSAndroid Build Coastguard Worker variables = atest_utils.AndroidVariables() 208*c2e18aaaSAndroid Build Coastguard Worker 209*c2e18aaaSAndroid Build Coastguard Worker self.assertEqual(variables.build_top, self.build_top) 210*c2e18aaaSAndroid Build Coastguard Worker self.assertEqual(variables.product_out, self.prod_out) 211*c2e18aaaSAndroid Build Coastguard Worker self.assertEqual(variables.target_out_cases, self.target_out_cases) 212*c2e18aaaSAndroid Build Coastguard Worker self.assertEqual(variables.host_out, self.host_out) 213*c2e18aaaSAndroid Build Coastguard Worker self.assertEqual(variables.host_out_cases, self.host_out_cases) 214*c2e18aaaSAndroid Build Coastguard Worker self.assertEqual(variables.target_product, self.target_product) 215*c2e18aaaSAndroid Build Coastguard Worker self.assertEqual(variables.build_variant, self.build_variant) 216*c2e18aaaSAndroid Build Coastguard Worker 217*c2e18aaaSAndroid Build Coastguard Worker def test_atest_utils_get_build_top(self): 218*c2e18aaaSAndroid Build Coastguard Worker """Test concatenating strings with get_build_top().""" 219*c2e18aaaSAndroid Build Coastguard Worker expected_path = Path(self.build_top, 'path/to/project') 220*c2e18aaaSAndroid Build Coastguard Worker 221*c2e18aaaSAndroid Build Coastguard Worker return_path = atest_utils.get_build_top('path/to/project') 222*c2e18aaaSAndroid Build Coastguard Worker 223*c2e18aaaSAndroid Build Coastguard Worker self.assertEqual(expected_path, return_path) 224*c2e18aaaSAndroid Build Coastguard Worker 225*c2e18aaaSAndroid Build Coastguard Worker def test_atest_utils_get_product_out(self): 226*c2e18aaaSAndroid Build Coastguard Worker """Test concatenating strings with get_product_out().""" 227*c2e18aaaSAndroid Build Coastguard Worker expected_path = Path(self.prod_out, 'module-info.json') 228*c2e18aaaSAndroid Build Coastguard Worker 229*c2e18aaaSAndroid Build Coastguard Worker return_path = atest_utils.get_product_out('module-info.json') 230*c2e18aaaSAndroid Build Coastguard Worker 231*c2e18aaaSAndroid Build Coastguard Worker self.assertEqual(expected_path, return_path) 232*c2e18aaaSAndroid Build Coastguard Worker 233*c2e18aaaSAndroid Build Coastguard Worker def test_atest_utils_get_host_out(self): 234*c2e18aaaSAndroid Build Coastguard Worker """Test concatenating strings with get_host_out().""" 235*c2e18aaaSAndroid Build Coastguard Worker expected_path = Path(self.host_out, 'bin/adb') 236*c2e18aaaSAndroid Build Coastguard Worker 237*c2e18aaaSAndroid Build Coastguard Worker return_path = atest_utils.get_host_out('bin', 'adb') 238*c2e18aaaSAndroid Build Coastguard Worker 239*c2e18aaaSAndroid Build Coastguard Worker self.assertEqual(expected_path, return_path) 240*c2e18aaaSAndroid Build Coastguard Worker 241*c2e18aaaSAndroid Build Coastguard Worker 242*c2e18aaaSAndroid Build Coastguard Workerclass GetBuildOutDirTests(unittest.TestCase): 243*c2e18aaaSAndroid Build Coastguard Worker """Test get_build_out_dir() for various conditions.""" 244*c2e18aaaSAndroid Build Coastguard Worker 245*c2e18aaaSAndroid Build Coastguard Worker def setUp(self) -> None: 246*c2e18aaaSAndroid Build Coastguard Worker self.abs_OUT_DIR = '/somewhere/out' 247*c2e18aaaSAndroid Build Coastguard Worker self.rel_OUT_DIR = 'somewhere/out' 248*c2e18aaaSAndroid Build Coastguard Worker self.abs_OUT_DIR_COMMON_BASE = '/somewhere/common_out' 249*c2e18aaaSAndroid Build Coastguard Worker self.rel_OUT_DIR_COMMON_BASE = 'somewhere/common_out' 250*c2e18aaaSAndroid Build Coastguard Worker 251*c2e18aaaSAndroid Build Coastguard Worker def test_get_build_abs_out_dir(self): 252*c2e18aaaSAndroid Build Coastguard Worker """Test when OUT_DIR is an absolute path.""" 253*c2e18aaaSAndroid Build Coastguard Worker with mock.patch.dict( 254*c2e18aaaSAndroid Build Coastguard Worker 'os.environ', 255*c2e18aaaSAndroid Build Coastguard Worker { 256*c2e18aaaSAndroid Build Coastguard Worker constants.ANDROID_BUILD_TOP: '/src/build/top', 257*c2e18aaaSAndroid Build Coastguard Worker 'OUT_DIR': self.abs_OUT_DIR, 258*c2e18aaaSAndroid Build Coastguard Worker }, 259*c2e18aaaSAndroid Build Coastguard Worker ): 260*c2e18aaaSAndroid Build Coastguard Worker expected_out_dir = Path(self.abs_OUT_DIR) 261*c2e18aaaSAndroid Build Coastguard Worker 262*c2e18aaaSAndroid Build Coastguard Worker returned_out_dir = atest_utils.get_build_out_dir() 263*c2e18aaaSAndroid Build Coastguard Worker 264*c2e18aaaSAndroid Build Coastguard Worker self.assertEqual(expected_out_dir, returned_out_dir) 265*c2e18aaaSAndroid Build Coastguard Worker 266*c2e18aaaSAndroid Build Coastguard Worker def test_get_build_rel_out_dir(self): 267*c2e18aaaSAndroid Build Coastguard Worker """Test when OUT_DIR is a relative path.""" 268*c2e18aaaSAndroid Build Coastguard Worker with mock.patch.dict( 269*c2e18aaaSAndroid Build Coastguard Worker 'os.environ', 270*c2e18aaaSAndroid Build Coastguard Worker { 271*c2e18aaaSAndroid Build Coastguard Worker constants.ANDROID_BUILD_TOP: '/src/build/top', 272*c2e18aaaSAndroid Build Coastguard Worker 'OUT_DIR': self.rel_OUT_DIR, 273*c2e18aaaSAndroid Build Coastguard Worker }, 274*c2e18aaaSAndroid Build Coastguard Worker ): 275*c2e18aaaSAndroid Build Coastguard Worker expected_out_dir = atest_utils.get_build_top(self.rel_OUT_DIR) 276*c2e18aaaSAndroid Build Coastguard Worker 277*c2e18aaaSAndroid Build Coastguard Worker returned_out_dir = atest_utils.get_build_out_dir() 278*c2e18aaaSAndroid Build Coastguard Worker 279*c2e18aaaSAndroid Build Coastguard Worker self.assertEqual(expected_out_dir, returned_out_dir) 280*c2e18aaaSAndroid Build Coastguard Worker 281*c2e18aaaSAndroid Build Coastguard Worker def test_get_build_abs_out_dir_common_base(self): 282*c2e18aaaSAndroid Build Coastguard Worker """Test whe OUT_DIR_COMMON_BASE is an absolute path.""" 283*c2e18aaaSAndroid Build Coastguard Worker build_top_path = '/src/build/top' 284*c2e18aaaSAndroid Build Coastguard Worker branch_name = Path(build_top_path).name 285*c2e18aaaSAndroid Build Coastguard Worker with mock.patch.dict( 286*c2e18aaaSAndroid Build Coastguard Worker 'os.environ', 287*c2e18aaaSAndroid Build Coastguard Worker { 288*c2e18aaaSAndroid Build Coastguard Worker constants.ANDROID_BUILD_TOP: build_top_path, 289*c2e18aaaSAndroid Build Coastguard Worker 'OUT_DIR_COMMON_BASE': self.abs_OUT_DIR_COMMON_BASE, 290*c2e18aaaSAndroid Build Coastguard Worker }, 291*c2e18aaaSAndroid Build Coastguard Worker ): 292*c2e18aaaSAndroid Build Coastguard Worker expected_out_dir = Path(self.abs_OUT_DIR_COMMON_BASE, branch_name) 293*c2e18aaaSAndroid Build Coastguard Worker 294*c2e18aaaSAndroid Build Coastguard Worker returned_out_dir = atest_utils.get_build_out_dir() 295*c2e18aaaSAndroid Build Coastguard Worker 296*c2e18aaaSAndroid Build Coastguard Worker self.assertEqual(expected_out_dir, returned_out_dir) 297*c2e18aaaSAndroid Build Coastguard Worker 298*c2e18aaaSAndroid Build Coastguard Worker def test_get_build_rel_out_dir_common_base(self): 299*c2e18aaaSAndroid Build Coastguard Worker """Test whe OUT_DIR_COMMON_BASE is a relative path.""" 300*c2e18aaaSAndroid Build Coastguard Worker build_top_path = '/src/build/top' 301*c2e18aaaSAndroid Build Coastguard Worker branch_name = Path(build_top_path).name 302*c2e18aaaSAndroid Build Coastguard Worker with mock.patch.dict( 303*c2e18aaaSAndroid Build Coastguard Worker 'os.environ', 304*c2e18aaaSAndroid Build Coastguard Worker { 305*c2e18aaaSAndroid Build Coastguard Worker constants.ANDROID_BUILD_TOP: build_top_path, 306*c2e18aaaSAndroid Build Coastguard Worker 'OUT_DIR_COMMON_BASE': self.rel_OUT_DIR_COMMON_BASE, 307*c2e18aaaSAndroid Build Coastguard Worker }, 308*c2e18aaaSAndroid Build Coastguard Worker ): 309*c2e18aaaSAndroid Build Coastguard Worker expected_out_dir = Path( 310*c2e18aaaSAndroid Build Coastguard Worker build_top_path, self.rel_OUT_DIR_COMMON_BASE, branch_name 311*c2e18aaaSAndroid Build Coastguard Worker ) 312*c2e18aaaSAndroid Build Coastguard Worker 313*c2e18aaaSAndroid Build Coastguard Worker returned_out_dir = atest_utils.get_build_out_dir() 314*c2e18aaaSAndroid Build Coastguard Worker 315*c2e18aaaSAndroid Build Coastguard Worker self.assertEqual(expected_out_dir, returned_out_dir) 316*c2e18aaaSAndroid Build Coastguard Worker 317*c2e18aaaSAndroid Build Coastguard Worker def test_get_build_out_dir(self): 318*c2e18aaaSAndroid Build Coastguard Worker """Test when OUT_DIR and OUT_DIR_COMMON_BASE are null.""" 319*c2e18aaaSAndroid Build Coastguard Worker with mock.patch.dict( 320*c2e18aaaSAndroid Build Coastguard Worker 'os.environ', {constants.ANDROID_BUILD_TOP: '/src/build/top'} 321*c2e18aaaSAndroid Build Coastguard Worker ): 322*c2e18aaaSAndroid Build Coastguard Worker expected_out_dir = atest_utils.get_build_top('out') 323*c2e18aaaSAndroid Build Coastguard Worker 324*c2e18aaaSAndroid Build Coastguard Worker returned_out_dir = atest_utils.get_build_out_dir() 325*c2e18aaaSAndroid Build Coastguard Worker 326*c2e18aaaSAndroid Build Coastguard Worker self.assertEqual(expected_out_dir, returned_out_dir) 327*c2e18aaaSAndroid Build Coastguard Worker 328*c2e18aaaSAndroid Build Coastguard Worker 329*c2e18aaaSAndroid Build Coastguard Worker# pylint: disable=protected-access 330*c2e18aaaSAndroid Build Coastguard Worker# pylint: disable=too-many-public-methods 331*c2e18aaaSAndroid Build Coastguard Workerclass AtestUtilsUnittests(unittest.TestCase): 332*c2e18aaaSAndroid Build Coastguard Worker """Unit tests for atest_utils.py""" 333*c2e18aaaSAndroid Build Coastguard Worker 334*c2e18aaaSAndroid Build Coastguard Worker def test_capture_fail_section_has_fail_section(self): 335*c2e18aaaSAndroid Build Coastguard Worker """Test capture_fail_section when has fail section.""" 336*c2e18aaaSAndroid Build Coastguard Worker test_list = [ 337*c2e18aaaSAndroid Build Coastguard Worker 'AAAAAA', 338*c2e18aaaSAndroid Build Coastguard Worker 'FAILED: Error1', 339*c2e18aaaSAndroid Build Coastguard Worker '^\n', 340*c2e18aaaSAndroid Build Coastguard Worker 'Error2\n', 341*c2e18aaaSAndroid Build Coastguard Worker '[ 6% 191/2997] BBBBBB\n', 342*c2e18aaaSAndroid Build Coastguard Worker 'CCCCC', 343*c2e18aaaSAndroid Build Coastguard Worker '[ 20% 322/2997] DDDDDD\n', 344*c2e18aaaSAndroid Build Coastguard Worker 'EEEEE', 345*c2e18aaaSAndroid Build Coastguard Worker ] 346*c2e18aaaSAndroid Build Coastguard Worker want_list = ['FAILED: Error1', '^\n', 'Error2\n'] 347*c2e18aaaSAndroid Build Coastguard Worker self.assertEqual(want_list, atest_utils._capture_fail_section(test_list)) 348*c2e18aaaSAndroid Build Coastguard Worker 349*c2e18aaaSAndroid Build Coastguard Worker def test_capture_fail_section_no_fail_section(self): 350*c2e18aaaSAndroid Build Coastguard Worker """Test capture_fail_section when no fail section.""" 351*c2e18aaaSAndroid Build Coastguard Worker test_list = ['[ 6% 191/2997] XXXXX', 'YYYYY: ZZZZZ'] 352*c2e18aaaSAndroid Build Coastguard Worker want_list = [] 353*c2e18aaaSAndroid Build Coastguard Worker self.assertEqual(want_list, atest_utils._capture_fail_section(test_list)) 354*c2e18aaaSAndroid Build Coastguard Worker 355*c2e18aaaSAndroid Build Coastguard Worker def test_is_test_mapping_none_test_mapping_args(self): 356*c2e18aaaSAndroid Build Coastguard Worker """Test method is_test_mapping.""" 357*c2e18aaaSAndroid Build Coastguard Worker non_tm_args = ['--host-unit-test-only'] 358*c2e18aaaSAndroid Build Coastguard Worker 359*c2e18aaaSAndroid Build Coastguard Worker for argument in non_tm_args: 360*c2e18aaaSAndroid Build Coastguard Worker args = arg_parser.create_atest_arg_parser().parse_args([argument]) 361*c2e18aaaSAndroid Build Coastguard Worker self.assertFalse( 362*c2e18aaaSAndroid Build Coastguard Worker atest_utils.is_test_mapping(args), 363*c2e18aaaSAndroid Build Coastguard Worker 'Option %s indicates NOT a test_mapping!' % argument, 364*c2e18aaaSAndroid Build Coastguard Worker ) 365*c2e18aaaSAndroid Build Coastguard Worker 366*c2e18aaaSAndroid Build Coastguard Worker def test_is_test_mapping_test_mapping_args(self): 367*c2e18aaaSAndroid Build Coastguard Worker """Test method is_test_mapping.""" 368*c2e18aaaSAndroid Build Coastguard Worker tm_args = ['--test-mapping', '--include-subdirs'] 369*c2e18aaaSAndroid Build Coastguard Worker 370*c2e18aaaSAndroid Build Coastguard Worker for argument in tm_args: 371*c2e18aaaSAndroid Build Coastguard Worker args = arg_parser.create_atest_arg_parser().parse_args([argument]) 372*c2e18aaaSAndroid Build Coastguard Worker self.assertTrue( 373*c2e18aaaSAndroid Build Coastguard Worker atest_utils.is_test_mapping(args), 374*c2e18aaaSAndroid Build Coastguard Worker 'Option %s indicates a test_mapping!' % argument, 375*c2e18aaaSAndroid Build Coastguard Worker ) 376*c2e18aaaSAndroid Build Coastguard Worker 377*c2e18aaaSAndroid Build Coastguard Worker def test_is_test_mapping_implicit_test_mapping(self): 378*c2e18aaaSAndroid Build Coastguard Worker """Test method is_test_mapping.""" 379*c2e18aaaSAndroid Build Coastguard Worker args = arg_parser.create_atest_arg_parser().parse_args( 380*c2e18aaaSAndroid Build Coastguard Worker ['--test', '--build', ':postsubmit'] 381*c2e18aaaSAndroid Build Coastguard Worker ) 382*c2e18aaaSAndroid Build Coastguard Worker self.assertTrue( 383*c2e18aaaSAndroid Build Coastguard Worker atest_utils.is_test_mapping(args), 384*c2e18aaaSAndroid Build Coastguard Worker 'Option %s indicates a test_mapping!' % args, 385*c2e18aaaSAndroid Build Coastguard Worker ) 386*c2e18aaaSAndroid Build Coastguard Worker 387*c2e18aaaSAndroid Build Coastguard Worker def test_is_test_mapping_with_testname(self): 388*c2e18aaaSAndroid Build Coastguard Worker """Test method is_test_mapping.""" 389*c2e18aaaSAndroid Build Coastguard Worker irrelevant_args = ['--test', ':postsubmit', 'testname'] 390*c2e18aaaSAndroid Build Coastguard Worker 391*c2e18aaaSAndroid Build Coastguard Worker args = arg_parser.create_atest_arg_parser().parse_args(irrelevant_args) 392*c2e18aaaSAndroid Build Coastguard Worker self.assertFalse( 393*c2e18aaaSAndroid Build Coastguard Worker atest_utils.is_test_mapping(args), 394*c2e18aaaSAndroid Build Coastguard Worker 'Option %s indicates a test_mapping!' % args, 395*c2e18aaaSAndroid Build Coastguard Worker ) 396*c2e18aaaSAndroid Build Coastguard Worker 397*c2e18aaaSAndroid Build Coastguard Worker def test_is_test_mapping_false(self): 398*c2e18aaaSAndroid Build Coastguard Worker """Test method is_test_mapping.""" 399*c2e18aaaSAndroid Build Coastguard Worker args = arg_parser.create_atest_arg_parser().parse_args( 400*c2e18aaaSAndroid Build Coastguard Worker ['--test', '--build', 'hello_atest'] 401*c2e18aaaSAndroid Build Coastguard Worker ) 402*c2e18aaaSAndroid Build Coastguard Worker 403*c2e18aaaSAndroid Build Coastguard Worker self.assertFalse(atest_utils.is_test_mapping(args)) 404*c2e18aaaSAndroid Build Coastguard Worker 405*c2e18aaaSAndroid Build Coastguard Worker def test_has_colors(self): 406*c2e18aaaSAndroid Build Coastguard Worker """Test method _has_colors.""" 407*c2e18aaaSAndroid Build Coastguard Worker # stream is file I/O 408*c2e18aaaSAndroid Build Coastguard Worker stream = open('/tmp/test_has_colors.txt', 'wb') 409*c2e18aaaSAndroid Build Coastguard Worker self.assertFalse(atest_utils._has_colors(stream)) 410*c2e18aaaSAndroid Build Coastguard Worker stream.close() 411*c2e18aaaSAndroid Build Coastguard Worker 412*c2e18aaaSAndroid Build Coastguard Worker # stream is not a tty(terminal). 413*c2e18aaaSAndroid Build Coastguard Worker stream = mock.Mock() 414*c2e18aaaSAndroid Build Coastguard Worker stream.isatty.return_value = False 415*c2e18aaaSAndroid Build Coastguard Worker self.assertFalse(atest_utils._has_colors(stream)) 416*c2e18aaaSAndroid Build Coastguard Worker 417*c2e18aaaSAndroid Build Coastguard Worker # stream is a tty(terminal). 418*c2e18aaaSAndroid Build Coastguard Worker stream = mock.Mock() 419*c2e18aaaSAndroid Build Coastguard Worker stream.isatty.return_value = True 420*c2e18aaaSAndroid Build Coastguard Worker self.assertTrue(atest_utils._has_colors(stream)) 421*c2e18aaaSAndroid Build Coastguard Worker 422*c2e18aaaSAndroid Build Coastguard Worker @mock.patch('atest.atest_utils._has_colors') 423*c2e18aaaSAndroid Build Coastguard Worker def test_colorize(self, mock_has_colors): 424*c2e18aaaSAndroid Build Coastguard Worker """Test method colorize.""" 425*c2e18aaaSAndroid Build Coastguard Worker original_str = 'test string' 426*c2e18aaaSAndroid Build Coastguard Worker green_no = 2 427*c2e18aaaSAndroid Build Coastguard Worker 428*c2e18aaaSAndroid Build Coastguard Worker # _has_colors() return False. 429*c2e18aaaSAndroid Build Coastguard Worker mock_has_colors.return_value = False 430*c2e18aaaSAndroid Build Coastguard Worker converted_str = atest_utils.colorize( 431*c2e18aaaSAndroid Build Coastguard Worker original_str, green_no, bp_color=constants.RED 432*c2e18aaaSAndroid Build Coastguard Worker ) 433*c2e18aaaSAndroid Build Coastguard Worker self.assertEqual(original_str, converted_str) 434*c2e18aaaSAndroid Build Coastguard Worker 435*c2e18aaaSAndroid Build Coastguard Worker # Green text with red background. 436*c2e18aaaSAndroid Build Coastguard Worker mock_has_colors.return_value = True 437*c2e18aaaSAndroid Build Coastguard Worker converted_str = atest_utils.colorize( 438*c2e18aaaSAndroid Build Coastguard Worker original_str, green_no, bp_color=constants.RED 439*c2e18aaaSAndroid Build Coastguard Worker ) 440*c2e18aaaSAndroid Build Coastguard Worker green_highlight_string = '\x1b[1;32;41m%s\x1b[0m' % original_str 441*c2e18aaaSAndroid Build Coastguard Worker self.assertEqual(green_highlight_string, converted_str) 442*c2e18aaaSAndroid Build Coastguard Worker 443*c2e18aaaSAndroid Build Coastguard Worker # Green text, no background. 444*c2e18aaaSAndroid Build Coastguard Worker mock_has_colors.return_value = True 445*c2e18aaaSAndroid Build Coastguard Worker converted_str = atest_utils.colorize(original_str, green_no) 446*c2e18aaaSAndroid Build Coastguard Worker green_no_highlight_string = '\x1b[1;32m%s\x1b[0m' % original_str 447*c2e18aaaSAndroid Build Coastguard Worker self.assertEqual(green_no_highlight_string, converted_str) 448*c2e18aaaSAndroid Build Coastguard Worker 449*c2e18aaaSAndroid Build Coastguard Worker @mock.patch('atest.atest_utils.colorful_print') 450*c2e18aaaSAndroid Build Coastguard Worker @mock.patch('logging.error') 451*c2e18aaaSAndroid Build Coastguard Worker def test_print_and_log_error_no_format_prints_and_logs( 452*c2e18aaaSAndroid Build Coastguard Worker self, mocked_print, locked_error_logging 453*c2e18aaaSAndroid Build Coastguard Worker ): 454*c2e18aaaSAndroid Build Coastguard Worker atest_utils.print_and_log_error('no format') 455*c2e18aaaSAndroid Build Coastguard Worker 456*c2e18aaaSAndroid Build Coastguard Worker mocked_print.assert_called_once() 457*c2e18aaaSAndroid Build Coastguard Worker locked_error_logging.assert_called_once() 458*c2e18aaaSAndroid Build Coastguard Worker 459*c2e18aaaSAndroid Build Coastguard Worker @mock.patch('atest.atest_utils.colorful_print') 460*c2e18aaaSAndroid Build Coastguard Worker def test_print_and_log_error_single_non_string_prints(self, mocked_print): 461*c2e18aaaSAndroid Build Coastguard Worker atest_utils.print_and_log_error(123) 462*c2e18aaaSAndroid Build Coastguard Worker 463*c2e18aaaSAndroid Build Coastguard Worker mocked_print.assert_called_once() 464*c2e18aaaSAndroid Build Coastguard Worker 465*c2e18aaaSAndroid Build Coastguard Worker @mock.patch('atest.atest_utils.colorful_print') 466*c2e18aaaSAndroid Build Coastguard Worker def test_print_and_log_error_with_format_prints(self, mocked_print): 467*c2e18aaaSAndroid Build Coastguard Worker atest_utils.print_and_log_error('1+1=%s', 2) 468*c2e18aaaSAndroid Build Coastguard Worker 469*c2e18aaaSAndroid Build Coastguard Worker mocked_print.assert_called_once() 470*c2e18aaaSAndroid Build Coastguard Worker 471*c2e18aaaSAndroid Build Coastguard Worker @mock.patch('atest.atest_utils.colorful_print') 472*c2e18aaaSAndroid Build Coastguard Worker def test_print_and_log_error_bad_value_no_throw_no_print(self, mocked_print): 473*c2e18aaaSAndroid Build Coastguard Worker atest_utils.print_and_log_error('bad format %', 'format arg') 474*c2e18aaaSAndroid Build Coastguard Worker 475*c2e18aaaSAndroid Build Coastguard Worker mocked_print.assert_not_called() 476*c2e18aaaSAndroid Build Coastguard Worker 477*c2e18aaaSAndroid Build Coastguard Worker @mock.patch('atest.atest_utils.colorful_print') 478*c2e18aaaSAndroid Build Coastguard Worker def test_print_and_log_error_missing_format_arg_no_print(self, mocked_print): 479*c2e18aaaSAndroid Build Coastguard Worker atest_utils.print_and_log_error('bad format %s %s', 'format arg') 480*c2e18aaaSAndroid Build Coastguard Worker 481*c2e18aaaSAndroid Build Coastguard Worker mocked_print.assert_not_called() 482*c2e18aaaSAndroid Build Coastguard Worker 483*c2e18aaaSAndroid Build Coastguard Worker @mock.patch('atest.atest_utils.colorful_print') 484*c2e18aaaSAndroid Build Coastguard Worker def test_print_and_log_error_extra_format_arg_no_print(self, mocked_print): 485*c2e18aaaSAndroid Build Coastguard Worker atest_utils.print_and_log_error( 486*c2e18aaaSAndroid Build Coastguard Worker 'bad format %s', 'format arg1', 'format arg2' 487*c2e18aaaSAndroid Build Coastguard Worker ) 488*c2e18aaaSAndroid Build Coastguard Worker 489*c2e18aaaSAndroid Build Coastguard Worker mocked_print.assert_not_called() 490*c2e18aaaSAndroid Build Coastguard Worker 491*c2e18aaaSAndroid Build Coastguard Worker @mock.patch('atest.atest_utils._has_colors') 492*c2e18aaaSAndroid Build Coastguard Worker def test_colorful_print(self, mock_has_colors): 493*c2e18aaaSAndroid Build Coastguard Worker """Test method colorful_print.""" 494*c2e18aaaSAndroid Build Coastguard Worker testing_str = 'color_print_test' 495*c2e18aaaSAndroid Build Coastguard Worker green_no = 2 496*c2e18aaaSAndroid Build Coastguard Worker 497*c2e18aaaSAndroid Build Coastguard Worker # _has_colors() return False. 498*c2e18aaaSAndroid Build Coastguard Worker mock_has_colors.return_value = False 499*c2e18aaaSAndroid Build Coastguard Worker capture_output = StringIO() 500*c2e18aaaSAndroid Build Coastguard Worker sys.stdout = capture_output 501*c2e18aaaSAndroid Build Coastguard Worker atest_utils.colorful_print( 502*c2e18aaaSAndroid Build Coastguard Worker testing_str, green_no, bp_color=constants.RED, auto_wrap=False 503*c2e18aaaSAndroid Build Coastguard Worker ) 504*c2e18aaaSAndroid Build Coastguard Worker sys.stdout = sys.__stdout__ 505*c2e18aaaSAndroid Build Coastguard Worker uncolored_string = testing_str 506*c2e18aaaSAndroid Build Coastguard Worker self.assertEqual(capture_output.getvalue(), uncolored_string) 507*c2e18aaaSAndroid Build Coastguard Worker 508*c2e18aaaSAndroid Build Coastguard Worker # Green text with red background, but no wrap. 509*c2e18aaaSAndroid Build Coastguard Worker mock_has_colors.return_value = True 510*c2e18aaaSAndroid Build Coastguard Worker capture_output = StringIO() 511*c2e18aaaSAndroid Build Coastguard Worker sys.stdout = capture_output 512*c2e18aaaSAndroid Build Coastguard Worker atest_utils.colorful_print( 513*c2e18aaaSAndroid Build Coastguard Worker testing_str, green_no, bp_color=constants.RED, auto_wrap=False 514*c2e18aaaSAndroid Build Coastguard Worker ) 515*c2e18aaaSAndroid Build Coastguard Worker sys.stdout = sys.__stdout__ 516*c2e18aaaSAndroid Build Coastguard Worker green_highlight_no_wrap_string = '\x1b[1;32;41m%s\x1b[0m' % testing_str 517*c2e18aaaSAndroid Build Coastguard Worker self.assertEqual(capture_output.getvalue(), green_highlight_no_wrap_string) 518*c2e18aaaSAndroid Build Coastguard Worker 519*c2e18aaaSAndroid Build Coastguard Worker # Green text, no background, no wrap. 520*c2e18aaaSAndroid Build Coastguard Worker mock_has_colors.return_value = True 521*c2e18aaaSAndroid Build Coastguard Worker capture_output = StringIO() 522*c2e18aaaSAndroid Build Coastguard Worker sys.stdout = capture_output 523*c2e18aaaSAndroid Build Coastguard Worker atest_utils.colorful_print(testing_str, green_no, auto_wrap=False) 524*c2e18aaaSAndroid Build Coastguard Worker sys.stdout = sys.__stdout__ 525*c2e18aaaSAndroid Build Coastguard Worker green_no_high_no_wrap_string = '\x1b[1;32m%s\x1b[0m' % testing_str 526*c2e18aaaSAndroid Build Coastguard Worker self.assertEqual(capture_output.getvalue(), green_no_high_no_wrap_string) 527*c2e18aaaSAndroid Build Coastguard Worker 528*c2e18aaaSAndroid Build Coastguard Worker # Green text with red background and wrap. 529*c2e18aaaSAndroid Build Coastguard Worker mock_has_colors.return_value = True 530*c2e18aaaSAndroid Build Coastguard Worker capture_output = StringIO() 531*c2e18aaaSAndroid Build Coastguard Worker sys.stdout = capture_output 532*c2e18aaaSAndroid Build Coastguard Worker atest_utils.colorful_print( 533*c2e18aaaSAndroid Build Coastguard Worker testing_str, green_no, bp_color=constants.RED, auto_wrap=True 534*c2e18aaaSAndroid Build Coastguard Worker ) 535*c2e18aaaSAndroid Build Coastguard Worker sys.stdout = sys.__stdout__ 536*c2e18aaaSAndroid Build Coastguard Worker green_highlight_wrap_string = '\x1b[1;32;41m%s\x1b[0m\n' % testing_str 537*c2e18aaaSAndroid Build Coastguard Worker self.assertEqual(capture_output.getvalue(), green_highlight_wrap_string) 538*c2e18aaaSAndroid Build Coastguard Worker 539*c2e18aaaSAndroid Build Coastguard Worker # Green text with wrap, but no background. 540*c2e18aaaSAndroid Build Coastguard Worker mock_has_colors.return_value = True 541*c2e18aaaSAndroid Build Coastguard Worker capture_output = StringIO() 542*c2e18aaaSAndroid Build Coastguard Worker sys.stdout = capture_output 543*c2e18aaaSAndroid Build Coastguard Worker atest_utils.colorful_print(testing_str, green_no, auto_wrap=True) 544*c2e18aaaSAndroid Build Coastguard Worker sys.stdout = sys.__stdout__ 545*c2e18aaaSAndroid Build Coastguard Worker green_wrap_no_highlight_string = '\x1b[1;32m%s\x1b[0m\n' % testing_str 546*c2e18aaaSAndroid Build Coastguard Worker self.assertEqual(capture_output.getvalue(), green_wrap_no_highlight_string) 547*c2e18aaaSAndroid Build Coastguard Worker 548*c2e18aaaSAndroid Build Coastguard Worker def test_is_supported_mainline_module(self): 549*c2e18aaaSAndroid Build Coastguard Worker """Test the installed artifacts are supported.""" 550*c2e18aaaSAndroid Build Coastguard Worker self.assertTrue(atest_utils.is_supported_mainline_module('out/foo.apk')) 551*c2e18aaaSAndroid Build Coastguard Worker self.assertTrue(atest_utils.is_supported_mainline_module('out/foo.apks')) 552*c2e18aaaSAndroid Build Coastguard Worker self.assertTrue(atest_utils.is_supported_mainline_module('out/foo.apex')) 553*c2e18aaaSAndroid Build Coastguard Worker self.assertFalse(atest_utils.is_supported_mainline_module('out/foo.capex')) 554*c2e18aaaSAndroid Build Coastguard Worker 555*c2e18aaaSAndroid Build Coastguard Worker def test_get_test_and_mainline_modules(self): 556*c2e18aaaSAndroid Build Coastguard Worker """Test whether the given test reference is a mainline module test.""" 557*c2e18aaaSAndroid Build Coastguard Worker # regular test. 558*c2e18aaaSAndroid Build Coastguard Worker self.assertIsNone(atest_utils.get_test_and_mainline_modules('test_name')) 559*c2e18aaaSAndroid Build Coastguard Worker # missing trailing bracket. 560*c2e18aaaSAndroid Build Coastguard Worker self.assertIsNone( 561*c2e18aaaSAndroid Build Coastguard Worker atest_utils.get_test_and_mainline_modules('test_name[foo.apk+goo.apex') 562*c2e18aaaSAndroid Build Coastguard Worker ) 563*c2e18aaaSAndroid Build Coastguard Worker # valid mainline module syntax 564*c2e18aaaSAndroid Build Coastguard Worker self.assertIsNotNone( 565*c2e18aaaSAndroid Build Coastguard Worker atest_utils.get_test_and_mainline_modules('test_name[foo.apk]') 566*c2e18aaaSAndroid Build Coastguard Worker ) 567*c2e18aaaSAndroid Build Coastguard Worker self.assertIsNotNone( 568*c2e18aaaSAndroid Build Coastguard Worker atest_utils.get_test_and_mainline_modules('test_name[foo.apk+goo.apex]') 569*c2e18aaaSAndroid Build Coastguard Worker ) 570*c2e18aaaSAndroid Build Coastguard Worker 571*c2e18aaaSAndroid Build Coastguard Worker def test_get_test_info_cache_path(self): 572*c2e18aaaSAndroid Build Coastguard Worker """Test method get_test_info_cache_path.""" 573*c2e18aaaSAndroid Build Coastguard Worker input_file_name = 'mytest_name' 574*c2e18aaaSAndroid Build Coastguard Worker cache_root = '/a/b/c' 575*c2e18aaaSAndroid Build Coastguard Worker expect_hashed_name = ( 576*c2e18aaaSAndroid Build Coastguard Worker '%s.cache' % hashlib.md5(str(input_file_name).encode()).hexdigest() 577*c2e18aaaSAndroid Build Coastguard Worker ) 578*c2e18aaaSAndroid Build Coastguard Worker self.assertEqual( 579*c2e18aaaSAndroid Build Coastguard Worker os.path.join(cache_root, expect_hashed_name), 580*c2e18aaaSAndroid Build Coastguard Worker atest_utils.get_test_info_cache_path(input_file_name, cache_root), 581*c2e18aaaSAndroid Build Coastguard Worker ) 582*c2e18aaaSAndroid Build Coastguard Worker 583*c2e18aaaSAndroid Build Coastguard Worker def test_get_and_load_cache(self): 584*c2e18aaaSAndroid Build Coastguard Worker """Test method update_test_info_cache and load_test_info_cache.""" 585*c2e18aaaSAndroid Build Coastguard Worker test_reference = 'myTestRefA' 586*c2e18aaaSAndroid Build Coastguard Worker test_cache_dir = tempfile.mkdtemp() 587*c2e18aaaSAndroid Build Coastguard Worker atest_utils.update_test_info_cache( 588*c2e18aaaSAndroid Build Coastguard Worker test_reference, [TEST_INFO_A], test_cache_dir 589*c2e18aaaSAndroid Build Coastguard Worker ) 590*c2e18aaaSAndroid Build Coastguard Worker unittest_utils.assert_equal_testinfo_sets( 591*c2e18aaaSAndroid Build Coastguard Worker self, 592*c2e18aaaSAndroid Build Coastguard Worker set([TEST_INFO_A]), 593*c2e18aaaSAndroid Build Coastguard Worker atest_utils.load_test_info_cache(test_reference, test_cache_dir), 594*c2e18aaaSAndroid Build Coastguard Worker ) 595*c2e18aaaSAndroid Build Coastguard Worker 596*c2e18aaaSAndroid Build Coastguard Worker @mock.patch('subprocess.check_output') 597*c2e18aaaSAndroid Build Coastguard Worker def test_get_modified_files(self, mock_co): 598*c2e18aaaSAndroid Build Coastguard Worker """Test method get_modified_files""" 599*c2e18aaaSAndroid Build Coastguard Worker mock_co.side_effect = [ 600*c2e18aaaSAndroid Build Coastguard Worker x.encode('utf-8') 601*c2e18aaaSAndroid Build Coastguard Worker for x in ['/a/b/', '\n', 'test_fp1.java\nc/test_fp2.java'] 602*c2e18aaaSAndroid Build Coastguard Worker ] 603*c2e18aaaSAndroid Build Coastguard Worker self.assertEqual( 604*c2e18aaaSAndroid Build Coastguard Worker {'/a/b/test_fp1.java', '/a/b/c/test_fp2.java'}, 605*c2e18aaaSAndroid Build Coastguard Worker atest_utils.get_modified_files(''), 606*c2e18aaaSAndroid Build Coastguard Worker ) 607*c2e18aaaSAndroid Build Coastguard Worker mock_co.side_effect = [ 608*c2e18aaaSAndroid Build Coastguard Worker x.encode('utf-8') for x in ['/a/b/', 'test_fp4', '/test_fp3.java'] 609*c2e18aaaSAndroid Build Coastguard Worker ] 610*c2e18aaaSAndroid Build Coastguard Worker self.assertEqual( 611*c2e18aaaSAndroid Build Coastguard Worker {'/a/b/test_fp4', '/a/b/test_fp3.java'}, 612*c2e18aaaSAndroid Build Coastguard Worker atest_utils.get_modified_files(''), 613*c2e18aaaSAndroid Build Coastguard Worker ) 614*c2e18aaaSAndroid Build Coastguard Worker 615*c2e18aaaSAndroid Build Coastguard Worker def test_delimiter(self): 616*c2e18aaaSAndroid Build Coastguard Worker """Test method delimiter""" 617*c2e18aaaSAndroid Build Coastguard Worker self.assertEqual('\n===\n\n', atest_utils.delimiter('=', 3, 1, 2)) 618*c2e18aaaSAndroid Build Coastguard Worker 619*c2e18aaaSAndroid Build Coastguard Worker def test_has_python_module(self): 620*c2e18aaaSAndroid Build Coastguard Worker """Test method has_python_module""" 621*c2e18aaaSAndroid Build Coastguard Worker self.assertFalse(atest_utils.has_python_module('M_M')) 622*c2e18aaaSAndroid Build Coastguard Worker self.assertTrue(atest_utils.has_python_module('os')) 623*c2e18aaaSAndroid Build Coastguard Worker 624*c2e18aaaSAndroid Build Coastguard Worker @mock.patch.object(atest_utils, 'matched_tf_error_log', return_value=True) 625*c2e18aaaSAndroid Build Coastguard Worker def test_read_zip_single_text(self, _matched): 626*c2e18aaaSAndroid Build Coastguard Worker """Test method extract_zip_text include only one text file.""" 627*c2e18aaaSAndroid Build Coastguard Worker zip_path = os.path.join( 628*c2e18aaaSAndroid Build Coastguard Worker unittest_constants.TEST_DATA_DIR, 629*c2e18aaaSAndroid Build Coastguard Worker TEST_ZIP_DATA_DIR, 630*c2e18aaaSAndroid Build Coastguard Worker TEST_SINGLE_ZIP_NAME, 631*c2e18aaaSAndroid Build Coastguard Worker ) 632*c2e18aaaSAndroid Build Coastguard Worker expect_content = '\nfile1_line1\nfile1_line2\n' 633*c2e18aaaSAndroid Build Coastguard Worker self.assertEqual(expect_content, atest_utils.extract_zip_text(zip_path)) 634*c2e18aaaSAndroid Build Coastguard Worker 635*c2e18aaaSAndroid Build Coastguard Worker @mock.patch.object(atest_utils, 'matched_tf_error_log', return_value=True) 636*c2e18aaaSAndroid Build Coastguard Worker def test_read_zip_multi_text(self, _matched): 637*c2e18aaaSAndroid Build Coastguard Worker """Test method extract_zip_text include multiple text files.""" 638*c2e18aaaSAndroid Build Coastguard Worker zip_path = os.path.join( 639*c2e18aaaSAndroid Build Coastguard Worker unittest_constants.TEST_DATA_DIR, TEST_ZIP_DATA_DIR, TEST_MULTI_ZIP_NAME 640*c2e18aaaSAndroid Build Coastguard Worker ) 641*c2e18aaaSAndroid Build Coastguard Worker expect_content = '\nfile1_line1\nfile1_line2\n\nfile2_line1\nfile2_line2\n' 642*c2e18aaaSAndroid Build Coastguard Worker self.assertEqual(expect_content, atest_utils.extract_zip_text(zip_path)) 643*c2e18aaaSAndroid Build Coastguard Worker 644*c2e18aaaSAndroid Build Coastguard Worker def test_matched_tf_error_log(self): 645*c2e18aaaSAndroid Build Coastguard Worker """Test method extract_zip_text include multiple text files.""" 646*c2e18aaaSAndroid Build Coastguard Worker matched_content = '05-25 17:37:04 E/XXXXX YYYYY' 647*c2e18aaaSAndroid Build Coastguard Worker not_matched_content = '05-25 17:37:04 I/XXXXX YYYYY' 648*c2e18aaaSAndroid Build Coastguard Worker # Test matched content 649*c2e18aaaSAndroid Build Coastguard Worker self.assertEqual(True, atest_utils.matched_tf_error_log(matched_content)) 650*c2e18aaaSAndroid Build Coastguard Worker # Test not matched content 651*c2e18aaaSAndroid Build Coastguard Worker self.assertEqual( 652*c2e18aaaSAndroid Build Coastguard Worker False, atest_utils.matched_tf_error_log(not_matched_content) 653*c2e18aaaSAndroid Build Coastguard Worker ) 654*c2e18aaaSAndroid Build Coastguard Worker 655*c2e18aaaSAndroid Build Coastguard Worker # pylint: disable=no-member 656*c2e18aaaSAndroid Build Coastguard Worker def test_read_test_record_proto(self): 657*c2e18aaaSAndroid Build Coastguard Worker """Test method read_test_record.""" 658*c2e18aaaSAndroid Build Coastguard Worker test_record_file_path = os.path.join( 659*c2e18aaaSAndroid Build Coastguard Worker unittest_constants.TEST_DATA_DIR, 'test_record.proto.testonly' 660*c2e18aaaSAndroid Build Coastguard Worker ) 661*c2e18aaaSAndroid Build Coastguard Worker test_record = atest_utils.read_test_record(test_record_file_path) 662*c2e18aaaSAndroid Build Coastguard Worker self.assertEqual( 663*c2e18aaaSAndroid Build Coastguard Worker test_record.children[0].inline_test_record.test_record_id, 664*c2e18aaaSAndroid Build Coastguard Worker 'x86 hello_world_test', 665*c2e18aaaSAndroid Build Coastguard Worker ) 666*c2e18aaaSAndroid Build Coastguard Worker 667*c2e18aaaSAndroid Build Coastguard Worker def test_load_json_safely_file_inexistent(self): 668*c2e18aaaSAndroid Build Coastguard Worker """Test method load_json_safely if file does not exist.""" 669*c2e18aaaSAndroid Build Coastguard Worker json_file_path = Path(unittest_constants.TEST_DATA_DIR).joinpath( 670*c2e18aaaSAndroid Build Coastguard Worker 'not_exist.json' 671*c2e18aaaSAndroid Build Coastguard Worker ) 672*c2e18aaaSAndroid Build Coastguard Worker self.assertEqual({}, atest_utils.load_json_safely(json_file_path)) 673*c2e18aaaSAndroid Build Coastguard Worker 674*c2e18aaaSAndroid Build Coastguard Worker def test_load_json_safely_valid_json_format(self): 675*c2e18aaaSAndroid Build Coastguard Worker """Test method load_json_safely if file exists and format is valid.""" 676*c2e18aaaSAndroid Build Coastguard Worker json_file_path = Path(unittest_constants.TEST_DATA_DIR).joinpath( 677*c2e18aaaSAndroid Build Coastguard Worker 'module-info.json' 678*c2e18aaaSAndroid Build Coastguard Worker ) 679*c2e18aaaSAndroid Build Coastguard Worker content = atest_utils.load_json_safely(json_file_path) 680*c2e18aaaSAndroid Build Coastguard Worker self.assertEqual( 681*c2e18aaaSAndroid Build Coastguard Worker 'MainModule1', content.get('MainModule1').get('module_name') 682*c2e18aaaSAndroid Build Coastguard Worker ) 683*c2e18aaaSAndroid Build Coastguard Worker self.assertEqual( 684*c2e18aaaSAndroid Build Coastguard Worker [], content.get('MainModule2').get('test_mainline_modules') 685*c2e18aaaSAndroid Build Coastguard Worker ) 686*c2e18aaaSAndroid Build Coastguard Worker 687*c2e18aaaSAndroid Build Coastguard Worker def test_load_json_safely_invalid_json_format(self): 688*c2e18aaaSAndroid Build Coastguard Worker """Test method load_json_safely if file exist but content is invalid.""" 689*c2e18aaaSAndroid Build Coastguard Worker json_file_path = Path(unittest_constants.TEST_DATA_DIR).joinpath( 690*c2e18aaaSAndroid Build Coastguard Worker 'not-valid-module-info.json' 691*c2e18aaaSAndroid Build Coastguard Worker ) 692*c2e18aaaSAndroid Build Coastguard Worker self.assertEqual({}, atest_utils.load_json_safely(json_file_path)) 693*c2e18aaaSAndroid Build Coastguard Worker 694*c2e18aaaSAndroid Build Coastguard Worker @mock.patch('os.getenv') 695*c2e18aaaSAndroid Build Coastguard Worker def test_get_manifest_branch(self, mock_env): 696*c2e18aaaSAndroid Build Coastguard Worker """Test method get_manifest_branch""" 697*c2e18aaaSAndroid Build Coastguard Worker build_top = tempfile.TemporaryDirectory() 698*c2e18aaaSAndroid Build Coastguard Worker mock_env.return_value = build_top.name 699*c2e18aaaSAndroid Build Coastguard Worker repo_dir = Path(build_top.name).joinpath('.repo') 700*c2e18aaaSAndroid Build Coastguard Worker portal_xml = repo_dir.joinpath('manifest.xml') 701*c2e18aaaSAndroid Build Coastguard Worker manifest_dir = repo_dir.joinpath('manifests') 702*c2e18aaaSAndroid Build Coastguard Worker target_xml = manifest_dir.joinpath('Default.xml') 703*c2e18aaaSAndroid Build Coastguard Worker repo_dir.mkdir() 704*c2e18aaaSAndroid Build Coastguard Worker manifest_dir.mkdir() 705*c2e18aaaSAndroid Build Coastguard Worker content_portal = '<manifest><include name="Default.xml" /></manifest>' 706*c2e18aaaSAndroid Build Coastguard Worker content_manifest = """<manifest> 707*c2e18aaaSAndroid Build Coastguard Worker <remote name="aosp" fetch=".." review="https://android-review.googlesource.com/" /> 708*c2e18aaaSAndroid Build Coastguard Worker <default revision="MONSTER-dev" remote="aosp" sync-j="4" /> 709*c2e18aaaSAndroid Build Coastguard Worker </manifest>""" 710*c2e18aaaSAndroid Build Coastguard Worker 711*c2e18aaaSAndroid Build Coastguard Worker # 1. The manifest.xml(portal) contains 'include' directive: 'Default.xml'. 712*c2e18aaaSAndroid Build Coastguard Worker # Search revision in .repo/manifests/Default.xml. 713*c2e18aaaSAndroid Build Coastguard Worker with open(portal_xml, 'w') as cache: 714*c2e18aaaSAndroid Build Coastguard Worker cache.write(content_portal) 715*c2e18aaaSAndroid Build Coastguard Worker with open(target_xml, 'w') as cache: 716*c2e18aaaSAndroid Build Coastguard Worker cache.write(content_manifest) 717*c2e18aaaSAndroid Build Coastguard Worker self.assertEqual('MONSTER-dev', atest_utils.get_manifest_branch()) 718*c2e18aaaSAndroid Build Coastguard Worker self.assertEqual('aosp-MONSTER-dev', atest_utils.get_manifest_branch(True)) 719*c2e18aaaSAndroid Build Coastguard Worker os.remove(target_xml) 720*c2e18aaaSAndroid Build Coastguard Worker os.remove(portal_xml) 721*c2e18aaaSAndroid Build Coastguard Worker 722*c2e18aaaSAndroid Build Coastguard Worker # 2. The manifest.xml contains neither 'include' nor 'revision' directive, 723*c2e18aaaSAndroid Build Coastguard Worker # keep searching revision in .repo/manifests/default.xml by default. 724*c2e18aaaSAndroid Build Coastguard Worker with open(portal_xml, 'w') as cache: 725*c2e18aaaSAndroid Build Coastguard Worker cache.write('<manifest></manifest>') 726*c2e18aaaSAndroid Build Coastguard Worker default_xml = manifest_dir.joinpath('default.xml') 727*c2e18aaaSAndroid Build Coastguard Worker with open(default_xml, 'w') as cache: 728*c2e18aaaSAndroid Build Coastguard Worker cache.write(content_manifest) 729*c2e18aaaSAndroid Build Coastguard Worker self.assertEqual('MONSTER-dev', atest_utils.get_manifest_branch()) 730*c2e18aaaSAndroid Build Coastguard Worker os.remove(default_xml) 731*c2e18aaaSAndroid Build Coastguard Worker os.remove(portal_xml) 732*c2e18aaaSAndroid Build Coastguard Worker 733*c2e18aaaSAndroid Build Coastguard Worker # 3. revision was directly defined in 'manifest.xml'. 734*c2e18aaaSAndroid Build Coastguard Worker with open(portal_xml, 'w') as cache: 735*c2e18aaaSAndroid Build Coastguard Worker cache.write(content_manifest) 736*c2e18aaaSAndroid Build Coastguard Worker self.assertEqual('MONSTER-dev', atest_utils.get_manifest_branch()) 737*c2e18aaaSAndroid Build Coastguard Worker os.remove(portal_xml) 738*c2e18aaaSAndroid Build Coastguard Worker 739*c2e18aaaSAndroid Build Coastguard Worker # 4. Return None if the included xml does not exist. 740*c2e18aaaSAndroid Build Coastguard Worker with open(portal_xml, 'w') as cache: 741*c2e18aaaSAndroid Build Coastguard Worker cache.write(content_portal) 742*c2e18aaaSAndroid Build Coastguard Worker self.assertEqual('', atest_utils.get_manifest_branch()) 743*c2e18aaaSAndroid Build Coastguard Worker os.remove(portal_xml) 744*c2e18aaaSAndroid Build Coastguard Worker 745*c2e18aaaSAndroid Build Coastguard Worker def test_has_wildcard(self): 746*c2e18aaaSAndroid Build Coastguard Worker """Test method of has_wildcard""" 747*c2e18aaaSAndroid Build Coastguard Worker self.assertFalse(atest_utils.has_wildcard('test1')) 748*c2e18aaaSAndroid Build Coastguard Worker self.assertFalse(atest_utils.has_wildcard(['test1'])) 749*c2e18aaaSAndroid Build Coastguard Worker self.assertTrue(atest_utils.has_wildcard('test1?')) 750*c2e18aaaSAndroid Build Coastguard Worker self.assertTrue(atest_utils.has_wildcard(['test1', 'b*', 'a?b*'])) 751*c2e18aaaSAndroid Build Coastguard Worker 752*c2e18aaaSAndroid Build Coastguard Worker # pylint: disable=anomalous-backslash-in-string 753*c2e18aaaSAndroid Build Coastguard Worker def test_quote(self): 754*c2e18aaaSAndroid Build Coastguard Worker """Test method of quote()""" 755*c2e18aaaSAndroid Build Coastguard Worker target_str = r'TEST_(F|P)[0-9].*\w$' 756*c2e18aaaSAndroid Build Coastguard Worker expected_str = "'TEST_(F|P)[0-9].*\w$'" 757*c2e18aaaSAndroid Build Coastguard Worker self.assertEqual(atest_utils.quote(target_str), expected_str) 758*c2e18aaaSAndroid Build Coastguard Worker self.assertEqual(atest_utils.quote('TEST_P224'), 'TEST_P224') 759*c2e18aaaSAndroid Build Coastguard Worker 760*c2e18aaaSAndroid Build Coastguard Worker @mock.patch('builtins.input', return_value='') 761*c2e18aaaSAndroid Build Coastguard Worker def test_prompt_with_yn_result(self, mock_input): 762*c2e18aaaSAndroid Build Coastguard Worker """Test method of prompt_with_yn_result""" 763*c2e18aaaSAndroid Build Coastguard Worker msg = 'Do you want to continue?' 764*c2e18aaaSAndroid Build Coastguard Worker mock_input.return_value = '' 765*c2e18aaaSAndroid Build Coastguard Worker self.assertTrue(atest_utils.prompt_with_yn_result(msg, True)) 766*c2e18aaaSAndroid Build Coastguard Worker self.assertFalse(atest_utils.prompt_with_yn_result(msg, False)) 767*c2e18aaaSAndroid Build Coastguard Worker mock_input.return_value = 'y' 768*c2e18aaaSAndroid Build Coastguard Worker self.assertTrue(atest_utils.prompt_with_yn_result(msg, True)) 769*c2e18aaaSAndroid Build Coastguard Worker mock_input.return_value = 'nO' 770*c2e18aaaSAndroid Build Coastguard Worker self.assertFalse(atest_utils.prompt_with_yn_result(msg, True)) 771*c2e18aaaSAndroid Build Coastguard Worker 772*c2e18aaaSAndroid Build Coastguard Worker def test_get_android_junit_config_filters(self): 773*c2e18aaaSAndroid Build Coastguard Worker """Test method of get_android_junit_config_filters""" 774*c2e18aaaSAndroid Build Coastguard Worker no_filter_test_config = os.path.join( 775*c2e18aaaSAndroid Build Coastguard Worker unittest_constants.TEST_DATA_DIR, 'filter_configs', 'no_filter.cfg' 776*c2e18aaaSAndroid Build Coastguard Worker ) 777*c2e18aaaSAndroid Build Coastguard Worker self.assertEqual( 778*c2e18aaaSAndroid Build Coastguard Worker {}, atest_utils.get_android_junit_config_filters(no_filter_test_config) 779*c2e18aaaSAndroid Build Coastguard Worker ) 780*c2e18aaaSAndroid Build Coastguard Worker 781*c2e18aaaSAndroid Build Coastguard Worker filtered_test_config = os.path.join( 782*c2e18aaaSAndroid Build Coastguard Worker unittest_constants.TEST_DATA_DIR, 'filter_configs', 'filter.cfg' 783*c2e18aaaSAndroid Build Coastguard Worker ) 784*c2e18aaaSAndroid Build Coastguard Worker filter_dict = atest_utils.get_android_junit_config_filters( 785*c2e18aaaSAndroid Build Coastguard Worker filtered_test_config 786*c2e18aaaSAndroid Build Coastguard Worker ) 787*c2e18aaaSAndroid Build Coastguard Worker include_annotations = filter_dict.get(constants.INCLUDE_ANNOTATION) 788*c2e18aaaSAndroid Build Coastguard Worker include_annotations.sort() 789*c2e18aaaSAndroid Build Coastguard Worker self.assertEqual(['include1', 'include2'], include_annotations) 790*c2e18aaaSAndroid Build Coastguard Worker exclude_annotation = filter_dict.get(constants.EXCLUDE_ANNOTATION) 791*c2e18aaaSAndroid Build Coastguard Worker exclude_annotation.sort() 792*c2e18aaaSAndroid Build Coastguard Worker self.assertEqual(['exclude1', 'exclude2'], exclude_annotation) 793*c2e18aaaSAndroid Build Coastguard Worker 794*c2e18aaaSAndroid Build Coastguard Worker def test_md5sum_file_existent(self): 795*c2e18aaaSAndroid Build Coastguard Worker """Test method of md5sum for an existent file.""" 796*c2e18aaaSAndroid Build Coastguard Worker with tempfile.NamedTemporaryFile() as tmp_file: 797*c2e18aaaSAndroid Build Coastguard Worker with open(tmp_file.name, 'w', encoding='utf-8') as f: 798*c2e18aaaSAndroid Build Coastguard Worker f.write('some context') 799*c2e18aaaSAndroid Build Coastguard Worker expected_md5 = '6d583707b0149c07cc19a05f5fdc320c' 800*c2e18aaaSAndroid Build Coastguard Worker 801*c2e18aaaSAndroid Build Coastguard Worker actual_md5 = atest_utils.md5sum(tmp_file.name) 802*c2e18aaaSAndroid Build Coastguard Worker 803*c2e18aaaSAndroid Build Coastguard Worker self.assertEqual(actual_md5, expected_md5) 804*c2e18aaaSAndroid Build Coastguard Worker 805*c2e18aaaSAndroid Build Coastguard Worker def test_md5sum_file_inexistent(self): 806*c2e18aaaSAndroid Build Coastguard Worker """Test method of md5sum for an inexistent file.""" 807*c2e18aaaSAndroid Build Coastguard Worker inexistent_file = os.path.join('/somewhere/does/not/exist') 808*c2e18aaaSAndroid Build Coastguard Worker expected_md5 = '' 809*c2e18aaaSAndroid Build Coastguard Worker 810*c2e18aaaSAndroid Build Coastguard Worker actual_md5 = atest_utils.md5sum(inexistent_file) 811*c2e18aaaSAndroid Build Coastguard Worker 812*c2e18aaaSAndroid Build Coastguard Worker self.assertEqual(actual_md5, expected_md5) 813*c2e18aaaSAndroid Build Coastguard Worker 814*c2e18aaaSAndroid Build Coastguard Worker def test_check_md5(self): 815*c2e18aaaSAndroid Build Coastguard Worker """Test method of check_md5""" 816*c2e18aaaSAndroid Build Coastguard Worker file1 = os.path.join( 817*c2e18aaaSAndroid Build Coastguard Worker unittest_constants.TEST_DATA_DIR, unittest_constants.JSON_FILE 818*c2e18aaaSAndroid Build Coastguard Worker ) 819*c2e18aaaSAndroid Build Coastguard Worker checksum_file = '/tmp/_tmp_module-info.json' 820*c2e18aaaSAndroid Build Coastguard Worker atest_utils.save_md5([file1], '/tmp/_tmp_module-info.json') 821*c2e18aaaSAndroid Build Coastguard Worker self.assertTrue(atest_utils.check_md5(checksum_file)) 822*c2e18aaaSAndroid Build Coastguard Worker os.remove(checksum_file) 823*c2e18aaaSAndroid Build Coastguard Worker self.assertFalse(atest_utils.check_md5(checksum_file)) 824*c2e18aaaSAndroid Build Coastguard Worker self.assertTrue(atest_utils.check_md5(checksum_file, missing_ok=True)) 825*c2e18aaaSAndroid Build Coastguard Worker 826*c2e18aaaSAndroid Build Coastguard Worker def test_get_config_parameter(self): 827*c2e18aaaSAndroid Build Coastguard Worker """Test method of get_config_parameter""" 828*c2e18aaaSAndroid Build Coastguard Worker parameter_config = os.path.join( 829*c2e18aaaSAndroid Build Coastguard Worker unittest_constants.TEST_DATA_DIR, 'parameter_config', 'parameter.cfg' 830*c2e18aaaSAndroid Build Coastguard Worker ) 831*c2e18aaaSAndroid Build Coastguard Worker no_parameter_config = os.path.join( 832*c2e18aaaSAndroid Build Coastguard Worker unittest_constants.TEST_DATA_DIR, 'parameter_config', 'no_parameter.cfg' 833*c2e18aaaSAndroid Build Coastguard Worker ) 834*c2e18aaaSAndroid Build Coastguard Worker 835*c2e18aaaSAndroid Build Coastguard Worker # Test parameter empty value 836*c2e18aaaSAndroid Build Coastguard Worker self.assertEqual( 837*c2e18aaaSAndroid Build Coastguard Worker set(), atest_utils.get_config_parameter(no_parameter_config) 838*c2e18aaaSAndroid Build Coastguard Worker ) 839*c2e18aaaSAndroid Build Coastguard Worker 840*c2e18aaaSAndroid Build Coastguard Worker # Test parameter empty value 841*c2e18aaaSAndroid Build Coastguard Worker self.assertEqual( 842*c2e18aaaSAndroid Build Coastguard Worker {'value_1', 'value_2', 'value_3', 'value_4'}, 843*c2e18aaaSAndroid Build Coastguard Worker atest_utils.get_config_parameter(parameter_config), 844*c2e18aaaSAndroid Build Coastguard Worker ) 845*c2e18aaaSAndroid Build Coastguard Worker 846*c2e18aaaSAndroid Build Coastguard Worker def test_get_config_device(self): 847*c2e18aaaSAndroid Build Coastguard Worker """Test method of get_config_device""" 848*c2e18aaaSAndroid Build Coastguard Worker device_config = os.path.join( 849*c2e18aaaSAndroid Build Coastguard Worker unittest_constants.TEST_DATA_DIR, 850*c2e18aaaSAndroid Build Coastguard Worker 'parameter_config', 851*c2e18aaaSAndroid Build Coastguard Worker 'multiple_device.cfg', 852*c2e18aaaSAndroid Build Coastguard Worker ) 853*c2e18aaaSAndroid Build Coastguard Worker self.assertEqual( 854*c2e18aaaSAndroid Build Coastguard Worker {'device_1', 'device_2'}, atest_utils.get_config_device(device_config) 855*c2e18aaaSAndroid Build Coastguard Worker ) 856*c2e18aaaSAndroid Build Coastguard Worker 857*c2e18aaaSAndroid Build Coastguard Worker def test_get_mainline_param(self): 858*c2e18aaaSAndroid Build Coastguard Worker """Test method of get_mainline_param""" 859*c2e18aaaSAndroid Build Coastguard Worker mainline_param_config = os.path.join( 860*c2e18aaaSAndroid Build Coastguard Worker unittest_constants.TEST_DATA_DIR, 861*c2e18aaaSAndroid Build Coastguard Worker 'parameter_config', 862*c2e18aaaSAndroid Build Coastguard Worker 'mainline_param.cfg', 863*c2e18aaaSAndroid Build Coastguard Worker ) 864*c2e18aaaSAndroid Build Coastguard Worker self.assertEqual( 865*c2e18aaaSAndroid Build Coastguard Worker {'foo1.apex', 'foo2.apk+foo3.apk'}, 866*c2e18aaaSAndroid Build Coastguard Worker atest_utils.get_mainline_param(mainline_param_config), 867*c2e18aaaSAndroid Build Coastguard Worker ) 868*c2e18aaaSAndroid Build Coastguard Worker no_mainline_param_config = os.path.join( 869*c2e18aaaSAndroid Build Coastguard Worker unittest_constants.TEST_DATA_DIR, 'parameter_config', 'parameter.cfg' 870*c2e18aaaSAndroid Build Coastguard Worker ) 871*c2e18aaaSAndroid Build Coastguard Worker self.assertEqual( 872*c2e18aaaSAndroid Build Coastguard Worker set(), atest_utils.get_mainline_param(no_mainline_param_config) 873*c2e18aaaSAndroid Build Coastguard Worker ) 874*c2e18aaaSAndroid Build Coastguard Worker 875*c2e18aaaSAndroid Build Coastguard Worker def test_get_full_annotation_class_name(self): 876*c2e18aaaSAndroid Build Coastguard Worker """Test method of get_full_annotation_class_name.""" 877*c2e18aaaSAndroid Build Coastguard Worker app_mode_full = 'android.platform.test.annotations.AppModeFull' 878*c2e18aaaSAndroid Build Coastguard Worker presubmit = 'android.platform.test.annotations.Presubmit' 879*c2e18aaaSAndroid Build Coastguard Worker module_info = { 880*c2e18aaaSAndroid Build Coastguard Worker 'srcs': [ 881*c2e18aaaSAndroid Build Coastguard Worker os.path.join( 882*c2e18aaaSAndroid Build Coastguard Worker unittest_constants.TEST_DATA_DIR, 883*c2e18aaaSAndroid Build Coastguard Worker 'annotation_testing', 884*c2e18aaaSAndroid Build Coastguard Worker 'Annotation.src', 885*c2e18aaaSAndroid Build Coastguard Worker ) 886*c2e18aaaSAndroid Build Coastguard Worker ] 887*c2e18aaaSAndroid Build Coastguard Worker } 888*c2e18aaaSAndroid Build Coastguard Worker # get annotation class from keyword 889*c2e18aaaSAndroid Build Coastguard Worker self.assertEqual( 890*c2e18aaaSAndroid Build Coastguard Worker atest_utils.get_full_annotation_class_name(module_info, 'presubmit'), 891*c2e18aaaSAndroid Build Coastguard Worker presubmit, 892*c2e18aaaSAndroid Build Coastguard Worker ) 893*c2e18aaaSAndroid Build Coastguard Worker # get annotation class from an accurate fqcn keyword. 894*c2e18aaaSAndroid Build Coastguard Worker self.assertEqual( 895*c2e18aaaSAndroid Build Coastguard Worker atest_utils.get_full_annotation_class_name(module_info, presubmit), 896*c2e18aaaSAndroid Build Coastguard Worker presubmit, 897*c2e18aaaSAndroid Build Coastguard Worker ) 898*c2e18aaaSAndroid Build Coastguard Worker # accept fqcn keyword in lowercase. 899*c2e18aaaSAndroid Build Coastguard Worker self.assertEqual( 900*c2e18aaaSAndroid Build Coastguard Worker atest_utils.get_full_annotation_class_name( 901*c2e18aaaSAndroid Build Coastguard Worker module_info, 'android.platform.test.annotations.presubmit' 902*c2e18aaaSAndroid Build Coastguard Worker ), 903*c2e18aaaSAndroid Build Coastguard Worker presubmit, 904*c2e18aaaSAndroid Build Coastguard Worker ) 905*c2e18aaaSAndroid Build Coastguard Worker # unable to get annotation class from keyword. 906*c2e18aaaSAndroid Build Coastguard Worker self.assertNotEqual( 907*c2e18aaaSAndroid Build Coastguard Worker atest_utils.get_full_annotation_class_name( 908*c2e18aaaSAndroid Build Coastguard Worker module_info, 'appleModefull' 909*c2e18aaaSAndroid Build Coastguard Worker ), 910*c2e18aaaSAndroid Build Coastguard Worker app_mode_full, 911*c2e18aaaSAndroid Build Coastguard Worker ) 912*c2e18aaaSAndroid Build Coastguard Worker # do not support partial-correct keyword. 913*c2e18aaaSAndroid Build Coastguard Worker self.assertNotEqual( 914*c2e18aaaSAndroid Build Coastguard Worker atest_utils.get_full_annotation_class_name( 915*c2e18aaaSAndroid Build Coastguard Worker module_info, 'android.platform.test.annotations.pres' 916*c2e18aaaSAndroid Build Coastguard Worker ), 917*c2e18aaaSAndroid Build Coastguard Worker presubmit, 918*c2e18aaaSAndroid Build Coastguard Worker ) 919*c2e18aaaSAndroid Build Coastguard Worker 920*c2e18aaaSAndroid Build Coastguard Worker def test_has_mixed_type_filters_one_module_with_one_type_return_false(self): 921*c2e18aaaSAndroid Build Coastguard Worker """Test method of has_mixed_type_filters""" 922*c2e18aaaSAndroid Build Coastguard Worker filter_1 = test_info.TestFilter('CLASS', frozenset(['METHOD'])) 923*c2e18aaaSAndroid Build Coastguard Worker test_data_1 = {constants.TI_FILTER: [filter_1]} 924*c2e18aaaSAndroid Build Coastguard Worker test_info_1 = test_info.TestInfo( 925*c2e18aaaSAndroid Build Coastguard Worker 'MODULE', 'RUNNER', set(), test_data_1, 'SUITE', '', set() 926*c2e18aaaSAndroid Build Coastguard Worker ) 927*c2e18aaaSAndroid Build Coastguard Worker self.assertFalse(atest_utils.has_mixed_type_filters([test_info_1])) 928*c2e18aaaSAndroid Build Coastguard Worker 929*c2e18aaaSAndroid Build Coastguard Worker def test_has_mixed_type_filters_one_module_with_mixed_types_return_true(self): 930*c2e18aaaSAndroid Build Coastguard Worker """Test method of has_mixed_type_filters""" 931*c2e18aaaSAndroid Build Coastguard Worker filter_1 = test_info.TestFilter('CLASS', frozenset(['METHOD'])) 932*c2e18aaaSAndroid Build Coastguard Worker filter_2 = test_info.TestFilter('CLASS', frozenset(['METHOD*'])) 933*c2e18aaaSAndroid Build Coastguard Worker test_data_2 = {constants.TI_FILTER: [filter_1, filter_2]} 934*c2e18aaaSAndroid Build Coastguard Worker test_info_2 = test_info.TestInfo( 935*c2e18aaaSAndroid Build Coastguard Worker 'MODULE', 'RUNNER', set(), test_data_2, 'SUITE', '', set() 936*c2e18aaaSAndroid Build Coastguard Worker ) 937*c2e18aaaSAndroid Build Coastguard Worker self.assertTrue(atest_utils.has_mixed_type_filters([test_info_2])) 938*c2e18aaaSAndroid Build Coastguard Worker 939*c2e18aaaSAndroid Build Coastguard Worker def test_has_mixed_type_filters_two_module_with_mixed_types_return_false( 940*c2e18aaaSAndroid Build Coastguard Worker self, 941*c2e18aaaSAndroid Build Coastguard Worker ): 942*c2e18aaaSAndroid Build Coastguard Worker """Test method of has_mixed_type_filters""" 943*c2e18aaaSAndroid Build Coastguard Worker filter_1 = test_info.TestFilter('CLASS', frozenset(['METHOD'])) 944*c2e18aaaSAndroid Build Coastguard Worker test_data_1 = {constants.TI_FILTER: [filter_1]} 945*c2e18aaaSAndroid Build Coastguard Worker test_info_1 = test_info.TestInfo( 946*c2e18aaaSAndroid Build Coastguard Worker 'MODULE', 'RUNNER', set(), test_data_1, 'SUITE', '', set() 947*c2e18aaaSAndroid Build Coastguard Worker ) 948*c2e18aaaSAndroid Build Coastguard Worker filter_3 = test_info.TestFilter('CLASS', frozenset(['METHOD*'])) 949*c2e18aaaSAndroid Build Coastguard Worker test_data_3 = {constants.TI_FILTER: [filter_3]} 950*c2e18aaaSAndroid Build Coastguard Worker test_info_3 = test_info.TestInfo( 951*c2e18aaaSAndroid Build Coastguard Worker 'MODULE3', 'RUNNER', set(), test_data_3, 'SUITE', '', set() 952*c2e18aaaSAndroid Build Coastguard Worker ) 953*c2e18aaaSAndroid Build Coastguard Worker self.assertFalse( 954*c2e18aaaSAndroid Build Coastguard Worker atest_utils.has_mixed_type_filters([test_info_1, test_info_3]) 955*c2e18aaaSAndroid Build Coastguard Worker ) 956*c2e18aaaSAndroid Build Coastguard Worker 957*c2e18aaaSAndroid Build Coastguard Worker def test_get_filter_types(self): 958*c2e18aaaSAndroid Build Coastguard Worker """Test method of get_filter_types.""" 959*c2e18aaaSAndroid Build Coastguard Worker filters = set(['CLASS#METHOD']) 960*c2e18aaaSAndroid Build Coastguard Worker expect_types = set([FilterType.REGULAR_FILTER.value]) 961*c2e18aaaSAndroid Build Coastguard Worker self.assertEqual(atest_utils.get_filter_types(filters), expect_types) 962*c2e18aaaSAndroid Build Coastguard Worker 963*c2e18aaaSAndroid Build Coastguard Worker filters = set(['CLASS#METHOD*']) 964*c2e18aaaSAndroid Build Coastguard Worker expect_types = set([FilterType.WILDCARD_FILTER.value]) 965*c2e18aaaSAndroid Build Coastguard Worker self.assertEqual(atest_utils.get_filter_types(filters), expect_types) 966*c2e18aaaSAndroid Build Coastguard Worker 967*c2e18aaaSAndroid Build Coastguard Worker filters = set(['CLASS#METHOD', 'CLASS#METHOD*']) 968*c2e18aaaSAndroid Build Coastguard Worker expect_types = set( 969*c2e18aaaSAndroid Build Coastguard Worker [FilterType.WILDCARD_FILTER.value, FilterType.REGULAR_FILTER.value] 970*c2e18aaaSAndroid Build Coastguard Worker ) 971*c2e18aaaSAndroid Build Coastguard Worker self.assertEqual(atest_utils.get_filter_types(filters), expect_types) 972*c2e18aaaSAndroid Build Coastguard Worker 973*c2e18aaaSAndroid Build Coastguard Worker filters = set(['CLASS#METHOD?', 'CLASS#METHOD*']) 974*c2e18aaaSAndroid Build Coastguard Worker expect_types = set([FilterType.WILDCARD_FILTER.value]) 975*c2e18aaaSAndroid Build Coastguard Worker self.assertEqual(atest_utils.get_filter_types(filters), expect_types) 976*c2e18aaaSAndroid Build Coastguard Worker 977*c2e18aaaSAndroid Build Coastguard Worker def test_get_bp_content(self): 978*c2e18aaaSAndroid Build Coastguard Worker """Method get_bp_content.""" 979*c2e18aaaSAndroid Build Coastguard Worker # 1. "manifest" and "instrumentation_for" are defined. 980*c2e18aaaSAndroid Build Coastguard Worker content = """android_test { 981*c2e18aaaSAndroid Build Coastguard Worker // comment 982*c2e18aaaSAndroid Build Coastguard Worker instrumentation_for: "AmSlam", // comment 983*c2e18aaaSAndroid Build Coastguard Worker manifest: "AndroidManifest-test.xml", 984*c2e18aaaSAndroid Build Coastguard Worker name: "AmSlamTests", 985*c2e18aaaSAndroid Build Coastguard Worker }""" 986*c2e18aaaSAndroid Build Coastguard Worker expected_result = { 987*c2e18aaaSAndroid Build Coastguard Worker 'AmSlamTests': { 988*c2e18aaaSAndroid Build Coastguard Worker 'target_module': 'AmSlam', 989*c2e18aaaSAndroid Build Coastguard Worker 'manifest': 'AndroidManifest-test.xml', 990*c2e18aaaSAndroid Build Coastguard Worker } 991*c2e18aaaSAndroid Build Coastguard Worker } 992*c2e18aaaSAndroid Build Coastguard Worker temp_dir = tempfile.TemporaryDirectory() 993*c2e18aaaSAndroid Build Coastguard Worker tmpbp = Path(temp_dir.name).joinpath('Android.bp') 994*c2e18aaaSAndroid Build Coastguard Worker with open(tmpbp, 'w') as cache: 995*c2e18aaaSAndroid Build Coastguard Worker cache.write(content) 996*c2e18aaaSAndroid Build Coastguard Worker self.assertEqual( 997*c2e18aaaSAndroid Build Coastguard Worker atest_utils.get_bp_content(tmpbp, 'android_test'), expected_result 998*c2e18aaaSAndroid Build Coastguard Worker ) 999*c2e18aaaSAndroid Build Coastguard Worker temp_dir.cleanup() 1000*c2e18aaaSAndroid Build Coastguard Worker 1001*c2e18aaaSAndroid Build Coastguard Worker # 2. Only name is defined, will give default manifest and null 1002*c2e18aaaSAndroid Build Coastguard Worker # target_module. 1003*c2e18aaaSAndroid Build Coastguard Worker content = """android_app { 1004*c2e18aaaSAndroid Build Coastguard Worker // comment 1005*c2e18aaaSAndroid Build Coastguard Worker name: "AmSlam", 1006*c2e18aaaSAndroid Build Coastguard Worker srcs: ["src1.java", "src2.java"] 1007*c2e18aaaSAndroid Build Coastguard Worker }""" 1008*c2e18aaaSAndroid Build Coastguard Worker expected_result = { 1009*c2e18aaaSAndroid Build Coastguard Worker 'AmSlam': {'target_module': '', 'manifest': 'AndroidManifest.xml'} 1010*c2e18aaaSAndroid Build Coastguard Worker } 1011*c2e18aaaSAndroid Build Coastguard Worker temp_dir = tempfile.TemporaryDirectory() 1012*c2e18aaaSAndroid Build Coastguard Worker tmpbp = Path(temp_dir.name).joinpath('Android.bp') 1013*c2e18aaaSAndroid Build Coastguard Worker with open(tmpbp, 'w') as cache: 1014*c2e18aaaSAndroid Build Coastguard Worker cache.write(content) 1015*c2e18aaaSAndroid Build Coastguard Worker self.assertEqual( 1016*c2e18aaaSAndroid Build Coastguard Worker atest_utils.get_bp_content(tmpbp, 'android_app'), expected_result 1017*c2e18aaaSAndroid Build Coastguard Worker ) 1018*c2e18aaaSAndroid Build Coastguard Worker temp_dir.cleanup() 1019*c2e18aaaSAndroid Build Coastguard Worker 1020*c2e18aaaSAndroid Build Coastguard Worker # 3. Not even an Android.bp. 1021*c2e18aaaSAndroid Build Coastguard Worker content = """LOCAL_PATH := $(call my-dir) 1022*c2e18aaaSAndroid Build Coastguard Worker # comment 1023*c2e18aaaSAndroid Build Coastguard Worker include $(call all-subdir-makefiles) 1024*c2e18aaaSAndroid Build Coastguard Worker LOCAL_MODULE := atest_foo_test 1025*c2e18aaaSAndroid Build Coastguard Worker }""" 1026*c2e18aaaSAndroid Build Coastguard Worker temp_dir = tempfile.TemporaryDirectory() 1027*c2e18aaaSAndroid Build Coastguard Worker tmpbp = Path(temp_dir.name).joinpath('Android.mk') 1028*c2e18aaaSAndroid Build Coastguard Worker with open(tmpbp, 'w') as cache: 1029*c2e18aaaSAndroid Build Coastguard Worker cache.write(content) 1030*c2e18aaaSAndroid Build Coastguard Worker self.assertEqual(atest_utils.get_bp_content(tmpbp, 'android_app'), {}) 1031*c2e18aaaSAndroid Build Coastguard Worker temp_dir.cleanup() 1032*c2e18aaaSAndroid Build Coastguard Worker 1033*c2e18aaaSAndroid Build Coastguard Worker def test_get_manifest_info(self): 1034*c2e18aaaSAndroid Build Coastguard Worker """test get_manifest_info method.""" 1035*c2e18aaaSAndroid Build Coastguard Worker # An instrumentation test: 1036*c2e18aaaSAndroid Build Coastguard Worker test_xml = os.path.join( 1037*c2e18aaaSAndroid Build Coastguard Worker unittest_constants.TEST_DATA_DIR, 1038*c2e18aaaSAndroid Build Coastguard Worker 'foo/bar/AmSlam/test/AndroidManifest.xml', 1039*c2e18aaaSAndroid Build Coastguard Worker ) 1040*c2e18aaaSAndroid Build Coastguard Worker expected = { 1041*c2e18aaaSAndroid Build Coastguard Worker 'package': 'com.android.settings.tests.unit', 1042*c2e18aaaSAndroid Build Coastguard Worker 'target_package': 'c0m.andr0id.settingS', 1043*c2e18aaaSAndroid Build Coastguard Worker 'persistent': False, 1044*c2e18aaaSAndroid Build Coastguard Worker } 1045*c2e18aaaSAndroid Build Coastguard Worker self.assertEqual(expected, atest_utils.get_manifest_info(test_xml)) 1046*c2e18aaaSAndroid Build Coastguard Worker 1047*c2e18aaaSAndroid Build Coastguard Worker # A target module: 1048*c2e18aaaSAndroid Build Coastguard Worker target_xml = os.path.join( 1049*c2e18aaaSAndroid Build Coastguard Worker unittest_constants.TEST_DATA_DIR, 'foo/bar/AmSlam/AndroidManifest.xml' 1050*c2e18aaaSAndroid Build Coastguard Worker ) 1051*c2e18aaaSAndroid Build Coastguard Worker expected = { 1052*c2e18aaaSAndroid Build Coastguard Worker 'package': 'c0m.andr0id.settingS', 1053*c2e18aaaSAndroid Build Coastguard Worker 'target_package': '', 1054*c2e18aaaSAndroid Build Coastguard Worker 'persistent': False, 1055*c2e18aaaSAndroid Build Coastguard Worker } 1056*c2e18aaaSAndroid Build Coastguard Worker self.assertEqual(expected, atest_utils.get_manifest_info(target_xml)) 1057*c2e18aaaSAndroid Build Coastguard Worker 1058*c2e18aaaSAndroid Build Coastguard Worker 1059*c2e18aaaSAndroid Build Coastguard Workerclass GetTradefedInvocationTimeTest(fake_filesystem_unittest.TestCase): 1060*c2e18aaaSAndroid Build Coastguard Worker """Tests of get_tradefed_invocation_time for various conditions.""" 1061*c2e18aaaSAndroid Build Coastguard Worker 1062*c2e18aaaSAndroid Build Coastguard Worker def setUp(self): 1063*c2e18aaaSAndroid Build Coastguard Worker self.setUpPyfakefs() 1064*c2e18aaaSAndroid Build Coastguard Worker self.log_path = '/somewhere/atest/log' 1065*c2e18aaaSAndroid Build Coastguard Worker 1066*c2e18aaaSAndroid Build Coastguard Worker def test_get_tradefed_invocation_time_second_only(self): 1067*c2e18aaaSAndroid Build Coastguard Worker """Test the parser can handle second and millisecond properly.""" 1068*c2e18aaaSAndroid Build Coastguard Worker end_host_log_file = Path( 1069*c2e18aaaSAndroid Build Coastguard Worker self.log_path, 1070*c2e18aaaSAndroid Build Coastguard Worker 'inv_hashed_path', 1071*c2e18aaaSAndroid Build Coastguard Worker 'inv_hashed_subpath', 1072*c2e18aaaSAndroid Build Coastguard Worker 'end_host_log_test1.txt', 1073*c2e18aaaSAndroid Build Coastguard Worker ) 1074*c2e18aaaSAndroid Build Coastguard Worker contents = """ 1075*c2e18aaaSAndroid Build Coastguard Worker=============== Consumed Time ============== 1076*c2e18aaaSAndroid Build Coastguard Worker x86_64 HelloWorldTests: 1s 1077*c2e18aaaSAndroid Build Coastguard Worker x86_64 hallo-welt: 768 ms 1078*c2e18aaaSAndroid Build Coastguard WorkerTotal aggregated tests run time: 1s 1079*c2e18aaaSAndroid Build Coastguard Worker============== Modules Preparation Times ============== 1080*c2e18aaaSAndroid Build Coastguard Worker x86_64 HelloWorldTests => prep = 2580 ms || clean = 298 ms 1081*c2e18aaaSAndroid Build Coastguard Worker x86_64 hallo-welt => prep = 1736 ms || clean = 243 ms 1082*c2e18aaaSAndroid Build Coastguard WorkerTotal preparation time: 4s || Total tear down time: 541 ms 1083*c2e18aaaSAndroid Build Coastguard Worker======================================================= 1084*c2e18aaaSAndroid Build Coastguard Worker=============== Summary =============== 1085*c2e18aaaSAndroid Build Coastguard WorkerTotal Run time: 6s 1086*c2e18aaaSAndroid Build Coastguard Worker2/2 modules completed 1087*c2e18aaaSAndroid Build Coastguard WorkerTotal Tests : 3 1088*c2e18aaaSAndroid Build Coastguard WorkerPASSED : 3 1089*c2e18aaaSAndroid Build Coastguard WorkerFAILED : 0 1090*c2e18aaaSAndroid Build Coastguard Worker============== End of Results ==============""" 1091*c2e18aaaSAndroid Build Coastguard Worker self.fs.create_file(end_host_log_file, contents=contents) 1092*c2e18aaaSAndroid Build Coastguard Worker test = 1 * 1000 + 768 1093*c2e18aaaSAndroid Build Coastguard Worker prep = 2580 + 1736 1094*c2e18aaaSAndroid Build Coastguard Worker teardown = 298 + 243 1095*c2e18aaaSAndroid Build Coastguard Worker expected_elapsed_time = (test, prep, teardown) 1096*c2e18aaaSAndroid Build Coastguard Worker 1097*c2e18aaaSAndroid Build Coastguard Worker actual_elapsed_time = atest_utils.get_tradefed_invocation_time( 1098*c2e18aaaSAndroid Build Coastguard Worker self.log_path 1099*c2e18aaaSAndroid Build Coastguard Worker ) 1100*c2e18aaaSAndroid Build Coastguard Worker 1101*c2e18aaaSAndroid Build Coastguard Worker self.assertEqual(actual_elapsed_time, expected_elapsed_time) 1102*c2e18aaaSAndroid Build Coastguard Worker 1103*c2e18aaaSAndroid Build Coastguard Worker def test_get_tradefed_invocation_time_from_hours_to_milliseconds(self): 1104*c2e18aaaSAndroid Build Coastguard Worker """Test whether the parse can handle from hour to ms properly.""" 1105*c2e18aaaSAndroid Build Coastguard Worker end_host_log_file = Path( 1106*c2e18aaaSAndroid Build Coastguard Worker self.log_path, 1107*c2e18aaaSAndroid Build Coastguard Worker 'inv_hashed_path', 1108*c2e18aaaSAndroid Build Coastguard Worker 'inv_hashed_subpath', 1109*c2e18aaaSAndroid Build Coastguard Worker 'end_host_log_test2.txt', 1110*c2e18aaaSAndroid Build Coastguard Worker ) 1111*c2e18aaaSAndroid Build Coastguard Worker contents = """ 1112*c2e18aaaSAndroid Build Coastguard Worker=============== Consumed Time ============== 1113*c2e18aaaSAndroid Build Coastguard Worker x86_64 HelloWorldTests: 27m 19s 1114*c2e18aaaSAndroid Build Coastguard Worker x86_64 hallo-welt: 3m 2s 1115*c2e18aaaSAndroid Build Coastguard WorkerTotal aggregated tests run time: 31m 1116*c2e18aaaSAndroid Build Coastguard Worker============== Modules Preparation Times ============== 1117*c2e18aaaSAndroid Build Coastguard Worker x86_64 HelloWorldTests => prep = 2580 ms || clean = 1298 ms 1118*c2e18aaaSAndroid Build Coastguard Worker x86_64 hallo-welt => prep = 1736 ms || clean = 1243 ms 1119*c2e18aaaSAndroid Build Coastguard WorkerTotal preparation time: 1h 24m 17s || Total tear down time: 3s 1120*c2e18aaaSAndroid Build Coastguard Worker======================================================= 1121*c2e18aaaSAndroid Build Coastguard Worker=============== Summary =============== 1122*c2e18aaaSAndroid Build Coastguard WorkerTotal Run time: 2h 5m 17s 1123*c2e18aaaSAndroid Build Coastguard Worker2/2 modules completed 1124*c2e18aaaSAndroid Build Coastguard WorkerTotal Tests : 3 1125*c2e18aaaSAndroid Build Coastguard WorkerPASSED : 3 1126*c2e18aaaSAndroid Build Coastguard WorkerFAILED : 0 1127*c2e18aaaSAndroid Build Coastguard Worker============== End of Results ==============""" 1128*c2e18aaaSAndroid Build Coastguard Worker self.fs.create_file(end_host_log_file, contents=contents) 1129*c2e18aaaSAndroid Build Coastguard Worker test = (27 * 60 + 19) * 1000 + (3 * 60 + 2) * 1000 1130*c2e18aaaSAndroid Build Coastguard Worker prep = 2580 + 1736 1131*c2e18aaaSAndroid Build Coastguard Worker teardown = 1298 + 1243 1132*c2e18aaaSAndroid Build Coastguard Worker expected_elapsed_time = (test, prep, teardown) 1133*c2e18aaaSAndroid Build Coastguard Worker 1134*c2e18aaaSAndroid Build Coastguard Worker actual_elapsed_time = atest_utils.get_tradefed_invocation_time( 1135*c2e18aaaSAndroid Build Coastguard Worker self.log_path 1136*c2e18aaaSAndroid Build Coastguard Worker ) 1137*c2e18aaaSAndroid Build Coastguard Worker 1138*c2e18aaaSAndroid Build Coastguard Worker self.assertEqual(actual_elapsed_time, expected_elapsed_time) 1139*c2e18aaaSAndroid Build Coastguard Worker 1140*c2e18aaaSAndroid Build Coastguard Worker def test_get_tradefed_invocation_time_null_result(self): 1141*c2e18aaaSAndroid Build Coastguard Worker """Test whether the parser returns null tuple when no keywords found.""" 1142*c2e18aaaSAndroid Build Coastguard Worker end_host_log_file = Path( 1143*c2e18aaaSAndroid Build Coastguard Worker self.log_path, 1144*c2e18aaaSAndroid Build Coastguard Worker 'inv_hashed_path', 1145*c2e18aaaSAndroid Build Coastguard Worker 'inv_hashed_subpath', 1146*c2e18aaaSAndroid Build Coastguard Worker 'end_host_log_test4.txt', 1147*c2e18aaaSAndroid Build Coastguard Worker ) 1148*c2e18aaaSAndroid Build Coastguard Worker contents = 'some\ncontext' 1149*c2e18aaaSAndroid Build Coastguard Worker self.fs.create_file(end_host_log_file, contents=contents) 1150*c2e18aaaSAndroid Build Coastguard Worker expected_elapsed_time = (0, 0, 0) 1151*c2e18aaaSAndroid Build Coastguard Worker 1152*c2e18aaaSAndroid Build Coastguard Worker actual_elapsed_time = atest_utils.get_tradefed_invocation_time( 1153*c2e18aaaSAndroid Build Coastguard Worker self.log_path 1154*c2e18aaaSAndroid Build Coastguard Worker ) 1155*c2e18aaaSAndroid Build Coastguard Worker 1156*c2e18aaaSAndroid Build Coastguard Worker self.assertEqual(actual_elapsed_time, expected_elapsed_time) 1157*c2e18aaaSAndroid Build Coastguard Worker 1158*c2e18aaaSAndroid Build Coastguard Worker 1159*c2e18aaaSAndroid Build Coastguard Workerif __name__ == '__main__': 1160*c2e18aaaSAndroid Build Coastguard Worker unittest.main() 1161