summaryrefslogtreecommitdiffstats
path: root/meta/lib/oeqa/selftest/context.py
diff options
context:
space:
mode:
Diffstat (limited to 'meta/lib/oeqa/selftest/context.py')
-rw-r--r--meta/lib/oeqa/selftest/context.py224
1 files changed, 224 insertions, 0 deletions
diff --git a/meta/lib/oeqa/selftest/context.py b/meta/lib/oeqa/selftest/context.py
new file mode 100644
index 0000000000..ca73070c0b
--- /dev/null
+++ b/meta/lib/oeqa/selftest/context.py
@@ -0,0 +1,224 @@
1# Copyright (C) 2017 Intel Corporation
2# Released under the MIT license (see COPYING.MIT)
3
4import os
5import time
6import glob
7import sys
8import imp
9from random import choice
10
11import oeqa
12
13from oeqa.core.context import OETestContext, OETestContextExecutor
14from oeqa.core.exception import OEQAPreRun
15
16from oeqa.utils.commands import runCmd, get_bb_vars, get_test_layer
17
18class OESelftestTestContext(OETestContext):
19 def __init__(self, td=None, logger=None, machines=None, testlayer_path=None):
20 super(OESelftestTestContext, self).__init__(td, logger)
21
22 self.machines = machines
23 self.custommachine = None
24
25 self.testlayer_path = testlayer_path
26
27 def runTests(self, machine=None):
28 if machine:
29 self.custommachine = machine
30 if machine == 'random':
31 self.custommachine = choice(self.machines)
32 self.logger.info('Run tests with custom MACHINE set to: %s' % \
33 self.custommachine)
34 return super(OESelftestTestContext, self).runTests()
35
36 def listTests(self, display_type, machine=None):
37 return super(OESelftestTestContext, self).listTests(display_type)
38
39class OESelftestTestContextExecutor(OETestContextExecutor):
40 _context_class = OESelftestTestContext
41 _script_executor = 'oe-selftest'
42
43 name = 'oe-selftest'
44 help = 'oe-selftest test component'
45 description = 'Executes selftest tests'
46
47 def register_commands(self, logger, parser):
48 group = parser.add_mutually_exclusive_group(required=True)
49
50 group.add_argument('-a', '--run-all-tests', default=False,
51 action="store_true", dest="run_all_tests",
52 help='Run all (unhidden) tests')
53 group.add_argument('-r', '--run-tests', required=False, action='store',
54 nargs='+', dest="run_tests", default=None,
55 help='Select what tests to run (modules, classes or test methods). Format should be: <module>.<class>.<test_method>')
56
57 group.add_argument('-m', '--list-modules', required=False,
58 action="store_true", default=False,
59 help='List all available test modules.')
60 group.add_argument('--list-classes', required=False,
61 action="store_true", default=False,
62 help='List all available test classes.')
63 group.add_argument('-l', '--list-tests', required=False,
64 action="store_true", default=False,
65 help='List all available tests.')
66
67 parser.add_argument('--machine', required=False, choices=['random', 'all'],
68 help='Run tests on different machines (random/all).')
69
70 parser.set_defaults(func=self.run)
71
72 def _get_available_machines(self):
73 machines = []
74
75 bbpath = self.tc_kwargs['init']['td']['BBPATH'].split(':')
76
77 for path in bbpath:
78 found_machines = glob.glob(os.path.join(path, 'conf', 'machine', '*.conf'))
79 if found_machines:
80 for i in found_machines:
81 # eg: '/home/<user>/poky/meta-intel/conf/machine/intel-core2-32.conf'
82 machines.append(os.path.splitext(os.path.basename(i))[0])
83
84 return machines
85
86 def _get_cases_paths(self, bbpath):
87 cases_paths = []
88 for layer in bbpath:
89 cases_dir = os.path.join(layer, 'lib', 'oeqa', 'selftest', 'cases')
90 if os.path.isdir(cases_dir):
91 cases_paths.append(cases_dir)
92 return cases_paths
93
94 def _process_args(self, logger, args):
95 args.output_log = '%s-results-%s.log' % (self.name,
96 time.strftime("%Y%m%d%H%M%S"))
97 args.test_data_file = None
98 args.CASES_PATHS = None
99
100 super(OESelftestTestContextExecutor, self)._process_args(logger, args)
101
102 if args.list_modules:
103 args.list_tests = 'module'
104 elif args.list_classes:
105 args.list_tests = 'class'
106 elif args.list_tests:
107 args.list_tests = 'name'
108
109 self.tc_kwargs['init']['td'] = get_bb_vars()
110 self.tc_kwargs['init']['machines'] = self._get_available_machines()
111 self.tc_kwargs['init']['testlayer_path'] = get_test_layer()
112
113 def _pre_run(self):
114 def _check_required_env_variables(vars):
115 for var in vars:
116 if not os.environ.get(var):
117 self.tc.logger.error("%s is not set. Did you forget to source your build environment setup script?" % var)
118 raise OEQAPreRun
119
120 def _check_presence_meta_selftest():
121 builddir = os.environ.get("BUILDDIR")
122 if os.getcwd() != builddir:
123 self.tc.logger.info("Changing cwd to %s" % builddir)
124 os.chdir(builddir)
125
126 if not "meta-selftest" in self.tc.td["BBLAYERS"]:
127 self.tc.logger.warn("meta-selftest layer not found in BBLAYERS, adding it")
128 meta_selftestdir = os.path.join(
129 self.tc.td["BBLAYERS_FETCH_DIR"], 'meta-selftest')
130 if os.path.isdir(meta_selftestdir):
131 runCmd("bitbake-layers add-layer %s" %meta_selftestdir)
132 # reload data is needed because a meta-selftest layer was add
133 self.tc.td = get_bb_vars()
134 else:
135 self.tc.logger.error("could not locate meta-selftest in:\n%s" % meta_selftestdir)
136 raise OEQAPreRun
137
138 def _add_layer_libs():
139 bbpath = self.tc.td['BBPATH'].split(':')
140 layer_libdirs = [p for p in (os.path.join(l, 'lib') \
141 for l in bbpath) if os.path.exists(p)]
142 if layer_libdirs:
143 self.tc.logger.info("Adding layer libraries:")
144 for l in layer_libdirs:
145 self.tc.logger.info("\t%s" % l)
146
147 sys.path.extend(layer_libdirs)
148 imp.reload(oeqa.selftest)
149
150 _check_required_env_variables(["BUILDDIR"])
151 _check_presence_meta_selftest()
152
153 if "buildhistory.bbclass" in self.tc.td["BBINCLUDED"]:
154 self.tc.logger.error("You have buildhistory enabled already and this isn't recommended for selftest, please disable it first.")
155 raise OEQAPreRun
156
157 if "PRSERV_HOST" in self.tc.td:
158 self.tc.logger.error("Please unset PRSERV_HOST in order to run oe-selftest")
159 raise OEQAPreRun
160
161 if "SANITY_TESTED_DISTROS" in self.tc.td:
162 self.tc.logger.error("Please unset SANITY_TESTED_DISTROS in order to run oe-selftest")
163 raise OEQAPreRun
164
165 _add_layer_libs()
166
167 self.tc.logger.info("Running bitbake -p")
168 runCmd("bitbake -p")
169
170 def _internal_run(self, logger, args):
171 self.module_paths = self._get_cases_paths(
172 self.tc_kwargs['init']['td']['BBPATH'].split(':'))
173
174 self.tc = self._context_class(**self.tc_kwargs['init'])
175 self.tc.loadTests(self.module_paths, **self.tc_kwargs['load'])
176
177 if args.list_tests:
178 rc = self.tc.listTests(args.list_tests, **self.tc_kwargs['run'])
179 else:
180 self._pre_run()
181 rc = self.tc.runTests(**self.tc_kwargs['run'])
182 rc.logSummary(self.name)
183 rc.logDetails()
184
185 return rc
186
187 def run(self, logger, args):
188 self._process_args(logger, args)
189 rc = None
190
191 if args.machine:
192 logger.info('Custom machine mode enabled. MACHINE set to %s' %
193 args.machine)
194
195 if args.machine == 'all':
196 results = []
197 for m in self.tc_kwargs['init']['machines']:
198 self.tc_kwargs['run']['machine'] = m
199 results.append(self._internal_run(logger, args))
200
201 # XXX: the oe-selftest script only needs to know if one
202 # machine run fails
203 for r in results:
204 rc = r
205 if not r.wasSuccessful():
206 break
207
208 else:
209 self.tc_kwargs['run']['machine'] = args.machine
210 return self._internal_run(logger, args)
211
212 else:
213 self.tc_kwargs['run']['machine'] = args.machine
214 rc = self._internal_run(logger, args)
215
216 output_link = os.path.join(os.path.dirname(args.output_log),
217 "%s-results.log" % self.name)
218 if os.path.exists(output_link):
219 os.remove(output_link)
220 os.symlink(args.output_log, output_link)
221
222 return rc
223
224_executor_class = OESelftestTestContextExecutor