Ejemplo n.º 1
0
    def test_json_request(self):
        session = utils.mockSession({}, status_code=200)
        req_id = "req-7b081d28-8272-45f4-9cf6-89649c1c7a1a"
        client = _session_client(
            session=session, additional_headers={"foo": "bar"},
            global_request_id=req_id)
        client.json_request('GET', 'url')

        session.request.assert_called_once_with(
            'url', 'GET', raise_exc=False, auth=None,
            headers={
                "foo": "bar",
                "X-OpenStack-Request-ID": req_id,
                "Content-Type": "application/json",
                "Accept": "application/json",
                "X-OpenStack-Ironic-API-Version": "1.6"
            },
            endpoint_filter={
                'interface': 'publicURL',
                'service_type': 'baremetal',
                'region_name': ''
            },
            endpoint_override='http://localhost:1234',
            user_agent=http.USER_AGENT
        )
Ejemplo n.º 2
0
 def _test_endpoint_override(self, endpoint):
     fake_session = utils.mockSession({'content-type': 'application/json'},
                                      status_code=http_client.NO_CONTENT)
     request_mock = mock.Mock()
     fake_session.request = request_mock
     request_mock.return_value = utils.mockSessionResponse(
         headers={'content-type': 'application/json'},
         status_code=http_client.NO_CONTENT)
     client = _session_client(session=fake_session,
                              endpoint_override=endpoint)
     client.json_request('DELETE', '/v1/nodes/aa/maintenance')
     expected_args_dict = {
         'headers': {
             'Content-Type': 'application/json',
             'Accept': 'application/json',
             'X-OpenStack-Ironic-API-Version': '1.6'
         },
         'auth': None, 'user_agent': 'python-ironicclient',
         'endpoint_filter': {
             'interface': 'publicURL',
             'service_type': 'baremetal',
             'region_name': ''
         }
     }
     if isinstance(endpoint, six.string_types):
         trimmed = http._trim_endpoint_api_version(endpoint)
         expected_args_dict['endpoint_override'] = trimmed
     request_mock.assert_called_once_with(
         '/v1/nodes/aa/maintenance', 'DELETE', raise_exc=False,
         **expected_args_dict
     )
Ejemplo n.º 3
0
 def _test_endpoint_override(self, endpoint):
     fake_session = utils.mockSession({'content-type': 'application/json'},
                                      status_code=http_client.NO_CONTENT)
     request_mock = mock.Mock()
     fake_session.request = request_mock
     request_mock.return_value = utils.mockSessionResponse(
         headers={'content-type': 'application/json'},
         status_code=http_client.NO_CONTENT)
     client = _session_client(session=fake_session,
                              endpoint_override=endpoint)
     client.json_request('DELETE', '/v1/nodes/aa/maintenance')
     expected_args_dict = {
         'headers': {
             'Content-Type': 'application/json',
             'Accept': 'application/json',
             'X-OpenStack-Ironic-API-Version': '1.6'
         },
         'auth': None,
         'user_agent': 'python-ironicclient',
         'endpoint_filter': {
             'interface': 'publicURL',
             'service_type': 'baremetal',
             'region_name': ''
         }
     }
     if isinstance(endpoint, six.string_types):
         trimmed = http._trim_endpoint_api_version(endpoint)
         expected_args_dict['endpoint_override'] = trimmed
     request_mock.assert_called_once_with('/v1/nodes/aa/maintenance',
                                          'DELETE',
                                          raise_exc=False,
                                          **expected_args_dict)
Ejemplo n.º 4
0
    def test_server_https_request_ok(self):
        client = http.HTTPClient('https://localhost/')
        client.session = utils.mockSession(
            {'Content-Type': 'application/json'},
            "Body",
            version=1,
            status_code=http_client.OK)

        client.json_request('GET', '/v1/resources')
Ejemplo n.º 5
0
    def test_server_https_request_ok(self):
        client = http.HTTPClient('https://localhost/')
        client.session = utils.mockSession(
            {'Content-Type': 'application/json'},
            "Body",
            version=1,
            status_code=http_client.OK)

        client.json_request('GET', '/v1/resources')
Ejemplo n.º 6
0
    def test_server_exception_description_only(self):
        error_msg = 'test error msg'
        error_body = _get_error_body(description=error_msg)
        fake_session = utils.mockSession({'Content-Type': 'application/json'},
                                         error_body,
                                         status_code=http_client.BAD_REQUEST)
        client = _session_client(session=fake_session)

        self.assertRaisesRegex(exc.BadRequest, 'test error msg',
                               client.json_request, 'GET', '/v1/resources')
