test_models.py 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675
  1. import tempfile
  2. import uuid
  3. from contextlib import contextmanager
  4. from datetime import timedelta
  5. from io import StringIO
  6. from pathlib import Path
  7. from unittest.mock import MagicMock, patch
  8. from django.contrib.contenttypes.models import ContentType
  9. from django.core.exceptions import ObjectDoesNotExist
  10. from django.core.management import call_command
  11. from django.db import IntegrityError, connection
  12. from django.test import TestCase, override_settings, tag
  13. from django.test.utils import CaptureQueriesContext
  14. from django.utils import timezone, translation
  15. from django.utils.functional import Promise
  16. from django.utils.translation import gettext
  17. from core.choices import DataSourceStatusChoices, JobNotificationChoices, JobStatusChoices, ObjectChangeActionChoices
  18. from core.data_backends import LocalBackend
  19. from core.events import JOB_COMPLETED, JOB_FAILED
  20. from core.exceptions import SyncError
  21. from core.models import AutoSyncRecord, DataFile, DataSource, Job, ObjectType
  22. from core.signals import post_sync
  23. from dcim.models import Device, Location, Site
  24. from extras.models import ConfigTemplate, Notification
  25. from netbox.constants import CENSOR_TOKEN, CENSOR_TOKEN_CHANGED
  26. from users.models import User
  27. from utilities.querysets import RestrictedQuerySet
  28. from utilities.testing import PinnedConnectionRouter
  29. class DataSourceTestCase(TestCase):
  30. def setUp(self):
  31. # A synced local source, used by the sync tests
  32. temp_dir = tempfile.TemporaryDirectory()
  33. self.addCleanup(temp_dir.cleanup)
  34. self.root = Path(temp_dir.name)
  35. (self.root / 'a.txt').write_text('old\n')
  36. (self.root / 'obsolete.txt').write_text('old\n')
  37. self.datasource = DataSource.objects.create(name='Data Source 2', type='local', source_url=self.root.as_uri())
  38. self.datasource.sync()
  39. self.original_files = list(self.datasource.datafiles.values())
  40. self.last_synced = self.datasource.last_synced
  41. # The next sync finds one file changed, one removed and one added
  42. (self.root / 'a.txt').write_text('new\n')
  43. (self.root / 'obsolete.txt').unlink()
  44. (self.root / 'b.txt').write_text('new\n')
  45. def assert_files_unchanged(self):
  46. self.assertEqual(list(self.datasource.datafiles.values()), self.original_files)
  47. self.datasource.refresh_from_db()
  48. self.assertEqual(self.datasource.last_synced, self.last_synced)
  49. def assert_files_synced(self):
  50. self.assertEqual(
  51. dict(self.datasource.datafiles.values_list('path', 'data')),
  52. {'a.txt': b'new\n', 'b.txt': b'new\n'},
  53. )
  54. self.datasource.refresh_from_db()
  55. self.assertEqual(self.datasource.status, DataSourceStatusChoices.COMPLETED)
  56. self.assertGreater(self.datasource.last_synced, self.last_synced)
  57. def test_no_ignore_rules(self):
  58. ds = DataSource(ignore_rules='')
  59. self.assertFalse(ds._ignore('README.md'))
  60. self.assertFalse(ds._ignore('subdir/file.py'))
  61. def test_ignore_by_filename(self):
  62. ds = DataSource(ignore_rules='*.txt')
  63. self.assertTrue(ds._ignore('notes.txt'))
  64. self.assertTrue(ds._ignore('subdir/notes.txt'))
  65. self.assertFalse(ds._ignore('notes.py'))
  66. def test_ignore_by_subdirectory(self):
  67. ds = DataSource(ignore_rules='dev/*')
  68. self.assertTrue(ds._ignore('dev/README.md'))
  69. self.assertTrue(ds._ignore('dev/script.py'))
  70. self.assertFalse(ds._ignore('prod/script.py'))
  71. def test_password_added_on_create(self):
  72. datasource = DataSource.objects.create(
  73. name='Data Source 1',
  74. type='git',
  75. source_url='http://localhost/',
  76. parameters={
  77. 'username': 'jeff',
  78. 'password': 'foobar123',
  79. }
  80. )
  81. objectchange = datasource.to_objectchange(ObjectChangeActionChoices.ACTION_CREATE)
  82. self.assertIsNone(objectchange.prechange_data)
  83. self.assertEqual(objectchange.postchange_data['parameters']['username'], 'jeff')
  84. self.assertEqual(objectchange.postchange_data['parameters']['password'], CENSOR_TOKEN_CHANGED)
  85. def test_password_added_on_update(self):
  86. datasource = DataSource.objects.create(
  87. name='Data Source 1',
  88. type='git',
  89. source_url='http://localhost/'
  90. )
  91. datasource.snapshot()
  92. # Add a blank password
  93. datasource.parameters = {
  94. 'username': 'jeff',
  95. 'password': '',
  96. }
  97. objectchange = datasource.to_objectchange(ObjectChangeActionChoices.ACTION_UPDATE)
  98. self.assertIsNone(objectchange.prechange_data['parameters'])
  99. self.assertEqual(objectchange.postchange_data['parameters']['username'], 'jeff')
  100. self.assertEqual(objectchange.postchange_data['parameters']['password'], '')
  101. # Add a password
  102. datasource.parameters = {
  103. 'username': 'jeff',
  104. 'password': 'foobar123',
  105. }
  106. objectchange = datasource.to_objectchange(ObjectChangeActionChoices.ACTION_UPDATE)
  107. self.assertEqual(objectchange.postchange_data['parameters']['username'], 'jeff')
  108. self.assertEqual(objectchange.postchange_data['parameters']['password'], CENSOR_TOKEN_CHANGED)
  109. def test_password_changed(self):
  110. datasource = DataSource.objects.create(
  111. name='Data Source 1',
  112. type='git',
  113. source_url='http://localhost/',
  114. parameters={
  115. 'username': 'jeff',
  116. 'password': 'password1',
  117. }
  118. )
  119. datasource.snapshot()
  120. # Change the password
  121. datasource.parameters['password'] = 'password2'
  122. objectchange = datasource.to_objectchange(ObjectChangeActionChoices.ACTION_UPDATE)
  123. self.assertEqual(objectchange.prechange_data['parameters']['username'], 'jeff')
  124. self.assertEqual(objectchange.prechange_data['parameters']['password'], CENSOR_TOKEN)
  125. self.assertEqual(objectchange.postchange_data['parameters']['username'], 'jeff')
  126. self.assertEqual(objectchange.postchange_data['parameters']['password'], CENSOR_TOKEN_CHANGED)
  127. def test_password_removed_on_update(self):
  128. datasource = DataSource.objects.create(
  129. name='Data Source 1',
  130. type='git',
  131. source_url='http://localhost/',
  132. parameters={
  133. 'username': 'jeff',
  134. 'password': 'foobar123',
  135. }
  136. )
  137. datasource.snapshot()
  138. objectchange = datasource.to_objectchange(ObjectChangeActionChoices.ACTION_UPDATE)
  139. self.assertEqual(objectchange.prechange_data['parameters']['username'], 'jeff')
  140. self.assertEqual(objectchange.prechange_data['parameters']['password'], CENSOR_TOKEN)
  141. self.assertEqual(objectchange.postchange_data['parameters']['username'], 'jeff')
  142. self.assertEqual(objectchange.postchange_data['parameters']['password'], CENSOR_TOKEN)
  143. # Remove the password
  144. datasource.parameters['password'] = ''
  145. objectchange = datasource.to_objectchange(ObjectChangeActionChoices.ACTION_UPDATE)
  146. self.assertEqual(objectchange.prechange_data['parameters']['username'], 'jeff')
  147. self.assertEqual(objectchange.prechange_data['parameters']['password'], CENSOR_TOKEN)
  148. self.assertEqual(objectchange.postchange_data['parameters']['username'], 'jeff')
  149. self.assertEqual(objectchange.postchange_data['parameters']['password'], '')
  150. def test_password_not_modified(self):
  151. datasource = DataSource.objects.create(
  152. name='Data Source 1',
  153. type='git',
  154. source_url='http://localhost/',
  155. parameters={
  156. 'username': 'username1',
  157. 'password': 'foobar123',
  158. }
  159. )
  160. datasource.snapshot()
  161. # Remove the password
  162. datasource.parameters['username'] = 'username2'
  163. objectchange = datasource.to_objectchange(ObjectChangeActionChoices.ACTION_UPDATE)
  164. self.assertEqual(objectchange.prechange_data['parameters']['username'], 'username1')
  165. self.assertEqual(objectchange.prechange_data['parameters']['password'], CENSOR_TOKEN)
  166. self.assertEqual(objectchange.postchange_data['parameters']['username'], 'username2')
  167. self.assertEqual(objectchange.postchange_data['parameters']['password'], CENSOR_TOKEN)
  168. def test_sync_rolls_back_read_failure(self):
  169. """A sync that fails reading a new file keeps the synced files and their links."""
  170. obsolete = self.datasource.datafiles.get(path='obsolete.txt')
  171. template = ConfigTemplate.objects.create(
  172. name='Config Template 1',
  173. data_source=self.datasource,
  174. data_file=obsolete,
  175. data_path=obsolete.path,
  176. template_code='old\n',
  177. auto_sync_enabled=True,
  178. )
  179. autosync = AutoSyncRecord.objects.get(datafile=obsolete)
  180. (self.root / 'link.txt').symlink_to('missing.txt')
  181. with patch.object(post_sync, 'send') as send:
  182. with self.assertRaises(FileNotFoundError):
  183. call_command('syncdatasource', self.datasource.name, stdout=StringIO())
  184. self.assert_files_unchanged()
  185. self.assertEqual(self.datasource.status, DataSourceStatusChoices.FAILED)
  186. template.refresh_from_db()
  187. self.assertEqual(template.data_file_id, obsolete.pk)
  188. self.assertTrue(AutoSyncRecord.objects.filter(pk=autosync.pk, datafile=obsolete).exists())
  189. send.assert_not_called()
  190. def test_sync_rolls_back_create_failure(self):
  191. """A sync that fails creating its new files keeps the synced files."""
  192. with patch.object(RestrictedQuerySet, 'bulk_create', side_effect=IntegrityError('Simulated bulk create error')):
  193. with self.assertRaisesMessage(IntegrityError, 'Simulated bulk create error'):
  194. self.datasource.sync()
  195. self.assert_files_unchanged()
  196. def test_sync_rolls_back_backend_cleanup_failure(self):
  197. """A sync whose backend fails during cleanup keeps the synced files."""
  198. @contextmanager
  199. def fetch(backend):
  200. yield str(self.root)
  201. raise OSError('Simulated cleanup error')
  202. with patch.object(LocalBackend, 'fetch', fetch):
  203. with self.assertRaisesMessage(OSError, 'Simulated cleanup error'):
  204. self.datasource.sync()
  205. self.assert_files_unchanged()
  206. def test_sync_locks_datasource_before_reading_files(self):
  207. """Take the sync lock inside the transaction, before the files are read and the sync completes."""
  208. with CaptureQueriesContext(connection) as context:
  209. self.datasource.sync()
  210. queries = [query['sql'] for query in context.captured_queries]
  211. begins = [i for i, sql in enumerate(queries) if sql.startswith('SAVEPOINT')]
  212. locks = [i for i, sql in enumerate(queries) if 'pg_advisory_xact_lock' in sql]
  213. reads = [i for i, sql in enumerate(queries) if sql.startswith('SELECT') and 'FROM "core_datafile"' in sql]
  214. completions = [
  215. i for i, sql in enumerate(queries) if sql.startswith('UPDATE "core_datasource"') and "'completed'" in sql
  216. ]
  217. commits = [i for i, sql in enumerate(queries) if sql.startswith('RELEASE SAVEPOINT')]
  218. self.assertTrue(locks)
  219. self.assertLess(begins[0], locks[0])
  220. self.assertLess(locks[0], reads[0])
  221. self.assertLess(reads[0], completions[0])
  222. self.assertLess(completions[0], commits[0])
  223. def test_post_sync_failure_preserves_synced_files(self):
  224. """A failing post_sync receiver leaves the completed sync in place."""
  225. with patch.object(post_sync, 'send', side_effect=SyncError('Simulated post_sync error')):
  226. with self.assertRaisesMessage(SyncError, 'Simulated post_sync error'):
  227. self.datasource.sync()
  228. self.assert_files_synced()
  229. def test_sync_pins_datafile_queries_to_transaction_connection(self):
  230. """Run the data file reads, updates and deletes of a sync on its transaction's connection."""
  231. # Django routes a new data file's construction and validation itself
  232. (self.root / 'b.txt').unlink()
  233. with override_settings(DATABASE_ROUTERS=[PinnedConnectionRouter(DataFile)]):
  234. self.datasource.sync()
  235. self.assertEqual(dict(self.datasource.datafiles.values_list('path', 'data')), {'a.txt': b'new\n'})
  236. def test_sync_keeps_paths_repeating_source_root(self):
  237. """A file below a directory repeating the source root syncs under its full path."""
  238. # Repeat the whole root string below it, as /scripts/scripts_old does for /scripts
  239. nested_path = f'{self.root}_old/legacy.py'.lstrip('/')
  240. (self.root / nested_path).parent.mkdir(parents=True)
  241. (self.root / nested_path).write_text('nested\n')
  242. (self.root / 'legacy.py').write_text('root\n')
  243. self.datasource.sync()
  244. self.assertEqual(
  245. dict(self.datasource.datafiles.values_list('path', 'data')),
  246. {'a.txt': b'new\n', 'b.txt': b'new\n', 'legacy.py': b'root\n', nested_path: b'nested\n'},
  247. )
  248. class ObjectTypeTestCase(TestCase):
  249. def test_create(self):
  250. """
  251. Test that an ObjectType created for a given app_label & model name will be automatically assigned to
  252. the appropriate ContentType.
  253. """
  254. kwargs = {
  255. 'app_label': 'foo',
  256. 'model': 'bar',
  257. }
  258. ct = ContentType.objects.create(**kwargs)
  259. ot = ObjectType.objects.create(**kwargs)
  260. self.assertEqual(ot.contenttype_ptr, ct)
  261. def test_get_by_natural_key(self):
  262. """
  263. Test that get_by_natural_key() returns the appropriate ObjectType.
  264. """
  265. self.assertEqual(
  266. ObjectType.objects.get_by_natural_key('dcim', 'site'),
  267. ObjectType.objects.get(app_label='dcim', model='site')
  268. )
  269. with self.assertRaises(ObjectDoesNotExist):
  270. ObjectType.objects.get_by_natural_key('foo', 'bar')
  271. def test_get_for_id(self):
  272. """
  273. Test that get_by_id() returns the appropriate ObjectType.
  274. """
  275. ot = ObjectType.objects.get_by_natural_key('dcim', 'site')
  276. self.assertEqual(
  277. ObjectType.objects.get_for_id(ot.pk),
  278. ObjectType.objects.get(pk=ot.pk)
  279. )
  280. with self.assertRaises(ObjectDoesNotExist):
  281. ObjectType.objects.get_for_id(0)
  282. def test_get_for_model(self):
  283. """
  284. Test that get_by_model() returns the appropriate ObjectType.
  285. """
  286. self.assertEqual(
  287. ObjectType.objects.get_for_model(Site),
  288. ObjectType.objects.get_by_natural_key('dcim', 'site')
  289. )
  290. def test_get_for_models(self):
  291. """
  292. Test that get_by_models() returns the appropriate ObjectType mapping.
  293. """
  294. self.assertEqual(
  295. ObjectType.objects.get_for_models(Site, Location, Device),
  296. {
  297. Site: ObjectType.objects.get_by_natural_key('dcim', 'site'),
  298. Location: ObjectType.objects.get_by_natural_key('dcim', 'location'),
  299. Device: ObjectType.objects.get_by_natural_key('dcim', 'device'),
  300. }
  301. )
  302. def test_public(self):
  303. """
  304. Test that public() returns only ObjectTypes for public models.
  305. """
  306. public_ots = ObjectType.objects.public()
  307. self.assertIn(ObjectType.objects.get_by_natural_key('dcim', 'site'), public_ots)
  308. self.assertNotIn(ObjectType.objects.get_by_natural_key('extras', 'taggeditem'), public_ots)
  309. def test_with_feature(self):
  310. """
  311. Test that with_feature() returns only ObjectTypes for models which support the specified feature.
  312. """
  313. bookmarks_ots = ObjectType.objects.with_feature('bookmarks')
  314. self.assertIn(ObjectType.objects.get_by_natural_key('dcim', 'site'), bookmarks_ots)
  315. self.assertNotIn(ObjectType.objects.get_by_natural_key('dcim', 'cabletermination'), bookmarks_ots)
  316. class JobTestCase(TestCase):
  317. def _make_job(self, user, notifications, **kwargs):
  318. """
  319. Create and return a persisted Job with the given user and notifications setting.
  320. """
  321. return Job.objects.create(
  322. name='Test Job',
  323. job_id=uuid.uuid4(),
  324. user=user,
  325. notifications=notifications,
  326. status=JobStatusChoices.STATUS_RUNNING,
  327. **kwargs,
  328. )
  329. @patch('core.models.jobs.django_rq.get_queue')
  330. def test_delete_cancels_job_from_correct_queue(self, mock_get_queue):
  331. """
  332. Test that when a job is deleted, it's canceled from the correct queue.
  333. """
  334. mock_queue = MagicMock()
  335. mock_rq_job = MagicMock()
  336. mock_queue.fetch_job.return_value = mock_rq_job
  337. mock_get_queue.return_value = mock_queue
  338. def dummy_func(**kwargs):
  339. pass
  340. # Enqueue a job with a custom queue name
  341. custom_queue = 'my_custom_queue'
  342. job = Job.enqueue(
  343. func=dummy_func,
  344. name='Test Job',
  345. queue_name=custom_queue
  346. )
  347. # Reset mock to clear enqueue call
  348. mock_get_queue.reset_mock()
  349. # Delete the job
  350. job.delete()
  351. # Verify the correct queue was used for cancellation
  352. mock_get_queue.assert_called_with(custom_queue)
  353. mock_queue.fetch_job.assert_called_with(str(job.job_id))
  354. mock_rq_job.cancel.assert_called_once()
  355. @patch('core.models.jobs.job_start')
  356. def test_start_waiting_job(self, mock_job_start):
  357. """A pending or scheduled Job is claimed and marked running."""
  358. for status in (JobStatusChoices.STATUS_PENDING, JobStatusChoices.STATUS_SCHEDULED):
  359. with self.subTest(status=status):
  360. job = Job.objects.create(name='Test Job', job_id=uuid.uuid4(), status=status)
  361. self.assertTrue(job.start())
  362. started = job.started
  363. self.assertIsNotNone(started)
  364. self.assertEqual(job.status, JobStatusChoices.STATUS_RUNNING)
  365. job.refresh_from_db()
  366. self.assertEqual(job.status, JobStatusChoices.STATUS_RUNNING)
  367. self.assertEqual(job.started, started)
  368. mock_job_start.send.assert_called_once_with(job)
  369. mock_job_start.reset_mock()
  370. @patch('core.models.jobs.job_start')
  371. def test_start_deleted_job(self, mock_job_start):
  372. """A deleted Job is neither claimed nor re-created."""
  373. job = Job.objects.create(name='Test Job', job_id=uuid.uuid4())
  374. Job.objects.filter(pk=job.pk).delete()
  375. self.assertFalse(job.start())
  376. self.assertFalse(Job.objects.filter(pk=job.pk).exists())
  377. self.assertIsNone(job.started)
  378. mock_job_start.send.assert_not_called()
  379. @patch('core.models.jobs.job_start')
  380. def test_start_non_waiting_job(self, mock_job_start):
  381. """A running or terminated Job is not claimed and keeps its status."""
  382. for status in (JobStatusChoices.STATUS_RUNNING, *JobStatusChoices.TERMINAL_STATE_CHOICES):
  383. with self.subTest(status=status):
  384. job = Job.objects.create(name='Test Job', job_id=uuid.uuid4())
  385. Job.objects.filter(pk=job.pk).update(status=status)
  386. self.assertFalse(job.start())
  387. job.refresh_from_db()
  388. self.assertEqual(job.status, status)
  389. self.assertIsNone(job.started)
  390. mock_job_start.send.assert_not_called()
  391. @patch('core.models.jobs.job_start')
  392. def test_start_already_started_job(self, mock_job_start):
  393. """A waiting Job that already has a start time is not claimed."""
  394. job = Job.objects.create(name='Test Job', job_id=uuid.uuid4())
  395. started = timezone.now()
  396. Job.objects.filter(pk=job.pk).update(started=started)
  397. self.assertFalse(job.start())
  398. job.refresh_from_db()
  399. self.assertEqual(job.started, started)
  400. self.assertEqual(job.status, JobStatusChoices.STATUS_PENDING)
  401. mock_job_start.send.assert_not_called()
  402. @patch('core.models.jobs.job_start')
  403. def test_start_once(self, mock_job_start):
  404. """Two copies of one Job claim it only once."""
  405. job = Job.objects.create(name='Test Job', job_id=uuid.uuid4())
  406. stale_job = Job.objects.get(pk=job.pk)
  407. self.assertTrue(job.start())
  408. started = job.started
  409. self.assertFalse(stale_job.start())
  410. self.assertFalse(job.start())
  411. job.refresh_from_db()
  412. self.assertEqual(job.started, started)
  413. mock_job_start.send.assert_called_once_with(job)
  414. @patch('core.models.jobs.job_start')
  415. def test_start_job_id_mismatch(self, mock_job_start):
  416. """A stale copy cannot claim a Job whose task ID has changed."""
  417. job = Job.objects.create(name='Test Job', job_id=uuid.uuid4())
  418. stale_job = Job.objects.get(pk=job.pk)
  419. Job.objects.filter(pk=job.pk).update(job_id=uuid.uuid4())
  420. self.assertFalse(stale_job.start())
  421. job.refresh_from_db()
  422. self.assertEqual(job.status, JobStatusChoices.STATUS_PENDING)
  423. self.assertIsNone(job.started)
  424. mock_job_start.send.assert_not_called()
  425. @patch('core.models.jobs.job_start')
  426. def test_start_preserves_other_fields(self, mock_job_start):
  427. """Claiming a Job writes only its status and start time."""
  428. job = Job.objects.create(name='Test Job', job_id=uuid.uuid4())
  429. stale_job = Job.objects.get(pk=job.pk)
  430. Job.objects.filter(pk=job.pk).update(name='Renamed')
  431. self.assertTrue(stale_job.start())
  432. job.refresh_from_db()
  433. self.assertEqual(job.name, 'Renamed')
  434. self.assertEqual(job.status, JobStatusChoices.STATUS_RUNNING)
  435. @patch('core.models.jobs.job_end')
  436. def test_terminate_notification_always(self, mock_job_end):
  437. """
  438. With notifications=always, a Notification should be created for every
  439. terminal status (completed, failed, errored).
  440. """
  441. user = User.objects.create_user(username='notification-always')
  442. for status in (
  443. JobStatusChoices.STATUS_COMPLETED,
  444. JobStatusChoices.STATUS_FAILED,
  445. JobStatusChoices.STATUS_ERRORED,
  446. ):
  447. with self.subTest(status=status):
  448. job = self._make_job(user, JobNotificationChoices.NOTIFICATION_ALWAYS)
  449. job.terminate(status=status)
  450. self.assertEqual(
  451. Notification.objects.filter(user=user, object_id=job.pk).count(),
  452. 1,
  453. msg=f"Expected a notification for status={status} with notifications=always",
  454. )
  455. @patch('core.models.jobs.job_end')
  456. def test_terminate_notification_on_failure(self, mock_job_end):
  457. """
  458. With notifications=on_failure, a Notification should be created only for
  459. non-completed terminal statuses (failed, errored), not for completed.
  460. """
  461. user = User.objects.create_user(username='notification-on-failure')
  462. # No notification on successful completion
  463. job = self._make_job(user, JobNotificationChoices.NOTIFICATION_ON_FAILURE)
  464. job.terminate(status=JobStatusChoices.STATUS_COMPLETED)
  465. self.assertEqual(
  466. Notification.objects.filter(user=user, object_id=job.pk).count(),
  467. 0,
  468. msg="Expected no notification for status=completed with notifications=on_failure",
  469. )
  470. # Notification on failure/error
  471. for status in (JobStatusChoices.STATUS_FAILED, JobStatusChoices.STATUS_ERRORED):
  472. with self.subTest(status=status):
  473. job = self._make_job(user, JobNotificationChoices.NOTIFICATION_ON_FAILURE)
  474. job.terminate(status=status)
  475. self.assertEqual(
  476. Notification.objects.filter(user=user, object_id=job.pk).count(),
  477. 1,
  478. msg=f"Expected a notification for status={status} with notifications=on_failure",
  479. )
  480. @patch('core.models.jobs.job_end')
  481. def test_terminate_notification_never(self, mock_job_end):
  482. """
  483. With notifications=never, no Notification should be created regardless
  484. of terminal status.
  485. """
  486. user = User.objects.create_user(username='notification-never')
  487. for status in (
  488. JobStatusChoices.STATUS_COMPLETED,
  489. JobStatusChoices.STATUS_FAILED,
  490. JobStatusChoices.STATUS_ERRORED,
  491. ):
  492. with self.subTest(status=status):
  493. job = self._make_job(user, JobNotificationChoices.NOTIFICATION_NEVER)
  494. job.terminate(status=status)
  495. self.assertEqual(
  496. Notification.objects.filter(user=user, object_id=job.pk).count(),
  497. 0,
  498. msg=f"Expected no notification for status={status} with notifications=never",
  499. )
  500. @patch('core.models.jobs.job_end')
  501. def test_terminate_updates_existing_notification(self, mock_job_end):
  502. """Terminating a Job re-issues its owner's existing notification as unread."""
  503. user = User.objects.create_user(username='notification-existing')
  504. datasource = DataSource.objects.create(name='Data Source 1', type='local')
  505. job = self._make_job(user, JobNotificationChoices.NOTIFICATION_ALWAYS, object=datasource)
  506. previous_time = timezone.now() - timedelta(days=1)
  507. notification = Notification.objects.create(user=user, object=job, event_type=JOB_FAILED, read=previous_time)
  508. Notification.objects.filter(pk=notification.pk).update(created=previous_time)
  509. job.name = 'Renamed Job'
  510. job.terminate()
  511. notification.refresh_from_db()
  512. self.assertEqual(notification.event_type, JOB_COMPLETED)
  513. self.assertEqual(notification.object_repr, 'Renamed Job')
  514. self.assertEqual(notification.created, job.completed)
  515. self.assertIsNone(notification.read)
  516. mock_job_end.send.assert_called_once_with(job)
  517. @patch('core.models.jobs.job_end')
  518. def test_terminate_without_dispatch(self, mock_job_end):
  519. """Terminating without dispatch saves the outcome and leaves the notification and job_end for later."""
  520. user = User.objects.create_user(username='notification-deferred')
  521. job = self._make_job(user, JobNotificationChoices.NOTIFICATION_ON_FAILURE)
  522. job.terminate(status=JobStatusChoices.STATUS_FAILED, dispatch=False)
  523. job.refresh_from_db()
  524. self.assertEqual(job.status, JobStatusChoices.STATUS_FAILED)
  525. self.assertIsNotNone(job.completed)
  526. self.assertFalse(Notification.objects.filter(user=user, object_id=job.pk).exists())
  527. mock_job_end.send.assert_not_called()
  528. job._dispatch_termination()
  529. notification = Notification.objects.get(user=user, object_id=job.pk)
  530. self.assertEqual(notification.event_type, JOB_FAILED)
  531. mock_job_end.send.assert_called_once_with(job)
  532. @patch('core.models.jobs.job_end')
  533. def test_execution_time_set_on_terminate(self, mock_job_end):
  534. """
  535. terminate() should set execution_time to (completed - started) for a started job.
  536. """
  537. job = self._make_job(None, JobNotificationChoices.NOTIFICATION_NEVER)
  538. job.started = timezone.now() - timedelta(seconds=90)
  539. job.save()
  540. job.terminate(status=JobStatusChoices.STATUS_COMPLETED)
  541. self.assertIsNotNone(job.execution_time)
  542. self.assertEqual(job.execution_time, job.completed - job.started)
  543. @patch('core.models.jobs.job_end')
  544. def test_execution_time_none_before_completion(self, mock_job_end):
  545. """
  546. A job which has not completed should have a null execution_time.
  547. """
  548. job = self._make_job(None, JobNotificationChoices.NOTIFICATION_NEVER)
  549. self.assertIsNone(job.execution_time)
  550. @patch('core.models.jobs.job_end')
  551. def test_execution_time_none_when_never_started(self, mock_job_end):
  552. """
  553. Terminating a job which was never started (no started timestamp) should leave
  554. execution_time null rather than computing a value from a missing start time.
  555. """
  556. job = self._make_job(None, JobNotificationChoices.NOTIFICATION_NEVER)
  557. self.assertIsNone(job.started)
  558. job.terminate(status=JobStatusChoices.STATUS_COMPLETED)
  559. self.assertIsNone(job.execution_time)
  560. @tag('regression') # Ref: #23702
  561. def test_verbose_name_translation(self):
  562. # Meta labels are set at import, so eager gettext keeps them in DEFAULT_LANGUAGE (#23702)
  563. verbose_name = Job._meta.verbose_name
  564. self.assertIsInstance(verbose_name, Promise)
  565. with translation.override('nl'):
  566. self.assertEqual(str(verbose_name), gettext('job'))