summaryrefslogtreecommitdiff
path: root/stevedore/tests/test_cache.py
blob: 8bf49c8f54ac95391d7fe24786ed07d89aa42de1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#  Licensed under the Apache License, Version 2.0 (the "License"); you may
#  not use this file except in compliance with the License. You may obtain
#  a copy of the License at
#
#       http://www.apache.org/licenses/LICENSE-2.0
#
#  Unless required by applicable law or agreed to in writing, software
#  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#  License for the specific language governing permissions and limitations
#  under the License.

"""Tests for stevedore._cache
"""
import sys

from unittest import mock

from stevedore import _cache
from stevedore.tests import utils


class TestCache(utils.TestCase):

    def test_disable_caching_executable(self):
        """Test caching is disabled if python interpreter is located under /tmp
        directory (Ansible)
        """
        with mock.patch.object(sys, 'executable', '/tmp/fake'):
            sot = _cache.Cache()
            self.assertTrue(sot._disable_caching)

    def test_disable_caching_file(self):
        """Test caching is disabled if .disable file is present in target
        dir
        """
        cache_dir = _cache._get_cache_dir()

        with mock.patch('os.path.isfile') as mock_path:
            mock_path.return_value = True
            sot = _cache.Cache()
            mock_path.assert_called_with('%s/.disable' % cache_dir)
            self.assertTrue(sot._disable_caching)

            mock_path.return_value = False
            sot = _cache.Cache()
            self.assertFalse(sot._disable_caching)

    @mock.patch('os.makedirs')
    @mock.patch('builtins.open')
    def test__get_data_for_path_no_write(self, mock_open, mock_mkdir):
        sot = _cache.Cache()
        sot._disable_caching = True
        mock_open.side_effect = IOError
        sot._get_data_for_path('fake')
        mock_mkdir.assert_not_called()