/
stubs.py
369 lines (318 loc) · 11.4 KB
/
stubs.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
# -*- coding: utf-8 -*-
# Copyright 2015 Metaswitch Networks
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
calico.etcddriver.test.stubs
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Stub objects used for testing driver/protocol code.
"""
import json
import threading
import logging
from Queue import Queue, Empty
from calico.etcddriver.protocol import (
MessageReader, MessageWriter, MSG_KEY_TYPE
)
_log = logging.getLogger(__name__)
# Singleton representing a flush in the stream of writes.
class Sigil(object):
def __init__(self, name):
self.name = name
def __str__(self):
return "<%s>" % self.name
FLUSH = Sigil("FLUSH")
class StubMessageReader(MessageReader):
"""
Replacement for the Driver's MessageReader, which is how it reads
from Felix.
Allows us to send messages as if we were Felix.
"""
def __init__(self, sck):
super(StubMessageReader, self).__init__(sck)
self.queue = Queue()
def send_msg(self, msg_type, fields=None):
"""Called by the test to send the driver a message."""
msg = {
MSG_KEY_TYPE: msg_type
}
msg.update(fields or {})
self.queue.put((msg_type, msg))
def send_timeout(self):
"""Called by the test to send the driver a timeout."""
self.queue.put(None)
def send_exception(self, exc):
"""Called by the test to raise an exception from the driver's read."""
self.queue.put(exc)
def new_messages(self, timeout=None):
"""Called by the driver to receive new messages."""
while True:
item = self.queue.get()
if item is None:
return # timeout
if isinstance(item, BaseException):
raise item
else:
yield item
class StubMessageWriter(MessageWriter):
"""
Replacement for the driver's MessageWriter, which it uses to send messages
to Felix.
Buffers the messages and flush calls in a queue for the test to
interrogate.
"""
def __init__(self, sck):
super(StubMessageWriter, self).__init__(sck)
self.queue = Queue()
self.exception = None
def send_message(self, msg_type, fields=None, flush=True):
if self.exception:
raise self.exception
self.queue.put((msg_type, fields))
if flush:
self.flush()
def next_msg(self):
return self.queue.get(timeout=1)
def flush(self):
self.queue.put(FLUSH)
class PipeFile(object):
def __init__(self):
self.queue = Queue()
self.buf = None
self._finished = False
def read(self, length):
data = ""
if not self.buf:
self.buf = self.queue.get()
while len(data) < length:
if isinstance(self.buf, BaseException):
raise self.buf
data += self.buf[:length - len(data)]
self.buf = self.buf[length - len(data):]
if not self.buf:
try:
self.buf = self.queue.get_nowait()
except Empty:
break
return data
def write(self, data):
self.queue.put(data)
if data == "" or isinstance(data, Exception):
self._finished = True
def __del__(self):
assert self._finished, "PipeFile wasn't correctly finished."
class StubRequest(object):
def __init__(self, stub_etcd, key, kwargs):
self.stub_etcd = stub_etcd
self.thread = threading.current_thread()
self.key = key
self.kwargs = kwargs
self.response = None
self.response_available = threading.Event()
self.pipe_file = None
def __str__(self):
return "Request<key=%s,args=%s,thread=%s>" % (self.key,
self.kwargs,
self.thread)
def respond_with_exception(self, exc):
"""
Called from the test to raise an exception from the current/next
request.
"""
self.response = exc
self.on_response_avail()
def on_response_avail(self):
self.response_available.set()
self.stub_etcd.on_req_closed(self)
def respond_with_value(self, key, value, dir=False, mod_index=None,
etcd_index=None, status=200, action="get"):
"""
Called from the test to return a simple single-key value to the
driver.
"""
node = {"key": key, "value": value, "modifiedIndex": mod_index}
if dir:
node["dir"] = True
data = json.dumps({
"action": action,
"node": node
})
self.respond_with_data(data, etcd_index, status)
def respond_with_dir(self, key, children, mod_index=None,
etcd_index=None, status=200):
"""
Called from the test to return a directory of key/values (from a
recursive request).
"""
nodes = []
for k, v in children.iteritems():
if v is not None:
nodes.append({"key": k, "value": v,
"modifiedIndex": mod_index})
else:
nodes.append({"key": k, "dir": True,
"modifiedIndex": mod_index,
"nodes": []})
data = json.dumps({
"action": "get",
"node": {
"key": key,
"dir": True,
"modifiedIndex": mod_index,
"nodes": nodes
}
})
self.respond_with_data(data, etcd_index, status)
def respond_with_data(self, data, etcd_index, status):
"""
Called from the test to return a raw response (e.g. to send
malformed JSON).
"""
headers = self.stub_etcd.headers.copy()
if etcd_index is not None:
headers["x-etcd-index"] = str(etcd_index)
resp = MockResponse(status, data, headers)
self.response = resp
self.on_response_avail()
def respond_with_stream(self, etcd_index, status=200):
"""
Called from the test to respond with a stream, allowing the test to
send chunks of data in response.
"""
headers = self.stub_etcd.headers.copy()
if etcd_index is not None:
headers["x-etcd-index"] = str(etcd_index)
self.pipe_file = PipeFile()
resp = MockResponse(status, self.pipe_file, headers)
self.response = resp
self.response_available.set() # We leave the req open in StubEtcd.
return self.pipe_file
def get_response(self):
self.response_available.wait(timeout=30) # returns None in Python 2.6
if self.response_available.is_set():
return self.response
else:
raise AssertionError("No response")
def assert_request(self, expected_key, **expected_args):
"""
Asserts the properies of the next request.
"""
default_args = {'wait_index': None,
'preload_content': None,
'recursive': False,
'timeout': 5}
key = self.key
args = self.kwargs
for k, v in default_args.iteritems():
if k in args and args[k] == v:
del args[k]
if expected_key != key:
raise AssertionError("Expected request for %s but got %s" %
(expected_key, key))
if expected_args != args:
raise AssertionError("Expected request args %s for %s but got %s" %
(expected_args, key, args))
def stop(self):
if self.response_available.is_set():
if self.pipe_file:
self.pipe_file.write(SystemExit())
else:
self.respond_with_exception(SystemExit())
class StubEtcd(object):
"""
A fake connection to etcd. We hook the driver's _issue_etcd_request
method and block the relevant thread until the test calls one of the
respond_... methods.
"""
def __init__(self):
self.request_queue = Queue()
self.response_queue = Queue()
self.headers = {
"x-etcd-cluster-id": "abcdefg"
}
self.lock = threading.Lock()
self.open_reqs = set()
def request(self, key, **kwargs):
"""
Called from the driver to make a request. Blocks until the
test thread sends a response.
"""
_log.info("New request on thread %s: %s, %s",
threading.current_thread(),
key, kwargs)
request = StubRequest(self, key, kwargs)
with self.lock:
self.open_reqs.add(request)
rq = self.request_queue
if rq is None:
_log.warn("Request after shutdown: %s, %s", key, kwargs)
raise SystemExit()
else:
rq.put(request)
response = request.get_response()
if isinstance(response, BaseException):
raise response
else:
return response
def get_next_request(self):
"""
Called from the test to get the next request from the driver.
"""
_log.info("Waiting for next request")
req = self.request_queue.get(timeout=1)
_log.info("Got request %s", req)
return req
def assert_request(self, expected_key, **expected_args):
"""
Asserts the properies of the next request.
"""
req = self.request_queue.get(timeout=1)
req.assert_request(expected_key, **expected_args)
return req
def on_req_closed(self, req):
with self.lock:
self.open_reqs.remove(req)
def stop(self):
_log.info("Stopping stub etcd")
with self.lock:
_log.info("stop() got rq_lock")
while True:
try:
req = self.request_queue.get_nowait()
except Empty:
break
else:
self.open_reqs.add(req)
self.request_queue = None
for req in list(self.open_reqs):
_log.info("Aborting request %s", req)
req.stop()
_log.info("Stub etcd stopped; future requests should self-abort")
class MockResponse(object):
def __init__(self, status, data_or_exc, headers=None):
self.status = status
self._data_or_exc = data_or_exc
self.headers = headers or {}
@property
def data(self):
if isinstance(self._data_or_exc, Exception):
raise self._data_or_exc
elif hasattr(self._data_or_exc, "read"):
return self._data_or_exc.read()
else:
return self._data_or_exc
def read(self, *args):
return self._data_or_exc.read(*args)
def getheader(self, header, default=None):
_log.debug("Asked for header %s", header)
return self.headers.get(header.lower(), default)