• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 logstorage import atest_gcp_utils
29from logstorage import logstorage_utils
30from test_finders import test_info
31from test_runners import suite_plan_test_runner
32
33
34# pylint: disable=protected-access
35class SuitePlanTestRunnerUnittests(unittest.TestCase):
36    """Unit tests for test_suite_test_runner.py"""
37
38    def setUp(self):
39        self.suite_tr = suite_plan_test_runner.SuitePlanTestRunner(
40            results_dir=uc.TEST_INFO_DIR)
41
42    def tearDown(self):
43        mock.patch.stopall()
44
45    @mock.patch('atest_utils.get_result_server_args')
46    def test_generate_run_commands(self, mock_resultargs):
47        """Test _generate_run_command method.
48        Strategy:
49            suite_name: cts --> run_cmd: cts-tradefed run commandAndExit cts
50            suite_name: cts-common --> run_cmd:
51                                cts-tradefed run commandAndExit cts-common
52        """
53        test_infos = set()
54        suite_name = 'cts'
55        t_info = test_info.TestInfo(suite_name,
56                                    suite_plan_test_runner.SuitePlanTestRunner.NAME,
57                                    {suite_name},
58                                    suite=suite_name)
59        test_infos.add(t_info)
60
61        # Basic Run Cmd
62        run_cmd = []
63        exe_cmd = suite_plan_test_runner.SuitePlanTestRunner.EXECUTABLE % suite_name
64        run_cmd.append(suite_plan_test_runner.SuitePlanTestRunner._RUN_CMD.format(
65            exe=exe_cmd,
66            test=suite_name,
67            args=''))
68        mock_resultargs.return_value = []
69        unittest_utils.assert_strict_equal(
70            self,
71            self.suite_tr.generate_run_commands(test_infos, ''),
72            run_cmd)
73
74        # Run cmd with --serial LG123456789.
75        run_cmd = []
76        run_cmd.append(suite_plan_test_runner.SuitePlanTestRunner._RUN_CMD.format(
77            exe=exe_cmd,
78            test=suite_name,
79            args='--serial LG123456789'))
80        unittest_utils.assert_strict_equal(
81            self,
82            self.suite_tr.generate_run_commands(test_infos, {'SERIAL':'LG123456789'}),
83            run_cmd)
84
85        test_infos = set()
86        suite_name = 'cts-common'
87        suite = 'cts'
88        t_info = test_info.TestInfo(suite_name,
89                                    suite_plan_test_runner.SuitePlanTestRunner.NAME,
90                                    {suite_name},
91                                    suite=suite)
92        test_infos.add(t_info)
93
94        # Basic Run Cmd
95        run_cmd = []
96        exe_cmd = suite_plan_test_runner.SuitePlanTestRunner.EXECUTABLE % suite
97        run_cmd.append(suite_plan_test_runner.SuitePlanTestRunner._RUN_CMD.format(
98            exe=exe_cmd,
99            test=suite_name,
100            args=''))
101        mock_resultargs.return_value = []
102        unittest_utils.assert_strict_equal(
103            self,
104            self.suite_tr.generate_run_commands(test_infos, ''),
105            run_cmd)
106
107        # Run cmd with --serial LG123456789.
108        run_cmd = []
109        run_cmd.append(suite_plan_test_runner.SuitePlanTestRunner._RUN_CMD.format(
110            exe=exe_cmd,
111            test=suite_name,
112            args='--serial LG123456789'))
113        unittest_utils.assert_strict_equal(
114            self,
115            self.suite_tr.generate_run_commands(test_infos, {'SERIAL':'LG123456789'}),
116            run_cmd)
117
118    @mock.patch.object(logstorage_utils, 'BuildClient')
119    @mock.patch.object(atest_gcp_utils, 'do_upload_flow')
120    @mock.patch('atest_utils.get_manifest_branch')
121    @mock.patch.object(logstorage_utils.BuildClient, 'update_invocation')
122    @mock.patch('subprocess.Popen')
123    @mock.patch.object(suite_plan_test_runner.SuitePlanTestRunner, 'run')
124    @mock.patch.object(suite_plan_test_runner.SuitePlanTestRunner,
125                       'generate_run_commands')
126    def test_run_tests(self, _mock_gen_cmd, _mock_run, _mock_popen,
127        _mock_upd_inv, _mock_get_branch, _mock_do_upload_flow, _mock_client):
128        """Test run_tests method."""
129        test_infos = []
130        extra_args = {}
131        mock_reporter = mock.Mock()
132        _mock_gen_cmd.return_value = ["cmd1", "cmd2"]
133        process = _mock_popen.return_value.__enter__.return_value
134        process.returncode = 0
135        process.communicate.return_value = (b'output', b'error')
136        _mock_do_upload_flow.return_value = None, None
137
138        # Test Build Pass
139        _mock_popen.return_value.returncode = 0
140        self.assertEqual(
141            0,
142            self.suite_tr.run_tests(test_infos, extra_args, mock_reporter))
143
144        # Test Build Pass
145        _mock_popen.return_value.returncode = 1
146        self.assertNotEqual(
147            0,
148            self.suite_tr.run_tests(test_infos, extra_args, mock_reporter))
149
150if __name__ == '__main__':
151    unittest.main()
152