1
# Copyright (c) 2010, 2011 Linaro
3
# This program is free software: you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation, either version 3 of the License, or
6
# (at your option) any later version.
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
# GNU General Public License for more details.
13
# You should have received a copy of the GNU General Public License
14
# along with this program. If not, see <http://www.gnu.org/licenses/>.
16
from __future__ import absolute_import
23
from lava_test.api.core import ITest
24
from lava_test.core.artifacts import TestArtifacts
25
from lava_test.core.config import get_config
26
from lava_test.core.installers import TestInstaller
27
from lava_test.core.parsers import TestParser, NativeTestParser
28
from lava_test.core.runners import TestRunner
29
from lava_test.utils import changed_directory
34
Reusable class for defining tests.
36
This class uses composition instead of inheritance. You should be able to
37
customize the parts you care about by providing delegate objects. This
38
class can be used by test definition files to create an object that
39
contains the building blocks for installing tests, running them, and
43
Name of the test or test suite
45
Version of the test or test suite
47
ITestInstaller instance to use
49
ITestRunner instance to use
51
ITestParser instance to use
54
def __init__(self, test_id, test_version=None,
55
installer=None, runner=None, parser=None):
56
self._test_id = test_id
57
self._test_version = test_version
59
self.installer = installer
63
self._config = get_config()
66
return ("<%s test_id=%r test_version=%r installer=%r runner=%r"
68
self.__class__.__name__, self.test_id, self.test_version,
69
self.installer, self.runner, self.parser)
74
Return the ID of the test.
79
def test_version(self):
81
Return the version of the test
83
return self._test_version
86
def install_dir(self):
88
Pathname of a directory with binary and data files installed by the
93
return os.path.join(self._config.installdir, self.test_id)
96
def is_installed(self):
97
return os.path.exists(self.install_dir)
99
def install(self, observer=None):
100
if self.is_installed:
102
"%s is already installed" % self.test_id)
103
if not self.installer:
105
"no installer defined for '%s'" % self.test_id)
106
with changed_directory(self.install_dir):
109
"Invoking %r.install(...)", self.installer)
110
self.installer.install(observer)
116
logging.debug("Removing test %r", self.test_id)
117
if os.path.exists(self.install_dir):
118
shutil.rmtree(self.install_dir)
120
def run(self, observer=None):
123
"no test runner defined for '%s'" % self.test_id)
124
artifacts = TestArtifacts.allocate(self.test_id, self._config)
125
with changed_directory(self.install_dir):
127
"Invoking %r.run_and_store_artifacts(...)",
128
self.runner, observer)
129
self.runner.run(artifacts, observer)
132
def parse(self, artifacts):
134
logging.debug("Invoking %r.parse()", self.parser)
135
with changed_directory(artifacts.results_dir, False):
136
self.parser.parse(artifacts)
137
return self.parser.results
140
class DeclarativeTest(Test):
142
Declaretive ITest implementation.
144
Declarative test is like :class:`lava_test.core.tests.Test` but cannot
145
contain any python code and is completely encapsulated in a .json file.
147
The idea is to write .json files that assemble a Test instance using
148
readily-available TestInstaller, TestRunner and TestParser subclasses.
151
def __init__(self, about):
153
super(DeclarativeTest, self).__init__(self.about.get('test_id'))
154
self.installer = TestInstaller(**self.about.get('install', {}))
155
self.runner = TestRunner(**self.about.get('run', {}))
156
if self.about.get('parse', {}).get('native', False) is True:
157
self.parser = NativeTestParser(self)
159
self.parser = TestParser(**self.about.get('parse', {}))
162
def load_from_stream(cls, stream):
163
return cls(json.load(stream))
165
def save_to_stream(self, stream):
166
json.dumps(self.about, stream, indent="2")