xref: /aosp_15_r20/tools/asuite/atest/atest_main_unittest.py (revision c2e18aaa1096c836b086f94603d04f4eb9cf37f5)
1*c2e18aaaSAndroid Build Coastguard Worker#!/usr/bin/env python3
2*c2e18aaaSAndroid Build Coastguard Worker#
3*c2e18aaaSAndroid Build Coastguard Worker# Copyright 2017, 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."""
18*c2e18aaaSAndroid Build Coastguard Worker
19*c2e18aaaSAndroid Build Coastguard Worker# pylint: disable=invalid-name
20*c2e18aaaSAndroid Build Coastguard Worker
21*c2e18aaaSAndroid Build Coastguard Workerimport datetime
22*c2e18aaaSAndroid Build Coastguard Workerfrom importlib import reload
23*c2e18aaaSAndroid Build Coastguard Workerfrom io import StringIO
24*c2e18aaaSAndroid Build Coastguard Workerimport os
25*c2e18aaaSAndroid Build Coastguard Workerimport sys
26*c2e18aaaSAndroid Build Coastguard Workerimport tempfile
27*c2e18aaaSAndroid Build Coastguard Workerimport unittest
28*c2e18aaaSAndroid Build Coastguard Workerfrom unittest import mock
29*c2e18aaaSAndroid Build Coastguard Workerfrom atest import arg_parser
30*c2e18aaaSAndroid Build Coastguard Workerfrom atest import atest_main
31*c2e18aaaSAndroid Build Coastguard Workerfrom atest import atest_utils
32*c2e18aaaSAndroid Build Coastguard Workerfrom atest import constants
33*c2e18aaaSAndroid Build Coastguard Workerfrom atest import module_info
34*c2e18aaaSAndroid Build Coastguard Workerfrom atest.atest_enum import DetectType
35*c2e18aaaSAndroid Build Coastguard Workerfrom atest.metrics import metrics
36*c2e18aaaSAndroid Build Coastguard Workerfrom atest.metrics import metrics_utils
37*c2e18aaaSAndroid Build Coastguard Workerfrom atest.test_finders import test_info
38*c2e18aaaSAndroid Build Coastguard Workerfrom pyfakefs import fake_filesystem_unittest
39*c2e18aaaSAndroid Build Coastguard Worker
40*c2e18aaaSAndroid Build Coastguard WorkerGREEN = '\x1b[1;32m'
41*c2e18aaaSAndroid Build Coastguard WorkerCYAN = '\x1b[1;36m'
42*c2e18aaaSAndroid Build Coastguard WorkerMAGENTA = '\x1b[1;35m'
43*c2e18aaaSAndroid Build Coastguard WorkerEND = '\x1b[0m'
44*c2e18aaaSAndroid Build Coastguard Worker
45*c2e18aaaSAndroid Build Coastguard Worker
46*c2e18aaaSAndroid Build Coastguard Worker# pylint: disable=protected-access
47*c2e18aaaSAndroid Build Coastguard Workerclass AtestUnittests(unittest.TestCase):
48*c2e18aaaSAndroid Build Coastguard Worker  """Unit tests for atest_main.py"""
49*c2e18aaaSAndroid Build Coastguard Worker
50*c2e18aaaSAndroid Build Coastguard Worker  @mock.patch('os.environ.get', return_value=None)
51*c2e18aaaSAndroid Build Coastguard Worker  def test_missing_environment_variables_uninitialized(self, _):
52*c2e18aaaSAndroid Build Coastguard Worker    """Test _has_environment_variables when no env vars."""
53*c2e18aaaSAndroid Build Coastguard Worker    self.assertTrue(atest_main._missing_environment_variables())
54*c2e18aaaSAndroid Build Coastguard Worker
55*c2e18aaaSAndroid Build Coastguard Worker  @mock.patch('os.environ.get', return_value='out/testcases/')
56*c2e18aaaSAndroid Build Coastguard Worker  def test_missing_environment_variables_initialized(self, _):
57*c2e18aaaSAndroid Build Coastguard Worker    """Test _has_environment_variables when env vars."""
58*c2e18aaaSAndroid Build Coastguard Worker    self.assertFalse(atest_main._missing_environment_variables())
59*c2e18aaaSAndroid Build Coastguard Worker
60*c2e18aaaSAndroid Build Coastguard Worker  def test_parse_args(self):
61*c2e18aaaSAndroid Build Coastguard Worker    """Test _parse_args parses command line args."""
62*c2e18aaaSAndroid Build Coastguard Worker    test_one = 'test_name_one'
63*c2e18aaaSAndroid Build Coastguard Worker    test_two = 'test_name_two'
64*c2e18aaaSAndroid Build Coastguard Worker    custom_arg = '--custom_arg'
65*c2e18aaaSAndroid Build Coastguard Worker    custom_arg_val = 'custom_arg_val'
66*c2e18aaaSAndroid Build Coastguard Worker    pos_custom_arg = 'pos_custom_arg'
67*c2e18aaaSAndroid Build Coastguard Worker
68*c2e18aaaSAndroid Build Coastguard Worker    # Test out test and custom args are properly retrieved.
69*c2e18aaaSAndroid Build Coastguard Worker    args = [test_one, test_two, '--', custom_arg, custom_arg_val]
70*c2e18aaaSAndroid Build Coastguard Worker    parsed_args = atest_main._parse_args(args)
71*c2e18aaaSAndroid Build Coastguard Worker    self.assertEqual(parsed_args.tests, [test_one, test_two])
72*c2e18aaaSAndroid Build Coastguard Worker    self.assertEqual(parsed_args.custom_args, [custom_arg, custom_arg_val])
73*c2e18aaaSAndroid Build Coastguard Worker
74*c2e18aaaSAndroid Build Coastguard Worker    # Test out custom positional args with no test args.
75*c2e18aaaSAndroid Build Coastguard Worker    args = ['--', pos_custom_arg, custom_arg_val]
76*c2e18aaaSAndroid Build Coastguard Worker    parsed_args = atest_main._parse_args(args)
77*c2e18aaaSAndroid Build Coastguard Worker    self.assertEqual(parsed_args.tests, [])
78*c2e18aaaSAndroid Build Coastguard Worker    self.assertEqual(parsed_args.custom_args, [pos_custom_arg, custom_arg_val])
79*c2e18aaaSAndroid Build Coastguard Worker
80*c2e18aaaSAndroid Build Coastguard Worker  def test_has_valid_test_mapping_args(self):
81*c2e18aaaSAndroid Build Coastguard Worker    """Test _has_valid_test_mapping_args method."""
82*c2e18aaaSAndroid Build Coastguard Worker    # Test test mapping related args are not mixed with incompatible args.
83*c2e18aaaSAndroid Build Coastguard Worker    options_no_tm_support = [
84*c2e18aaaSAndroid Build Coastguard Worker        (
85*c2e18aaaSAndroid Build Coastguard Worker            '--annotation-filter',
86*c2e18aaaSAndroid Build Coastguard Worker            'androidx.test.filters.SmallTest',
87*c2e18aaaSAndroid Build Coastguard Worker        ),
88*c2e18aaaSAndroid Build Coastguard Worker    ]
89*c2e18aaaSAndroid Build Coastguard Worker    tm_options = ['--test-mapping', '--include-subdirs']
90*c2e18aaaSAndroid Build Coastguard Worker
91*c2e18aaaSAndroid Build Coastguard Worker    for tm_option in tm_options:
92*c2e18aaaSAndroid Build Coastguard Worker      for no_tm_option, no_tm_option_value in options_no_tm_support:
93*c2e18aaaSAndroid Build Coastguard Worker        args = [tm_option, no_tm_option]
94*c2e18aaaSAndroid Build Coastguard Worker        if no_tm_option_value is not None:
95*c2e18aaaSAndroid Build Coastguard Worker          args.append(no_tm_option_value)
96*c2e18aaaSAndroid Build Coastguard Worker        parsed_args = atest_main._parse_args(args)
97*c2e18aaaSAndroid Build Coastguard Worker        self.assertFalse(
98*c2e18aaaSAndroid Build Coastguard Worker            atest_main._has_valid_test_mapping_args(parsed_args),
99*c2e18aaaSAndroid Build Coastguard Worker            'Failed to validate: %s' % args,
100*c2e18aaaSAndroid Build Coastguard Worker        )
101*c2e18aaaSAndroid Build Coastguard Worker
102*c2e18aaaSAndroid Build Coastguard Worker  @mock.patch.object(atest_utils, 'get_adb_devices')
103*c2e18aaaSAndroid Build Coastguard Worker  @mock.patch.object(metrics_utils, 'send_exit_event')
104*c2e18aaaSAndroid Build Coastguard Worker  def test_validate_exec_mode(self, _send_exit, _devs):
105*c2e18aaaSAndroid Build Coastguard Worker    """Test _validate_exec_mode."""
106*c2e18aaaSAndroid Build Coastguard Worker    _devs.return_value = ['127.0.0.1:34556']
107*c2e18aaaSAndroid Build Coastguard Worker    args = []
108*c2e18aaaSAndroid Build Coastguard Worker    no_install_test_info = test_info.TestInfo(
109*c2e18aaaSAndroid Build Coastguard Worker        'mod',
110*c2e18aaaSAndroid Build Coastguard Worker        '',
111*c2e18aaaSAndroid Build Coastguard Worker        set(),
112*c2e18aaaSAndroid Build Coastguard Worker        data={},
113*c2e18aaaSAndroid Build Coastguard Worker        module_class=['JAVA_LIBRARIES'],
114*c2e18aaaSAndroid Build Coastguard Worker        install_locations=set(['device']),
115*c2e18aaaSAndroid Build Coastguard Worker    )
116*c2e18aaaSAndroid Build Coastguard Worker    host_test_info = test_info.TestInfo(
117*c2e18aaaSAndroid Build Coastguard Worker        'mod',
118*c2e18aaaSAndroid Build Coastguard Worker        '',
119*c2e18aaaSAndroid Build Coastguard Worker        set(),
120*c2e18aaaSAndroid Build Coastguard Worker        data={},
121*c2e18aaaSAndroid Build Coastguard Worker        module_class=['NATIVE_TESTS'],
122*c2e18aaaSAndroid Build Coastguard Worker        install_locations=set(['host']),
123*c2e18aaaSAndroid Build Coastguard Worker    )
124*c2e18aaaSAndroid Build Coastguard Worker    device_test_info = test_info.TestInfo(
125*c2e18aaaSAndroid Build Coastguard Worker        'mod',
126*c2e18aaaSAndroid Build Coastguard Worker        '',
127*c2e18aaaSAndroid Build Coastguard Worker        set(),
128*c2e18aaaSAndroid Build Coastguard Worker        data={},
129*c2e18aaaSAndroid Build Coastguard Worker        module_class=['NATIVE_TESTS'],
130*c2e18aaaSAndroid Build Coastguard Worker        install_locations=set(['device']),
131*c2e18aaaSAndroid Build Coastguard Worker    )
132*c2e18aaaSAndroid Build Coastguard Worker    both_test_info = test_info.TestInfo(
133*c2e18aaaSAndroid Build Coastguard Worker        'mod',
134*c2e18aaaSAndroid Build Coastguard Worker        '',
135*c2e18aaaSAndroid Build Coastguard Worker        set(),
136*c2e18aaaSAndroid Build Coastguard Worker        data={},
137*c2e18aaaSAndroid Build Coastguard Worker        module_class=['NATIVE_TESTS'],
138*c2e18aaaSAndroid Build Coastguard Worker        install_locations=set(['host', 'device']),
139*c2e18aaaSAndroid Build Coastguard Worker    )
140*c2e18aaaSAndroid Build Coastguard Worker
141*c2e18aaaSAndroid Build Coastguard Worker    # $atest <Both-support>
142*c2e18aaaSAndroid Build Coastguard Worker    parsed_args = atest_main._parse_args(args)
143*c2e18aaaSAndroid Build Coastguard Worker    test_infos = [host_test_info]
144*c2e18aaaSAndroid Build Coastguard Worker    atest_main._validate_exec_mode(parsed_args, test_infos)
145*c2e18aaaSAndroid Build Coastguard Worker    self.assertFalse(parsed_args.host)
146*c2e18aaaSAndroid Build Coastguard Worker
147*c2e18aaaSAndroid Build Coastguard Worker    # $atest <Both-support> with host_tests set to True
148*c2e18aaaSAndroid Build Coastguard Worker    parsed_args = atest_main._parse_args([])
149*c2e18aaaSAndroid Build Coastguard Worker    test_infos = [host_test_info]
150*c2e18aaaSAndroid Build Coastguard Worker    atest_main._validate_exec_mode(parsed_args, test_infos, host_tests=True)
151*c2e18aaaSAndroid Build Coastguard Worker    # Make sure the host option is not set.
152*c2e18aaaSAndroid Build Coastguard Worker    self.assertFalse(parsed_args.host)
153*c2e18aaaSAndroid Build Coastguard Worker
154*c2e18aaaSAndroid Build Coastguard Worker    # $atest <Both-support> with host_tests set to False
155*c2e18aaaSAndroid Build Coastguard Worker    parsed_args = atest_main._parse_args([])
156*c2e18aaaSAndroid Build Coastguard Worker    test_infos = [host_test_info]
157*c2e18aaaSAndroid Build Coastguard Worker    atest_main._validate_exec_mode(parsed_args, test_infos, host_tests=False)
158*c2e18aaaSAndroid Build Coastguard Worker    self.assertFalse(parsed_args.host)
159*c2e18aaaSAndroid Build Coastguard Worker
160*c2e18aaaSAndroid Build Coastguard Worker    # $atest <device-only> with host_tests set to False
161*c2e18aaaSAndroid Build Coastguard Worker    parsed_args = atest_main._parse_args([])
162*c2e18aaaSAndroid Build Coastguard Worker    test_infos = [device_test_info]
163*c2e18aaaSAndroid Build Coastguard Worker    atest_main._validate_exec_mode(parsed_args, test_infos, host_tests=False)
164*c2e18aaaSAndroid Build Coastguard Worker    # Make sure the host option is not set.
165*c2e18aaaSAndroid Build Coastguard Worker    self.assertFalse(parsed_args.host)
166*c2e18aaaSAndroid Build Coastguard Worker
167*c2e18aaaSAndroid Build Coastguard Worker    # $atest <device-only> with host_tests set to True
168*c2e18aaaSAndroid Build Coastguard Worker    parsed_args = atest_main._parse_args([])
169*c2e18aaaSAndroid Build Coastguard Worker    test_infos = [device_test_info]
170*c2e18aaaSAndroid Build Coastguard Worker    self.assertRaises(
171*c2e18aaaSAndroid Build Coastguard Worker        SystemExit,
172*c2e18aaaSAndroid Build Coastguard Worker        atest_main._validate_exec_mode,
173*c2e18aaaSAndroid Build Coastguard Worker        parsed_args,
174*c2e18aaaSAndroid Build Coastguard Worker        test_infos,
175*c2e18aaaSAndroid Build Coastguard Worker        host_tests=True,
176*c2e18aaaSAndroid Build Coastguard Worker    )
177*c2e18aaaSAndroid Build Coastguard Worker
178*c2e18aaaSAndroid Build Coastguard Worker    # $atest <Both-support>
179*c2e18aaaSAndroid Build Coastguard Worker    parsed_args = atest_main._parse_args([])
180*c2e18aaaSAndroid Build Coastguard Worker    test_infos = [both_test_info]
181*c2e18aaaSAndroid Build Coastguard Worker    atest_main._validate_exec_mode(parsed_args, test_infos)
182*c2e18aaaSAndroid Build Coastguard Worker    self.assertFalse(parsed_args.host)
183*c2e18aaaSAndroid Build Coastguard Worker
184*c2e18aaaSAndroid Build Coastguard Worker    # $atest <no_install_test_info>
185*c2e18aaaSAndroid Build Coastguard Worker    parsed_args = atest_main._parse_args([])
186*c2e18aaaSAndroid Build Coastguard Worker    test_infos = [no_install_test_info]
187*c2e18aaaSAndroid Build Coastguard Worker    atest_main._validate_exec_mode(parsed_args, test_infos)
188*c2e18aaaSAndroid Build Coastguard Worker    self.assertFalse(parsed_args.host)
189*c2e18aaaSAndroid Build Coastguard Worker
190*c2e18aaaSAndroid Build Coastguard Worker  def test_make_test_run_dir(self):
191*c2e18aaaSAndroid Build Coastguard Worker    """Test make_test_run_dir."""
192*c2e18aaaSAndroid Build Coastguard Worker    tmp_dir = tempfile.mkdtemp()
193*c2e18aaaSAndroid Build Coastguard Worker    constants.ATEST_RESULT_ROOT = tmp_dir
194*c2e18aaaSAndroid Build Coastguard Worker    date_time = None
195*c2e18aaaSAndroid Build Coastguard Worker
196*c2e18aaaSAndroid Build Coastguard Worker    work_dir = atest_main.make_test_run_dir()
197*c2e18aaaSAndroid Build Coastguard Worker    folder_name = os.path.basename(work_dir)
198*c2e18aaaSAndroid Build Coastguard Worker    date_time = datetime.datetime.strptime(
199*c2e18aaaSAndroid Build Coastguard Worker        '_'.join(folder_name.split('_')[0:2]), atest_main.TEST_RUN_DIR_PREFIX
200*c2e18aaaSAndroid Build Coastguard Worker    )
201*c2e18aaaSAndroid Build Coastguard Worker    reload(constants)
202*c2e18aaaSAndroid Build Coastguard Worker    self.assertTrue(date_time)
203*c2e18aaaSAndroid Build Coastguard Worker
204*c2e18aaaSAndroid Build Coastguard Worker  def test_has_set_sufficient_devices_no_device_no_require(self):
205*c2e18aaaSAndroid Build Coastguard Worker    required_num = 0
206*c2e18aaaSAndroid Build Coastguard Worker    self.assertTrue(atest_main.has_set_sufficient_devices(required_num))
207*c2e18aaaSAndroid Build Coastguard Worker
208*c2e18aaaSAndroid Build Coastguard Worker  def test_has_set_sufficient_devices_equal_required_attached_devices(self):
209*c2e18aaaSAndroid Build Coastguard Worker    required_num = 2
210*c2e18aaaSAndroid Build Coastguard Worker    attached_devices = ['serial1', 'serial2']
211*c2e18aaaSAndroid Build Coastguard Worker
212*c2e18aaaSAndroid Build Coastguard Worker    self.assertTrue(
213*c2e18aaaSAndroid Build Coastguard Worker        atest_main.has_set_sufficient_devices(required_num, attached_devices)
214*c2e18aaaSAndroid Build Coastguard Worker    )
215*c2e18aaaSAndroid Build Coastguard Worker
216*c2e18aaaSAndroid Build Coastguard Worker  def test_has_set_sufficient_devices_attached_devices_more_than_required(self):
217*c2e18aaaSAndroid Build Coastguard Worker    required_num = 2
218*c2e18aaaSAndroid Build Coastguard Worker    attached_devices = ['serial1', 'serial2', 'serial3']
219*c2e18aaaSAndroid Build Coastguard Worker
220*c2e18aaaSAndroid Build Coastguard Worker    self.assertTrue(
221*c2e18aaaSAndroid Build Coastguard Worker        atest_main.has_set_sufficient_devices(required_num, attached_devices)
222*c2e18aaaSAndroid Build Coastguard Worker    )
223*c2e18aaaSAndroid Build Coastguard Worker
224*c2e18aaaSAndroid Build Coastguard Worker  def test_has_set_sufficient_devices_not_enough_devices(self):
225*c2e18aaaSAndroid Build Coastguard Worker    required_num = 2
226*c2e18aaaSAndroid Build Coastguard Worker    attached_devices = ['serial1']
227*c2e18aaaSAndroid Build Coastguard Worker
228*c2e18aaaSAndroid Build Coastguard Worker    self.assertFalse(
229*c2e18aaaSAndroid Build Coastguard Worker        atest_main.has_set_sufficient_devices(required_num, attached_devices)
230*c2e18aaaSAndroid Build Coastguard Worker    )
231*c2e18aaaSAndroid Build Coastguard Worker
232*c2e18aaaSAndroid Build Coastguard Worker  def test_ravenwood_tests_is_deviceless(self):
233*c2e18aaaSAndroid Build Coastguard Worker    ravenwood_test_info = test_info.TestInfo(
234*c2e18aaaSAndroid Build Coastguard Worker        'mod',
235*c2e18aaaSAndroid Build Coastguard Worker        '',
236*c2e18aaaSAndroid Build Coastguard Worker        set(),
237*c2e18aaaSAndroid Build Coastguard Worker        compatibility_suites=[
238*c2e18aaaSAndroid Build Coastguard Worker            test_info.MODULE_COMPATIBILITY_SUITES_RAVENWOOD_TESTS
239*c2e18aaaSAndroid Build Coastguard Worker        ],
240*c2e18aaaSAndroid Build Coastguard Worker    )
241*c2e18aaaSAndroid Build Coastguard Worker
242*c2e18aaaSAndroid Build Coastguard Worker    self.assertEqual(
243*c2e18aaaSAndroid Build Coastguard Worker        constants.DEVICELESS_TEST,
244*c2e18aaaSAndroid Build Coastguard Worker        ravenwood_test_info.get_supported_exec_mode(),
245*c2e18aaaSAndroid Build Coastguard Worker        'If compatibility suites contains ravenwood-tests, '
246*c2e18aaaSAndroid Build Coastguard Worker        'the test should be recognized as deviceless.',
247*c2e18aaaSAndroid Build Coastguard Worker    )
248*c2e18aaaSAndroid Build Coastguard Worker
249*c2e18aaaSAndroid Build Coastguard Worker
250*c2e18aaaSAndroid Build Coastguard Workerclass AtestMainUnitTests(unittest.TestCase):
251*c2e18aaaSAndroid Build Coastguard Worker
252*c2e18aaaSAndroid Build Coastguard Worker  def test_performance_tests_inject_default_args(self):
253*c2e18aaaSAndroid Build Coastguard Worker    non_perf_test_info = test_info.TestInfo(
254*c2e18aaaSAndroid Build Coastguard Worker        'some_module',
255*c2e18aaaSAndroid Build Coastguard Worker        'TestRunner',
256*c2e18aaaSAndroid Build Coastguard Worker        set(),
257*c2e18aaaSAndroid Build Coastguard Worker        compatibility_suites=['not-performance'],
258*c2e18aaaSAndroid Build Coastguard Worker    )
259*c2e18aaaSAndroid Build Coastguard Worker    perf_test_info = test_info.TestInfo(
260*c2e18aaaSAndroid Build Coastguard Worker        'some_module',
261*c2e18aaaSAndroid Build Coastguard Worker        'TestRunner',
262*c2e18aaaSAndroid Build Coastguard Worker        set(),
263*c2e18aaaSAndroid Build Coastguard Worker        compatibility_suites=['performance-tests'],
264*c2e18aaaSAndroid Build Coastguard Worker    )
265*c2e18aaaSAndroid Build Coastguard Worker    args_original = atest_main._parse_args([])
266*c2e18aaaSAndroid Build Coastguard Worker    args = atest_main._parse_args([])
267*c2e18aaaSAndroid Build Coastguard Worker
268*c2e18aaaSAndroid Build Coastguard Worker    with self.subTest(name='does not inject default args for non-perf tests'):
269*c2e18aaaSAndroid Build Coastguard Worker      atest_main._AtestMain._inject_default_arguments_based_on_test_infos(
270*c2e18aaaSAndroid Build Coastguard Worker          [non_perf_test_info], args
271*c2e18aaaSAndroid Build Coastguard Worker      )
272*c2e18aaaSAndroid Build Coastguard Worker
273*c2e18aaaSAndroid Build Coastguard Worker      self.assertEqual(args_original, args)
274*c2e18aaaSAndroid Build Coastguard Worker
275*c2e18aaaSAndroid Build Coastguard Worker    with self.subTest(name='injects default args for perf tests'):
276*c2e18aaaSAndroid Build Coastguard Worker      atest_main._AtestMain._inject_default_arguments_based_on_test_infos(
277*c2e18aaaSAndroid Build Coastguard Worker          [perf_test_info], args
278*c2e18aaaSAndroid Build Coastguard Worker      )
279*c2e18aaaSAndroid Build Coastguard Worker
280*c2e18aaaSAndroid Build Coastguard Worker      self.assertNotEqual(args_original, args)
281*c2e18aaaSAndroid Build Coastguard Worker
282*c2e18aaaSAndroid Build Coastguard Worker
283*c2e18aaaSAndroid Build Coastguard Worker# pylint: disable=missing-function-docstring
284*c2e18aaaSAndroid Build Coastguard Workerclass AtestUnittestFixture(fake_filesystem_unittest.TestCase):
285*c2e18aaaSAndroid Build Coastguard Worker  """Fixture for ModuleInfo tests."""
286*c2e18aaaSAndroid Build Coastguard Worker
287*c2e18aaaSAndroid Build Coastguard Worker  def setUp(self):
288*c2e18aaaSAndroid Build Coastguard Worker    self.setUpPyfakefs()
289*c2e18aaaSAndroid Build Coastguard Worker
290*c2e18aaaSAndroid Build Coastguard Worker  # pylint: disable=protected-access
291*c2e18aaaSAndroid Build Coastguard Worker  def create_empty_module_info(self):
292*c2e18aaaSAndroid Build Coastguard Worker    fake_temp_file_name = next(tempfile._get_candidate_names())
293*c2e18aaaSAndroid Build Coastguard Worker    self.fs.create_file(fake_temp_file_name, contents='{}')
294*c2e18aaaSAndroid Build Coastguard Worker    return module_info.load_from_file(module_file=fake_temp_file_name)
295*c2e18aaaSAndroid Build Coastguard Worker
296*c2e18aaaSAndroid Build Coastguard Worker  def create_module_info(self, modules=None):
297*c2e18aaaSAndroid Build Coastguard Worker    mod_info = self.create_empty_module_info()
298*c2e18aaaSAndroid Build Coastguard Worker    modules = modules or []
299*c2e18aaaSAndroid Build Coastguard Worker
300*c2e18aaaSAndroid Build Coastguard Worker    for m in modules:
301*c2e18aaaSAndroid Build Coastguard Worker      mod_info.name_to_module_info[m['module_name']] = m
302*c2e18aaaSAndroid Build Coastguard Worker
303*c2e18aaaSAndroid Build Coastguard Worker    return mod_info
304*c2e18aaaSAndroid Build Coastguard Worker
305*c2e18aaaSAndroid Build Coastguard Worker  def create_test_info(
306*c2e18aaaSAndroid Build Coastguard Worker      self,
307*c2e18aaaSAndroid Build Coastguard Worker      test_name='hello_world_test',
308*c2e18aaaSAndroid Build Coastguard Worker      test_runner='AtestTradefedRunner',
309*c2e18aaaSAndroid Build Coastguard Worker      build_targets=None,
310*c2e18aaaSAndroid Build Coastguard Worker  ):
311*c2e18aaaSAndroid Build Coastguard Worker    """Create a test_info.TestInfo object."""
312*c2e18aaaSAndroid Build Coastguard Worker    if not build_targets:
313*c2e18aaaSAndroid Build Coastguard Worker      build_targets = set()
314*c2e18aaaSAndroid Build Coastguard Worker    return test_info.TestInfo(test_name, test_runner, build_targets)
315*c2e18aaaSAndroid Build Coastguard Worker
316*c2e18aaaSAndroid Build Coastguard Worker
317*c2e18aaaSAndroid Build Coastguard Workerclass PrintModuleInfoTest(AtestUnittestFixture):
318*c2e18aaaSAndroid Build Coastguard Worker  """Test conditions for _print_module_info."""
319*c2e18aaaSAndroid Build Coastguard Worker
320*c2e18aaaSAndroid Build Coastguard Worker  def tearDown(self):
321*c2e18aaaSAndroid Build Coastguard Worker    sys.stdout = sys.__stdout__
322*c2e18aaaSAndroid Build Coastguard Worker
323*c2e18aaaSAndroid Build Coastguard Worker  def test_has_valid_test_mapping_args_is_test_mapping_detect_event_send_1(
324*c2e18aaaSAndroid Build Coastguard Worker      self,
325*c2e18aaaSAndroid Build Coastguard Worker  ):
326*c2e18aaaSAndroid Build Coastguard Worker    # Arrange
327*c2e18aaaSAndroid Build Coastguard Worker    expected_detect_type = DetectType.IS_TEST_MAPPING
328*c2e18aaaSAndroid Build Coastguard Worker    expected_result = 1
329*c2e18aaaSAndroid Build Coastguard Worker    metrics.LocalDetectEvent = mock.MagicMock()
330*c2e18aaaSAndroid Build Coastguard Worker    args = arg_parser.create_atest_arg_parser().parse_args([])
331*c2e18aaaSAndroid Build Coastguard Worker
332*c2e18aaaSAndroid Build Coastguard Worker    # Act
333*c2e18aaaSAndroid Build Coastguard Worker    atest_main._has_valid_test_mapping_args(args)
334*c2e18aaaSAndroid Build Coastguard Worker
335*c2e18aaaSAndroid Build Coastguard Worker    # Assert
336*c2e18aaaSAndroid Build Coastguard Worker    metrics.LocalDetectEvent.assert_called_once_with(
337*c2e18aaaSAndroid Build Coastguard Worker        detect_type=expected_detect_type, result=expected_result
338*c2e18aaaSAndroid Build Coastguard Worker    )
339*c2e18aaaSAndroid Build Coastguard Worker
340*c2e18aaaSAndroid Build Coastguard Worker  def test_has_valid_test_mapping_args_mpt_test_mapping_detect_event_send_0(
341*c2e18aaaSAndroid Build Coastguard Worker      self,
342*c2e18aaaSAndroid Build Coastguard Worker  ):
343*c2e18aaaSAndroid Build Coastguard Worker    # Arrange
344*c2e18aaaSAndroid Build Coastguard Worker    expected_detect_type = DetectType.IS_TEST_MAPPING
345*c2e18aaaSAndroid Build Coastguard Worker    expected_result = 0
346*c2e18aaaSAndroid Build Coastguard Worker    metrics.LocalDetectEvent = mock.MagicMock()
347*c2e18aaaSAndroid Build Coastguard Worker    args = arg_parser.create_atest_arg_parser().parse_args(['test1'])
348*c2e18aaaSAndroid Build Coastguard Worker
349*c2e18aaaSAndroid Build Coastguard Worker    # Act
350*c2e18aaaSAndroid Build Coastguard Worker    atest_main._has_valid_test_mapping_args(args)
351*c2e18aaaSAndroid Build Coastguard Worker
352*c2e18aaaSAndroid Build Coastguard Worker    # Assert
353*c2e18aaaSAndroid Build Coastguard Worker    metrics.LocalDetectEvent.assert_called_once_with(
354*c2e18aaaSAndroid Build Coastguard Worker        detect_type=expected_detect_type, result=expected_result
355*c2e18aaaSAndroid Build Coastguard Worker    )
356*c2e18aaaSAndroid Build Coastguard Worker
357*c2e18aaaSAndroid Build Coastguard Worker
358*c2e18aaaSAndroid Build Coastguard Worker# pylint: disable=too-many-arguments
359*c2e18aaaSAndroid Build Coastguard Workerdef module(
360*c2e18aaaSAndroid Build Coastguard Worker    name=None,
361*c2e18aaaSAndroid Build Coastguard Worker    path=None,
362*c2e18aaaSAndroid Build Coastguard Worker    installed=None,
363*c2e18aaaSAndroid Build Coastguard Worker    classes=None,
364*c2e18aaaSAndroid Build Coastguard Worker    auto_test_config=None,
365*c2e18aaaSAndroid Build Coastguard Worker    test_config=None,
366*c2e18aaaSAndroid Build Coastguard Worker    shared_libs=None,
367*c2e18aaaSAndroid Build Coastguard Worker    dependencies=None,
368*c2e18aaaSAndroid Build Coastguard Worker    runtime_dependencies=None,
369*c2e18aaaSAndroid Build Coastguard Worker    data=None,
370*c2e18aaaSAndroid Build Coastguard Worker    data_dependencies=None,
371*c2e18aaaSAndroid Build Coastguard Worker    compatibility_suites=None,
372*c2e18aaaSAndroid Build Coastguard Worker    host_dependencies=None,
373*c2e18aaaSAndroid Build Coastguard Worker    srcs=None,
374*c2e18aaaSAndroid Build Coastguard Worker):
375*c2e18aaaSAndroid Build Coastguard Worker  name = name or 'libhello'
376*c2e18aaaSAndroid Build Coastguard Worker
377*c2e18aaaSAndroid Build Coastguard Worker  m = {}
378*c2e18aaaSAndroid Build Coastguard Worker
379*c2e18aaaSAndroid Build Coastguard Worker  m['module_name'] = name
380*c2e18aaaSAndroid Build Coastguard Worker  m['class'] = classes
381*c2e18aaaSAndroid Build Coastguard Worker  m['path'] = [path or '']
382*c2e18aaaSAndroid Build Coastguard Worker  m['installed'] = installed or []
383*c2e18aaaSAndroid Build Coastguard Worker  m['is_unit_test'] = 'false'
384*c2e18aaaSAndroid Build Coastguard Worker  m['auto_test_config'] = auto_test_config or []
385*c2e18aaaSAndroid Build Coastguard Worker  m['test_config'] = test_config or []
386*c2e18aaaSAndroid Build Coastguard Worker  m['shared_libs'] = shared_libs or []
387*c2e18aaaSAndroid Build Coastguard Worker  m['runtime_dependencies'] = runtime_dependencies or []
388*c2e18aaaSAndroid Build Coastguard Worker  m['dependencies'] = dependencies or []
389*c2e18aaaSAndroid Build Coastguard Worker  m['data'] = data or []
390*c2e18aaaSAndroid Build Coastguard Worker  m['data_dependencies'] = data_dependencies or []
391*c2e18aaaSAndroid Build Coastguard Worker  m['compatibility_suites'] = compatibility_suites or []
392*c2e18aaaSAndroid Build Coastguard Worker  m['host_dependencies'] = host_dependencies or []
393*c2e18aaaSAndroid Build Coastguard Worker  m['srcs'] = srcs or []
394*c2e18aaaSAndroid Build Coastguard Worker  return m
395*c2e18aaaSAndroid Build Coastguard Worker
396*c2e18aaaSAndroid Build Coastguard Worker
397*c2e18aaaSAndroid Build Coastguard Workerif __name__ == '__main__':
398*c2e18aaaSAndroid Build Coastguard Worker  unittest.main()
399