Ejemplo n.º 7
0
    def test_session_retry_retriable_connection_failure(self):
        ok_resp = utils.mockSessionResponse(
            {'Content-Type': 'application/json'}, b"OK", http_client.OK)
        fake_session = utils.mockSession({})
        fake_session.request.side_effect = iter(
            (kexc.RetriableConnectionFailure(), ok_resp))

        client = _session_client(session=fake_session)
        client.json_request('GET', '/v1/resources')
        self.assertEqual(2, fake_session.request.call_count)
Ejemplo n.º 8
0
    def test_server_exception_description_only(self):
        error_msg = 'test error msg'
        error_body = _get_error_body(description=error_msg)
        fake_session = utils.mockSession(
            {'Content-Type': 'application/json'},
            error_body, status_code=http_client.BAD_REQUEST)
        client = _session_client(session=fake_session)

        self.assertRaisesRegex(exc.BadRequest, 'test error msg',
                               client.json_request,
                               'GET', '/v1/resources')
Ejemplo n.º 9
0
    def test_401_unauthorized_exception(self):
        error_body = _get_error_body()
        client = http.HTTPClient('http://localhost/')
        client.session = utils.mockSession(
            {'Content-Type': 'text/plain'},
            error_body,
            version=1,
            status_code=http_client.UNAUTHORIZED)

        self.assertRaises(exc.Unauthorized, client.json_request,
                          'GET', '/v1/resources')
Ejemplo n.º 10
0
    def test_server_https_request_with_application_octet_stream(self):
        client = http.HTTPClient('https://localhost/')
        client.session = utils.mockSession(
            {'Content-Type': 'application/octet-stream'},
            "Body",
            version=1,
            status_code=http_client.OK)

        response, body = client.json_request('GET', '/v1/resources')
        self.assertEqual(client.session.request.return_value, response)
        self.assertIsNone(body)
Ejemplo n.º 11
0
    def test_server_https_request_with_application_octet_stream(self):
        client = http.HTTPClient('https://localhost/')
        client.session = utils.mockSession(
            {'Content-Type': 'application/octet-stream'},
            "Body",
            version=1,
            status_code=http_client.OK)

        response, body = client.json_request('GET', '/v1/resources')
        self.assertEqual(client.session.request.return_value, response)
        self.assertIsNone(body)
Ejemplo n.º 12
0
    def test_server_exception_empty_body(self):
        error_body = _get_error_body()

        fake_session = utils.mockSession({'Content-Type': 'application/json'},
                                         error_body,
                                         http_client.INTERNAL_SERVER_ERROR)

        client = _session_client(session=fake_session)

        self.assertRaises(exc.InternalServerError, client.json_request, 'GET',
                          '/v1/resources')
Ejemplo n.º 13
0
    def test_401_unauthorized_exception(self):
        error_body = _get_error_body()
        client = http.HTTPClient('http://localhost/')
        client.session = utils.mockSession(
            {'Content-Type': 'text/plain'},
            error_body,
            version=1,
            status_code=http_client.UNAUTHORIZED)

        self.assertRaises(exc.Unauthorized, client.json_request, 'GET',
                          '/v1/resources')
Ejemplo n.º 14
0
    def test_server_exception_empty_body(self):
        error_body = _get_error_body()
        client = http.HTTPClient('http://localhost/')
        client.session = utils.mockSession(
            {'Content-Type': 'application/json'},
            error_body,
            version=1,
            status_code=http_client.INTERNAL_SERVER_ERROR)

        self.assertRaises(exc.InternalServerError, client.json_request, 'GET',
                          '/v1/resources')
Ejemplo n.º 15
0
 def test__parse_version_headers(self):
     # Test parsing of version headers from SessionClient
     fake_session = utils.mockSession(
         {
             'X-OpenStack-Ironic-API-Minimum-Version': '1.1',
             'X-OpenStack-Ironic-API-Maximum-Version': '1.6',
             'content-type': 'text/plain',
         }, None, http_client.HTTP_VERSION_NOT_SUPPORTED)
     expected_result = ('1.1', '1.6')
     client = _session_client(session=fake_session)
     result = client._parse_version_headers(fake_session.request())
     self.assertEqual(expected_result, result)
Ejemplo n.º 16
0
    def test_server_exception_empty_body(self):
        error_body = _get_error_body()

        fake_session = utils.mockSession({'Content-Type': 'application/json'},
                                         error_body,
                                         http_client.INTERNAL_SERVER_ERROR)

        client = _session_client(session=fake_session)

        self.assertRaises(exc.InternalServerError,
                          client.json_request,
                          'GET', '/v1/resources')
