def post(self, request, sessions, *args, **kwargs): """Release the given resources one by one.""" errors = {} username = get_username(request) session = sessions[request.model.token] with transaction.atomic(): for name in request.model.resources: try: resource_data = ResourceData.objects.select_for_update() \ .get(name=name) except ObjectDoesNotExist: errors[name] = (ResourceDoesNotExistError.ERROR_CODE, "Resource %r doesn't exist" % name) continue resource = get_sub_model(resource_data) try: self.release_resource(resource, username) session.resources.remove(resource) except ServerError as ex: errors[name] = (ex.ERROR_CODE, ex.get_error_content()) if len(errors) > 0: return Response( { "errors": errors, "details": "errors occurred while releasing resource" }, status=httplib.BAD_REQUEST) return Response({}, status=httplib.NO_CONTENT)
def _validate_tests_tree(self, main_test): """Validate that the tests' data in the DB is as expected. Args: main_test (TestCase / TestCase / TestSuite): main test of the run. Returns: GeneralData. the test's data as appears in the DB. """ try: db_clone = GeneralData.objects.get(name=main_test.data.name) except GeneralData.DoesNotExist: self.fail("DB instance of %r wasn't created" % main_test.data.name) self.assertEqual(db_clone.success, main_test.data.success) self.assertEqual(db_clone.status, main_test.data.status) if main_test.IS_COMPLEX: for sub_test in main_test: sub_data = self._validate_tests_tree(sub_test) data_leaf = get_sub_model(sub_data) sub_test_parent = data_leaf.parent self.assertIsNotNone(sub_test_parent, "%r's data is not connected to a parent" % sub_test.data.name) self.assertEqual(sub_test_parent.name, main_test.data.name, "%r's data is not connected to the right" "parent (%r instead of %r)" % (sub_test.data.name, sub_test_parent.name, main_test.data.name)) return db_clone
def get_sub_tests_data(self): """Get the sub tests' data. Returns: list. list of the sub tests' data. Raises: NotImplementedError: calling on abstract class. RuntimeError: calling on a non-complex test. """ return [get_sub_model(test_data) for test_data in self.tests.all()]
def get_leaf(model): """"Get the leaf resource data inheriting from model given. Arguments: model (django.db.models.Model): a model instance to get its leaf. Returns: django.db.models.Model. the leaf model of the given instance. """ sub_model = get_sub_model(model) if sub_model is None: return model return get_leaf(sub_model)
def release_resources(self, request): """Release the given resources one by one. Args: request (Request): ReleaseResources request. Returns: SuccessReply. a reply indicating on a successful operation. Raises: ResourceReleaseError: when error occurred while trying to release at least one of the given resources. """ errors = {} for name in request.message.requests: try: resource_data = ResourceData.objects.get(name=name) except ObjectDoesNotExist: errors[name] = (ResourceDoesNotExistError.ERROR_CODE, "Resource %r doesn't exist" % name) continue resource = get_sub_model(resource_data) self.logger.debug("Releasing %r resource", name) try: self._release_resource(resource, request.worker.name) self.logger.debug("Resource %r released successfully", name) except ServerError as ex: errors[name] = (ex.ERROR_CODE, ex.get_error_content()) if len(errors) > 0: raise ResourceReleaseError(errors) return SuccessReply()
def get_parent(self): """When overridden, gets the parent field's value.""" return get_sub_model(self.parent)