def test_reset(self): fileCache = FileCache(self.config) fileCache.uptodate_paths.append('dummy-path') fileCache.reset() self.assertFalse(fileCache.uptodate_paths)
def test_provide_directory(self, write_hash_sum_mock, unpack_archive_mock, invalidate_directory_mock, read_hash_sum_mock, fetch_url_mock, build_download_url_mock): build_download_url_mock.return_value = "http://dummy-url/" HASH1 = "hash1" membuffer = MagicMock() membuffer.getvalue.return_value.strip.return_value = HASH1 fileCache = FileCache(self.config) # Test uptodate dirs after start self.assertFalse(fileCache.uptodate_paths) path = os.path.join("cache_path", "subdirectory") # Test initial downloading (when dir does not exist) fetch_url_mock.return_value = membuffer read_hash_sum_mock.return_value = "hash2" res = fileCache.provide_directory("cache_path", "subdirectory", "server_url_prefix") self.assertTrue(invalidate_directory_mock.called) self.assertTrue(write_hash_sum_mock.called) self.assertEquals(fetch_url_mock.call_count, 2) self.assertEquals(pprint.pformat(fileCache.uptodate_paths), pprint.pformat([path])) self.assertEquals(res, path) fetch_url_mock.reset_mock() write_hash_sum_mock.reset_mock() invalidate_directory_mock.reset_mock() unpack_archive_mock.reset_mock() # Test cache invalidation when local hash does not differ fetch_url_mock.return_value = membuffer read_hash_sum_mock.return_value = HASH1 fileCache.reset() res = fileCache.provide_directory("cache_path", "subdirectory", "server_url_prefix") self.assertFalse(invalidate_directory_mock.called) self.assertFalse(write_hash_sum_mock.called) self.assertEquals(fetch_url_mock.call_count, 1) self.assertEquals(pprint.pformat(fileCache.uptodate_paths), pprint.pformat([path])) self.assertEquals(res, path) fetch_url_mock.reset_mock() write_hash_sum_mock.reset_mock() invalidate_directory_mock.reset_mock() unpack_archive_mock.reset_mock() # Test execution path when path is up-to date (already checked) res = fileCache.provide_directory("cache_path", "subdirectory", "server_url_prefix") self.assertFalse(invalidate_directory_mock.called) self.assertFalse(write_hash_sum_mock.called) self.assertEquals(fetch_url_mock.call_count, 0) self.assertEquals(pprint.pformat(fileCache.uptodate_paths), pprint.pformat([path])) self.assertEquals(res, path) # Check exception handling when tolerance is disabled self.config.set('agent', 'tolerate_download_failures', "false") fetch_url_mock.side_effect = self.caching_exc_side_effect fileCache = FileCache(self.config) try: fileCache.provide_directory("cache_path", "subdirectory", "server_url_prefix") self.fail('CachingException not thrown') except CachingException: pass # Expected except Exception, e: self.fail('Unexpected exception thrown:' + str(e))