File: check-test-format

package info (click to toggle)
snapd 2.72-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 80,412 kB
  • sloc: sh: 16,506; ansic: 16,211; python: 11,213; makefile: 1,919; exp: 190; awk: 58; xml: 22
file content (150 lines) | stat: -rwxr-xr-x 3,638 bytes parent folder | download | duplicates (3)
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
#!/usr/bin/python3

"""
This tool is used to verify a correct format of spread tests
The input is a directory which is scanned recursively and all
the task.yaml files are check
"""

import argparse
import glob
import os
import sys
import yaml
import yamlordereddictloader

SUPPORTED_KEYS = [
    "summary",
    "details",
    "backends",
    "systems",
    "manual",
    "priority",
    "warn-timeout",
    "kill-timeout",
    "environment",
    "artifacts",
    "prepare",
    "restore",
    "debug",
    "execute",
]
MANDATORY_KEYS = ["summary", "details", "execute"]


def check_mandatory_keys(task_keys):
    findings = []
    for key in MANDATORY_KEYS:
        if key not in task_keys:
            findings.append("Key '{}' is mandatory".format(key))

    return findings


def check_keys_order(task_keys):
    last_index = -1
    last_key = ""
    findings = []

    for curr_key in task_keys:
        try:
            curr_index = SUPPORTED_KEYS.index(curr_key)
            if curr_index <= last_index:
                findings.append(
                    "Keys '{}' and '{}' do not follow the desired order: {}".format(
                        last_key, curr_key, SUPPORTED_KEYS
                    )
                )

            last_index = curr_index
            last_key = curr_key

        except ValueError:
            findings.append(
                "key '{}' is not among the supported keys: {}".format(
                    curr_key, SUPPORTED_KEYS
                )
            )

    return findings


def check_task_format(filepath):
    if not os.path.isfile(filepath):
        print("Format checks failed for task {}".format(filepath))
        print(" - The path is not a file")
        return False

    filemap = dict()
    try:
        with open(filepath, "r") as task:
            filemap = yaml.load(task, Loader=yamlordereddictloader.Loader)
    except yaml.scanner.ScannerError:
        print("Invalid task format, checks failed for task {}".format(filepath))
        return False

    findings = check_keys_order(filemap.keys())
    findings.extend(check_mandatory_keys(filemap.keys()))
    if findings:
        print("Format checks failed for task {}".format(filepath))
        for finding in findings:
            print(" - " + finding)
        return False

    return True

def check_dir(directory):
    if not os.path.isdir(directory):
        print("Format checks failed for directory {}".format(directory))
        print(" - The path is not a directory")
        return False

    status = True
    for file in glob.glob(os.path.join(directory, "**/task.yaml"), recursive=True):
        if not check_task_format(file):
            status = False

    return status

def check_tests(tests):
    status = True
    for test in tests:
        if not os.path.isfile(test):
            print("Format checks failed for test {}".format(test))
            print(" - The path is not a file")
            status = False
            continue

        if not check_task_format(test):
            status = False

    return status

def _make_parser():
    parser = argparse.ArgumentParser()
    parser.add_argument(
        "--dir", help="path to the directory to check recursively"
    )
    parser.add_argument(
        "--tests", help="list of tests path to check", nargs='+'
    )
    return parser


def main():
    parser = _make_parser()
    args = parser.parse_args()

    status = 0
    if args.tests:
        if not check_tests(args.tests):
            status = 1

    if args.dir:
        if not check_dir(args.dir):
            status = 1

    sys.exit(status)

if __name__ == "__main__":
    main()