def test_delete_rhic_lookup(self): self.assertFalse(identity.delete_rhic_lookup(None)) task = RHICLookupTask(uuid=self.dummy_uuid) task.save() self.assertTrue(identity.delete_rhic_lookup(task)) self.assertEquals(len(RHICLookupTask.objects()), 0)
def test_complete_rhic_lookup_task_200(self): task = RHICLookupTask(uuid="11a1aa11-a11a-1a11-111a-a11111111111", completed=False, task_id=None) task.save() found = RHICLookupTask.objects() self.assertEquals(len(found), 1) self.assertEquals(found[0].uuid, task.uuid) # Mark as '200', a successful complete which will remove the task from the lookup db accepted = 200 ret_val = identity_lookup.complete_rhic_lookup_task(task.uuid, accepted) self.assertIsNone(ret_val) found = RHICLookupTask.objects() self.assertEquals(len(found), 0)
def test_not_found(self): # Create a stored rhic lookup that is valid and set response to 404 rhic_uuid = "11a1aa11-a11a-1a11-111a-a22222222222" task = RHICLookupTask(uuid=rhic_uuid, completed=True, initiated=datetime.now(tzutc()), modified=datetime.now(tzutc()), status_code=404) task.save() # Verify we return this '404' caught = False try: self.checkin.get_identity_object(rhic_uuid) except NotFoundConsumerIdentity, e: caught = True
def test_complete_rhic_lookup_task_unexpected_value(self): task = RHICLookupTask(uuid="11a1aa11-a11a-1a11-111a-a11111111111", completed=False, task_id=None) task.save() found = RHICLookupTask.objects() self.assertEquals(len(found), 1) self.assertEquals(found[0].uuid, task.uuid) # Mark task with an odd unexpected value # We expect the task to be deleted and the unexpected value is not cached. unexpected = 123 ret_val = identity_lookup.complete_rhic_lookup_task(task.uuid, unexpected) self.assertIsNone(ret_val) found = RHICLookupTask.objects() self.assertEquals(len(found), 0)
def test_unexpected_cached_status_code(self): # Create a stored rhic lookup that is valid and set response to 483 # Verify we return this 'unexpected status code' rhic_uuid = "11a1aa11-a11a-1a11-111a-a22222222222" task = RHICLookupTask(uuid=rhic_uuid, completed=True, initiated=datetime.now(tzutc()), modified=datetime.now(tzutc()), status_code=483) task.save() caught = False try: self.checkin.get_identity_object(rhic_uuid) except UnexpectedStatusCodeException, e: caught = True self.assertEqual(e.status_code, 483)
def test_complete_rhic_lookup_task_202(self): task = RHICLookupTask(uuid="11a1aa11-a11a-1a11-111a-a11111111111", completed=False, task_id=None) task.save() found = RHICLookupTask.objects() self.assertEquals(len(found), 1) self.assertEquals(found[0].uuid, task.uuid) # Mark as '202', meaning we haven't found an answer yet, let the tasks continue # task should remain in DB, should be marked as 'completed=False' in_progress = 202 ret_val = identity_lookup.complete_rhic_lookup_task(task.uuid, in_progress) self.assertIsNone(ret_val) found = RHICLookupTask.objects() self.assertEquals(len(found), 1) self.assertEquals(found[0].uuid, task.uuid) self.assertIsNone(found[0].task_id) self.assertEquals(found[0].status_code, in_progress) self.assertFalse(found[0].completed)
def test_complete_rhic_lookup_task_404(self): task = RHICLookupTask(uuid="11a1aa11-a11a-1a11-111a-a11111111111", completed=False, task_id=None) task.save() found = RHICLookupTask.objects() self.assertEquals(len(found), 1) self.assertEquals(found[0].uuid, task.uuid) # Mark as '404', task finished and received answer RHIC is unknown # task should be cached in DB with '404' status_code # it should be marked as 'completed=True' not_found = 404 ret_val = identity_lookup.complete_rhic_lookup_task(task.uuid, not_found) self.assertIsNone(ret_val) found = RHICLookupTask.objects() self.assertEquals(len(found), 1) self.assertEquals(found[0].uuid, task.uuid) self.assertIsNone(found[0].task_id) self.assertEquals(found[0].status_code, not_found) self.assertTrue(found[0].completed)
def test_get_in_progress_rhic_lookups(self): # Create a valid, in_progress task task_a = RHICLookupTask(uuid="11a1aa11-a11a-1a11-111a-a11111111111", completed=False) task_a.save() # Create a completed task task_b = RHICLookupTask(uuid="11a1aa11-a11a-1a11-111a-a22222222222", completed=True, initiated=datetime.now(tzutc()), modified=datetime.now(tzutc())) task_b.save() # Create a timedout incomplete task cfg = config.get_rhic_serve_config_info() timeout_in_minutes = cfg["single_rhic_lookup_timeout_in_minutes"] expired_time = datetime.now(tzutc()) - timedelta(minutes=timeout_in_minutes+1) task_c = RHICLookupTask(uuid="11a1aa11-a11a-1a11-111a-a333333333333", completed=False, initiated=expired_time) task_c.save() current_tasks = identity.get_in_progress_rhic_lookups() self.assertEquals(len(current_tasks), 1) self.assertEquals(current_tasks[0].uuid, task_a.uuid)
def test_update_rhic_lookup_task(self): task_a = RHICLookupTask(uuid="11a1aa11-a11a-1a11-111a-a11111111111", completed=False, task_id=None) task_a.save() found = RHICLookupTask.objects() self.assertEquals(len(found), 1) self.assertEquals(task_a.uuid, found[0].uuid) self.assertIsNone(found[0].task_id) prior_modified = task_a.modified # Ensure that 'modified' has been updated to new time # and the 'task_id' has been noted task_id = "1" ret_val = identity_lookup.update_rhic_lookup_task(task_a.uuid, task_id) self.assertEquals(ret_val.uuid, task_a.uuid) self.assertFalse(ret_val.completed) self.assertEquals(ret_val.task_id, task_id) self.assertTrue(ret_val.modified > prior_modified) found = RHICLookupTask.objects() self.assertEquals(len(found), 1) self.assertEquals(found[0].uuid, task_a.uuid) self.assertFalse(found[0].completed) self.assertEquals(found[0].task_id, task_id) self.assertTrue(found[0].modified > prior_modified)
def update_rhic_lookup_task(uuid, task_id): _LOG.info("update_rhic_lookup_task(rhic_uuid='%s', task_id='%s')" % (uuid, task_id)) current_task = identity.get_current_rhic_lookup_tasks(uuid) if not current_task: current_task = RHICLookupTask(uuid=uuid, initiated=datetime.now(tzutc())) current_task.task_id = task_id current_task.modified = datetime.now(tzutc()) current_task.completed = False current_task.save() _LOG.info("update_rhic_lookup_task(%s, %s) updated task to %s" % (uuid, task_id, current_task)) return current_task
def get_current_rhic_lookup_tasks(uuid): """ Returns a valid RHICLookupTask for this 'uuid' if one exists, or None. If an older, or invalid RHICLookupTask is found, it will be deleted from the database and None will be returned. Usage: A task can be returned which is either in progress or completed, check the "completed" value to determine. Additionally, the 'status_code' of the task holds the cached response code. @param uuid: uuid of a RHIC @return: a valid RHICLookupTask associated to this 'uuid' or None @rtype: L{splice.common.models.RHICLookupTask} """ _LOG.info("get_current_rhic_lookup_tasks(rhic_uuid='%s')" % (uuid)) current_task = RHICLookupTask.objects(uuid=uuid).first() if not current_task: _LOG.info("Unable to find lookup task '%s', all lookup tasks are: %s" % (uuid, RHICLookupTask.objects())) return None expired = is_rhic_lookup_task_expired(current_task) if expired: delete_rhic_lookup(current_task) return None return current_task
def test_get_current_rhic_lookup_tasks(self): cfg = config.get_rhic_serve_config_info() # Create a valid, in_progress task task_a = RHICLookupTask(uuid="11a1aa11-a11a-1a11-111a-a11111111111", completed=False) task_a.save() # Create a completed task task_b = RHICLookupTask(uuid="11a1aa11-a11a-1a11-111a-a22222222222", completed=True, initiated=datetime.now(tzutc()), modified=datetime.now(tzutc())) task_b.save() # Create a timedout incomplete task timeout_in_minutes = cfg["single_rhic_lookup_timeout_in_minutes"] expired_time = datetime.now(tzutc()) - timedelta(minutes=timeout_in_minutes+1) task_c = RHICLookupTask(uuid="11a1aa11-a11a-1a11-111a-a333333333333", completed=False, initiated=expired_time) task_c.save() # Create a completed expired task expired_hours = cfg["single_rhic_lookup_cache_unknown_in_hours"] expired_time = datetime.now(tzutc()) - timedelta(hours=expired_hours+1) task_d = RHICLookupTask(uuid="11a1aa11-a11a-1a11-111a-a444444444444", completed=True, modified=expired_time) task_d.save() # Ensure all tasks where created and have been saved in mongo current_tasks = [x.uuid for x in RHICLookupTask.objects()] for t in [task_a, task_b, task_c, task_d]: self.assertTrue(t.uuid in current_tasks) # In-progress, valid task task = identity.get_current_rhic_lookup_tasks(task_a.uuid) self.assertIsNotNone(task) self.assertEquals(task.uuid, task_a.uuid) # Completed, valid task task = identity.get_current_rhic_lookup_tasks(task_b.uuid) self.assertIsNotNone(task) self.assertEquals(task.uuid, task_b.uuid) # In-progress, timed out task task = identity.get_current_rhic_lookup_tasks(task_c.uuid) self.assertIsNone(task) found = [x.uuid for x in RHICLookupTask.objects()] self.assertTrue(task_c.uuid not in found) # Completed, cache time expired task task = identity.get_current_rhic_lookup_tasks(task_d.uuid) self.assertIsNone(task) found = [x.uuid for x in RHICLookupTask.objects()] self.assertTrue(task_d.uuid not in found) # Be sure of the 4 tasks we created, the expired and timedout were removed # while the 2 good tasks remained self.assertEquals(len(found), 2) self.assertTrue(task_a.uuid in found) self.assertTrue(task_b.uuid in found)
def test_purge_expired_rhic_lookups(self): cfg = config.get_rhic_serve_config_info() # Create a valid, in_progress task task_a = RHICLookupTask(uuid="11a1aa11-a11a-1a11-111a-a11111111111", completed=False) task_a.save() # Create a completed task task_b = RHICLookupTask(uuid="11a1aa11-a11a-1a11-111a-a22222222222", completed=True, initiated=datetime.now(tzutc()), modified=datetime.now(tzutc())) task_b.save() # Create a timedout incomplete task timeout_in_minutes = cfg["single_rhic_lookup_timeout_in_minutes"] expired_time = datetime.now(tzutc()) - timedelta(minutes=timeout_in_minutes+1) task_c = RHICLookupTask(uuid="11a1aa11-a11a-1a11-111a-a333333333333", completed=False, initiated=expired_time) task_c.save() # Create a completed expired task expired_hours = cfg["single_rhic_lookup_cache_unknown_in_hours"] expired_time = datetime.now(tzutc()) - timedelta(hours=expired_hours+1) task_d = RHICLookupTask(uuid="11a1aa11-a11a-1a11-111a-a444444444444", completed=True, modified=expired_time) task_d.save() identity.purge_expired_rhic_lookups() found = RHICLookupTask.objects() self.assertEquals(len(found), 2) for f in found: self.assertTrue(f.uuid in [task_a.uuid, task_b.uuid]) self.assertTrue(f.uuid not in [task_c.uuid, task_d.uuid])
def test_is_rhic_lookup_task_expired(self): # Create a valid, in_progress task task_a = RHICLookupTask(uuid="11a1aa11-a11a-1a11-111a-a11111111111", completed=False) task_a.save() self.assertFalse(identity.is_rhic_lookup_task_expired(task_a)) # Create a completed task task_b = RHICLookupTask(uuid="11a1aa11-a11a-1a11-111a-a22222222222", completed=True, initiated=datetime.now(tzutc()), modified=datetime.now(tzutc())) task_b.save() self.assertFalse(identity.is_rhic_lookup_task_expired(task_b)) # Create a timedout incomplete task cfg = config.get_rhic_serve_config_info() timeout_in_minutes = cfg["single_rhic_lookup_timeout_in_minutes"] expired_time = datetime.now(tzutc()) - timedelta(minutes=timeout_in_minutes+1) task_c = RHICLookupTask(uuid="11a1aa11-a11a-1a11-111a-a333333333333", completed=False, initiated=expired_time) task_c.save() self.assertTrue(identity.is_rhic_lookup_task_expired(task_c)) # Create a completed expired task expired_hours = cfg["single_rhic_lookup_cache_unknown_in_hours"] expired_time = datetime.now(tzutc()) - timedelta(hours=expired_hours+1) task_d = RHICLookupTask(uuid="11a1aa11-a11a-1a11-111a-a444444444444", completed=True, modified=expired_time) task_d.save() self.assertTrue(identity.is_rhic_lookup_task_expired(task_d))
def get_in_progress_rhic_lookups(): all_tasks = RHICLookupTask.objects(completed=False) ret_vals = [ x for x in all_tasks if not is_rhic_lookup_task_expired(x)] return ret_vals
def purge_expired_rhic_lookups(): all_tasks = RHICLookupTask.objects() for current_task in all_tasks: if is_rhic_lookup_task_expired(current_task): delete_rhic_lookup(current_task)