PageRenderTime 42ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://github.com/devops-senpai/pulp
Python | 167 lines | 113 code | 10 blank | 44 comment | 0 complexity | 4eb74d58663395bb091f3eaee8bb11fd MD5 | raw file
Possible License(s): GPL-2.0
  1. # -*- coding: utf-8 -*-
  2. #
  3. # Copyright © 2013 Red Hat, Inc.
  4. #
  5. # This software is licensed to you under the GNU General Public
  6. # License as published by the Free Software Foundation; either version
  7. # 2 of the License (GPLv2) or (at your option) any later version.
  8. # There is NO WARRANTY for this software, express or implied,
  9. # including the implied warranties of MERCHANTABILITY,
  10. # NON-INFRINGEMENT, or FITNESS FOR A PARTICULAR PURPOSE. You should
  11. # have received a copy of GPLv2 along with this software; if not, see
  12. # http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt.
  13. """
  14. This module contains tests for the pulp.server.async.celery_instance module.
  15. """
  16. from datetime import timedelta
  17. from functools import partial
  18. import unittest
  19. import mock
  20. from pulp.server.async import celery_instance
  21. from pulp.server.async.tasks import babysit
  22. from pulp.server.config import config
  23. from pulp.server.db.reaper import reap_expired_documents
  24. from pulp.server.maintenance.monthly import monthly_maintenance
  25. class TestCelerybeatSchedule(unittest.TestCase):
  26. """
  27. Assert that the CELERYBEAT_SCHEDULE structure has the expected tasks in it.
  28. """
  29. def test_num_tasks(self):
  30. """
  31. Assert that the expected number of beat tasks are in the CELERYBEAT_SCHEDULE. If you find
  32. youself adjusting this test because you added a new task to CELERYBEAT_SCHEDULE, please do
  33. add another unit test to this test class asserting that your new task is present with the
  34. correct information. Thanks!
  35. """
  36. # Please read the docblock to this test if you find yourself needing to adjust this
  37. # assertion.
  38. self.assertEqual(len(celery_instance.celery.conf['CELERYBEAT_SCHEDULE']), 3)
  39. def test_babysit(self):
  40. """
  41. Make sure the babysit Task is present and properly configured.
  42. """
  43. expected_babysit = {
  44. 'task': babysit.name,
  45. 'schedule': timedelta(seconds=60),
  46. 'args': tuple(),
  47. }
  48. self.assertEqual(celery_instance.celery.conf['CELERYBEAT_SCHEDULE']['babysit'],
  49. expected_babysit)
  50. def test_reap_expired_documents(self):
  51. """
  52. Make sure the reap_expired_documents Task is present and properly configured.
  53. """
  54. reap = celery_instance.celery.conf['CELERYBEAT_SCHEDULE']['reap_expired_documents']
  55. expected_reap = {
  56. 'task': reap_expired_documents.name,
  57. 'schedule': timedelta(days=(config.getfloat('data_reaping', 'reaper_interval'))),
  58. 'args': tuple(),
  59. }
  60. self.assertEqual(reap, expected_reap)
  61. def test_monthly_maintenance(self):
  62. """
  63. Make sure the monthly maintenance Task is present and properly configured.
  64. """
  65. expected_monthly_maintenance = {
  66. 'task': monthly_maintenance.name,
  67. 'schedule': timedelta(days=30),
  68. 'args': tuple(),
  69. }
  70. self.assertEqual(celery_instance.celery.conf['CELERYBEAT_SCHEDULE']['monthly_maintenance'],
  71. expected_monthly_maintenance)
  72. def test_celery_conf_updated(self):
  73. """
  74. Make sure the Celery config was updated with our CELERYBEAT_SCHEDULE.
  75. """
  76. self.assertEqual(celery_instance.celery.conf['CELERYBEAT_SCHEDULE'],
  77. celery_instance.CELERYBEAT_SCHEDULE)
  78. def fake_get(config, section, key):
  79. """
  80. Fake version of the get() method from pulp's config object. This is useful
  81. so we can have concrete values to test for.
  82. """
  83. return config.get(key)
  84. CONFIG = {
  85. 'name': 'database_name',
  86. 'seeds': 'host1:27017,host2:27017',
  87. 'user': 'someguy',
  88. 'password': 'letmein',
  89. }
  90. class TestMongoBackendConfig(unittest.TestCase):
  91. def test_celery_conf_updated(self):
  92. self.assertEqual(celery_instance.celery.conf['CELERY_RESULT_BACKEND'], 'mongodb')
  93. self.assertTrue(isinstance(celery_instance.celery.conf['CELERY_MONGODB_BACKEND_SETTINGS'], dict))
  94. conf = celery_instance.celery.conf['CELERY_MONGODB_BACKEND_SETTINGS']
  95. self.assertTrue(conf.get('host') is not None)
  96. self.assertTrue(conf.get('database') is not None)
  97. @mock.patch('pulp.server.async.celery_instance.config.has_option', new=lambda x, y: True)
  98. def test_create_config(self):
  99. custom_fake_get = partial(fake_get, CONFIG)
  100. with mock.patch('pulp.server.async.celery_instance.config.get', new=custom_fake_get):
  101. result = celery_instance.create_mongo_config()
  102. self.assertEqual(result['host'], 'host1')
  103. self.assertEqual(result['port'], '27017')
  104. self.assertEqual(result['database'], 'database_name')
  105. self.assertEqual(result['user'], 'someguy')
  106. self.assertEqual(result['password'], 'letmein')
  107. @mock.patch('pulp.server.async.celery_instance.config.has_option', new=lambda x, y: True)
  108. def test_create_config_one_seed(self):
  109. config = CONFIG.copy()
  110. config['seeds'] = config['seeds'].split(',')[0]
  111. custom_fake_get = partial(fake_get, config)
  112. with mock.patch('pulp.server.async.celery_instance.config.get', new=custom_fake_get):
  113. result = celery_instance.create_mongo_config()
  114. self.assertEqual(result['host'], 'host1')
  115. self.assertEqual(result['port'], '27017')
  116. @mock.patch('pulp.server.async.celery_instance.config.has_option', new=lambda x, y: True)
  117. def test_create_config_no_port(self):
  118. config = CONFIG.copy()
  119. config['seeds'] = 'host1'
  120. custom_fake_get = partial(fake_get, config)
  121. with mock.patch('pulp.server.async.celery_instance.config.get', new=custom_fake_get):
  122. result = celery_instance.create_mongo_config()
  123. self.assertEqual(result['host'], 'host1')
  124. self.assertTrue('port' not in result)
  125. @mock.patch('pulp.server.config.config.has_option',
  126. new=lambda x, y: False if y == 'password' else True)
  127. def test_no_password(self):
  128. custom_fake_get = partial(fake_get, CONFIG)
  129. with mock.patch('pulp.server.async.celery_instance.config.get', new=custom_fake_get):
  130. result = celery_instance.create_mongo_config()
  131. self.assertTrue('user' not in result)
  132. self.assertTrue('password' not in result)
  133. @mock.patch('pulp.server.config.config.has_option',
  134. new=lambda x, y: False if y == 'user' else True)
  135. def test_no_user(self):
  136. custom_fake_get = partial(fake_get, CONFIG)
  137. with mock.patch('pulp.server.async.celery_instance.config.get', new=custom_fake_get):
  138. result = celery_instance.create_mongo_config()
  139. self.assertTrue('user' not in result)
  140. self.assertTrue('password' not in result)