summaryrefslogtreecommitdiff
path: root/framework/xsuite.py
blob: f85a84f14a2d0ab55e95fe8149dce40f153d5d58 (plain)
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
#
# Copyright © 2012 Intel Corporation
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice (including the next
# paragraph) shall be included in all copies or substantial portions of the
# Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
# IN THE SOFTWARE.
#
import json
import xml.etree.ElementTree as ET
import os.path as path
import re

#import framework.test

#__all__ = ['TestSuite', 'TestKind']

kinds = {}

class TestSuite:
    """
    Description of a test suite.

    For now, this contains the following information:
    - Name
    - Description
    - List of registered TestKinds
    - Absolute path to the XML test list file
    - Dictionary of tests
    """
    def __init__(self, xmlFile):
        tree = ET.parse(xmlFile)
        xmlSuite = tree.getroot()

        self.name = xmlSuite.attrib['name']
        self.description = xmlSuite.attrib['description']
        self.testlist_file = path.abspath(path.join(path.dirname(xmlFile.name), xmlSuite.attrib['tests']))
        self.kinds = {}
        for xmlKind in xmlSuite[0]:
            self.kinds[xmlKind.attrib['tag']] = TestKind(xmlKind)

        # Register the kinds...
        kinds.update(self.kinds)

        self.tests = {}

class TestKind:
    """
    Description of a class of tests (e.g. Piglit's ExecTest, GleanTest, ...).

    Robyn needs to know several facts about tests:
    - How to invoke them
    - How to interpret results
    - How to pass specific flags such as platform, quick mode, and so on.

    Test suites may register several different types of tests.  In the
    JSON test list, the "type" tag indicates what kind of test it is.
    """
    def __init__(self, xmlKind):
        xmlResult = xmlKind.find('result')

        self.default_result = xmlResult.attrib['default']
        self.regexs = {}
        for rx in xmlResult:
            self.regexs[rx.attrib['status']] = re.compile(rx.text)

        # XXX: parse <platforms> and <quick>

        # Register this Kind so it's available
        # XXX: may want to move this into the caller...or caller's caller
        #assert name not in kinds
        #kinds[name] = self

    def interpretResult(self):
        #for status, re in self.regexs.items():
            #if re.search(out) is not None:
                #result = status
                #break
        result = self.default_result

def availableSuites():
    """
    Read the ~/.robynrc file and fetch a list of available test suites.

    Returns a dictionary whose keys are suite names, and whose items are
    absolute paths to the JSON suite description file.

    Does not create TestSuite objects and populate the test suite information.
    """
    # XXX: load list of available suites from ~/.robynrc or such
    return {'piglit': '/home/kwg/Projects/piglit/robyn-suite.xml'}

### [str] x bool -> {str -> TestSuite}
def loadSuiteInfo(desiredSuites, loadTests):
    """
    Load TestSuite descriptions for a list of requested suites.
    """
    suiteFiles = availableSuites()

    suiteInfo = {}

    for name in desiredSuites:
        if name not in suiteFiles:
            print('error: unknown test suite "%s".\n' % name)
            print('       Available Suites:')
            for s in suiteFiles.keys():
                print('       -', s)
            return {}

        with open(suiteFiles[name], 'r') as fp:
            suiteInfo[name] = TestSuite(fp)

        if loadTests:
            loadTestList(suiteInfo[name])

    return suiteInfo

### TestSuite -> None
def loadTestList(suite):
    """Actually load the tests."""

    with open(suite.testlist_file) as fp:
        js = json.load(fp)

    suite.tests = js

    return None