15 # Use this hostname when you want to make sure the traffic will be
16 # instantly refused. 100::/64 is a dedicated black hole.
19 skip_sleep = mock.patch('time.sleep', lambda n: None) # clown'll eat me
21 # fake_httplib2_response and mock_responses
22 # mock calls to httplib2.Http.request()
23 def fake_httplib2_response(code, **headers):
24 headers.update(status=str(code),
25 reason=httplib.responses.get(code, "Unknown Response"))
26 return httplib2.Response(headers)
28 def mock_responses(body, *codes, **headers):
29 return mock.patch('httplib2.Http.request', side_effect=(
30 (fake_httplib2_response(code, **headers), body) for code in codes))
32 # fake_requests_response, mock_get_responses and mock_put_responses
33 # mock calls to requests.get() and requests.put()
34 def fake_requests_response(code, body, **headers):
35 r = requests.Response()
37 r.reason = httplib.responses.get(code, "Unknown Response")
39 r.raw = io.BytesIO(body)
42 def mock_get_responses(body, *codes, **headers):
43 return mock.patch('requests.get', side_effect=(
44 fake_requests_response(code, body, **headers) for code in codes))
46 def mock_put_responses(body, *codes, **headers):
47 return mock.patch('requests.put', side_effect=(
48 fake_requests_response(code, body, **headers) for code in codes))
50 def mock_requestslib_responses(method, body, *codes, **headers):
51 return mock.patch(method, side_effect=(
52 fake_requests_response(code, body, **headers) for code in codes))
54 class MockStreamReader(object):
55 def __init__(self, name='.', *data):
57 self._data = ''.join(data)
58 self._data_locators = ['{}+{}'.format(hashlib.md5(d).hexdigest(),
59 len(d)) for d in data]
65 def readfrom(self, start, size, num_retries=None):
66 return self._data[start:start + size]
69 class ArvadosBaseTestCase(unittest.TestCase):
70 # This class provides common utility functions for our tests.
76 for workdir in self._tempdirs:
77 shutil.rmtree(workdir, ignore_errors=True)
79 def make_tmpdir(self):
80 self._tempdirs.append(tempfile.mkdtemp())
81 return self._tempdirs[-1]
83 def data_file(self, filename):
85 basedir = os.path.dirname(__file__)
88 return open(os.path.join(basedir, 'data', filename))
90 def build_directory_tree(self, tree):
91 tree_root = self.make_tmpdir()
93 path = os.path.join(tree_root, leaf)
95 os.makedirs(os.path.dirname(path))
96 except OSError as error:
97 if error.errno != errno.EEXIST:
99 with open(path, 'w') as tmpfile:
103 def make_test_file(self, text="test"):
104 testfile = tempfile.NamedTemporaryFile()