|
1 | 1 | # Copyright 2021 Contributors to the Parsec project.
|
2 | 2 | # SPDX-License-Identifier: Apache-2.0
|
| 3 | +import click |
| 4 | + |
| 5 | +import socket |
| 6 | +import os |
| 7 | +from os import listdir |
| 8 | +from os.path import isfile, join |
| 9 | + |
| 10 | +import base64 |
| 11 | + |
| 12 | +from yaml import safe_load |
| 13 | + |
| 14 | + |
| 15 | +@click.command() |
| 16 | +@click.option('--test-folder', default='./testdata', help='Load all tests from folder') |
| 17 | +@click.option('--parsec-socket', default='./parsec.sock', help='Path to parsec unix socket') |
| 18 | +def run_test(test_folder, parsec_socket): |
| 19 | + print('Mock parsec service listening on unix://{}.'.format(parsec_socket)) |
| 20 | + |
| 21 | + test_cases = load_tests_from_folder(test_folder) |
| 22 | + |
| 23 | + print('Serving all {} tests in folder {}'.format(len(test_cases),test_folder)) |
| 24 | + |
| 25 | + # Make sure socket doesn't already exist |
| 26 | + try: |
| 27 | + os.unlink(parsec_socket) |
| 28 | + except OSError: |
| 29 | + if os.path.exists(parsec_socket): |
| 30 | + print('Error removing old parsec socket, exiting') |
| 31 | + return |
| 32 | + |
| 33 | + # Create a unix socket |
| 34 | + sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) |
| 35 | + sock.bind(parsec_socket) |
| 36 | + |
| 37 | + sock.listen(1) |
| 38 | + |
| 39 | + while True: |
| 40 | + connection, client_addr = sock.accept() |
| 41 | + try: |
| 42 | + print('Connection received from {}'.format(client_addr)) |
| 43 | + received_data = connection.recv(4096) |
| 44 | + b64_received_data = base64.b64encode(received_data).decode('ascii') |
| 45 | + if b64_received_data in test_cases: |
| 46 | + test_case = test_cases[b64_received_data] |
| 47 | + print('Received expected request for test case {}'.format(test_case.spec.name)) |
| 48 | + bin_response = base64.b64decode(test_case.test_data.response) |
| 49 | + connection.sendall(bin_response) |
| 50 | + else: |
| 51 | + print('Received unexpected request {}'.format(b64_received_data)) |
| 52 | + finally: |
| 53 | + connection.close() |
| 54 | + |
| 55 | + |
| 56 | +class TestSpec(object): |
| 57 | + """Class to represent a test specification. Used to convert |
| 58 | + dictionary created by pyaml to object format, making code easier to read.""" |
| 59 | + |
| 60 | + def __init__(self, dictionary): |
| 61 | + def _traverse(key, element): |
| 62 | + if isinstance(element, dict): |
| 63 | + return key, TestSpec(element) |
| 64 | + else: |
| 65 | + return key, element |
| 66 | + |
| 67 | + objd = dict(_traverse(k, v) for k, v in dictionary.items()) |
| 68 | + self.__dict__.update(objd) |
| 69 | + self.basedict = dictionary |
| 70 | + |
| 71 | + def is_valid(self): |
| 72 | + return True |
| 73 | + |
| 74 | + |
| 75 | +def load_tests_from_folder(test_folder): |
| 76 | + tests = {} |
| 77 | + """Read test specs from a folder""" |
| 78 | + specfiles = [f for f in listdir(test_folder) if isfile(join(test_folder, f))] |
| 79 | + |
| 80 | + for file in specfiles: |
| 81 | + print(f"Parsing spec file: {file}") |
| 82 | + with open(os.path.join(test_folder, file), 'r') as f: |
| 83 | + spec = safe_load(f) |
| 84 | + testspec = TestSpec(spec) |
| 85 | + if testspec.is_valid(): |
| 86 | + tests[testspec.test_data.request] = testspec |
| 87 | + else: |
| 88 | + print(f"Error loading test spec from {file}") |
| 89 | + |
| 90 | + return tests |
| 91 | + |
| 92 | + |
| 93 | +if __name__ == "__main__": |
| 94 | + run_test() |
0 commit comments