def test_do_request_socket_error(self): conn = mock.Mock() conn.request.side_effect = socket.error data = _("An OFC exception has occurred: Failed to connect OFC : ") with mock.patch.object(ofc_client.OFCClient, 'get_connection', return_value=conn): client = ofc_client.OFCClient() e = self.assertRaises(nexc.OFCException, client.do_request, 'GET', '/somewhere', body={}) self.assertEqual(data, str(e)) for k in ['status', 'err_code', 'err_msg']: self.assertIsNone(getattr(e, k)) headers = {"Content-Type": "application/json"} expected = [ mock.call.request('GET', '/somewhere', '{}', headers), ] conn.assert_has_calls(expected)
def __init__(self, conf_ofc): self.client = ofc_client.OFCClient(host=conf_ofc.host, port=conf_ofc.port, use_ssl=conf_ofc.use_ssl, key_file=conf_ofc.key_file, cert_file=conf_ofc.cert_file, insecure_ssl=conf_ofc.insecure_ssl)
def _test_do_request(self, status, resbody, expected_data, exctype=None, exc_checks=None, path_prefix=None): req = mock.Mock(return_value=(FakeResponse(status, resbody))) with mock.patch.object(requests, 'request', req): client = ofc_client.OFCClient() path = '/somewhere' realpath = path_prefix + path if path_prefix else path if exctype: e = self.assertRaises(exctype, client.do_request, 'GET', path, body={}) self.assertEqual(expected_data, str(e)) if exc_checks: for k, v in exc_checks.items(): self.assertEqual(v, getattr(e, k)) else: response = client.do_request('GET', path, body={}) self.assertEqual(response, expected_data) headers = {"Content-Type": "application/json"} req.assert_called_with('GET', 'http://127.0.0.1:8888' + realpath, verify=True, cert={}, data='{}', headers=headers)
def test_do_request_socket_error(self): data = _("An OFC exception has occurred: Failed to connect OFC : ") req = mock.Mock() req.side_effect = requests.exceptions.RequestException with mock.patch.object(requests, 'request', req): client = ofc_client.OFCClient() e = self.assertRaises(nexc.OFCException, client.do_request, 'GET', '/somewhere', body={}) self.assertEqual(data, str(e)) for k in ['status', 'err_code', 'err_msg']: self.assertIsNone(getattr(e, k)) headers = {"Content-Type": "application/json"} req.assert_called_with('GET', 'http://127.0.0.1:8888/somewhere', verify=True, cert={}, data='{}', headers=headers)
def _test_do_request_retry_after(self, exp_request_count, api_max_attempts=None, succeed_final=False): if api_max_attempts is not None: cfg.CONF.set_override('api_max_attempts', api_max_attempts, group='OFC') res_unavail = FakeResponse(503, headers={'retry-after': '10'}) res_ok = FakeResponse(200) req = mock.Mock() if succeed_final: req.side_effect = ([res_unavail] * (exp_request_count - 1) + [res_ok]) else: req.side_effect = [res_unavail] * exp_request_count with mock.patch.object(requests, 'request', req): with mock.patch('time.sleep') as sleep: client = ofc_client.OFCClient() if succeed_final: ret = client.do_request('GET', '/somewhere') self.assertIsNone(ret) else: e = self.assertRaises(nexc.OFCServiceUnavailable, client.do_request, 'GET', '/somewhere') self.assertEqual('10', e.retry_after) headers = {"Content-Type": "application/json"} req.assert_called_with('GET', 'http://127.0.0.1:8888/somewhere', verify=True, cert={}, data=None, headers=headers) self.assertEqual(exp_request_count, req.call_count) self.assertEqual(exp_request_count - 1, sleep.call_count)
def _test_do_request(self, status, resbody, data, exctype=None, exc_checks=None): res = mock.Mock() res.status = status res.read.return_value = resbody conn = mock.Mock() conn.getresponse.return_value = res with mock.patch.object(ofc_client.OFCClient, 'get_connection', return_value=conn): client = ofc_client.OFCClient() if exctype: e = self.assertRaises(exctype, client.do_request, 'GET', '/somewhere', body={}) self.assertEqual(data, str(e)) if exc_checks: for k, v in exc_checks.items(): self.assertEqual(v, getattr(e, k)) else: response = client.do_request('GET', '/somewhere', body={}) self.assertEqual(response, data) headers = {"Content-Type": "application/json"} expected = [ mock.call.request('GET', '/somewhere', '{}', headers), mock.call.getresponse(), ] conn.assert_has_calls(expected)
def __init__(self, conf_ofc): # Trema sliceable REST API does not support HTTPS self.client = ofc_client.OFCClient(host=conf_ofc.host, port=conf_ofc.port)