Ejemplo n.º 17
0
    def test_server_exception_empty_body(self):
        error_body = _get_error_body()
        client = http.HTTPClient('http://localhost/')
        client.session = utils.mockSession(
            {'Content-Type': 'application/json'},
            error_body,
            version=1,
            status_code=http_client.INTERNAL_SERVER_ERROR)

        self.assertRaises(exc.InternalServerError,
                          client.json_request,
                          'GET', '/v1/resources')
Ejemplo n.º 18
0
 def test__parse_version_headers(self):
     # Test parsing of version headers from SessionClient
     fake_session = utils.mockSession(
         {'X-OpenStack-Ironic-API-Minimum-Version': '1.1',
          'X-OpenStack-Ironic-API-Maximum-Version': '1.6',
          'content-type': 'text/plain',
          },
         None,
         http_client.HTTP_VERSION_NOT_SUPPORTED)
     expected_result = ('1.1', '1.6')
     client = _session_client(session=fake_session)
     result = client._parse_version_headers(fake_session.request())
     self.assertEqual(expected_result, result)
Ejemplo n.º 19
0
    def test_session_retry_503(self):
        error_body = _get_error_body()

        fake_resp = utils.mockSessionResponse(
            {'Content-Type': 'application/json'}, error_body,
            http_client.SERVICE_UNAVAILABLE)
        ok_resp = utils.mockSessionResponse(
            {'Content-Type': 'application/json'}, b"OK", http_client.OK)
        fake_session = utils.mockSession({})
        fake_session.request.side_effect = iter((fake_resp, ok_resp))

        client = _session_client(session=fake_session)
        client.json_request('GET', '/v1/resources')
        self.assertEqual(2, fake_session.request.call_count)
Ejemplo n.º 20
0
    def test_session_retry_fail(self):
        error_body = _get_error_body()

        fake_resp = utils.mockSessionResponse(
            {'Content-Type': 'application/json'}, error_body,
            http_client.CONFLICT)
        fake_session = utils.mockSession({})
        fake_session.request.return_value = fake_resp

        client = _session_client(session=fake_session)

        self.assertRaises(exc.Conflict, client.json_request, 'GET',
                          '/v1/resources')
        self.assertEqual(http.DEFAULT_MAX_RETRIES + 1,
                         fake_session.request.call_count)
Ejemplo n.º 21
0
    def test_make_simple_request(self):
        session = utils.mockSession({})

        client = _session_client(session=session)
        res = client._make_simple_request(session, 'GET', 'url')

        session.request.assert_called_once_with(
            'url', 'GET', raise_exc=False,
            endpoint_filter={
                'interface': 'publicURL',
                'service_type': 'baremetal',
                'region_name': ''
            },
            endpoint_override='http://localhost:1234',
            user_agent=http.USER_AGENT)
        self.assertEqual(res, session.request.return_value)
Ejemplo n.º 22
0
    def test__http_request_client_fallback_fail(self, mock_save_data):
        # Test when fallback to a supported version fails
        host, port, latest_ver = 'localhost', '1234', '1.6'
        error_body = _get_error_body()

        client = http.HTTPClient('http://%s:%s/' % (host, port))
        client.session = utils.mockSession(
            {
                'X-OpenStack-Ironic-API-Minimum-Version': '1.1',
                'X-OpenStack-Ironic-API-Maximum-Version': latest_ver,
                'content-type': 'text/plain',
            },
            error_body,
            version=1,
            status_code=http_client.NOT_ACCEPTABLE)
        self.assertRaises(exc.UnsupportedVersion, client._http_request,
                          '/v1/resources', 'GET')
        mock_save_data.assert_called_once_with(host=host,
                                               data=latest_ver,
                                               port=port)
Ejemplo n.º 23
0
    def test__http_request_client_fallback_fail(self, mock_save_data):
        # Test when fallback to a supported version fails
        host, port, latest_ver = 'localhost', '1234', '1.6'
        error_body = _get_error_body()

        client = http.HTTPClient('http://%s:%s/' % (host, port))
        client.session = utils.mockSession(
            {'X-OpenStack-Ironic-API-Minimum-Version': '1.1',
             'X-OpenStack-Ironic-API-Maximum-Version': latest_ver,
             'content-type': 'text/plain',
             },
            error_body,
            version=1,
            status_code=http_client.NOT_ACCEPTABLE)
        self.assertRaises(
            exc.UnsupportedVersion,
            client._http_request,
            '/v1/resources',
            'GET')
        mock_save_data.assert_called_once_with(host=host, data=latest_ver,
                                               port=port)
Ejemplo n.º 24
0
    def test_endpoint_not_found(self, mock_get_endpoint):
        mock_get_endpoint.return_value = None

        self.assertRaises(exc.EndpointNotFound, _session_client,
                          session=utils.mockSession({}))