|
|
@@ -4,13 +4,19 @@ from unittest.mock import MagicMock, patch
|
|
|
|
|
|
import requests
|
|
|
from django.conf import settings
|
|
|
+from django.db import connection
|
|
|
from django.test import TestCase, override_settings, tag
|
|
|
+from django.test.utils import CaptureQueriesContext
|
|
|
from django.utils import timezone
|
|
|
|
|
|
-from core.choices import DataSourceStatusChoices, JobStatusChoices
|
|
|
-from core.jobs import SyncDataSourceJob, SystemHousekeepingJob
|
|
|
-from core.models import DataFile, DataSource, Job
|
|
|
+from core.choices import DataSourceStatusChoices, JobIntervalChoices, JobStatusChoices
|
|
|
+from core.events import JOB_COMPLETED
|
|
|
+from core.jobs import HourlyCleanupJob, SyncDataSourceJob, SystemHousekeepingJob
|
|
|
+from core.models import DataFile, DataSource, Job, ObjectType
|
|
|
from core.signals import job_end
|
|
|
+from extras.models import Notification
|
|
|
+from netbox.registry import registry
|
|
|
+from users.models import User
|
|
|
|
|
|
|
|
|
def _make_runner(cls, **job_attrs):
|
|
|
@@ -310,6 +316,69 @@ class DeleteExpiredJobsTestCase(HousekeepingRunnerMixin, TestCase):
|
|
|
self.assertTrue(Job.objects.filter(pk=recent_job.pk).exists())
|
|
|
|
|
|
|
|
|
+class HourlyCleanupJobTestCase(TestCase):
|
|
|
+ def test_registered_as_hourly_system_job(self):
|
|
|
+ self.assertEqual(
|
|
|
+ registry['system_jobs'][HourlyCleanupJob]['interval'], JobIntervalChoices.INTERVAL_HOURLY
|
|
|
+ )
|
|
|
+
|
|
|
+ def test_cleanup_jobs_expire(self):
|
|
|
+ self.assertEqual(HourlyCleanupJob.Meta.lifetime, JobIntervalChoices.INTERVAL_DAILY)
|
|
|
+
|
|
|
+ def test_run_executes_all_cleanup_tasks(self):
|
|
|
+ with patch.object(HourlyCleanupJob, 'delete_expired_jobs') as delete_expired_jobs:
|
|
|
+ _make_runner(HourlyCleanupJob).run()
|
|
|
+ delete_expired_jobs.assert_called_once_with()
|
|
|
+
|
|
|
+ def test_delete_expired_jobs_deletes_only_expired_jobs(self):
|
|
|
+ now = timezone.now()
|
|
|
+ Job.objects.bulk_create([
|
|
|
+ Job(name='expired', job_id=uuid.uuid4(), expires=now - timedelta(minutes=1)),
|
|
|
+ Job(name='unexpired', job_id=uuid.uuid4(), expires=now + timedelta(minutes=1)),
|
|
|
+ Job(name='no expiration', job_id=uuid.uuid4()),
|
|
|
+ ])
|
|
|
+ # Jobs without an expiration are left to housekeeping, however old
|
|
|
+ Job.objects.filter(name='no expiration').update(created=now - timedelta(days=365))
|
|
|
+
|
|
|
+ _make_runner(HourlyCleanupJob).delete_expired_jobs()
|
|
|
+
|
|
|
+ self.assertEqual(set(Job.objects.values_list('name', flat=True)), {'unexpired', 'no expiration'})
|
|
|
+
|
|
|
+ def test_delete_expired_jobs_in_batches(self):
|
|
|
+ expires = timezone.now() - timedelta(minutes=1)
|
|
|
+ Job.objects.bulk_create([Job(name=f'expired {i}', job_id=uuid.uuid4(), expires=expires) for i in range(5)])
|
|
|
+
|
|
|
+ runner = _make_runner(HourlyCleanupJob)
|
|
|
+ with patch('core.jobs.JOB_DELETE_BATCH_SIZE', 2), CaptureQueriesContext(connection) as queries:
|
|
|
+ runner.delete_expired_jobs()
|
|
|
+
|
|
|
+ self.assertFalse(Job.objects.exists())
|
|
|
+ job_queries = [q['sql'] for q in queries if '"core_job"' in q['sql']]
|
|
|
+ self.assertEqual(sum(sql.startswith('DELETE FROM "core_job"') for sql in job_queries), 3)
|
|
|
+ # Deletion loads only each job's PK, not its data or log entries
|
|
|
+ self.assertFalse(any('"core_job"."data"' in sql for sql in job_queries))
|
|
|
+ runner.logger.info.assert_called_once_with('Deleted 5 expired jobs')
|
|
|
+
|
|
|
+ def test_delete_expired_jobs_deletes_notifications(self):
|
|
|
+ user = User.objects.create_user(username='job-owner')
|
|
|
+ now = timezone.now()
|
|
|
+ expired, unexpired = Job.objects.bulk_create([
|
|
|
+ Job(name='expired', job_id=uuid.uuid4(), user=user, expires=now - timedelta(minutes=1)),
|
|
|
+ Job(name='unexpired', job_id=uuid.uuid4(), user=user, expires=now + timedelta(minutes=1)),
|
|
|
+ ])
|
|
|
+ job_type = ObjectType.objects.get_for_model(Job)
|
|
|
+ Notification.objects.bulk_create([
|
|
|
+ Notification(
|
|
|
+ user=user, object_type=job_type, object_id=job.pk, object_repr=job.name, event_type=JOB_COMPLETED
|
|
|
+ )
|
|
|
+ for job in (expired, unexpired)
|
|
|
+ ])
|
|
|
+
|
|
|
+ _make_runner(HourlyCleanupJob).delete_expired_jobs()
|
|
|
+
|
|
|
+ self.assertEqual(list(Notification.objects.values_list('object_id', flat=True)), [unexpired.pk])
|
|
|
+
|
|
|
+
|
|
|
class CheckForNewReleasesTestCase(HousekeepingRunnerMixin, TestCase):
|
|
|
@override_settings(ISOLATED_DEPLOYMENT=True)
|
|
|
def test_check_for_new_releases_skips_when_isolated(self):
|