@@ -142,8 +142,9 @@ def test_dead_primary():
142142
143143@override_settings (CACHES = cache_settings )
144144def test_ensure_temp_file_is_removed_on_init ():
145- temp_file = Path (tempfile .NamedTemporaryFile ().name )
146- with mock .patch ('ansible_base.lib.cache.fallback_cache._temp_file' , temp_file ):
145+ tmp_path = tempfile .gettempdir ()
146+ temp_file = Path ().joinpath (tmp_path , 'gw_primary_cache_failed' )
147+ with override_settings (ANSIBLE_BASE_FALLBACK_CACHE_FILE_PATH = tmp_path ):
147148 temp_file .touch ()
148149 # Remove singleton instance
149150 DABCacheWithFallback ._instance = None
@@ -196,72 +197,64 @@ def test_all_methods_are_overwritten(method):
196197@override_settings (CACHES = cache_settings )
197198def test_check_primary_cache (file_exists ):
198199 temp_file = Path (tempfile .NamedTemporaryFile ().name )
199- with mock . patch ( 'ansible_base.lib.cache.fallback_cache._temp_file' , temp_file ):
200- # Remove singleton instance
201- DABCacheWithFallback . _instance = None
202- # Initialization of the cache will clear the temp file so do this first
203- cache = DABCacheWithFallback ( None , {})
204- # Ensure cache is working
205- cache ._primary_cache .fixit ()
206-
207- # Create the temp file if needed
208- if file_exists :
209- temp_file .touch ()
210- else :
211- try :
212- temp_file .unlink ()
213- except Exception :
214- pass
215- mocked_function = mock .MagicMock (return_value = None )
216- cache ._primary_cache .clear = mocked_function
217- cache .check_primary_cache ()
218- if file_exists :
219- mocked_function .assert_called_once ()
220- else :
221- mocked_function .assert_not_called ()
222- assert temp_file .exists () is False
200+ # Remove singleton instance
201+ DABCacheWithFallback . _instance = None
202+ # Initialization of the cache will clear the temp file so do this first
203+ cache = DABCacheWithFallback ( None , {})
204+ cache . _temp_file = temp_file
205+ # Ensure cache is working
206+ cache ._primary_cache .fixit ()
207+
208+ # Create the temp file if needed
209+ if file_exists :
210+ temp_file .touch ()
211+ else :
212+ try :
213+ temp_file .unlink ()
214+ except Exception :
215+ pass
216+ mocked_function = mock .MagicMock (return_value = None )
217+ cache ._primary_cache .clear = mocked_function
218+ cache .check_primary_cache ()
219+ if file_exists :
220+ mocked_function .assert_called_once ()
221+ else :
222+ mocked_function .assert_not_called ()
223+ assert temp_file .exists () is False
223224
224225
225226@override_settings (CACHES = cache_settings )
226227def test_file_unlink_exception_does_not_cause_failure ():
227228 temp_file = Path (tempfile .NamedTemporaryFile ().name )
228- with mock . patch ( 'ansible_base.lib.cache.fallback_cache._temp_file' , temp_file ):
229- cache = DABCacheWithFallback ( None , {})
230- # We can't do: temp_file.unlink = mock.MagicMock(side_effect=Exception('failed to unlink exception'))
231- # Because unlink is marked as read only so we will just mock the cache.clear to raise in its place
232- mocked_function = mock .MagicMock (side_effect = Exception ('failed to delete a file exception' ))
233- cache ._primary_cache .clear = mocked_function
229+ cache = DABCacheWithFallback ( None , {})
230+ cache . _temp_file = temp_file
231+ # We can't do: temp_file.unlink = mock.MagicMock(side_effect=Exception('failed to unlink exception'))
232+ # Because unlink is marked as read only so we will just mock the cache.clear to raise in its place
233+ mocked_function = mock .MagicMock (side_effect = Exception ('failed to delete a file exception' ))
234+ cache ._primary_cache .clear = mocked_function
234235
235- temp_file .touch ()
236- cache .check_primary_cache ()
237- # No assertion needed because we just want to make sure check_primary_cache does not raise
236+ temp_file .touch ()
237+ cache .check_primary_cache ()
238+ # No assertion needed because we just want to make sure check_primary_cache does not raise
238239
239240
241+ @override_settings (CACHES = cache_settings )
240242def test_temp_file_setting (tmp_path ):
241- with override_settings (ANSIBLE_BASE_FALLBACK_CACHE_FILE_PATH = tmp_path ):
242- import importlib
243-
244- from ansible_base .lib .cache import fallback_cache
245-
246- importlib .reload (fallback_cache )
243+ import os
247244
245+ with override_settings (ANSIBLE_BASE_FALLBACK_CACHE_FILE_PATH = tmp_path ):
246+ # Remove singleton instance
247+ DABCacheWithFallback ._instance = None
248+ fallback_cache = DABCacheWithFallback (None , {})
248249 # Assert that the temp file does not exist yet
249250 assert not fallback_cache ._temp_file .exists ()
250251
251252 fallback_cache .create_temp_file ()
252253
253254 # The temp file should now exist at the specified cache path
254255 assert fallback_cache ._temp_file .exists ()
256+
255257 expected_path = Path ().joinpath (tmp_path , 'junk.txt' )
256258 assert fallback_cache ._temp_file .parent in expected_path .parents
257-
258-
259- def test_temp_file_permissions ():
260- import os
261-
262- from ansible_base .lib .cache .fallback_cache import _temp_file , create_temp_file
263-
264- create_temp_file ()
265- assert _temp_file .exists
266- status = os .stat (_temp_file )
267- assert oct (status .st_mode )[- 3 :] == "660"
259+ status = os .stat (fallback_cache ._temp_file )
260+ assert oct (status .st_mode )[- 3 :] == "660"
0 commit comments