1#!/usr/bin/env python
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"""Unittests for test_suite_test_runner."""
17
18import unittest
19import mock
20
21# pylint: disable=import-error
22import suite_plan_test_runner
23import unittest_utils
24from test_finders import test_info
25
26
27# pylint: disable=protected-access
28class SuitePlanTestRunnerUnittests(unittest.TestCase):
29    """Unit tests for test_suite_test_runner.py"""
30
31    def setUp(self):
32        self.suite_tr = suite_plan_test_runner.SuitePlanTestRunner(results_dir='')
33
34    def tearDown(self):
35        mock.patch.stopall()
36
37    @mock.patch('atest_utils.get_result_server_args')
38    def test_generate_run_commands(self, mock_resultargs):
39        """Test _generate_run_command method.
40        Strategy:
41            suite_name: cts --> run_cmd: cts-tradefed run commandAndExit cts
42            suite_name: cts-common --> run_cmd:
43                                cts-tradefed run commandAndExit cts-common
44        """
45        test_infos = set()
46        suite_name = 'cts'
47        t_info = test_info.TestInfo(suite_name,
48                                    suite_plan_test_runner.SuitePlanTestRunner.NAME,
49                                    {suite_name},
50                                    suite=suite_name)
51        test_infos.add(t_info)
52
53        # Basic Run Cmd
54        run_cmd = []
55        exe_cmd = suite_plan_test_runner.SuitePlanTestRunner.EXECUTABLE % suite_name
56        run_cmd.append(suite_plan_test_runner.SuitePlanTestRunner._RUN_CMD.format(
57            exe=exe_cmd,
58            test=suite_name,
59            args=''))
60        mock_resultargs.return_value = []
61        unittest_utils.assert_strict_equal(
62            self,
63            self.suite_tr.generate_run_commands(test_infos, ''),
64            run_cmd)
65
66        # Run cmd with --serial LG123456789.
67        run_cmd = []
68        run_cmd.append(suite_plan_test_runner.SuitePlanTestRunner._RUN_CMD.format(
69            exe=exe_cmd,
70            test=suite_name,
71            args='--serial LG123456789'))
72        unittest_utils.assert_strict_equal(
73            self,
74            self.suite_tr.generate_run_commands(test_infos, {'SERIAL':'LG123456789'}),
75            run_cmd)
76
77        test_infos = set()
78        suite_name = 'cts-common'
79        suite = 'cts'
80        t_info = test_info.TestInfo(suite_name,
81                                    suite_plan_test_runner.SuitePlanTestRunner.NAME,
82                                    {suite_name},
83                                    suite=suite)
84        test_infos.add(t_info)
85
86        # Basic Run Cmd
87        run_cmd = []
88        exe_cmd = suite_plan_test_runner.SuitePlanTestRunner.EXECUTABLE % suite
89        run_cmd.append(suite_plan_test_runner.SuitePlanTestRunner._RUN_CMD.format(
90            exe=exe_cmd,
91            test=suite_name,
92            args=''))
93        mock_resultargs.return_value = []
94        unittest_utils.assert_strict_equal(
95            self,
96            self.suite_tr.generate_run_commands(test_infos, ''),
97            run_cmd)
98
99        # Run cmd with --serial LG123456789.
100        run_cmd = []
101        run_cmd.append(suite_plan_test_runner.SuitePlanTestRunner._RUN_CMD.format(
102            exe=exe_cmd,
103            test=suite_name,
104            args='--serial LG123456789'))
105        unittest_utils.assert_strict_equal(
106            self,
107            self.suite_tr.generate_run_commands(test_infos, {'SERIAL':'LG123456789'}),
108            run_cmd)
109
110    @mock.patch('subprocess.Popen')
111    @mock.patch.object(suite_plan_test_runner.SuitePlanTestRunner, 'run')
112    @mock.patch.object(suite_plan_test_runner.SuitePlanTestRunner,
113                       'generate_run_commands')
114    def test_run_tests(self, _mock_gen_cmd, _mock_run, _mock_popen):
115        """Test run_tests method."""
116        test_infos = []
117        extra_args = []
118        mock_reporter = mock.Mock()
119        _mock_gen_cmd.return_value = ["cmd1", "cmd2"]
120        # Test Build Pass
121        _mock_popen.return_value.returncode = 0
122        self.assertEqual(
123            0,
124            self.suite_tr.run_tests(test_infos, extra_args, mock_reporter))
125
126        # Test Build Pass
127        _mock_popen.return_value.returncode = 1
128        self.assertNotEqual(
129            0,
130            self.suite_tr.run_tests(test_infos, extra_args, mock_reporter))
131
132if __name__ == '__main__':
133    unittest.main()
134