1#!/usr/bin/env python3
2#
3# Copyright 2018, The Android Open Source Project
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9#     http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
17"""Unittests for test_suite_test_runner."""
18
19# pylint: disable=line-too-long
20
21import unittest
22
23from unittest import mock
24
25import unittest_constants as uc
26import unittest_utils
27
28from test_finders import test_info
29from test_runners import suite_plan_test_runner
30
31
32# pylint: disable=protected-access
33class SuitePlanTestRunnerUnittests(unittest.TestCase):
34    """Unit tests for test_suite_test_runner.py"""
35
36    def setUp(self):
37        self.suite_tr = suite_plan_test_runner.SuitePlanTestRunner(
38            results_dir=uc.TEST_INFO_DIR)
39
40    def tearDown(self):
41        mock.patch.stopall()
42
43    @mock.patch('atest_utils.get_result_server_args')
44    def test_generate_run_commands(self, mock_resultargs):
45        """Test _generate_run_command method.
46        Strategy:
47            suite_name: cts --> run_cmd: cts-tradefed run commandAndExit cts
48            suite_name: cts-common --> run_cmd:
49                                cts-tradefed run commandAndExit cts-common
50        """
51        test_infos = set()
52        suite_name = 'cts'
53        t_info = test_info.TestInfo(suite_name,
54                                    suite_plan_test_runner.SuitePlanTestRunner.NAME,
55                                    {suite_name},
56                                    suite=suite_name)
57        test_infos.add(t_info)
58
59        # Basic Run Cmd
60        run_cmd = []
61        exe_cmd = suite_plan_test_runner.SuitePlanTestRunner.EXECUTABLE % suite_name
62        run_cmd.append(suite_plan_test_runner.SuitePlanTestRunner._RUN_CMD.format(
63            exe=exe_cmd,
64            test=suite_name,
65            args=''))
66        mock_resultargs.return_value = []
67        unittest_utils.assert_strict_equal(
68            self,
69            self.suite_tr.generate_run_commands(test_infos, ''),
70            run_cmd)
71
72        # Run cmd with --serial LG123456789.
73        run_cmd = []
74        run_cmd.append(suite_plan_test_runner.SuitePlanTestRunner._RUN_CMD.format(
75            exe=exe_cmd,
76            test=suite_name,
77            args='--serial LG123456789'))
78        unittest_utils.assert_strict_equal(
79            self,
80            self.suite_tr.generate_run_commands(test_infos, {'SERIAL':'LG123456789'}),
81            run_cmd)
82
83        test_infos = set()
84        suite_name = 'cts-common'
85        suite = 'cts'
86        t_info = test_info.TestInfo(suite_name,
87                                    suite_plan_test_runner.SuitePlanTestRunner.NAME,
88                                    {suite_name},
89                                    suite=suite)
90        test_infos.add(t_info)
91
92        # Basic Run Cmd
93        run_cmd = []
94        exe_cmd = suite_plan_test_runner.SuitePlanTestRunner.EXECUTABLE % suite
95        run_cmd.append(suite_plan_test_runner.SuitePlanTestRunner._RUN_CMD.format(
96            exe=exe_cmd,
97            test=suite_name,
98            args=''))
99        mock_resultargs.return_value = []
100        unittest_utils.assert_strict_equal(
101            self,
102            self.suite_tr.generate_run_commands(test_infos, ''),
103            run_cmd)
104
105        # Run cmd with --serial LG123456789.
106        run_cmd = []
107        run_cmd.append(suite_plan_test_runner.SuitePlanTestRunner._RUN_CMD.format(
108            exe=exe_cmd,
109            test=suite_name,
110            args='--serial LG123456789'))
111        unittest_utils.assert_strict_equal(
112            self,
113            self.suite_tr.generate_run_commands(test_infos, {'SERIAL':'LG123456789'}),
114            run_cmd)
115
116    @mock.patch('subprocess.Popen')
117    @mock.patch.object(suite_plan_test_runner.SuitePlanTestRunner, 'run')
118    @mock.patch.object(suite_plan_test_runner.SuitePlanTestRunner,
119                       'generate_run_commands')
120    def test_run_tests(self, _mock_gen_cmd, _mock_run, _mock_popen):
121        """Test run_tests method."""
122        test_infos = []
123        extra_args = []
124        mock_reporter = mock.Mock()
125        _mock_gen_cmd.return_value = ["cmd1", "cmd2"]
126        # Test Build Pass
127        _mock_popen.return_value.returncode = 0
128        self.assertEqual(
129            0,
130            self.suite_tr.run_tests(test_infos, extra_args, mock_reporter))
131
132        # Test Build Pass
133        _mock_popen.return_value.returncode = 1
134        self.assertNotEqual(
135            0,
136            self.suite_tr.run_tests(test_infos, extra_args, mock_reporter))
137
138if __name__ == '__main__':
139    unittest.main()
140