test_cache.py 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. # Licensed under the Apache License, Version 2.0 (the "License"); you may
  2. # not use this file except in compliance with the License. You may obtain
  3. # a copy of the License at
  4. #
  5. # http://www.apache.org/licenses/LICENSE-2.0
  6. #
  7. # Unless required by applicable law or agreed to in writing, software
  8. # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  9. # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  10. # License for the specific language governing permissions and limitations
  11. # under the License.
  12. """Tests for stevedore._cache
  13. """
  14. import sys
  15. from unittest import mock
  16. from stevedore import _cache
  17. from stevedore.tests import utils
  18. class TestCache(utils.TestCase):
  19. def test_disable_caching_executable(self):
  20. """Test caching is disabled if python interpreter is located under /tmp
  21. directory (Ansible)
  22. """
  23. with mock.patch.object(sys, 'executable', '/tmp/fake'):
  24. sot = _cache.Cache()
  25. self.assertTrue(sot._disable_caching)
  26. def test_disable_caching_file(self):
  27. """Test caching is disabled if .disable file is present in target
  28. dir
  29. """
  30. cache_dir = _cache._get_cache_dir()
  31. with mock.patch('os.path.isfile') as mock_path:
  32. mock_path.return_value = True
  33. sot = _cache.Cache()
  34. mock_path.assert_called_with('%s/.disable' % cache_dir)
  35. self.assertTrue(sot._disable_caching)
  36. mock_path.return_value = False
  37. sot = _cache.Cache()
  38. self.assertFalse(sot._disable_caching)
  39. @mock.patch('os.makedirs')
  40. @mock.patch('builtins.open')
  41. def test__get_data_for_path_no_write(self, mock_open, mock_mkdir):
  42. sot = _cache.Cache()
  43. sot._disable_caching = True
  44. mock_open.side_effect = IOError
  45. sot._get_data_for_path('fake')
  46. mock_mkdir.assert_not_called()
  47. def test__build_cacheable_data(self):
  48. # this is a rubbish test as we don't actually do anything with the
  49. # data, but it's too hard to script since it's totally environmentally
  50. # dependent and mocking out the underlying calls would remove the value
  51. # of this test (we want to test those underlying API calls)
  52. ret = _cache._build_cacheable_data()
  53. self.assertIsInstance(ret['groups'], dict)