PageRenderTime 64ms CodeModel.GetById 32ms RepoModel.GetById 0ms app.codeStats 0ms

/server/test/unit/server/async/test_celery_instance.py

https://github.com/alexxa/pulp
Python | 173 lines | 119 code | 16 blank | 38 comment | 2 complexity | ea6eabccd09a3756a0dcc23499393c8f MD5 | raw file
Possible License(s): GPL-2.0
  1. """
  2. This module contains tests for the pulp.server.async.celery_instance module.
  3. """
  4. from datetime import timedelta
  5. from functools import partial
  6. import os
  7. import ssl
  8. import unittest
  9. import mock
  10. from pulp.server.async import celery_instance
  11. from pulp.server.config import config, _default_values
  12. from pulp.server.constants import PULP_DJANGO_SETTINGS_MODULE
  13. from pulp.server.controllers.repository import queue_download_deferred
  14. from pulp.server.db.reaper import queue_reap_expired_documents
  15. from pulp.server.maintenance.monthly import queue_monthly_maintenance
  16. class TestCelerybeatSchedule(unittest.TestCase):
  17. """
  18. Assert that the CELERYBEAT_SCHEDULE structure has the expected tasks in it.
  19. """
  20. def test_num_tasks(self):
  21. """
  22. Assert that the expected number of beat tasks are in the CELERYBEAT_SCHEDULE. If you find
  23. youself adjusting this test because you added a new task to CELERYBEAT_SCHEDULE, please do
  24. add another unit test to this test class asserting that your new task is present with the
  25. correct information. Thanks!
  26. """
  27. # Please read the docblock to this test if you find yourself needing to adjust this
  28. # assertion.
  29. self.assertEqual(len(celery_instance.celery.conf['CELERYBEAT_SCHEDULE']), 3)
  30. def test_reap_expired_documents(self):
  31. """
  32. Make sure the reap_expired_documents Task is present and properly configured.
  33. """
  34. reap = celery_instance.celery.conf['CELERYBEAT_SCHEDULE']['reap_expired_documents']
  35. expected_reap = {
  36. 'task': queue_reap_expired_documents.name,
  37. 'schedule': timedelta(days=(config.getfloat('data_reaping', 'reaper_interval'))),
  38. 'args': tuple(),
  39. }
  40. self.assertEqual(reap, expected_reap)
  41. def test_monthly_maintenance(self):
  42. """
  43. Make sure the monthly maintenance Task is present and properly configured.
  44. """
  45. expected_monthly_maintenance = {
  46. 'task': queue_monthly_maintenance.name,
  47. 'schedule': timedelta(days=30),
  48. 'args': tuple(),
  49. }
  50. self.assertEqual(celery_instance.celery.conf['CELERYBEAT_SCHEDULE']['monthly_maintenance'],
  51. expected_monthly_maintenance)
  52. def test_download_deferred_content(self):
  53. """
  54. Make sure the monthly maintenance Task is present and properly configured.
  55. """
  56. expected_download_deferred = {
  57. 'task': queue_download_deferred.name,
  58. 'schedule': timedelta(minutes=config.getint('lazy', 'download_interval')),
  59. 'args': tuple(),
  60. }
  61. self.assertEqual(
  62. celery_instance.celery.conf['CELERYBEAT_SCHEDULE']['download_deferred_content'],
  63. expected_download_deferred
  64. )
  65. def test_celery_conf_updated(self):
  66. """
  67. Make sure the Celery config was updated with our CELERYBEAT_SCHEDULE.
  68. """
  69. self.assertEqual(celery_instance.celery.conf['CELERYBEAT_SCHEDULE'],
  70. celery_instance.CELERYBEAT_SCHEDULE)
  71. class TestCelerySerializerConfig(unittest.TestCase):
  72. def test_json_serializer_is_used(self):
  73. self.assertEqual(celery_instance.celery.conf['CELERY_TASK_SERIALIZER'], 'json')
  74. def test_json_serializer_is_the_only_one_allowed(self):
  75. self.assertEqual(celery_instance.celery.conf['CELERY_ACCEPT_CONTENT'], ['json'])
  76. def fake_get(new_config, section, key):
  77. """
  78. Fake version of the get() method from pulp's config object. This is useful
  79. so we can have concrete values to test for.
  80. """
  81. if section in new_config.keys() and new_config[section].get(key, False):
  82. return new_config[section].get(key)
  83. else:
  84. return _default_values[section][key]
  85. def _get_database_test_config():
  86. """
  87. Returns a configuration that contains a definition for the 'database' section of the config.
  88. This is a method so that every copy returned is independent since the caller usually modifies
  89. it some.
  90. :return: A dict containing configuration values for the 'database' section.
  91. :rtype dict
  92. """
  93. config = {
  94. 'database': {
  95. 'name': 'database_name',
  96. 'seeds': 'host1:27017,host2:27017',
  97. 'username': 'someguy',
  98. 'password': 'letmein'}
  99. }
  100. return config
  101. class TestCeleryInstanceDjango(unittest.TestCase):
  102. """
  103. Assert that the DJANGO_SETTINGS_MODULE environment variable is set.
  104. """
  105. def test_django_env_variable(self):
  106. self.assertEqual(os.getenv('DJANGO_SETTINGS_MODULE'), PULP_DJANGO_SETTINGS_MODULE)
  107. class TestCeleryInstanceSSLConfig(unittest.TestCase):
  108. """
  109. Assert that the BROKER_USE_SSL structure has the expected configuration in it.
  110. """
  111. @mock.patch('pulp.server.async.celery_instance.config.getboolean')
  112. def test_configure_SSL_when_SSL_disabled(self, mock_getboolean):
  113. """
  114. Make sure that the Celery config has BROKER_USE_SSL set to False if SSL is disabled.
  115. """
  116. mock_getboolean.return_value = False
  117. celery_instance.configure_SSL()
  118. self.assertFalse(celery_instance.celery.conf['BROKER_USE_SSL'])
  119. @mock.patch('pulp.server.async.celery_instance.celery')
  120. @mock.patch('pulp.server.async.celery_instance.config.getboolean')
  121. def test_configure_SSL_when_SSL_enabled(self, mock_getboolean, mock_celery):
  122. """
  123. Make sure that the Celery config has BROKER_USE_SSL properly formed if SSL enabled.
  124. """
  125. mock_getboolean.return_value = True
  126. mock_cacert = mock.Mock()
  127. mock_keyfile = mock.Mock()
  128. mock_certfile = mock.Mock()
  129. CONFIG_OVERRIDE = {
  130. 'tasks': {
  131. 'cacert': mock_cacert,
  132. 'keyfile': mock_keyfile,
  133. 'certfile': mock_certfile,
  134. 'cert_reqs': ssl.CERT_REQUIRED}
  135. }
  136. EXPECTED_BROKER_USE_SSL = {
  137. 'ca_certs': mock_cacert,
  138. 'keyfile': mock_keyfile,
  139. 'certfile': mock_certfile,
  140. 'cert_reqs': ssl.CERT_REQUIRED,
  141. }
  142. custom_fake_get = partial(fake_get, CONFIG_OVERRIDE)
  143. with mock.patch('pulp.server.async.celery_instance.config.get', new=custom_fake_get):
  144. celery_instance.configure_SSL()
  145. mock_celery.conf.update.assert_called_once_with(BROKER_USE_SSL=EXPECTED_BROKER_USE_SSL)