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_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_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_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_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_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_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 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 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)