17 # Use this hostname when you want to make sure the traffic will be
18 # instantly refused. 100::/64 is a dedicated black hole.
21 skip_sleep = mock.patch('time.sleep', lambda n: None) # clown'll eat me
23 def queue_with(items):
24 """Return a thread-safe iterator that yields the given items.
26 +items+ can be given as an array or an iterator. If an iterator is
27 given, it will be consumed to fill the queue before queue_with()
33 return lambda *args, **kwargs: queue.get(block=False)
35 # fake_httplib2_response and mock_responses
36 # mock calls to httplib2.Http.request()
37 def fake_httplib2_response(code, **headers):
38 headers.update(status=str(code),
39 reason=httplib.responses.get(code, "Unknown Response"))
40 return httplib2.Response(headers)
42 def mock_responses(body, *codes, **headers):
43 return mock.patch('httplib2.Http.request', side_effect=queue_with((
44 (fake_httplib2_response(code, **headers), body) for code in codes)))
46 def mock_api_responses(api_client, body, codes, headers={}):
47 return mock.patch.object(api_client._http, 'request', side_effect=queue_with((
48 (fake_httplib2_response(code, **headers), body) for code in codes)))
53 def make(cls, code, body='', headers={}):
54 return mock.Mock(spec=cls, wraps=cls(code, body, headers))
56 def __init__(self, code=200, body='', headers={}):
60 self._headerfunction = None
61 self._resp_code = code
62 self._resp_body = body
63 self._resp_headers = headers
65 def getopt(self, opt):
66 return self._opt.get(str(opt), None)
68 def setopt(self, opt, val):
69 self._opt[str(opt)] = val
70 if opt == pycurl.WRITEFUNCTION:
72 elif opt == pycurl.HEADERFUNCTION:
73 self._headerfunction = val
76 if not isinstance(self._resp_code, int):
78 if self.getopt(pycurl.URL) is None:
80 if self._writer is None:
82 if self._headerfunction:
83 self._headerfunction("HTTP/1.1 {} Status".format(self._resp_code))
84 for k, v in self._resp_headers.iteritems():
85 self._headerfunction(k + ': ' + str(v))
86 self._writer(self._resp_body)
92 """Prevent fake UAs from going back into the user agent pool."""
95 def getinfo(self, opt):
96 if opt == pycurl.RESPONSE_CODE:
97 return self._resp_code
100 def mock_keep_responses(body, *codes, **headers):
101 """Patch pycurl to return fake responses and raise exceptions.
103 body can be a string to return as the response body; an exception
104 to raise when perform() is called; or an iterable that returns a
105 sequence of such values.
107 cm = mock.MagicMock()
108 if isinstance(body, tuple):
110 codes.insert(0, body)
112 FakeCurl.make(code=code, body=b, headers=headers)
117 FakeCurl.make(code=code, body=body, headers=headers)
120 cm.side_effect = queue_with(responses)
121 cm.responses = responses
122 return mock.patch('pycurl.Curl', cm)
125 class MockStreamReader(object):
126 def __init__(self, name='.', *data):
128 self._data = ''.join(data)
129 self._data_locators = ['{}+{}'.format(hashlib.md5(d).hexdigest(),
130 len(d)) for d in data]
136 def readfrom(self, start, size, num_retries=None):
137 return self._data[start:start + size]
139 class ApiClientMock(object):
140 def api_client_mock(self):
141 return mock.MagicMock(name='api_client_mock')
143 def mock_keep_services(self, api_mock=None, status=200, count=12,
147 service_ssl_flag=False,
148 additional_services=[],
151 api_mock = self.api_client_mock()
153 'items_available': count,
155 'uuid': 'zzzzz-bi6l4-{:015x}'.format(i),
156 'owner_uuid': 'zzzzz-tpzed-000000000000000',
157 'service_host': service_host or 'keep0x{:x}'.format(i),
158 'service_port': service_port or 65535-i,
159 'service_ssl_flag': service_ssl_flag,
160 'service_type': service_type,
161 'read_only': read_only,
162 } for i in range(0, count)] + additional_services
164 self._mock_api_call(api_mock.keep_services().accessible, status, body)
167 def _mock_api_call(self, mock_method, code, body):
168 mock_method = mock_method().execute
170 mock_method.return_value = body
172 mock_method.side_effect = arvados.errors.ApiError(
173 fake_httplib2_response(code), "{}")
176 class ArvadosBaseTestCase(unittest.TestCase):
177 # This class provides common utility functions for our tests.
183 for workdir in self._tempdirs:
184 shutil.rmtree(workdir, ignore_errors=True)
186 def make_tmpdir(self):
187 self._tempdirs.append(tempfile.mkdtemp())
188 return self._tempdirs[-1]
190 def data_file(self, filename):
192 basedir = os.path.dirname(__file__)
195 return open(os.path.join(basedir, 'data', filename))
197 def build_directory_tree(self, tree):
198 tree_root = self.make_tmpdir()
200 path = os.path.join(tree_root, leaf)
202 os.makedirs(os.path.dirname(path))
203 except OSError as error:
204 if error.errno != errno.EEXIST:
206 with open(path, 'w') as tmpfile:
210 def make_test_file(self, text="test"):
211 testfile = tempfile.NamedTemporaryFile()