1 # Copyright (C) The Arvados Authors. All rights reserved.
3 # SPDX-License-Identifier: Apache-2.0
5 from future import standard_library
6 standard_library.install_aliases()
7 from builtins import str
8 from builtins import range
9 from builtins import object
26 if sys.version_info >= (3, 0):
27 from io import StringIO, BytesIO
29 from cStringIO import StringIO
32 # Use this hostname when you want to make sure the traffic will be
33 # instantly refused. 100::/64 is a dedicated black hole.
36 skip_sleep = mock.patch('time.sleep', lambda n: None) # clown'll eat me
38 def queue_with(items):
39 """Return a thread-safe iterator that yields the given items.
41 +items+ can be given as an array or an iterator. If an iterator is
42 given, it will be consumed to fill the queue before queue_with()
48 return lambda *args, **kwargs: q.get(block=False)
50 # fake_httplib2_response and mock_responses
51 # mock calls to httplib2.Http.request()
52 def fake_httplib2_response(code, **headers):
53 headers.update(status=str(code),
54 reason=http.client.responses.get(code, "Unknown Response"))
55 return httplib2.Response(headers)
57 def mock_responses(body, *codes, **headers):
58 if not isinstance(body, bytes) and hasattr(body, 'encode'):
60 return mock.patch('httplib2.Http.request', side_effect=queue_with((
61 (fake_httplib2_response(code, **headers), body) for code in codes)))
63 def mock_api_responses(api_client, body, codes, headers={}):
64 if not isinstance(body, bytes) and hasattr(body, 'encode'):
66 return mock.patch.object(api_client._http, 'request', side_effect=queue_with((
67 (fake_httplib2_response(code, **headers), body) for code in codes)))
69 def str_keep_locator(s):
70 return '{}+{}'.format(hashlib.md5(s if isinstance(s, bytes) else s.encode()).hexdigest(), len(s))
72 @contextlib.contextmanager
73 def redirected_streams(stdout=None, stderr=None):
74 if stdout == StringIO:
76 if stderr == StringIO:
78 orig_stdout, sys.stdout = sys.stdout, stdout or sys.stdout
79 orig_stderr, sys.stderr = sys.stderr, stderr or sys.stderr
81 yield (stdout, stderr)
83 sys.stdout = orig_stdout
84 sys.stderr = orig_stderr
87 class VersionChecker(object):
88 def assertVersionOutput(self, out, err):
89 if sys.version_info >= (3, 0):
90 self.assertEqual(err.getvalue(), '')
93 # Python 2 writes version info on stderr.
94 self.assertEqual(out.getvalue(), '')
96 self.assertRegex(v, r"[0-9]+\.[0-9]+\.[0-9]+$\n")
99 class FakeCurl(object):
101 def make(cls, code, body=b'', headers={}):
102 if not isinstance(body, bytes) and hasattr(body, 'encode'):
104 return mock.Mock(spec=cls, wraps=cls(code, body, headers))
106 def __init__(self, code=200, body=b'', headers={}):
110 self._headerfunction = None
111 self._resp_code = code
112 self._resp_body = body
113 self._resp_headers = headers
115 def getopt(self, opt):
116 return self._opt.get(str(opt), None)
118 def setopt(self, opt, val):
119 self._opt[str(opt)] = val
120 if opt == pycurl.WRITEFUNCTION:
122 elif opt == pycurl.HEADERFUNCTION:
123 self._headerfunction = val
126 if not isinstance(self._resp_code, int):
127 raise self._resp_code
128 if self.getopt(pycurl.URL) is None:
130 if self._writer is None:
132 if self._headerfunction:
133 self._headerfunction("HTTP/1.1 {} Status".format(self._resp_code))
134 for k, v in self._resp_headers.items():
135 self._headerfunction(k + ': ' + str(v))
136 if type(self._resp_body) is not bool:
137 self._writer(self._resp_body)
143 """Prevent fake UAs from going back into the user agent pool."""
146 def getinfo(self, opt):
147 if opt == pycurl.RESPONSE_CODE:
148 return self._resp_code
151 def mock_keep_responses(body, *codes, **headers):
152 """Patch pycurl to return fake responses and raise exceptions.
154 body can be a string to return as the response body; an exception
155 to raise when perform() is called; or an iterable that returns a
156 sequence of such values.
158 cm = mock.MagicMock()
159 if isinstance(body, tuple):
161 codes.insert(0, body)
163 FakeCurl.make(code=code, body=b, headers=headers)
168 FakeCurl.make(code=code, body=body, headers=headers)
171 cm.side_effect = queue_with(responses)
172 cm.responses = responses
173 return mock.patch('pycurl.Curl', cm)
176 class MockStreamReader(object):
177 def __init__(self, name='.', *data):
179 self._data = b''.join([
180 b if isinstance(b, bytes) else b.encode()
182 self._data_locators = [str_keep_locator(d) for d in data]
188 def readfrom(self, start, size, num_retries=None):
189 return self._data[start:start + size]
191 class ApiClientMock(object):
192 def api_client_mock(self):
193 return mock.MagicMock(name='api_client_mock')
195 def mock_keep_services(self, api_mock=None, status=200, count=12,
199 service_ssl_flag=False,
200 additional_services=[],
203 api_mock = self.api_client_mock()
205 'items_available': count,
207 'uuid': 'zzzzz-bi6l4-{:015x}'.format(i),
208 'owner_uuid': 'zzzzz-tpzed-000000000000000',
209 'service_host': service_host or 'keep0x{:x}'.format(i),
210 'service_port': service_port or 65535-i,
211 'service_ssl_flag': service_ssl_flag,
212 'service_type': service_type,
213 'read_only': read_only,
214 } for i in range(0, count)] + additional_services
216 self._mock_api_call(api_mock.keep_services().accessible, status, body)
219 def _mock_api_call(self, mock_method, code, body):
220 mock_method = mock_method().execute
222 mock_method.return_value = body
224 mock_method.side_effect = arvados.errors.ApiError(
225 fake_httplib2_response(code), b"{}")
228 class ArvadosBaseTestCase(unittest.TestCase):
229 # This class provides common utility functions for our tests.
235 for workdir in self._tempdirs:
236 shutil.rmtree(workdir, ignore_errors=True)
238 def make_tmpdir(self):
239 self._tempdirs.append(tempfile.mkdtemp())
240 return self._tempdirs[-1]
242 def data_file(self, filename):
244 basedir = os.path.dirname(__file__)
247 return open(os.path.join(basedir, 'data', filename))
249 def build_directory_tree(self, tree):
250 tree_root = self.make_tmpdir()
252 path = os.path.join(tree_root, leaf)
254 os.makedirs(os.path.dirname(path))
255 except OSError as error:
256 if error.errno != errno.EEXIST:
258 with open(path, 'w') as tmpfile:
262 def make_test_file(self, text=b"test"):
263 testfile = tempfile.NamedTemporaryFile()
268 if sys.version_info < (3, 0):
269 # There is no assert[Not]Regex that works in both Python 2 and 3,
270 # so we backport Python 3 style to Python 2.
271 def assertRegex(self, *args, **kwargs):
272 return self.assertRegexpMatches(*args, **kwargs)
273 def assertNotRegex(self, *args, **kwargs):
274 return self.assertNotRegexpMatches(*args, **kwargs)
275 unittest.TestCase.assertRegex = assertRegex
276 unittest.TestCase.assertNotRegex = assertNotRegex