Python six.moves.http_client.CONFLICT Examples

The following are 17 code examples of six.moves.http_client.CONFLICT(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module six.moves.http_client , or try the search function .
Example #1
Source File: test_core.py    From osbs-client with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_check_response_bad_nostream(self, caplog, log_errors):
        status_code = http_client.CONFLICT
        content = b'content'
        response = Response(status_code, content=content)
        if log_errors:
            log_type = logging.ERROR
        else:
            log_type = logging.DEBUG

        with pytest.raises(OsbsResponseException):
            if log_errors:
                check_response(response)
            else:
                check_response(response, log_level=log_type)

        logged = [(l.getMessage(), l.levelno) for l in caplog.records]
        assert len(logged) == 1
        assert logged[0][0] == '[{code}] {message}'.format(code=status_code,
                                                           message=content)
        assert logged[0][1] == log_type 
Example #2
Source File: restclient.py    From treadmill with Apache License 2.0 6 votes vote down vote up
def _handle_error(url, response):
    """Handle response status codes."""
    handlers = {
        http_client.NOT_FOUND: NotFoundError(
            'Resource not found: {}'.format(url)
        ),
        # XXX: Correct code is CONFLICT. Support invalid FOUND during
        #      migration.
        http_client.FOUND: AlreadyExistsError(
            'Resource already exists: {}'.format(url)
        ),
        http_client.CONFLICT: AlreadyExistsError(
            'Resource already exists: {}'.format(url)
        ),
        http_client.TOO_MANY_REQUESTS: TooManyRequestsError(response),
        http_client.FAILED_DEPENDENCY: ValidationError(response),
        # XXX: Correct code is FORBIDDEN. Support invalid UNAUTHORIZED during
        #      migration.
        http_client.UNAUTHORIZED: NotAuthorizedError(response),
        http_client.FORBIDDEN: NotAuthorizedError(response),
        http_client.BAD_REQUEST: BadRequestError(response),
    }

    if response.status_code in handlers:
        raise handlers[response.status_code] 
Example #3
Source File: test_controller.py    From tacker with Apache License 2.0 6 votes vote down vote up
def test_delete_with_incorrect_task_state(self, mock_vnf_by_id):
        req = fake_request.HTTPRequest.blank(
            '/vnf_instances/%s' % uuidsentinel.vnf_instance_id)
        req.method = 'DELETE'

        vnf_instance = fakes.return_vnf_instance(
            fields.VnfInstanceState.NOT_INSTANTIATED,
            task_state=fields.VnfInstanceTaskState.ERROR)
        mock_vnf_by_id.return_value = vnf_instance

        # Call delete API
        resp = req.get_response(self.app)

        self.assertEqual(http_client.CONFLICT, resp.status_code)
        expected_msg = ("Vnf instance %s in task_state ERROR. "
                       "Cannot delete while the vnf instance "
                       "is in this state.")
        self.assertEqual(expected_msg % uuidsentinel.vnf_instance_id,
                resp.json['conflictingRequest']['message']) 
Example #4
Source File: test_controller.py    From tacker with Apache License 2.0 6 votes vote down vote up
def test_delete_with_incorrect_instantiation_state(self, mock_vnf_by_id):
        req = fake_request.HTTPRequest.blank(
            '/vnf_instances/%s' % uuidsentinel.vnf_instance_id)
        req.method = 'DELETE'

        vnf_instance = fakes.return_vnf_instance(
            fields.VnfInstanceState.INSTANTIATED)
        mock_vnf_by_id.return_value = vnf_instance

        # Call delete API
        resp = req.get_response(self.app)

        self.assertEqual(http_client.CONFLICT, resp.status_code)
        expected_msg = ("Vnf instance %s in instantiation_state "
                       "INSTANTIATED. Cannot delete while the vnf instance "
                       "is in this state.")
        self.assertEqual(expected_msg % uuidsentinel.vnf_instance_id,
                resp.json['conflictingRequest']['message']) 
Example #5
Source File: test_controller.py    From tacker with Apache License 2.0 6 votes vote down vote up
def test_heal_incorrect_task_state(self, mock_vnf_by_id):
        vnf_instance_obj = fakes.return_vnf_instance(
            fields.VnfInstanceState.INSTANTIATED,
            task_state=fields.VnfInstanceTaskState.HEALING)
        mock_vnf_by_id.return_value = vnf_instance_obj

        body = {}
        req = fake_request.HTTPRequest.blank(
            '/vnf_instances/%s/heal' % uuidsentinel.vnf_instance_id)
        req.body = jsonutils.dump_as_bytes(body)
        req.headers['Content-Type'] = 'application/json'
        req.method = 'POST'

        resp = req.get_response(self.app)
        self.assertEqual(http_client.CONFLICT, resp.status_code)
        expected_msg = ("Vnf instance %s in task_state "
                       "HEALING. Cannot heal while the vnf instance "
                       "is in this state.")
        self.assertEqual(expected_msg % uuidsentinel.vnf_instance_id,
                resp.json['conflictingRequest']['message']) 
Example #6
Source File: test_controller.py    From tacker with Apache License 2.0 6 votes vote down vote up
def test_terminate_incorrect_task_state(self, mock_vnf_by_id):
        vnf_instance = fakes.return_vnf_instance(
            instantiated_state=fields.VnfInstanceState.INSTANTIATED,
            task_state=fields.VnfInstanceTaskState.TERMINATING)
        mock_vnf_by_id.return_value = vnf_instance

        body = {"terminationType": "FORCEFUL"}
        req = fake_request.HTTPRequest.blank(
            '/vnf_instances/%s/terminate' % uuidsentinel.vnf_instance_id)
        req.body = jsonutils.dump_as_bytes(body)
        req.headers['Content-Type'] = 'application/json'
        req.method = 'POST'

        resp = req.get_response(self.app)

        self.assertEqual(http_client.CONFLICT, resp.status_code)
        expected_msg = ("Vnf instance %s in task_state TERMINATING. Cannot "
                        "terminate while the vnf instance is in this state.")
        self.assertEqual(expected_msg % uuidsentinel.vnf_instance_id,
            resp.json['conflictingRequest']['message']) 
Example #7
Source File: test_controller.py    From tacker with Apache License 2.0 6 votes vote down vote up
def test_terminate_incorrect_instantiation_state(self, mock_vnf_by_id):
        mock_vnf_by_id.return_value = fakes.return_vnf_instance()
        body = {"terminationType": "FORCEFUL"}
        req = fake_request.HTTPRequest.blank(
            '/vnf_instances/%s/terminate' % uuidsentinel.vnf_instance_id)
        req.body = jsonutils.dump_as_bytes(body)
        req.headers['Content-Type'] = 'application/json'
        req.method = 'POST'

        resp = req.get_response(self.app)

        self.assertEqual(http_client.CONFLICT, resp.status_code)
        expected_msg = ("Vnf instance %s in instantiation_state "
                        "NOT_INSTANTIATED. Cannot terminate while the vnf "
                        "instance is in this state.")
        self.assertEqual(expected_msg % uuidsentinel.vnf_instance_id,
            resp.json['conflictingRequest']['message']) 
Example #8
Source File: test_controller.py    From tacker with Apache License 2.0 6 votes vote down vote up
def test_instantiate_incorrect_task_state(self, mock_vnf_by_id):
        vnf_instance = fakes.return_vnf_instance_model(
            task_state=fields.VnfInstanceTaskState.INSTANTIATING)
        mock_vnf_by_id.return_value = vnf_instance

        body = {"flavourId": "simple"}
        req = fake_request.HTTPRequest.blank(
            '/vnf_instances/%s/instantiate' % uuidsentinel.vnf_instance_id)
        req.body = jsonutils.dump_as_bytes(body)
        req.headers['Content-Type'] = 'application/json'
        req.method = 'POST'

        resp = req.get_response(self.app)

        self.assertEqual(http_client.CONFLICT, resp.status_code)
        expected_msg = ("Vnf instance %s in task_state INSTANTIATING. Cannot "
                        "instantiate while the vnf instance is in this state.")
        self.assertEqual(expected_msg % uuidsentinel.vnf_instance_id,
            resp.json['conflictingRequest']['message']) 
Example #9
Source File: test_blob.py    From python-storage with Apache License 2.0 6 votes vote down vote up
def test_upload_from_file_failure(self):
        import requests

        from google.resumable_media import InvalidResponse
        from google.cloud import exceptions

        message = "Someone is already in this spot."
        response = requests.Response()
        response.status_code = http_client.CONFLICT
        response.request = requests.Request("POST", "http://example.com").prepare()
        side_effect = InvalidResponse(response, message)

        with self.assertRaises(exceptions.Conflict) as exc_info:
            self._upload_from_file_helper(side_effect=side_effect)

        self.assertIn(message, exc_info.exception.message)
        self.assertEqual(exc_info.exception.errors, []) 
Example #10
Source File: core.py    From osbs-client with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def create_resource_quota(self, name, quota_json):
        """
        Prevent builds being scheduled and wait for running builds to finish.

        :return:
        """

        url = self._build_k8s_url("resourcequotas/")
        response = self._post(url, data=json.dumps(quota_json),
                              headers={"Content-Type": "application/json"})
        if response.status_code == http_client.CONFLICT:
            url = self._build_k8s_url("resourcequotas/%s" % name)
            response = self._put(url, data=json.dumps(quota_json),
                                 headers={"Content-Type": "application/json"})

        check_response(response)
        return response 
Example #11
Source File: test_core.py    From osbs-client with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_check_response_bad_stream(self, caplog, log_errors):
        iterable = [b'iter', b'lines']
        status_code = http_client.CONFLICT
        response = Response(status_code, iterable=iterable)
        if log_errors:
            log_type = logging.ERROR
        else:
            log_type = logging.DEBUG

        with pytest.raises(OsbsResponseException):
            if log_errors:
                check_response(response)
            else:
                check_response(response, log_level=log_type)

        logged = [(l.getMessage(), l.levelno) for l in caplog.records]
        assert len(logged) == 1
        assert logged[0][0] == '[{code}] {message}'.format(code=status_code,
                                                           message=b'iterlines')
        assert logged[0][1] == log_type 
Example #12
Source File: test_controller.py    From tacker with Apache License 2.0 5 votes vote down vote up
def test_instantiate_incorrect_instantiation_state(self, mock_vnf_by_id):
        vnf_instance = fakes.return_vnf_instance_model()
        vnf_instance.instantiation_state = 'INSTANTIATED'
        mock_vnf_by_id.return_value = vnf_instance

        body = {"flavourId": "simple"}
        req = fake_request.HTTPRequest.blank(
            '/vnf_instances/%s/instantiate' % uuidsentinel.vnf_instance_id)
        req.body = jsonutils.dump_as_bytes(body)
        req.headers['Content-Type'] = 'application/json'
        req.method = 'POST'

        # Call Instantiate API
        resp = req.get_response(self.app)
        self.assertEqual(http_client.CONFLICT, resp.status_code) 
Example #13
Source File: test_controller.py    From tacker with Apache License 2.0 5 votes vote down vote up
def test_get_vnf_package_vnfd_failed_with_invalid_status(
            self, mock_vnf_by_id):
        vnf_package_updates = {
            'onboarding_state': 'CREATED',
            'operational_state': 'DISABLED'
        }
        mock_vnf_by_id.return_value = fakes.return_vnfpkg_obj(
            vnf_package_updates=vnf_package_updates)
        req = fake_request.HTTPRequest.blank(
            '/vnf_packages/%s/vnfd'
            % constants.UUID)
        req.headers['Accept'] = 'application/zip'
        req.method = 'GET'
        resp = req.get_response(self.app)
        self.assertEqual(http_client.CONFLICT, resp.status_code) 
Example #14
Source File: __init__.py    From osbs-client with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def retry_on_conflict(func):
    @wraps(func)
    def retry(*args, **kwargs):
        # Only retry when OsbsResponseException was raised due to a conflict
        def should_retry_cb(ex):
            return ex.status_code == http_client.CONFLICT

        retry_func = RetryFunc(OsbsResponseException, should_retry_cb=should_retry_cb)
        return retry_func.go(func, *args, **kwargs)

    return retry 
Example #15
Source File: test_policies.py    From st2 with Apache License 2.0 5 votes vote down vote up
def test_post_duplicate(self):
        instance = self.__create_instance()

        post_resp = self.__do_post(instance)
        self.assertEqual(post_resp.status_int, http_client.CREATED)

        post_dup_resp = self.__do_post(instance)
        self.assertEqual(post_dup_resp.status_int, http_client.CONFLICT)

        del_resp = self.__do_delete(self.__get_obj_id(post_resp))
        self.assertEqual(del_resp.status_int, http_client.NO_CONTENT) 
Example #16
Source File: restclient_test.py    From treadmill with Apache License 2.0 5 votes vote down vote up
def test_get_409(self, resp_mock):
        """Test treadmill.restclient.get CONFLICT (409)"""
        resp_mock.return_value.status_code = http_client.CONFLICT

        with self.assertRaises(restclient.AlreadyExistsError):
            restclient.get('http://foo.com', '/') 
Example #17
Source File: exc.py    From python-egnyte with MIT License 5 votes vote down vote up
def __init__(self, values=None, ok_statuses=(http_client.OK, ), ignored_errors=None):
        super(ErrorMapping, self).__init__({
            http_client.BAD_REQUEST: RequestError,
            http_client.UNAUTHORIZED: NotAuthorized,
            http_client.FORBIDDEN: InsufficientPermissions,
            http_client.NOT_FOUND: NotFound,
            http_client.CONFLICT: DuplicateRecordExists,
            http_client.REQUEST_ENTITY_TOO_LARGE: FileSizeExceedsLimit,
            http_client.SEE_OTHER: Redirected,
        })
        if values:
            self.update(values)
        if ignored_errors:
            self.ignored_errors = recursive_tuple(ignored_errors)
        self.ok_statuses = ok_statuses