|
| 1 | +import argparse |
| 2 | +import re |
| 3 | +import subprocess |
| 4 | +import yaml |
| 5 | + |
| 6 | + |
| 7 | +class TestRunner: |
| 8 | + |
| 9 | + def __init__(self): |
| 10 | + pass |
| 11 | + |
| 12 | + def load_yaml_file(self, filepath): |
| 13 | + with open(filepath, 'r') as f: |
| 14 | + data = yaml.safe_load(f) |
| 15 | + return data |
| 16 | + |
| 17 | + # Function to process YAML |
| 18 | + def process_data(self, data): |
| 19 | + for test in data: |
| 20 | + self.run_test(test) |
| 21 | + |
| 22 | + # Function to execute a single test case |
| 23 | + def run_test(self, test): |
| 24 | + test_name = test["name"] |
| 25 | + print("\n========== " + test_name + " =========") |
| 26 | + inputs = test["inputs"] |
| 27 | + env_map = {} |
| 28 | + for input in inputs: |
| 29 | + cmd = input['cmd'] |
| 30 | + container = input.get("node", "sei-node-0") |
| 31 | + output = self.run_bash_command(cmd, True, container, env_map, False) |
| 32 | + if input.get('env'): |
| 33 | + env_map[input['env']] = output |
| 34 | + result = output |
| 35 | + print(f'Input : {cmd}') |
| 36 | + print(f'Output: {result}') |
| 37 | + for verifier in test["verifiers"]: |
| 38 | + if not self.verify_result(env_map, verifier): |
| 39 | + print("Test failed for {}".format(verifier)) |
| 40 | + exit(1) |
| 41 | + print("Test Passed") |
| 42 | + |
| 43 | + # Function to verify the result of a single test case |
| 44 | + def verify_result(self, env_map, verifier): |
| 45 | + type = verifier["type"] |
| 46 | + if type == "eval": |
| 47 | + elems = verifier["expr"].strip().split() |
| 48 | + if len(elems) == 3: |
| 49 | + if env_map.get(elems[0]): |
| 50 | + elems[0] = env_map[elems[0]] |
| 51 | + if env_map.get(elems[2]): |
| 52 | + elems[2] = env_map[elems[2]] |
| 53 | + expr = f'{elems[0]} {elems[1]} {elems[2]}' |
| 54 | + return eval(expr) |
| 55 | + return False |
| 56 | + elif type == "regex": |
| 57 | + env_key = verifier["result"] |
| 58 | + expression = str(verifier["expr"]) |
| 59 | + result = env_map[env_key].strip() |
| 60 | + return re.match(expression, result) |
| 61 | + else: |
| 62 | + return False |
| 63 | + |
| 64 | + # Helper function to execute a single command within the docker container |
| 65 | + def run_bash_command(self, command, docker=False, container="", env_map=None, verbose=False): |
| 66 | + if docker: |
| 67 | + envs = "" |
| 68 | + if env_map: |
| 69 | + for key in env_map: |
| 70 | + envs += f'-e {key}=\'{env_map[key]}\' ' |
| 71 | + full_cmd = f'docker exec {envs} {container} /bin/bash -c \'export PATH=$PATH:/root/go/bin && {command}\'' |
| 72 | + else: |
| 73 | + full_cmd = command |
| 74 | + if verbose: |
| 75 | + print(f'Command: {full_cmd}') |
| 76 | + process = subprocess.Popen(full_cmd, stdout=subprocess.PIPE, shell=True) |
| 77 | + output, error = process.communicate() |
| 78 | + return output.decode().strip() |
| 79 | + |
| 80 | + |
| 81 | +def main(): |
| 82 | + parser = argparse.ArgumentParser( |
| 83 | + description='Integration test runner', |
| 84 | + formatter_class=argparse.ArgumentDefaultsHelpFormatter, |
| 85 | + ) |
| 86 | + parser.add_argument('filepath', type=str, help='The path to the JSON file') |
| 87 | + args = parser.parse_args() |
| 88 | + runner = TestRunner() |
| 89 | + data = runner.load_yaml_file(args.filepath) |
| 90 | + runner.process_data(data) |
| 91 | + |
| 92 | + |
| 93 | +if __name__ == '__main__': |
| 94 | + main() |
| 95 | + |
| 96 | + |
0 commit comments