1 from future import standard_library
2 standard_library.install_aliases()
3 from builtins import str
4 from builtins import range
5 from builtins import object
22 if sys.version_info >= (3, 0):
23 from io import StringIO
25 from cStringIO import StringIO
27 # Use this hostname when you want to make sure the traffic will be
28 # instantly refused. 100::/64 is a dedicated black hole.
31 skip_sleep = mock.patch('time.sleep', lambda n: None) # clown'll eat me
33 def queue_with(items):
34 """Return a thread-safe iterator that yields the given items.
36 +items+ can be given as an array or an iterator. If an iterator is
37 given, it will be consumed to fill the queue before queue_with()
43 return lambda *args, **kwargs: q.get(block=False)
45 # fake_httplib2_response and mock_responses
46 # mock calls to httplib2.Http.request()
47 def fake_httplib2_response(code, **headers):
48 headers.update(status=str(code),
49 reason=http.client.responses.get(code, "Unknown Response"))
50 return httplib2.Response(headers)
52 def mock_responses(body, *codes, **headers):
53 if not isinstance(body, bytes) and hasattr(body, 'encode'):
55 return mock.patch('httplib2.Http.request', side_effect=queue_with((
56 (fake_httplib2_response(code, **headers), body) for code in codes)))
58 def mock_api_responses(api_client, body, codes, headers={}):
59 if not isinstance(body, bytes) and hasattr(body, 'encode'):
61 return mock.patch.object(api_client._http, 'request', side_effect=queue_with((
62 (fake_httplib2_response(code, **headers), body) for code in codes)))
64 def str_keep_locator(s):
65 return '{}+{}'.format(hashlib.md5(s if isinstance(s, bytes) else s.encode()).hexdigest(), len(s))
67 @contextlib.contextmanager
68 def redirected_streams(stdout=None, stderr=None):
69 if stdout == StringIO:
71 if stderr == StringIO:
73 orig_stdout, sys.stdout = sys.stdout, stdout or sys.stdout
74 orig_stderr, sys.stderr = sys.stderr, stderr or sys.stderr
76 yield (stdout, stderr)
78 sys.stdout = orig_stdout
79 sys.stderr = orig_stderr
82 class VersionChecker(object):
83 def assertVersionOutput(self, out, err):
84 if sys.version_info >= (3, 0):
85 self.assertEqual(err.getvalue(), '')
88 # Python 2 writes version info on stderr.
89 self.assertEqual(out.getvalue(), '')
91 self.assertRegex(v, r"[0-9]+\.[0-9]+\.[0-9]+$\n")
94 class FakeCurl(object):
96 def make(cls, code, body=b'', headers={}):
97 if not isinstance(body, bytes) and hasattr(body, 'encode'):
99 return mock.Mock(spec=cls, wraps=cls(code, body, headers))
101 def __init__(self, code=200, body=b'', headers={}):
105 self._headerfunction = None
106 self._resp_code = code
107 self._resp_body = body
108 self._resp_headers = headers
110 def getopt(self, opt):
111 return self._opt.get(str(opt), None)
113 def setopt(self, opt, val):
114 self._opt[str(opt)] = val
115 if opt == pycurl.WRITEFUNCTION:
117 elif opt == pycurl.HEADERFUNCTION:
118 self._headerfunction = val
121 if not isinstance(self._resp_code, int):
122 raise self._resp_code
123 if self.getopt(pycurl.URL) is None:
125 if self._writer is None:
127 if self._headerfunction:
128 self._headerfunction("HTTP/1.1 {} Status".format(self._resp_code))
129 for k, v in self._resp_headers.items():
130 self._headerfunction(k + ': ' + str(v))
131 if type(self._resp_body) is not bool:
132 self._writer(self._resp_body)
138 """Prevent fake UAs from going back into the user agent pool."""
141 def getinfo(self, opt):
142 if opt == pycurl.RESPONSE_CODE:
143 return self._resp_code
146 def mock_keep_responses(body, *codes, **headers):
147 """Patch pycurl to return fake responses and raise exceptions.
149 body can be a string to return as the response body; an exception
150 to raise when perform() is called; or an iterable that returns a
151 sequence of such values.
153 cm = mock.MagicMock()
154 if isinstance(body, tuple):
156 codes.insert(0, body)
158 FakeCurl.make(code=code, body=b, headers=headers)
163 FakeCurl.make(code=code, body=body, headers=headers)
166 cm.side_effect = queue_with(responses)
167 cm.responses = responses
168 return mock.patch('pycurl.Curl', cm)
171 class MockStreamReader(object):
172 def __init__(self, name='.', *data):
174 self._data = b''.join([
175 b if isinstance(b, bytes) else b.encode()
177 self._data_locators = [str_keep_locator(d) for d in data]
183 def readfrom(self, start, size, num_retries=None):
184 return self._data[start:start + size]
186 class ApiClientMock(object):
187 def api_client_mock(self):
188 return mock.MagicMock(name='api_client_mock')
190 def mock_keep_services(self, api_mock=None, status=200, count=12,
194 service_ssl_flag=False,
195 additional_services=[],
198 api_mock = self.api_client_mock()
200 'items_available': count,
202 'uuid': 'zzzzz-bi6l4-{:015x}'.format(i),
203 'owner_uuid': 'zzzzz-tpzed-000000000000000',
204 'service_host': service_host or 'keep0x{:x}'.format(i),
205 'service_port': service_port or 65535-i,
206 'service_ssl_flag': service_ssl_flag,
207 'service_type': service_type,
208 'read_only': read_only,
209 } for i in range(0, count)] + additional_services
211 self._mock_api_call(api_mock.keep_services().accessible, status, body)
214 def _mock_api_call(self, mock_method, code, body):
215 mock_method = mock_method().execute
217 mock_method.return_value = body
219 mock_method.side_effect = arvados.errors.ApiError(
220 fake_httplib2_response(code), b"{}")
223 class ArvadosBaseTestCase(unittest.TestCase):
224 # This class provides common utility functions for our tests.
230 for workdir in self._tempdirs:
231 shutil.rmtree(workdir, ignore_errors=True)
233 def make_tmpdir(self):
234 self._tempdirs.append(tempfile.mkdtemp())
235 return self._tempdirs[-1]
237 def data_file(self, filename):
239 basedir = os.path.dirname(__file__)
242 return open(os.path.join(basedir, 'data', filename))
244 def build_directory_tree(self, tree):
245 tree_root = self.make_tmpdir()
247 path = os.path.join(tree_root, leaf)
249 os.makedirs(os.path.dirname(path))
250 except OSError as error:
251 if error.errno != errno.EEXIST:
253 with open(path, 'w') as tmpfile:
257 def make_test_file(self, text=b"test"):
258 testfile = tempfile.NamedTemporaryFile()
263 if sys.version_info < (3, 0):
264 # There is no assert[Not]Regex that works in both Python 2 and 3,
265 # so we backport Python 3 style to Python 2.
266 def assertRegex(self, *args, **kwargs):
267 return self.assertRegexpMatches(*args, **kwargs)
268 def assertNotRegex(self, *args, **kwargs):
269 return self.assertNotRegexpMatches(*args, **kwargs)
270 unittest.TestCase.assertRegex = assertRegex
271 unittest.TestCase.assertNotRegex = assertNotRegex