PageRenderTime 52ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/server/test/unit/server/db/test_reaper.py

https://github.com/devops-senpai/pulp
Python | 91 lines | 39 code | 13 blank | 39 comment | 0 complexity | 3f606b2e592fa3dc7311b8705f5b3f95 MD5 | raw file
Possible License(s): GPL-2.0
  1. # -*- coding: utf-8 -*-
  2. #
  3. # Copyright © 2012 Red Hat, Inc.
  4. #
  5. # This software is licensed to you under the GNU General Public License as
  6. # published by the Free Software Foundation; either version 2 of the License
  7. # (GPLv2) or (at your option) any later version.
  8. # There is NO WARRANTY for this software, express or implied, including the
  9. # implied warranties of MERCHANTABILITY, NON-INFRINGEMENT, or FITNESS FOR A
  10. # PARTICULAR PURPOSE.
  11. # You should have received a copy of GPLv2 along with this software; if not,
  12. # see http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt
  13. """
  14. This module contains tests for the pulp.server.db.reaper module.
  15. """
  16. from datetime import timedelta
  17. import unittest
  18. from isodate import Duration
  19. import mock
  20. from ... import base
  21. from pulp.server.compat import ObjectId
  22. from pulp.server.db import reaper
  23. from pulp.server.db.model.consumer import ConsumerHistoryEvent
  24. class TestCreateExpiredObjectId(unittest.TestCase):
  25. """
  26. Assert correct behavior from _create_expired_object_id().
  27. """
  28. def test_expired_object_id(self):
  29. """
  30. Make sure that _create_expired_object_id() generates correct ObjectIds.
  31. """
  32. expired_oid = reaper._create_expired_object_id(timedelta(seconds=1))
  33. # The oid should be about a second, but since we didn't create the now_oid until after we
  34. # ran the _create_expired_object_id() function, we can't assert that the timedelta is
  35. # exactly one second. It should definitely be less than two seconds of difference between
  36. # them, however (unless Nose is really struggling to run the tests), so we can make sure
  37. # it fits inside the window of 1 - 2 seconds.
  38. now_oid = ObjectId()
  39. self.assertTrue(
  40. (now_oid.generation_time - expired_oid.generation_time) >= timedelta(seconds=1))
  41. self.assertTrue(
  42. (now_oid.generation_time - expired_oid.generation_time) < timedelta(seconds=2))
  43. # Also, let's make sure the type is correct
  44. self.assertTrue(isinstance(expired_oid, ObjectId))
  45. class TestReapExpiredDocuments(base.PulpServerTests):
  46. """
  47. This test class asserts correct behavior from the reap_expired_documents() Task.
  48. """
  49. def tearDown(self):
  50. """
  51. Clean up the records we made.
  52. """
  53. super(TestReapExpiredDocuments, self).tearDown()
  54. ConsumerHistoryEvent.get_collection().remove()
  55. @mock.patch('pulp.server.db.reaper.pulp_config.config.getfloat')
  56. def test_leave_unexpired_entries(self, getfloat):
  57. chec = ConsumerHistoryEvent.get_collection()
  58. event = ConsumerHistoryEvent('consumer', 'originator', 'consumer_registered', {})
  59. chec.insert(event, safe=True)
  60. self.assertTrue(chec.find({'_id': event['_id']}).count() == 1)
  61. # Let's mock getfloat to pretend that the user said they want to reap things that are a day
  62. # old. This means that the event should not get reaped.
  63. getfloat.return_value = 1.0
  64. reaper.reap_expired_documents()
  65. # The event should still exist
  66. self.assertTrue(chec.find({'_id': event['_id']}).count() == 1)
  67. @mock.patch('pulp.server.db.reaper.pulp_config.config.getfloat')
  68. def test_remove_expired_entries(self, getfloat):
  69. chec = ConsumerHistoryEvent.get_collection()
  70. event = ConsumerHistoryEvent('consumer', 'originator', 'consumer_registered', {})
  71. chec.insert(event, safe=True)
  72. self.assertTrue(chec.find({'_id': event['_id']}).count() == 1)
  73. # Let's mock getfloat to pretend that the user said they want to reap things from the
  74. # future, which should make the event we just created look old enough to delete
  75. getfloat.return_value = -1.0
  76. reaper.reap_expired_documents()
  77. # The event should no longer exist
  78. self.assertTrue(chec.find({'_id': event['_id']}).count() == 0)