def record_event(self, consumer_id, event_type, event_details=None): """ @ivar consumer_id: identifies the consumer @type id: str @param type: event type @type type: str @param details: event details @type details: dict @raises MissingResource: if the given consumer does not exist @raises InvalidValue: if any of the fields is unacceptable """ # Check that consumer exists for all except registration event existing_consumer = Consumer.get_collection().find_one({'id': consumer_id}) if not existing_consumer and event_type != TYPE_CONSUMER_UNREGISTERED: raise MissingResource(consumer=consumer_id) invalid_values = [] if event_type not in TYPES: invalid_values.append('event_type') if event_details is not None and not isinstance(event_details, dict): invalid_values.append('event_details') if invalid_values: raise InvalidValue(invalid_values) event = ConsumerHistoryEvent(consumer_id, self._originator(), event_type, event_details) ConsumerHistoryEvent.get_collection().save(event)
def record_event(self, consumer_id, event_type, event_details=None): """ @ivar consumer_id: identifies the consumer @type id: str @param type: event type @type type: str @param details: event details @type details: dict @raises MissingResource: if the given consumer does not exist @raises InvalidValue: if any of the fields is unacceptable """ # Check that consumer exists for all except registration event existing_consumer = Consumer.get_collection().find_one( {'id': consumer_id}) if not existing_consumer and event_type != TYPE_CONSUMER_UNREGISTERED: raise MissingResource(consumer=consumer_id) invalid_values = [] if event_type not in TYPES: invalid_values.append('event_type') if event_details is not None and not isinstance(event_details, dict): invalid_values.append('event_details') if invalid_values: raise InvalidValue(invalid_values) event = ConsumerHistoryEvent(consumer_id, self._originator(), event_type, event_details) ConsumerHistoryEvent.get_collection().save(event)
def tearDown(self): super(BindManagerTests, self).tearDown() Consumer.get_collection().remove() model.Repository.objects.delete() model.Distributor.objects.delete() Bind.get_collection().remove() ConsumerHistoryEvent.get_collection().remove() mock_plugins.reset()
def tearDown(self): super(BindManagerTests, self).tearDown() Consumer.get_collection().remove() Repo.get_collection().remove() RepoDistributor.get_collection().remove() Bind.get_collection().remove() ConsumerHistoryEvent.get_collection().remove() mock_plugins.reset()
def setUp(self): super(BindManagerTests, self).setUp() Consumer.get_collection().remove() model.Distributor.objects.delete() Bind.get_collection().remove() ConsumerHistoryEvent.get_collection().remove() plugin_api._create_manager() mock_plugins.install()
def test_leave_unexpired_entries(self, getfloat): chec = ConsumerHistoryEvent.get_collection() event = ConsumerHistoryEvent('consumer', 'originator', 'consumer_registered', {}) chec.insert(event) self.assertTrue(chec.find({'_id': event['_id']}).count() == 1) # Let's mock getfloat to pretend that the user said they want to reap things that are a day # old. This means that the event should not get reaped. getfloat.return_value = 1.0 reaper.reap_expired_documents() # The event should still exist self.assertTrue(chec.find({'_id': event['_id']}).count() == 1)
def test_remove_expired_entries(self, getfloat): chec = ConsumerHistoryEvent.get_collection() event = ConsumerHistoryEvent('consumer', 'originator', 'consumer_registered', {}) chec.insert(event) self.assertTrue(chec.find({'_id': event['_id']}).count() == 1) # Let's mock getfloat to pretend that the user said they want to reap things from the # future, which should make the event we just created look old enough to delete getfloat.return_value = -1.0 reaper.reap_expired_documents() # The event should no longer exist self.assertTrue(chec.find({'_id': event['_id']}).count() == 0)
def test_add_remove(self): collection = ConsumerHistoryEvent.get_collection() self.reaper.add_collection(collection, days=1) self.assertTrue(collection in self.reaper.collections) self.assertTrue(isinstance(self.reaper.collections[collection], timedelta)) self.reaper.remove_collection(collection) self.assertFalse(collection in self.reaper.collections)
def test_add_remove_duration(self): collection = ConsumerHistoryEvent.get_collection() self.reaper.add_collection(collection, months=1) self.assertTrue(collection in self.reaper.collections) self.assertTrue(isinstance(self.reaper.collections[collection], Duration)) self.reaper.remove_collection(collection) self.assertFalse(collection in self.reaper.collections)
def test_add_remove_duration(self): collection = ConsumerHistoryEvent.get_collection() self.reaper.add_collection(collection, months=1) self.assertTrue(collection in self.reaper.collections) self.assertTrue( isinstance(self.reaper.collections[collection], Duration)) self.reaper.remove_collection(collection) self.assertFalse(collection in self.reaper.collections)
def test_add_remove(self): collection = ConsumerHistoryEvent.get_collection() self.reaper.add_collection(collection, days=1) self.assertTrue(collection in self.reaper.collections) self.assertTrue( isinstance(self.reaper.collections[collection], timedelta)) self.reaper.remove_collection(collection) self.assertFalse(collection in self.reaper.collections)
def test_bind_consumer_history(self, mock_repo_qs): self.populate() manager = factory.consumer_bind_manager() manager.bind(self.CONSUMER_ID, self.REPO_ID, self.DISTRIBUTOR_ID, self.NOTIFY_AGENT, self.BINDING_CONFIG) # Verify collection = ConsumerHistoryEvent.get_collection() history = collection.find_one(self.QUERY1) self.assertTrue(history is not None) self.assertEqual(history["consumer_id"], self.CONSUMER_ID) self.assertEqual(history["type"], "repo_bound") self.assertEqual(history["originator"], "SYSTEM") self.assertEqual(history["details"], self.DETAILS)
def test_bind_consumer_history(self, mock_repo_qs): self.populate() manager = factory.consumer_bind_manager() manager.bind(self.CONSUMER_ID, self.REPO_ID, self.DISTRIBUTOR_ID, self.NOTIFY_AGENT, self.BINDING_CONFIG) # Verify collection = ConsumerHistoryEvent.get_collection() history = collection.find_one(self.QUERY1) self.assertTrue(history is not None) self.assertEqual(history['consumer_id'], self.CONSUMER_ID) self.assertEqual(history['type'], 'repo_bound') self.assertEqual(history['originator'], 'SYSTEM') self.assertEqual(history['details'], self.DETAILS)
def test_remove_expired_entries(self): event = ConsumerHistoryEvent('consumer', 'originator', 'consumer_registered', {}) self.collection.insert(event, safe=True) self.assertTrue( self.collection.find({ '_id': event['_id'] }).count() == 1) expired_oid = self.reaper._create_expired_object_id( timedelta(seconds=-1)) self.reaper._remove_expired_entries(self.collection, expired_oid) self.assertTrue( self.collection.find({ '_id': event['_id'] }).count() == 0)
def test_unbind_consumer_history(self, mock_repo_qs): self.populate() manager = factory.consumer_bind_manager() manager.bind(self.CONSUMER_ID, self.REPO_ID, self.DISTRIBUTOR_ID, self.NOTIFY_AGENT, self.BINDING_CONFIG) # Test manager.unbind(self.CONSUMER_ID, self.REPO_ID, self.DISTRIBUTOR_ID) # Verify collection = ConsumerHistoryEvent.get_collection() history = collection.find_one(self.QUERY2) self.assertTrue(history is not None) self.assertEqual(history['consumer_id'], self.CONSUMER_ID) self.assertEqual(history['type'], 'repo_unbound') self.assertEqual(history['originator'], 'SYSTEM') self.assertEqual(history['details'], self.DETAILS)
def test_update_with_consumer_history(self): # Setup self.populate() manager = factory.consumer_profile_manager() manager.update(self.CONSUMER_ID, self.TYPE_1, self.PROFILE_1) # Test manager.update(self.CONSUMER_ID, self.TYPE_1, self.PROFILE_2) # Verify collection = ConsumerHistoryEvent.get_collection() history = collection.find_one({'consumer_id': self.CONSUMER_ID, 'type': 'unit_profile_changed', 'details': {'profile_content_type': self.TYPE_1}}) self.assertTrue(history is not None) self.assertEqual(history['consumer_id'], self.CONSUMER_ID) self.assertEqual(history['type'], 'unit_profile_changed') self.assertEqual(history['originator'], 'SYSTEM') self.assertEqual(history['details'], {'profile_content_type': self.TYPE_1})
def test_remove_single_consumer_history(self): group_id = 'test_group' self.manager.create_consumer_group(group_id) group = self.collection.find_one({'id': group_id}) consumer = self._create_consumer('test_consumer') self.assertFalse(consumer['id'] in group['consumer_ids']) criteria = Criteria(filters={'id': consumer['id']}, fields=['id']) self.manager.unassociate(group_id, criteria) collection = ConsumerHistoryEvent.get_collection() history = collection.find_one({'consumer_id': 'test_consumer', 'type': 'removed_from_group', 'details': {'group_id': group_id}}) self.assertTrue(history is not None) self.assertEqual(history['consumer_id'], 'test_consumer') self.assertEqual(history['type'], 'removed_from_group') self.assertEqual(history['originator'], 'SYSTEM') self.assertEqual(history['details'], {'group_id': group_id})
def test_remove_single_consumer_history(self): group_id = 'test_group' self.manager.create_consumer_group(group_id) group = self.collection.find_one({'id': group_id}) consumer = self._create_consumer('test_consumer') self.assertFalse(consumer['id'] in group['consumer_ids']) criteria = Criteria(filters={'id': consumer['id']}, fields=['id']) self.manager.unassociate(group_id, criteria) collection = ConsumerHistoryEvent.get_collection() history = collection.find_one({ 'consumer_id': 'test_consumer', 'type': 'removed_from_group', 'details': { 'group_id': group_id } }) self.assertTrue(history is not None) self.assertEqual(history['consumer_id'], 'test_consumer') self.assertEqual(history['type'], 'removed_from_group') self.assertEqual(history['originator'], 'SYSTEM') self.assertEqual(history['details'], {'group_id': group_id})
def query(self, consumer_id=None, event_type=None, limit=None, sort='descending', start_date=None, end_date=None): ''' Queries the consumer history storage. @param consumer_id: if specified, events will only be returned for the the consumer referenced @type consumer_id: string or number @param event_type: if specified, only events of the given type are returned @type event_type: string (enumeration found in TYPES) @param limit: if specified, the query will only return up to this amount of entries; default is to not limit the entries returned @type limit: number greater than zero @param sort: indicates the sort direction of the results; results are sorted by timestamp @type sort: string; valid values are 'ascending' and 'descending' @param start_date: if specified, no events prior to this date will be returned @type start_date: datetime.datetime @param end_date: if specified, no events after this date will be returned @type end_date: datetime.datetime @return: list of consumer history entries that match the given parameters; empty list (not None) if no matching entries are found @rtype: list of ConsumerHistoryEvent instances @raises MissingResource: if the given consumer does not exist @raises InvalidValue: if any of the fields is unacceptable ''' invalid_values = [] if event_type is not None and event_type not in TYPES: invalid_values.append('event_type') # Verify the limit makes sense if limit is not None and limit < 1: invalid_values.append('limit') # Verify the sort direction is valid if sort not in SORT_DIRECTION: invalid_values.append('sort') # Verify that start_date and end_date is valid if start_date is not None: try: dateutils.parse_iso8601_date(start_date) except (ValueError, isodate.ISO8601Error): invalid_values.append('start_date') if end_date is not None: try: dateutils.parse_iso8601_date(end_date) except (ValueError, isodate.ISO8601Error): invalid_values.append('end_date') if invalid_values: raise InvalidValue(invalid_values) # Assemble the mongo search parameters search_params = {} if consumer_id: search_params['consumer_id'] = consumer_id if event_type: search_params['type'] = event_type # Add in date range limits if specified date_range = {} if start_date: date_range['$gte'] = start_date if end_date: date_range['$lte'] = end_date if len(date_range) > 0: search_params['timestamp'] = date_range # Determine the correct mongo cursor to retrieve if len(search_params) == 0: cursor = ConsumerHistoryEvent.get_collection().find() else: cursor = ConsumerHistoryEvent.get_collection().find(search_params) # Sort by most recent entry first cursor.sort('timestamp', direction=SORT_DIRECTION[sort]) # If a limit was specified, add it to the cursor if limit: cursor.limit(limit) # Finally convert to a list before returning return list(cursor)
def clean(self): super(ScheduledUnitUninstallTests, self).clean() Consumer.get_collection().remove(safe=True) ConsumerHistoryEvent.get_collection().remove(safe=True)
def clean(self): base.PulpServerTests.clean(self) Consumer.get_collection().remove() ConsumerHistoryEvent.get_collection().remove()
def setUp(self): super(ReaperReapingTests, self).setUp() self.collection = ConsumerHistoryEvent.get_collection()
def _validate_consumer_history(): objectdb = ConsumerHistoryEvent.get_collection() reference = ConsumerHistoryEvent('', '', '', {}) return _validate_model(ConsumerHistoryEvent.__name__, objectdb, reference)
def tearDown(self): """ Clean up the records we made. """ super(TestReapExpiredDocuments, self).tearDown() ConsumerHistoryEvent.get_collection().remove()