Skip to content
Snippets Groups Projects
parser.py 2.37 KiB
Newer Older
#!/usr/bin/env python3
#
# This module defines the test case format and implement a
# parser for such a format.
#

import sys
import yaml


test_case_format = {
    'metadata' : (True, {
        'name' : (True, ""),
        'format' : (False, ""),
        'image-type' : (True, [ 'target', 'minimal', 'ostree',
                                'development', 'SDK', 'any' ]),
        'image-arch' : (True, ['amd64', 'arm64', 'armhf', 'any']),
        'type' : (True, ['functional', 'sanity', 'system']),
        'exec-type' : (True, ['manual' , 'automated']),
        'priority' : (True, ['low', 'medium', 'high', 'critical']),
        'maintainer' : (False, ""),
        'description' : (True, ""),
        'resources' : (False, []),
        'pre-conditions' : (False, []),
        'expected' : (True, []),
        'notes' : (False, []),
        'post-conditions' : (False, [])
    }),
    'install' : (False, {
        'deps' : (True, [])
    }),
    'run' : (True, {
        'steps' : (True, [])
    }),
    'parse' : (False, {})
}

def parse_format(test_case, test_case_format):
    for tagf, valuestr in test_case_format.items():
        mandatory, valuef = valuestr
        value = test_case.get(tagf)
        if not value:
            if mandatory:
                sys.stderr.write("Error: mandatory field missing: '{}'\n"
                                 .format(tagf))
                sys.exit(1)
            # Test case doesn't have this non-mandatory tag, so just continue.
            continue

        if type(value) == str and type(valuef) == list:
            if tagf in ['image-type' , 'image-arch', 'type',
                        'exec-type', 'priority']:
                try:
                    valuef.index(value)
                except ValueError:
                    sys.stderr.write("Warning: value '{}' not found in '{}' for "\
                                     "field '{}'\n".format(value, valuef, tagf))
                continue

        if type(value) != type(valuef):
            sys.stderr.write("Error: incorrect type for field: '{}'\n"
                             .format(tagf))
            sys.exit(1)

        if type(value) == dict:
            parse_format(value, valuef)

    return True


if '__main__' == __name__:
    testcase = sys.argv[1]
    with open(testcase) as test_case:
        test_case_data = yaml.safe_load(test_case)

    parse_format(test_case_data, test_case_format)