def test_timeout_reset_after_call(self): old_timeout = socket.getdefaulttimeout() self.stub_urlopen_with_timeout_check(30, None, "url") try: socket.setdefaulttimeout(1234) utils.urlopen("url", timeout=30) self.assertEquals(1234, socket.getdefaulttimeout()) finally: socket.setdefaulttimeout(old_timeout)
def get_results(results_files): """ Download the results if needed. Return results of each run in a numerically-indexed dictionary of dictionaries keyed on testnames. Return dictionary keyed on unique testnames across all runs. """ r = re.compile('(\S+\s+\S+)\s+(\S+)\s+:') i = 0 runs = {} testnames = {} for file in results_files: runs[i] = {} try: fh = utils.urlopen(file) results = fh.readlines() fh.close() except Exception: print "ERROR: reading results resource [%s]" % (file) usage() for line in results: try: s = r.match(line) testname = s.group(1) status = s.group(2) runs[i][testname] = status testnames[testname] = 1 except Exception: pass i += 1 return (runs, testnames)
def copy_remote(data, dest, use_put=None): """ Copy data to a remote server using http calls POST or PUT Using http POST and PUT methods, copy data over http. To use PUT method, provide a dictionary of values to be populated in the Content-Range and Content-Length headers. Otherwise default is to use POST method. Traps on HTTPError 500 and 400 :param data: encoded data string to copy remotely :param dest: remote server URL :param use_put: dictionary of items if using PUT method :return: html header info for post processing """ ret = None req = urllib2.Request(dest, data=data) if use_put: req.add_header('Content-Type', 'application/octet-stream') end = use_put['start'] + use_put['size'] - 1 req.add_header( 'Content-Range', 'bytes %s-%s/%s' % (use_put['start'], end, use_put['total'])) req.add_header('Content-Length', '%s' % use_put['size']) req.get_method = lambda: 'PUT' try: res = utils.urlopen(req) ret = res.info() res.close() except urllib2.HTTPError as e: if e.code == 500: # the server aborted this recipe DIE DIE DIE raise BkrProxyException("We have been aborted!!!") elif e.code == 400 and use_put: log.error("Error(%s) failed to upload file %s" % (e.code, dest)) return ret
def copy_remote(data, dest, use_put=None): """ Copy data to a remote server using http calls POST or PUT Using http POST and PUT methods, copy data over http. To use PUT method, provide a dictionary of values to be populated in the Content-Range and Content-Length headers. Otherwise default is to use POST method. Traps on HTTPError 500 and 400 @param data: encoded data string to copy remotely @param dest: remote server URL @param use_put: dictionary of items if using PUT method :return: html header info for post processing """ ret = None req = urllib2.Request(dest, data=data) if use_put: req.add_header('Content-Type', 'application/octet-stream') end = use_put['start'] + use_put['size'] - 1 req.add_header('Content-Range', 'bytes %s-%s/%s' % (use_put['start'], end, use_put['total'])) req.add_header('Content-Length', '%s' % use_put['size']) req.get_method = lambda: 'PUT' try: res = utils.urlopen(req) ret = res.info() res.close() except urllib2.HTTPError, e: if e.code == 500: # the server aborted this recipe DIE DIE DIE raise BkrProxyException("We have been aborted!!!") elif e.code == 400 and use_put: log.error("Error(%s) failed to upload file %s" % (e.code, dest))
def test_args_are_untouched(self): self.stub_urlopen_with_timeout_check(30, None, "http://url", "POST data") utils.urlopen("http://url", timeout=30, data="POST data")
def test_timeout_set_by_default(self): def test_func(timeout): self.assertTrue(timeout is not None) self.stub_urlopen_with_timeout_comparison(test_func, None, "url") utils.urlopen("url")
def test_timeout_set_during_call(self): self.stub_urlopen_with_timeout_check(30, "retval", "url") retval = utils.urlopen("url", timeout=30) self.assertEquals(retval, "retval")