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