def request(self, url, *args, **kwargs): try: stream = HttpStream(url, *args, verbose=self.verbose, **kwargs) if kwargs.get('stream', False): return stream with stream as s: # joining at once is much faster than doing += in a loop all_chunks = list(s.iter_chunks()) content = ''.join(all_chunks) return HttpResponse(s.status_code, s.headers, content) except pycurl.error as ex: code = ex.args[0] try: message = ex.args[1] except IndexError: # happened on rhel 6 message = "" if code in PYCURL_NETWORK_CODES: raise OsbsNetworkException(url, message, code, *ex.args[2:], cause=ex, traceback=sys.exc_info()[2]) raise OsbsException(cause=ex, traceback=sys.exc_info()[2])
def test_stream_logs_bad_initial_connection_and_error_in_iter( self, openshift, exc): response = flexmock(status_code=http_client.OK) (response.should_receive('iter_lines').and_return([ b"{'stream': 'foo\n'}" ]).and_raise(requests.exceptions.ConnectionError).and_return( [b"{'stream': 'ham\n'}"])) wrapped_exc = OsbsNetworkException('http://spam.com', str(exc), status_code=None, cause=exc) (flexmock(openshift).should_receive('_get') # First: simulate initial connection problem .and_raise(wrapped_exc) # Next: return a real response .and_return(response).and_return(response) # we want to call it just as many times so we read all from response, # but not more times because it would start again looping from 1st raise .and_return(response).times(4)) mocked_time = (flexmock(time).should_receive('time')) # times are tricky, because we use time.time() explicitly in # stream logs, BUT also those two log.debug use it # so this is caluculated that it will be increasing times for all operations # we want, after that it will stop iteration for num in range(0, 1500, 100): mocked_time.and_return(num) logs = openshift.stream_logs(TEST_BUILD) assert len([log for log in logs]) == 2
def request(self, url, *args, **kwargs): try: stream = HttpStream(url, *args, verbose=self.verbose, **kwargs) if kwargs.get('stream', False): return stream with stream as s: content = s.req.content return HttpResponse(s.status_code, s.headers, content) # Timeout will catch both ConnectTimout and ReadTimeout except (RetryError, Timeout) as ex: raise OsbsNetworkException(url, str(ex), '', cause=ex, traceback=sys.exc_info()[2]) except HTTPError as ex: raise OsbsNetworkException(url, str(ex), ex.response.status_code, cause=ex, traceback=sys.exc_info()[2]) except Exception as ex: raise OsbsException(cause=ex, traceback=sys.exc_info()[2])
def test_stream_logs(self, openshift): ex = OsbsNetworkException('/', '', pycurl.FOLLOWLOCATION) response = flexmock(status_code=httplib.OK) (response.should_receive('iter_lines').and_return( ["{'stream': 'foo\n'}"]).and_raise(StopIteration)) (flexmock(openshift).should_receive('_get') # First: timeout in response after 100s .and_raise(ex) # Next: return a real response .and_return(response)) (flexmock(time).should_receive('time').and_return(0).and_return(100)) logs = openshift.stream_logs(TEST_BUILD) assert len([log for log in logs]) == 1
def test_stream_logs_bad_initial_connection(self, openshift, exc): response = flexmock(status_code=httplib.OK) (response.should_receive('iter_lines').and_return( [b"{'stream': 'foo\n'}"]).and_raise(StopIteration)) wrapped_exc = OsbsNetworkException('http://spam.com', str(exc), status_code=None, cause=exc) (flexmock(openshift).should_receive('_get') # First: simulate initial connection problem .and_raise(wrapped_exc) # Next: return a real response .and_return(response)) (flexmock(time).should_receive('time').and_return(0).and_return(100)) logs = openshift.stream_logs(TEST_BUILD) assert len([log for log in logs]) == 1
def _perform(self): while True: ret, num_handles = self.curl_multi.perform() if ret != pycurl.E_CALL_MULTI_PERFORM: # see curl_multi_perform manpage break num_q, ok_list, err_list = self.curl_multi.info_read() if num_q != 0: logger.warning("CurlMulti.info_read() has %s remaining messages", num_q) if err_list: err_obj = err_list[0] # For anything but the connection being closed, raise if err_obj[1] != pycurl.E_PARTIAL_FILE: raise OsbsNetworkException(self.url, err_obj[2], err_obj[1]) self.finished = (num_handles == 0)
def test_stream_logs_error(self, openshift): ex = OsbsNetworkException('/', '', pycurl.E_COULDNT_RESOLVE_HOST) (flexmock(openshift).should_receive('_get').and_raise(ex)) with pytest.raises(OsbsNetworkException): list(openshift.stream_logs(TEST_BUILD))