def test__http_request_client_fallback_success(self, mock_getcon, mock_negotiate): # Test when fallback to a supported version succeeds mock_negotiate.return_value = '1.6' error_body = _get_error_body() bad_resp = utils.FakeResponse( { 'X-OpenStack-Ironic-API-Minimum-Version': '1.1', 'X-OpenStack-Ironic-API-Maximum-Version': '1.6', 'content-type': 'text/plain', }, six.StringIO(error_body), version=1, status=406) good_resp = utils.FakeResponse( { 'X-OpenStack-Ironic-API-Minimum-Version': '1.1', 'X-OpenStack-Ironic-API-Maximum-Version': '1.6', 'content-type': 'text/plain', }, six.StringIO("We got some text"), version=1, status=200) client = http.HTTPClient('http://localhost/') mock_getcon.side_effect = iter( [utils.FakeConnection(bad_resp), utils.FakeConnection(good_resp)]) response, body_iter = client._http_request('/v1/resources', 'GET') self.assertEqual(200, response.status) self.assertEqual(1, mock_negotiate.call_count)
def test_http_retry_503(self, mock_getcon): error_body = _get_error_body() bad_resp = utils.FakeResponse({'content-type': 'text/plain'}, six.StringIO(error_body), version=1, status=503) good_resp = utils.FakeResponse({'content-type': 'text/plain'}, six.StringIO("meow"), version=1, status=200) client = http.HTTPClient('http://localhost/') mock_getcon.side_effect = iter( (utils.FakeConnection(bad_resp), utils.FakeConnection(good_resp))) response, body_iter = client._http_request('/v1/resources', 'GET') self.assertEqual(200, response.status) self.assertEqual(2, mock_getcon.call_count)
def test_log_http_response_mask_password(self, mock_log): client = http.HTTPClient('http://localhost/') fake_response = utils.FakeResponse({}, version=1, reason='foo', status=200) body = '{"password": "******"}' client.log_http_response(fake_response, body=body) expected_log = ("\nHTTP/0.1 200 foo\n\n{\"password\": \"***\"}\n") mock_log.assert_called_once_with(expected_log)
def setUp(self): super(VersionNegotiationMixinTest, self).setUp() self.test_object = http.VersionNegotiationMixin() self.test_object.os_ironic_api_version = '1.6' self.test_object.api_version_select_state = 'default' self.mock_mcu = mock.MagicMock() self.test_object._make_connection_url = self.mock_mcu self.response = utils.FakeResponse({}, status=406)
def setUp(self): super(VersionNegotiationMixinTest, self).setUp() self.test_object = http.VersionNegotiationMixin() self.test_object.os_ironic_api_version = '1.6' self.test_object.api_version_select_state = 'default' self.test_object.endpoint = "http://localhost:1234" self.mock_mcu = mock.MagicMock() self.test_object._make_connection_url = self.mock_mcu self.response = utils.FakeResponse({}, status=406) self.test_object.get_server = mock.MagicMock(return_value=('localhost', '1234'))
def test_http_max_retries_none(self, mock_getcon): error_body = _get_error_body() bad_resp = utils.FakeResponse({'content-type': 'text/plain'}, six.StringIO(error_body), version=1, status=409) client = http.HTTPClient('http://localhost/', max_retries=None) mock_getcon.return_value = utils.FakeConnection(bad_resp) self.assertRaises(exc.Conflict, client._http_request, '/v1/resources', 'GET') self.assertEqual(http.DEFAULT_MAX_RETRIES + 1, mock_getcon.call_count)
def test_401_unauthorized_exception(self): error_body = _get_error_body() fake_resp = utils.FakeResponse({'content-type': 'text/plain'}, six.StringIO(error_body), version=1, status=401) client = http.HTTPClient('http://localhost/') client.get_connection = ( lambda *a, **kw: utils.FakeConnection(fake_resp)) self.assertRaises(exc.Unauthorized, client.json_request, 'GET', '/v1/resources')
def test_server_exception_empty_body(self): error_body = _get_error_body() fake_resp = utils.FakeResponse({'content-type': 'application/json'}, six.StringIO(error_body), version=1, status=500) client = http.HTTPClient('http://localhost/') client.get_connection = ( lambda *a, **kw: utils.FakeConnection(fake_resp)) error = self.assertRaises(exc.InternalServerError, client.json_request, 'GET', '/v1/resources') self.assertEqual('Internal Server Error (HTTP 500)', str(error))
def test__parse_version_headers(self): # Test parsing of version headers from HTTPClient error_body = _get_error_body() fake_resp = utils.FakeResponse( {'X-OpenStack-Ironic-API-Minimum-Version': '1.1', 'X-OpenStack-Ironic-API-Maximum-Version': '1.6', 'content-type': 'text/plain', }, six.StringIO(error_body), version=1, status=406) expected_result = ('1.1', '1.6') client = http.HTTPClient('http://localhost/') result = client._parse_version_headers(fake_resp) self.assertEqual(expected_result, result)
def test_negotiate_version_explicit_version_request( self, mock_pvh, mock_msr, mock_save_data): mock_pvh.side_effect = iter([(None, None), ('1.1', '1.99')]) mock_conn = mock.MagicMock() self.test_object.api_version_select_state = 'negotiated' self.test_object.os_ironic_api_version = '1.30' req_header = {'X-OpenStack-Ironic-API-Version': '1.29'} response = utils.FakeResponse( {}, status=http_client.NOT_ACCEPTABLE, request_headers=req_header) self.assertRaisesRegex(exc.UnsupportedVersion, ".*is not supported by the server.*", self.test_object.negotiate_version, mock_conn, response) self.assertTrue(mock_msr.called) self.assertEqual(2, mock_pvh.call_count) self.assertFalse(mock_save_data.called)
def test__http_request_client_fallback_fail(self, mock_getcon): # Test when fallback to a supported version fails error_body = _get_error_body() fake_resp = utils.FakeResponse( {'X-OpenStack-Ironic-API-Minimum-Version': '1.1', 'X-OpenStack-Ironic-API-Maximum-Version': '1.6', 'content-type': 'text/plain', }, six.StringIO(error_body), version=1, status=406) client = http.HTTPClient('http://localhost/') mock_getcon.return_value = utils.FakeConnection(fake_resp) self.assertRaises( exc.UnsupportedVersion, client._http_request, '/v1/resources', 'GET')
def test__http_request_client_fallback_fail(self, mock_getcon, mock_save_data): # Test when fallback to a supported version fails host, port, latest_ver = 'localhost', '1234', '1.6' error_body = _get_error_body() fake_resp = utils.FakeResponse( { 'X-OpenStack-Ironic-API-Minimum-Version': '1.1', 'X-OpenStack-Ironic-API-Maximum-Version': latest_ver, 'content-type': 'text/plain', }, six.StringIO(error_body), version=1, status=406) client = http.HTTPClient('http://%s:%s/' % (host, port)) mock_getcon.return_value = utils.FakeConnection(fake_resp) self.assertRaises(exc.UnsupportedVersion, client._http_request, '/v1/resources', 'GET') mock_save_data.assert_called_once_with(host=host, data=latest_ver, port=port)
def test_server_exception_msg_and_traceback(self): error_msg = 'another test error' error_trace = ("\"Traceback (most recent call last):\\n\\n " "File \\\"/usr/local/lib/python2.7/...") error_body = _get_error_body(error_msg, error_trace) fake_resp = utils.FakeResponse({'content-type': 'application/json'}, six.StringIO(error_body), version=1, status=500) client = http.HTTPClient('http://localhost/') client.get_connection = ( lambda *a, **kw: utils.FakeConnection(fake_resp)) error = self.assertRaises(exc.InternalServerError, client.json_request, 'GET', '/v1/resources') self.assertEqual( '%(error)s (HTTP 500)\n%(trace)s' % {'error': error_msg, 'trace': error_trace}, "%(error)s\n%(details)s" % {'error': str(error), 'details': str(error.details)})