|
15 | 15 | # License for the specific language governing permissions and limitations |
16 | 16 | # under the License. |
17 | 17 |
|
18 | | -import gzip |
19 | | -import io |
20 | 18 | import json |
21 | 19 | import os |
22 | 20 | import random |
23 | 21 | import sys |
24 | 22 |
|
25 | 23 | import pytest |
26 | | -from functools import wraps |
27 | | -from itertools import chain |
28 | 24 | from sys import float_info |
29 | | -from tempfile import mktemp |
30 | 25 | from time import time |
31 | | -from types import MethodType |
32 | 26 | from unittest import main, TestCase |
33 | 27 |
|
34 | 28 | from collections import OrderedDict |
35 | | -from collections import namedtuple, deque |
| 29 | +from collections import namedtuple |
36 | 30 |
|
37 | 31 | from splunklib.searchcommands.internals import ( |
38 | 32 | MetadataDecoder, |
39 | 33 | MetadataEncoder, |
40 | | - Recorder, |
41 | 34 | RecordWriterV2, |
42 | 35 | ) |
43 | 36 | from splunklib.searchcommands import SearchMetric |
44 | 37 | from io import BytesIO |
45 | | -import pickle |
46 | 38 |
|
47 | 39 |
|
48 | 40 | # region Functions for producing random apps |
@@ -299,154 +291,5 @@ def _load_chunks(self, ifile): |
299 | 291 | _package_path = os.path.dirname(os.path.abspath(__file__)) |
300 | 292 |
|
301 | 293 |
|
302 | | -class TestRecorder: |
303 | | - def __init__(self, test_case): |
304 | | - self._test_case = test_case |
305 | | - self._output = None |
306 | | - self._recording = None |
307 | | - self._recording_part = None |
308 | | - |
309 | | - def _not_implemented(self): |
310 | | - raise NotImplementedError( |
311 | | - "class {} is not in playback or record mode".format( |
312 | | - self.__class__.__name__ |
313 | | - ) |
314 | | - ) |
315 | | - |
316 | | - self.get = self.next_part = self.stop = MethodType( |
317 | | - _not_implemented, self, self.__class__ |
318 | | - ) |
319 | | - |
320 | | - @property |
321 | | - def output(self): |
322 | | - return self._output |
323 | | - |
324 | | - def playback(self, path): |
325 | | - with open(path, "rb") as f: |
326 | | - test_data = pickle.load(f) |
327 | | - |
328 | | - self._output = BytesIO() |
329 | | - self._recording = test_data["inputs"] |
330 | | - self._recording_part = self._recording.popleft() |
331 | | - |
332 | | - def get(self, method, *args, **kwargs): |
333 | | - return self._recording_part[method.__name__].popleft() |
334 | | - |
335 | | - self.get = MethodType(get, self, self.__class__) |
336 | | - |
337 | | - def next_part(self): |
338 | | - self._recording_part = self._recording.popleft() |
339 | | - |
340 | | - self.next_part = MethodType(next_part, self, self.__class__) |
341 | | - |
342 | | - def stop(self): |
343 | | - self._test_case.assertEqual(test_data["results"], self._output.getvalue()) |
344 | | - |
345 | | - self.stop = MethodType(stop, self, self.__class__) |
346 | | - |
347 | | - def record(self, path): |
348 | | - self._output = BytesIO() |
349 | | - self._recording = deque() |
350 | | - self._recording_part = OrderedDict() |
351 | | - self._recording.append(self._recording_part) |
352 | | - |
353 | | - def get(self, method, *args, **kwargs): |
354 | | - result = method(*args, **kwargs) |
355 | | - part = self._recording_part |
356 | | - key = method.__name__ |
357 | | - try: |
358 | | - results = part[key] |
359 | | - except KeyError: |
360 | | - part[key] = results = deque() |
361 | | - results.append(result) |
362 | | - return result |
363 | | - |
364 | | - self.get = MethodType(get, self, self.__class__) |
365 | | - |
366 | | - def next_part(self): |
367 | | - part = OrderedDict() |
368 | | - self._recording_part = part |
369 | | - self._recording.append(part) |
370 | | - |
371 | | - self.next_part = MethodType(next_part, self, self.__class__) |
372 | | - |
373 | | - def stop(self): |
374 | | - with io.open(path, "wb") as f: |
375 | | - test = OrderedDict( |
376 | | - (("inputs", self._recording), ("results", self._output.getvalue())) |
377 | | - ) |
378 | | - pickle.dump(test, f) |
379 | | - |
380 | | - self.stop = MethodType(stop, self, self.__class__) |
381 | | - |
382 | | - |
383 | | -def recorded(method): |
384 | | - @wraps(method) |
385 | | - def _record(*args, **kwargs): |
386 | | - return args[0].recorder.get(method, *args, **kwargs) |
387 | | - |
388 | | - return _record |
389 | | - |
390 | | - |
391 | | -class Test: |
392 | | - def __init__(self, fieldnames, data_generators): |
393 | | - TestCase.__init__(self) |
394 | | - self._data_generators = list( |
395 | | - chain((lambda: self._serial_number, time), data_generators) |
396 | | - ) |
397 | | - self._fieldnames = list(chain(("_serial", "_time"), fieldnames)) |
398 | | - self._recorder = TestRecorder(self) |
399 | | - self._serial_number = None |
400 | | - |
401 | | - @property |
402 | | - @recorded |
403 | | - def fieldnames(self): |
404 | | - return self._fieldnames |
405 | | - |
406 | | - @property |
407 | | - @recorded |
408 | | - def row(self): |
409 | | - return [data_generator.__call__() for data_generator in self._data_generators] |
410 | | - |
411 | | - @property |
412 | | - def recorder(self): |
413 | | - return self._recorder |
414 | | - |
415 | | - @property |
416 | | - def serial_number(self): |
417 | | - return self._serial_number |
418 | | - |
419 | | - def playback(self): |
420 | | - self.recorder.playback( |
421 | | - os.path.join(TestInternals._package_path, "TestRecorder.recording") |
422 | | - ) |
423 | | - self._run() |
424 | | - self.recorder.stop() |
425 | | - |
426 | | - def record(self): |
427 | | - self.recorder.record( |
428 | | - os.path.join(TestInternals._package_path, "TestRecorder.recording") |
429 | | - ) |
430 | | - self._run() |
431 | | - self.recorder.stop() |
432 | | - |
433 | | - def runTest(self): |
434 | | - pass # We'll adopt the new test recording mechanism a little later |
435 | | - |
436 | | - def _run(self): |
437 | | - writer = RecordWriterV2(self.recorder.output, maxresultrows=10) |
438 | | - write_record = writer.write_record |
439 | | - names = self.fieldnames |
440 | | - |
441 | | - for self._serial_number in range(0, 31): |
442 | | - record = OrderedDict(list(zip(names, self.row))) |
443 | | - write_record(record) |
444 | | - |
445 | | - |
446 | | -# test = Test(['random_bytes', 'random_unicode'], [random_bytes, random_unicode]) |
447 | | -# test.record() |
448 | | -# test.playback() |
449 | | - |
450 | | - |
451 | 294 | if __name__ == "__main__": |
452 | 295 | main() |
0 commit comments