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 unittest_constants as uc
28
29from test_finders import cache_finder
30
31
32#pylint: disable=protected-access
33class CacheFinderUnittests(unittest.TestCase):
34    """Unit tests for cache_finder.py"""
35    def setUp(self):
36        """Set up stuff for testing."""
37        self.cache_finder = cache_finder.CacheFinder()
38
39    @mock.patch.object(atest_utils, 'get_test_info_cache_path')
40    def test_find_test_by_cache(self, mock_get_cache_path):
41        """Test find_test_by_cache method."""
42        uncached_test = 'mytest1'
43        cached_test = 'hello_world_test'
44        uncached_test2 = 'mytest2'
45        test_cache_root = os.path.join(uc.TEST_DATA_DIR, 'cache_root')
46        # Hit matched cache file but no original_finder in it,
47        # should return None.
48        mock_get_cache_path.return_value = os.path.join(
49            test_cache_root,
50            'cd66f9f5ad63b42d0d77a9334de6bb73.cache')
51        self.assertIsNone(self.cache_finder.find_test_by_cache(uncached_test))
52        # Hit matched cache file and original_finder is in it,
53        # should return cached test infos.
54        mock_get_cache_path.return_value = os.path.join(
55            test_cache_root,
56            '78ea54ef315f5613f7c11dd1a87f10c7.cache')
57        self.assertIsNotNone(self.cache_finder.find_test_by_cache(cached_test))
58        # Does not hit matched cache file, should return cached test infos.
59        mock_get_cache_path.return_value = os.path.join(
60            test_cache_root,
61            '39488b7ac83c56d5a7d285519fe3e3fd.cache')
62        self.assertIsNone(self.cache_finder.find_test_by_cache(uncached_test2))
63
64if __name__ == '__main__':
65    unittest.main()
66