def test_clean_old_jobs_jid_file_is_cleaned(self): ''' Test that the entire JID dir is removed when a job is old enough to be removed. ''' # Create temp job cache dir and jid file jid_dir, jid_file = self._make_tmp_jid_dirs() # File timestamps on Windows aren't as precise. Let a little time pass if salt.utils.platform.is_windows(): time.sleep(.01) # Make sure there is a jid directory jid_dir_name = jid_file.rpartition('/')[2] self.assertEqual(jid_dir_name, 'jid') # Call clean_old_jobs function, patching the keep_jobs value with a # very small value to force the call to clean the job. with patch.dict(local_cache.__opts__, {'keep_jobs': 0.00000001}): # Sleep on Windows because time.time is only precise to 3 decimal # points, and therefore subtracting the jid_ctime from time.time # will result in a negative number if salt.utils.platform.is_windows(): time.sleep(0.25) local_cache.clean_old_jobs() # Assert that the JID dir was removed self.assertEqual([], os.listdir(TMP_JID_DIR))
def test_clean_old_jobs_jid_file_corrupted(self): """ Tests that the entire JID dir is removed when the jid_file is not a file. This scenario indicates a corrupted cache entry, so the entire dir is scrubbed. """ # Create temp job cache dir and jid file jid_dir, jid_file = self._make_tmp_jid_dirs() # Make sure there is a jid file in a new job cache director jid_dir_name = jid_file.rpartition("/")[2] self.assertEqual(jid_dir_name, "jid") # Even though we created a valid jid file in the _make_tmp_jid_dirs call to get # into the correct loop, we need to mock the 'os.path.isfile' check to force the # "corrupted file" check in the clean_old_jobs call. with patch("os.path.isfile", MagicMock(return_value=False)) as mock: local_cache.clean_old_jobs() # there should be only 1 dir in TMP_JID_DIR self.assertEqual(1, len(os.listdir(self.TMP_JID_DIR))) # top level dir should still be present self.assertEqual(True, os.path.exists(jid_dir)) self.assertEqual(True, os.path.isdir(jid_dir)) # while the 'jid' dir inside it should be gone self.assertEqual(False, os.path.exists(jid_dir_name))
def test_clean_old_jobs_jid_file_is_cleaned(self): """ Test that the entire JID dir is removed when a job is old enough to be removed. """ # Create temp job cache dir and jid file jid_dir, jid_file = self._make_tmp_jid_dirs() # File timestamps on Windows aren't as precise. Let a little time pass if salt.utils.platform.is_windows(): time.sleep(0.01) # Make sure there is a jid directory jid_dir_name = jid_file.rpartition("/")[2] self.assertEqual(jid_dir_name, "jid") # Call clean_old_jobs function, patching the keep_jobs value with a # very small value to force the call to clean the job. with patch.dict(local_cache.__opts__, {"keep_jobs": 0.00000001}): # Sleep on Windows because time.time is only precise to 3 decimal # points, and therefore subtracting the jid_ctime from time.time # will result in a negative number if salt.utils.platform.is_windows(): time.sleep(0.25) local_cache.clean_old_jobs() # there should be only 1 dir in TMP_JID_DIR self.assertEqual(1, len(os.listdir(self.TMP_JID_DIR))) # top level dir should still be present self.assertEqual(True, os.path.exists(jid_dir)) self.assertEqual(True, os.path.isdir(jid_dir)) # while the 'jid' dir inside it should be gone self.assertEqual(False, os.path.exists(jid_dir_name))
def test_clean_old_jobs_empty_jid_dir_removed(self): """ Tests that an empty JID dir is removed when it is old enough to be deleted. """ # Create temp job cache dir without files in it. jid_dir, jid_file = self._make_tmp_jid_dirs(create_files=False) # File timestamps on Windows aren't as precise. Let a little time pass if salt.utils.platform.is_windows(): time.sleep(0.01) # Make sure there are no files in the directory before continuing self.assertEqual(jid_file, None) # Call clean_old_jobs function, patching the keep_jobs value with a # very small value to force the call to clean the job. with patch.dict(local_cache.__opts__, {"keep_jobs": 0.00000001}): # Sleep on Windows because time.time is only precise to 3 decimal # points, and therefore subtracting the jid_ctime from time.time # will result in a negative number if salt.utils.platform.is_windows(): time.sleep(0.25) local_cache.clean_old_jobs() # Assert that the JID dir was removed self.assertEqual([], os.listdir(self.TMP_JID_DIR))
def test_clean_old_jobs_jid_file_is_cleaned(self): ''' Test that the entire JID dir is removed when a job is old enough to be removed. ''' # Create temp job cache dir and jid file jid_dir, jid_file = self._make_tmp_jid_dirs() # File timestamps on Windows aren't as precise. Let a little time pass if salt.utils.platform.is_windows(): time.sleep(.01) # Make sure there is a jid directory jid_dir_name = jid_file.rpartition('/')[2] self.assertEqual(jid_dir_name, 'jid') # Call clean_old_jobs function, patching the keep_jobs value with a # very small value to force the call to clean the job. with patch.dict(local_cache.__opts__, {'keep_jobs': 0.00000001}): local_cache.clean_old_jobs() # there should be only 1 dir in TMP_JID_DIR self.assertEqual(1, len(os.listdir(TMP_JID_DIR))) # top level dir should still be present self.assertEqual(True, os.path.exists(jid_dir)) self.assertEqual(True, os.path.isdir(jid_dir)) # while the 'jid' dir inside it should be gone self.assertEqual(False, os.path.exists(jid_dir_name))
def test_clean_old_jobs_empty_jid_dir_removed(self): ''' Tests that an empty JID dir is removed when it is old enough to be deleted. ''' # Create temp job cache dir without files in it. jid_dir, jid_file = self._make_tmp_jid_dirs(create_files=False) # Make sure there are no files in the directory before continuing self.assertEqual(jid_file, None) # Call clean_old_jobs function, patching the keep_jobs value with a # very small value to force the call to clean the job. with patch.dict(local_cache.__opts__, {'keep_jobs': 0.00000001}): local_cache.clean_old_jobs() # Assert that the JID dir was removed self.assertEqual([], os.listdir(TMP_JID_DIR))
def test_clean_old_jobs_jid_file_is_cleaned(self): ''' Test that the entire JID dir is removed when a job is old enough to be removed. ''' # Create temp job cache dir and jid file jid_dir, jid_file = self._make_tmp_jid_dirs() # Make sure there is a jid directory jid_dir_name = jid_file.rpartition('/')[2] self.assertEqual(jid_dir_name, 'jid') # Call clean_old_jobs function, patching the keep_jobs value with a # very small value to force the call to clean the job. with patch.dict(local_cache.__opts__, {'keep_jobs': 0.00000001}): local_cache.clean_old_jobs() # Assert that the JID dir was removed self.assertEqual([], os.listdir(TMP_JID_DIR))
def test_clean_old_jobs_empty_jid_dir_remains(self): ''' Tests that an empty JID dir is NOT removed because it was created within the keep_jobs time frame. ''' # Create temp job cache dir without files in it. jid_dir, jid_file = self._make_tmp_jid_dirs(create_files=False) # Make sure there are no files in the directory self.assertEqual(jid_file, None) # Call clean_old_jobs function local_cache.clean_old_jobs() # Get the name of the JID directory that was created to test against jid_dir_name = jid_dir.rpartition('/')[2] # Assert the JID directory is still present to be cleaned after keep_jobs interval self.assertEqual([jid_dir_name], os.listdir(TMP_JID_DIR))
def test_clean_old_jobs_jid_file_corrupted(self): ''' Tests that the entire JID dir is removed when the jid_file is not a file. This scenario indicates a corrupted cache entry, so the entire dir is scrubbed. ''' # Create temp job cache dir and jid file jid_dir, jid_file = self._make_tmp_jid_dirs() # Make sure there is a jid file in a new job cache director jid_dir_name = jid_file.rpartition('/')[2] self.assertEqual(jid_dir_name, 'jid') # Even though we created a valid jid file in the _make_tmp_jid_dirs call to get # into the correct loop, we need to mock the 'os.path.isfile' check to force the # "corrupted file" check in the clean_old_jobs call. with patch('os.path.isfile', MagicMock(return_value=False)) as mock: local_cache.clean_old_jobs() # Assert that the JID dir was removed self.assertEqual([], os.listdir(TMP_JID_DIR))
def test_clean_old_jobs(self): """ test to ensure jobs are removed from job cache """ self._add_job() # remove job self.assertEqual(local_cache.clean_old_jobs(), None) self._check_dir_files("job cache was not removed: ", self.JOB_CACHE_DIR_FILES, status="removed")
def test_clean_old_jobs(self): ''' test to ensure jobs are removed from job cache ''' self._add_job() # remove job self.assertEqual(local_cache.clean_old_jobs(), None) self._check_dir_files('job cache was not removed: ', self.JOB_CACHE_DIR_FILES, status='removed')
def test_clean_old_jobs(self): ''' test to ensure jobs are removed from job cache ''' self._add_job() # remove job self.assertEqual(local_cache.clean_old_jobs(), None) self._check_dir_files('job cache was not removed: ', JOB_CACHE_DIR_FILES, status='removed')
def test_not_clean_new_jobs(self): ''' test to ensure jobs are not removed when jobs dir is new ''' self._add_job() with patch.dict(local_cache.__opts__, {'keep_jobs': 24}): self.assertEqual(local_cache.clean_old_jobs(), None) self._check_dir_files('job cache was removed: ', self.JOB_CACHE_DIR_FILES, status='present')
def test_not_clean_new_jobs(self): """ test to ensure jobs are not removed when jobs dir is new """ self._add_job() with patch.dict(local_cache.__opts__, {"keep_jobs": 24}): self.assertEqual(local_cache.clean_old_jobs(), None) self._check_dir_files("job cache was removed: ", self.JOB_CACHE_DIR_FILES, status="present")
def test_clean_old_jobs(self): """ test to ensure jobs are removed from job cache """ self._add_job() if salt.utils.platform.is_windows(): time.sleep(0.01) # remove job self.assertEqual(local_cache.clean_old_jobs(), None) self._check_dir_files( "job cache was not removed: ", self.JOB_CACHE_DIR_FILES, status="removed" )
def test_not_clean_new_jobs(self): ''' test to ensure jobs are not removed when jobs dir is new ''' self._add_job() local_cache.__opts__['keep_jobs'] = 24 self.assertEqual(local_cache.clean_old_jobs(), None) self._check_dir_files('job cache was removed: ', JOB_CACHE_DIR_FILES, status='present') # need to set back to initial KEEP_JOBS local_cache.__opts__['keep_jobs'] = KEEP_JOBS
def test_empty_jid_dir(self): """ test to ensure removal of empty jid dir """ # add empty jid dir new_jid_dir = os.path.join(self.JOBS_DIR, "z0") self.EMPTY_JID_DIR.append(new_jid_dir) os.makedirs(new_jid_dir) # This needed due to a race condition in Windows # `os.makedirs` hasn't released the handle before # `local_cache.clean_old_jobs` tries to delete the new_jid_dir if salt.utils.platform.is_windows(): import time lock_dir = new_jid_dir + ".lckchk" tries = 0 while True: tries += 1 if tries > 10: break # Rename the directory and name it back # If it fails, the directory handle is not released, try again # If it succeeds, break and continue test try: os.rename(new_jid_dir, lock_dir) time.sleep(1) os.rename(lock_dir, new_jid_dir) break except WindowsError: # pylint: disable=E0602 continue # check dir exists self._check_dir_files("new_jid_dir was not created", self.EMPTY_JID_DIR, status="present") # remove job self.assertEqual(local_cache.clean_old_jobs(), None) # check jid dir is removed self._check_dir_files("new_jid_dir was not removed", self.EMPTY_JID_DIR, status="removed")
def test_empty_jid_dir(self): ''' test to ensure removal of empty jid dir ''' # add empty jid dir new_jid_dir = os.path.join(JOBS_DIR, 'z0') EMPTY_JID_DIR.append(new_jid_dir) os.makedirs(new_jid_dir) # check dir exists self._check_dir_files('new_jid_dir was not created', EMPTY_JID_DIR, status='present') # remove job self.assertEqual(local_cache.clean_old_jobs(), None) # check jid dir is removed self._check_dir_files('new_jid_dir was not removed', EMPTY_JID_DIR, status='removed')
def test_clean_old_jobs_no_jid_root(self): ''' Tests that the function returns None when no jid_root is found. ''' with patch('os.path.exists', MagicMock(return_value=False)): self.assertEqual(local_cache.clean_old_jobs(), None)
def test_clean_old_jobs_no_jid_root(self): ''' Tests that the function returns None when no jid_root is found. ''' self.assertEqual(local_cache.clean_old_jobs(), None)