• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python3
2#
3# Copyright 2019, 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 cache_finder."""
18
19# pylint: disable=line-too-long
20
21import unittest
22import os
23
24from unittest import mock
25
26import atest_utils
27import constants
28import module_info
29import unittest_constants as uc
30
31from test_finders import cache_finder
32from test_finders import test_info
33
34
35#pylint: disable=protected-access
36class CacheFinderUnittests(unittest.TestCase):
37    """Unit tests for cache_finder.py"""
38    def setUp(self):
39        """Set up stuff for testing."""
40        self.cache_finder = cache_finder.CacheFinder()
41        self.cache_finder.module_info = mock.Mock(spec=module_info.ModuleInfo)
42
43    @mock.patch.object(cache_finder.CacheFinder, '_is_test_filter_valid',
44                       return_value=True)
45    @mock.patch.object(cache_finder.CacheFinder, '_is_test_build_target_valid',
46                       return_value=True)
47    @mock.patch.object(atest_utils, 'get_test_info_cache_path')
48    def test_find_test_by_cache(self, mock_get_cache_path,
49            _mock_build_target_valid, _mock_filter_valid):
50        """Test find_test_by_cache method."""
51        uncached_test = 'mytest1'
52        cached_test = 'hello_world_test'
53        uncached_test2 = 'mytest2'
54        test_cache_root = os.path.join(uc.TEST_DATA_DIR, 'cache_root')
55        # Hit matched cache file but no original_finder in it,
56        # should return None.
57        mock_get_cache_path.return_value = os.path.join(
58            test_cache_root,
59            'cd66f9f5ad63b42d0d77a9334de6bb73.cache')
60        self.assertIsNone(self.cache_finder.find_test_by_cache(uncached_test))
61        # Hit matched cache file and original_finder is in it,
62        # should return cached test infos.
63        self.cache_finder.module_info.get_paths.return_value = [
64            'platform_testing/tests/example/native']
65        mock_get_cache_path.return_value = os.path.join(
66            test_cache_root,
67            '78ea54ef315f5613f7c11dd1a87f10c7.cache')
68        self.assertIsNotNone(self.cache_finder.find_test_by_cache(cached_test))
69        # Does not hit matched cache file, should return cached test infos.
70        mock_get_cache_path.return_value = os.path.join(
71            test_cache_root,
72            '39488b7ac83c56d5a7d285519fe3e3fd.cache')
73        self.assertIsNone(self.cache_finder.find_test_by_cache(uncached_test2))
74
75    @mock.patch.object(cache_finder.CacheFinder, '_is_test_build_target_valid',
76                       return_value=True)
77    @mock.patch.object(atest_utils, 'get_test_info_cache_path')
78    def test_find_test_by_cache_wo_valid_path(self, mock_get_cache_path,
79            _mock_build_target_valid):
80        """Test find_test_by_cache method."""
81        cached_test = 'hello_world_test'
82        test_cache_root = os.path.join(uc.TEST_DATA_DIR, 'cache_root')
83        # Return None when the actual test_path is not identical to that in the
84        # existing cache.
85        self.cache_finder.module_info.get_paths.return_value = [
86            'not/matched/test/path']
87        mock_get_cache_path.return_value = os.path.join(
88            test_cache_root,
89            '78ea54ef315f5613f7c11dd1a87f10c7.cache')
90        self.assertIsNone(self.cache_finder.find_test_by_cache(cached_test))
91
92    @mock.patch.object(cache_finder.CacheFinder, '_is_test_build_target_valid',
93                       return_value=False)
94    @mock.patch.object(cache_finder.CacheFinder, '_is_test_path_valid',
95                       return_value=True)
96    @mock.patch.object(atest_utils, 'get_test_info_cache_path')
97    def test_find_test_by_cache_wo_valid_build_target(self, mock_get_cache_path,
98            _mock_path_valid, _mock_build_target_valid):
99        """Test find_test_by_cache method."""
100        cached_test = 'hello_world_test'
101        test_cache_root = os.path.join(uc.TEST_DATA_DIR, 'cache_root')
102        # Return None when the build target is not exist in module-info.
103        mock_get_cache_path.return_value = os.path.join(
104            test_cache_root,
105            '78ea54ef315f5613f7c11dd1a87f10c7.cache')
106        self.assertIsNone(self.cache_finder.find_test_by_cache(cached_test))
107
108    @mock.patch.object(cache_finder.CacheFinder, '_is_test_filter_valid',
109                       return_value=False)
110    @mock.patch.object(cache_finder.CacheFinder, '_is_test_build_target_valid',
111                       return_value=True)
112    @mock.patch.object(cache_finder.CacheFinder, '_is_test_path_valid',
113                       return_value=True)
114    @mock.patch.object(atest_utils, 'get_test_info_cache_path')
115    def test_find_test_by_cache_wo_valid_java_filter(self, mock_get_cache_path,
116        _mock_path_valid, _mock_build_target_valid, _mock_filter_valid):
117        """Test _is_test_filter_valid method."""
118        cached_test = 'hello_world_test'
119        test_cache_root = os.path.join(uc.TEST_DATA_DIR, 'cache_root')
120        # Return None if the cached test filter is not valid.
121        mock_get_cache_path.return_value = os.path.join(
122            test_cache_root,
123            '78ea54ef315f5613f7c11dd1a87f10c7.cache')
124        self.assertIsNone(self.cache_finder.find_test_by_cache(cached_test))
125
126    def test_is_java_filter_in_module_for_java_class(self):
127        """Test _is_java_filter_in_module method if input is java class."""
128        mock_mod = {constants.MODULE_SRCS:
129                             ['src/a/b/c/MyTestClass1.java']}
130        self.cache_finder.module_info.get_module_info.return_value = mock_mod
131        # Should not match if class name does not exist.
132        self.assertFalse(
133            self.cache_finder._is_java_filter_in_module(
134                'MyModule', 'a.b.c.MyTestClass'))
135        # Should match if class name exist.
136        self.assertTrue(
137            self.cache_finder._is_java_filter_in_module(
138                'MyModule', 'a.b.c.MyTestClass1'))
139
140    def test_is_java_filter_in_module_for_java_package(self):
141        """Test _is_java_filter_in_module method if input is java package."""
142        mock_mod = {constants.MODULE_SRCS:
143                        ['src/a/b/c/MyTestClass1.java']}
144        self.cache_finder.module_info.get_module_info.return_value = mock_mod
145        # Should not match if package name does not match the src.
146        self.assertFalse(
147            self.cache_finder._is_java_filter_in_module(
148                'MyModule', 'a.b.c.d'))
149        # Should match if package name matches the src.
150        self.assertTrue(
151            self.cache_finder._is_java_filter_in_module(
152                'MyModule', 'a.b.c'))
153
154    def test_is_test_build_target_valid_module_in(self):
155        """Test _is_test_build_target_valid method if target has MODULES-IN."""
156        t_info = test_info.TestInfo('mock_name', 'mock_runner',
157                                    {'MODULES-IN-my-test-dir'})
158        self.cache_finder.module_info.is_module.return_value = False
159        self.assertTrue(self.cache_finder._is_test_build_target_valid(t_info))
160
161    def test_is_test_build_target_valid(self):
162        """Test _is_test_build_target_valid method."""
163        t_info = test_info.TestInfo('mock_name', 'mock_runner',
164                                    {'my-test-target'})
165        self.cache_finder.module_info.is_module.return_value = False
166        self.assertFalse(self.cache_finder._is_test_build_target_valid(t_info))
167
168if __name__ == '__main__':
169    unittest.main()
170