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 suite_plan_finder."""
18
19# pylint: disable=line-too-long
20
21import os
22import unittest
23
24from unittest import mock
25
26import unittest_constants as uc
27import unittest_utils
28
29from test_finders import test_finder_utils
30from test_finders import test_info
31from test_finders import suite_plan_finder
32from test_runners import suite_plan_test_runner
33
34
35# pylint: disable=protected-access
36class SuitePlanFinderUnittests(unittest.TestCase):
37    """Unit tests for suite_plan_finder.py"""
38
39    def setUp(self):
40        """Set up stuff for testing."""
41        self.suite_plan_finder = suite_plan_finder.SuitePlanFinder()
42        self.suite_plan_finder.suite_plan_dirs = [os.path.join(uc.ROOT, uc.CTS_INT_DIR)]
43        self.suite_plan_finder.root_dir = uc.ROOT
44
45    def test_get_test_info_from_path(self):
46        """Test _get_test_info_from_path.
47        Strategy:
48            If suite_path is to cts file -->
49                test_info: test_name=cts,
50                           test_runner=TestSuiteTestRunner,
51                           build_target=set(['cts']
52                           suite='cts')
53            If suite_path is to cts-common file -->
54                test_info: test_name=cts-common,
55                           test_runner=TestSuiteTestRunner,
56                           build_target=set(['cts']
57                           suite='cts')
58            If suite_path is to common file --> test_info: None
59            If suite_path is to non-existing file --> test_info: None
60        """
61        suite_plan = 'cts'
62        path = os.path.join(uc.ROOT, uc.CTS_INT_DIR, suite_plan+'.xml')
63        want_info = test_info.TestInfo(test_name=suite_plan,
64                                       test_runner=suite_plan_test_runner.SuitePlanTestRunner.NAME,
65                                       build_targets={suite_plan},
66                                       suite=suite_plan)
67        unittest_utils.assert_equal_testinfos(
68            self, want_info, self.suite_plan_finder._get_test_info_from_path(path))
69
70        suite_plan = 'cts-common'
71        path = os.path.join(uc.ROOT, uc.CTS_INT_DIR, suite_plan+'.xml')
72        want_info = test_info.TestInfo(test_name=suite_plan,
73                                       test_runner=suite_plan_test_runner.SuitePlanTestRunner.NAME,
74                                       build_targets={'cts'},
75                                       suite='cts')
76        unittest_utils.assert_equal_testinfos(
77            self, want_info, self.suite_plan_finder._get_test_info_from_path(path))
78
79        suite_plan = 'common'
80        path = os.path.join(uc.ROOT, uc.CTS_INT_DIR, 'cts-common.xml')
81        want_info = None
82        unittest_utils.assert_equal_testinfos(
83            self, want_info, self.suite_plan_finder._get_test_info_from_path(path, suite_plan))
84
85        path = os.path.join(uc.ROOT, 'cts-common.xml')
86        want_info = None
87        unittest_utils.assert_equal_testinfos(
88            self, want_info, self.suite_plan_finder._get_test_info_from_path(path))
89
90    @mock.patch.object(test_finder_utils, 'search_integration_dirs')
91    def test_find_test_by_suite_name(self, _search):
92        """Test find_test_by_suite_name.
93        Strategy:
94            suite_name: cts --> test_info: test_name=cts,
95                                           test_runner=TestSuiteTestRunner,
96                                           build_target=set(['cts']
97                                           suite='cts')
98            suite_name: CTS --> test_info: None
99            suite_name: cts-common --> test_info: test_name=cts-common,
100                                                  test_runner=TestSuiteTestRunner,
101                                                  build_target=set(['cts'],
102                                                  suite='cts')
103        """
104        suite_name = 'cts'
105        t_info = self.suite_plan_finder.find_test_by_suite_name(suite_name)
106        want_info = test_info.TestInfo(test_name=suite_name,
107                                       test_runner=suite_plan_test_runner.SuitePlanTestRunner.NAME,
108                                       build_targets={suite_name},
109                                       suite=suite_name)
110        unittest_utils.assert_equal_testinfos(self, t_info[0], want_info)
111
112        suite_name = 'CTS'
113        _search.return_value = None
114        t_info = self.suite_plan_finder.find_test_by_suite_name(suite_name)
115        want_info = None
116        unittest_utils.assert_equal_testinfos(self, t_info, want_info)
117
118        suite_name = 'cts-common'
119        suite = 'cts'
120        _search.return_value = [os.path.join(uc.ROOT, uc.CTS_INT_DIR, suite_name + '.xml')]
121        t_info = self.suite_plan_finder.find_test_by_suite_name(suite_name)
122        want_info = test_info.TestInfo(test_name=suite_name,
123                                       test_runner=suite_plan_test_runner.SuitePlanTestRunner.NAME,
124                                       build_targets=set([suite]),
125                                       suite=suite)
126        unittest_utils.assert_equal_testinfos(self, t_info[0], want_info)
127
128    @mock.patch('os.path.realpath',
129                side_effect=unittest_utils.realpath_side_effect)
130    @mock.patch('os.path.isdir', return_value=True)
131    @mock.patch('os.path.isfile', return_value=True)
132    @mock.patch.object(test_finder_utils, 'get_int_dir_from_path')
133    @mock.patch('os.path.exists', return_value=True)
134    def test_find_suite_plan_test_by_suite_path(self, _exists, _find, _isfile, _isdir, _real):
135        """Test find_test_by_suite_name.
136        Strategy:
137            suite_name: cts.xml --> test_info:
138                                        test_name=cts,
139                                        test_runner=TestSuiteTestRunner,
140                                        build_target=set(['cts']
141                                        suite='cts')
142            suite_name: cts-common.xml --> test_info:
143                                               test_name=cts-common,
144                                               test_runner=TestSuiteTestRunner,
145                                               build_target=set(['cts'],
146                                               suite='cts')
147            suite_name: cts-camera.xml --> test_info:
148                                               test_name=cts-camera,
149                                               test_runner=TestSuiteTestRunner,
150                                               build_target=set(['cts'],
151                                               suite='cts')
152        """
153        suite_int_name = 'cts'
154        suite = 'cts'
155        path = os.path.join(uc.CTS_INT_DIR, suite_int_name + '.xml')
156        _find.return_value = uc.CTS_INT_DIR
157        t_info = self.suite_plan_finder.find_test_by_suite_path(path)
158        want_info = test_info.TestInfo(test_name=suite_int_name,
159                                       test_runner=suite_plan_test_runner.SuitePlanTestRunner.NAME,
160                                       build_targets=set([suite]),
161                                       suite=suite)
162        unittest_utils.assert_equal_testinfos(self, t_info[0], want_info)
163
164        suite_int_name = 'cts-common'
165        suite = 'cts'
166        path = os.path.join(uc.CTS_INT_DIR, suite_int_name + '.xml')
167        _find.return_value = uc.CTS_INT_DIR
168        t_info = self.suite_plan_finder.find_test_by_suite_path(path)
169        want_info = test_info.TestInfo(test_name=suite_int_name,
170                                       test_runner=suite_plan_test_runner.SuitePlanTestRunner.NAME,
171                                       build_targets=set([suite]),
172                                       suite=suite)
173        unittest_utils.assert_equal_testinfos(self, t_info[0], want_info)
174
175        suite_int_name = 'cts-camera'
176        suite = 'cts'
177        path = os.path.join(uc.CTS_INT_DIR, suite_int_name + '.xml')
178        _find.return_value = uc.CTS_INT_DIR
179        t_info = self.suite_plan_finder.find_test_by_suite_path(path)
180        want_info = test_info.TestInfo(test_name=suite_int_name,
181                                       test_runner=suite_plan_test_runner.SuitePlanTestRunner.NAME,
182                                       build_targets=set([suite]),
183                                       suite=suite)
184        unittest_utils.assert_equal_testinfos(self, t_info[0], want_info)
185
186
187if __name__ == '__main__':
188    unittest.main()
189