|
| 1 | +usage = """Parse Polarion stats from test suites. |
| 2 | +
|
| 3 | +
|
| 4 | +In the context of CephCI, |
| 5 | + path full path to suites directory |
| 6 | +
|
| 7 | + Example: |
| 8 | + python parse_polarion_data.py <path> --exclude luminous,nautilus |
| 9 | +
|
| 10 | +Usage: |
| 11 | + parse_polarion_data.py <path> [--exclude <str>] |
| 12 | + parse_polarion_data.py -h | --help |
| 13 | +
|
| 14 | +Options: |
| 15 | + --exclude <pattern> patterns to be excluded |
| 16 | + -h --help Show this screen |
| 17 | +
|
| 18 | +""" |
| 19 | +import logging |
| 20 | +import sys |
| 21 | +from os import walk |
| 22 | +from os.path import join |
| 23 | + |
| 24 | +from docopt import docopt |
| 25 | +from yaml import dump, safe_load |
| 26 | + |
| 27 | + |
| 28 | +def is_excluded(regex, string): |
| 29 | + """Return true if one of the excluded pattern matches. |
| 30 | +
|
| 31 | + Args: |
| 32 | + regex: patterns for comparison |
| 33 | + string |
| 34 | + Returns: |
| 35 | + boolean True if matches else False |
| 36 | + """ |
| 37 | + for exc in regex: |
| 38 | + if exc in string: |
| 39 | + return True |
| 40 | + return False |
| 41 | + |
| 42 | + |
| 43 | +def retrieve_polarion_data(file_string): |
| 44 | + """Retrieve polarion data |
| 45 | +
|
| 46 | + Return Test cases |
| 47 | + - with multiple Ids along with test case name, file |
| 48 | + - with no Ids along with test case name, file |
| 49 | +
|
| 50 | + - test: |
| 51 | + name: Install ceph pre-requisites |
| 52 | + desc: installation of ceph pre-requisites |
| 53 | + module: install_prereq.py |
| 54 | + abort-on-fail: true |
| 55 | +
|
| 56 | + Args: |
| 57 | + file_string: file path |
| 58 | + """ |
| 59 | + |
| 60 | + multiple_ids = [] |
| 61 | + missing_ids = [] |
| 62 | + good = [] |
| 63 | + _total = 0 |
| 64 | + |
| 65 | + with open(file_string, "r") as fs: |
| 66 | + print(file_string) |
| 67 | + ts = safe_load(fs) |
| 68 | + for tc in ts["tests"]: |
| 69 | + _tc = tc["test"] |
| 70 | + print(_tc) |
| 71 | + if not _tc or "install_prereq.py" in _tc.get("module", "NO_MODULE_DEFINED"): |
| 72 | + continue |
| 73 | + _total += 1 |
| 74 | + |
| 75 | + tc_name = _tc.get("name", f"NO-TESTNAME-{file_string}") |
| 76 | + |
| 77 | + if not _tc.get("polarion-id"): |
| 78 | + missing_ids.append(tc_name) |
| 79 | + continue |
| 80 | + pol_ids = _tc["polarion-id"] |
| 81 | + pol_ids = pol_ids.split(",") |
| 82 | + |
| 83 | + if len(pol_ids) > 1: |
| 84 | + multiple_ids.append({tc_name: pol_ids}) |
| 85 | + else: |
| 86 | + good.append({tc_name: pol_ids[-1]}) |
| 87 | + |
| 88 | + return { |
| 89 | + "multiple_ids": multiple_ids, |
| 90 | + "missing_ids": missing_ids, |
| 91 | + "good": good, |
| 92 | + "total": _total, |
| 93 | + } |
| 94 | + |
| 95 | + |
| 96 | +if __name__ == "__main__": |
| 97 | + logging.basicConfig( |
| 98 | + handlers=[logging.StreamHandler(sys.stdout)], |
| 99 | + level=logging.INFO, |
| 100 | + format="%(asctime)s - %(levelname)s - %(filename)s:%(lineno)d - %(message)s", |
| 101 | + ) |
| 102 | + |
| 103 | + _args = docopt(usage) |
| 104 | + suite_path = _args["<path>"] |
| 105 | + excludes = None |
| 106 | + if _args.get("--exclude"): |
| 107 | + excludes = _args["--exclude"] |
| 108 | + excludes = excludes.split(",") |
| 109 | + |
| 110 | + test_suites = [] |
| 111 | + test_data = {} |
| 112 | + total_count = 0 |
| 113 | + missing_id_count = 0 |
| 114 | + multiple_id_count = 0 |
| 115 | + |
| 116 | + for _root, _dir, _files in walk(suite_path): |
| 117 | + if not _files: |
| 118 | + continue |
| 119 | + if excludes and is_excluded(excludes, _root): |
| 120 | + continue |
| 121 | + |
| 122 | + for _file in _files: |
| 123 | + if ".yaml" in _file: |
| 124 | + __file = join(_root, _file) |
| 125 | + test_suites.append(__file) |
| 126 | + _data = retrieve_polarion_data(__file) |
| 127 | + total_count += _data["total"] |
| 128 | + missing_id_count += len(_data["missing_ids"]) |
| 129 | + multiple_id_count += len(_data["multiple_ids"]) |
| 130 | + |
| 131 | + test_data.update({__file: retrieve_polarion_data(__file)}) |
| 132 | + logging.info("\n".join(test_suites)) |
| 133 | + logging.info(test_data) |
| 134 | + |
| 135 | + test_data.update( |
| 136 | + { |
| 137 | + "total_test_cases": total_count, |
| 138 | + "missing_polarion_id_count": missing_id_count, |
| 139 | + "testcases_with_multiple_ids": multiple_id_count, |
| 140 | + } |
| 141 | + ) |
| 142 | + |
| 143 | + with open("polarion_data.yaml", "w") as outfile: |
| 144 | + dump(test_data, outfile, indent=4, default_flow_style=False) |
0 commit comments