def test_filter_expired_tmp_jobs(mock_get_temporary_jobs): two_days_ago = datetime.datetime.now(dateutil.tz.tzutc()) - datetime.timedelta(days=2) one_hour_ago = datetime.datetime.now(dateutil.tz.tzutc()) - datetime.timedelta(hours=1) mock_get_temporary_jobs.side_effect = [ [{'name': 'tmp foo bar', 'lastSuccess': two_days_ago.isoformat()}], [{'name': 'tmp anotherservice anotherinstance', 'lastSuccess': one_hour_ago.isoformat()}], ] actual = cleanup_chronos_jobs.filter_expired_tmp_jobs(mock.Mock(), ['foo bar', 'anotherservice anotherinstance']) assert actual == ['foo bar']
def test_filter_expired_tmp_jobs(mock_get_temporary_jobs): two_days_ago = datetime.datetime.now(dateutil.tz.tzutc()) - datetime.timedelta(days=2) one_hour_ago = datetime.datetime.now(dateutil.tz.tzutc()) - datetime.timedelta(hours=1) mock_get_temporary_jobs.side_effect = [ [{'name': 'tmp foo bar', 'lastSuccess': two_days_ago.isoformat()}], [{'name': 'tmp anotherservice anotherinstance', 'lastSuccess': one_hour_ago.isoformat()}], ] actual = cleanup_chronos_jobs.filter_expired_tmp_jobs(mock.Mock(), ['foo bar', 'anotherservice anotherinstance']) assert actual == ['foo bar']
def test_filter_expired_tmp_jobs(mock_get_temporary_jobs, mock_load_chronos_job_config, mock_load_chronos_config): two_days_ago = datetime.datetime.now(dateutil.tz.tzutc()) - datetime.timedelta(days=2) one_hour_ago = datetime.datetime.now(dateutil.tz.tzutc()) - datetime.timedelta(hours=1) # Ensure that a tmp job with a very long schedule interval is only cleaned # up if the last success was outside the schedule interval seconds_in_one_month = 2592000 seconds_in_one_day = 86400 # Ensure that a tmp job with a very short schedule is kept for at least a day seconds_in_half_hour = 1300 mock_chronos_job = mock.Mock(autospec=True) mock_chronos_job.get_schedule_interval_in_seconds.side_effect = [ seconds_in_one_month, seconds_in_one_day, seconds_in_one_day, seconds_in_half_hour, seconds_in_one_day, ] mock_load_chronos_job_config.side_effect = [ mock_chronos_job, mock_chronos_job, mock_chronos_job, mock_chronos_job, NoConfigurationForServiceError, # Test that we handle not being able to get a job's config ] mock_get_temporary_jobs.side_effect = [ [{'name': 'tmp long batch', 'lastSuccess': two_days_ago.isoformat()}], [{'name': 'tmp foo bar', 'lastSuccess': two_days_ago.isoformat()}], [{'name': 'tmp anotherservice anotherinstance', 'lastSuccess': one_hour_ago.isoformat()}], [{'name': 'tmp short batch', 'lastSuccess': one_hour_ago.isoformat()}], [{'name': 'tmp nonexistent batch', 'lastSuccess': one_hour_ago.isoformat()}], ] actual = cleanup_chronos_jobs.filter_expired_tmp_jobs( mock.Mock(), ['long batch', 'foo bar', 'anotherservice anotherinstance', 'short batch', 'nonexistent batch'], cluster='fake_cluster', soa_dir='/soa/dir', ) assert actual == ['foo bar']