summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLeonardo Sandoval <leonardo.sandoval.gonzalez@linux.intel.com>2017-05-25 12:23:42 -0500
committerRichard Purdie <richard.purdie@linuxfoundation.org>2017-06-06 19:02:43 +0100
commita80aa4c025cb9af621fae474e8f1361cdf7b124f (patch)
treed259048249aa26f135eac4a9f640e1a13fbb4ade
parentcb1b1eeb3f41bbb183694399b24c327b111a1421 (diff)
downloadpoky-a80aa4c025cb9af621fae474e8f1361cdf7b124f.tar.gz
oeqa/selftest/case: Migrate case class to the new OEQA framework
Summary of the changes: - Use OETestCase as base class instead of unittest.TestCase - Remove LogResults decorator the new framework provides logging into the core functionality. - Logger is now self.logger instead of self.log - Move comments into docstrings in several help methods - Use get_test_layer() method instead of access monkey patched variable in old oeSelfTest case class. (From OE-Core rev: c38cab77893f9d8fd505f050cc880a15677b73db) Signed-off-by: Leonardo Sandoval <leonardo.sandoval.gonzalez@linux.intel.com> Signed-off-by: Aníbal Limón <anibal.limon@linux.intel.com> Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
-rw-r--r--meta/lib/oeqa/selftest/case.py111
1 files changed, 51 insertions, 60 deletions
diff --git a/meta/lib/oeqa/selftest/case.py b/meta/lib/oeqa/selftest/case.py
index 43a1951be3..95a8769bef 100644
--- a/meta/lib/oeqa/selftest/case.py
+++ b/meta/lib/oeqa/selftest/case.py
@@ -1,31 +1,19 @@
1# Copyright (c) 2013 Intel Corporation 1# Copyright (C) 2013-2017 Intel Corporation
2#
3# Released under the MIT license (see COPYING.MIT) 2# Released under the MIT license (see COPYING.MIT)
4 3
5
6# DESCRIPTION
7# Base class inherited by test classes in meta/lib/oeqa/selftest
8
9import unittest
10import os
11import sys 4import sys
5import os
12import shutil 6import shutil
13import logging 7import glob
14import errno 8import errno
15
16import oeqa.utils.ftools as ftools
17from oeqa.utils.commands import runCmd, bitbake, get_bb_var, get_test_layer
18from oeqa.utils.decorators import LogResults
19from random import choice 9from random import choice
20import glob
21from unittest.util import safe_repr 10from unittest.util import safe_repr
22 11
23@LogResults 12import oeqa.utils.ftools as ftools
24class oeSelfTest(unittest.TestCase): 13from oeqa.utils.commands import runCmd, bitbake, get_bb_var, get_test_layer
25 14from oeqa.core.case import OETestCase
26 log = logging.getLogger("selftest.base")
27 longMessage = True
28 15
16class OESelftestTestCase(OETestCase):
29 def __init__(self, methodName="runTest"): 17 def __init__(self, methodName="runTest"):
30 self.builddir = os.environ.get("BUILDDIR") 18 self.builddir = os.environ.get("BUILDDIR")
31 self.localconf_path = os.path.join(self.builddir, "conf/local.conf") 19 self.localconf_path = os.path.join(self.builddir, "conf/local.conf")
@@ -36,23 +24,26 @@ class oeSelfTest(unittest.TestCase):
36 "conf/bblayers.bk") 24 "conf/bblayers.bk")
37 self.testinc_bblayers_path = os.path.join(self.builddir, "conf/bblayers.inc") 25 self.testinc_bblayers_path = os.path.join(self.builddir, "conf/bblayers.inc")
38 self.machineinc_path = os.path.join(self.builddir, "conf/machine.inc") 26 self.machineinc_path = os.path.join(self.builddir, "conf/machine.inc")
39 self.testlayer_path = oeSelfTest.testlayer_path 27 self.testlayer_path = get_test_layer()
40 self._extra_tear_down_commands = [] 28 self._extra_tear_down_commands = []
41 self._track_for_cleanup = [ 29 self._track_for_cleanup = [
42 self.testinc_path, self.testinc_bblayers_path, 30 self.testinc_path, self.testinc_bblayers_path,
43 self.machineinc_path, self.localconf_backup, 31 self.machineinc_path, self.localconf_backup,
44 self.local_bblayers_backup] 32 self.local_bblayers_backup]
45 super(oeSelfTest, self).__init__(methodName) 33
34 super(OESelftestTestCase, self).__init__(methodName)
46 35
47 def setUp(self): 36 def setUp(self):
37 super(OESelftestTestCase, self).setUp()
48 os.chdir(self.builddir) 38 os.chdir(self.builddir)
49 # Check if local.conf or bblayers.conf files backup exists 39 # Check if local.conf or bblayers.conf files backup exists
50 # from a previous failed test and restore them 40 # from a previous failed test and restore them
51 if os.path.isfile(self.localconf_backup) or os.path.isfile( 41 if os.path.isfile(self.localconf_backup) or os.path.isfile(
52 self.local_bblayers_backup): 42 self.local_bblayers_backup):
53 self.log.debug("Found a local.conf and/or bblayers.conf backup \ 43 self.logger.debug("\
54from a previously aborted test. Restoring these files now, but tests should \ 44Found a local.conf and/or bblayers.conf backup from a previously aborted test.\
55be re-executed from a clean environment to ensure accurate results.") 45Restoring these files now, but tests should be re-executed from a clean environment\
46to ensure accurate results.")
56 try: 47 try:
57 shutil.copyfile(self.localconf_backup, self.localconf_path) 48 shutil.copyfile(self.localconf_backup, self.localconf_path)
58 except OSError as e: 49 except OSError as e:
@@ -67,9 +58,8 @@ be re-executed from a clean environment to ensure accurate results.")
67 else: 58 else:
68 # backup local.conf and bblayers.conf 59 # backup local.conf and bblayers.conf
69 shutil.copyfile(self.localconf_path, self.localconf_backup) 60 shutil.copyfile(self.localconf_path, self.localconf_backup)
70 shutil.copyfile(self.local_bblayers_path, 61 shutil.copyfile(self.local_bblayers_path, self.local_bblayers_backup)
71 self.local_bblayers_backup) 62 self.logger.debug("Creating local.conf and bblayers.conf backups.")
72 self.log.debug("Creating local.conf and bblayers.conf backups.")
73 # we don't know what the previous test left around in config or inc files 63 # we don't know what the previous test left around in config or inc files
74 # if it failed so we need a fresh start 64 # if it failed so we need a fresh start
75 try: 65 try:
@@ -116,8 +106,8 @@ be re-executed from a clean environment to ensure accurate results.")
116 if not result.status == 0: 106 if not result.status == 0:
117 failed_extra_commands.append(command) 107 failed_extra_commands.append(command)
118 if failed_extra_commands: 108 if failed_extra_commands:
119 self.log.warning("tearDown commands have failed: %s" % ', '.join(map(str, failed_extra_commands))) 109 self.logger.warning("tearDown commands have failed: %s" % ', '.join(map(str, failed_extra_commands)))
120 self.log.debug("Trying to move on.") 110 self.logger.debug("Trying to move on.")
121 self._extra_tear_down_commands = [] 111 self._extra_tear_down_commands = []
122 112
123 if self._track_for_cleanup: 113 if self._track_for_cleanup:
@@ -129,90 +119,92 @@ be re-executed from a clean environment to ensure accurate results.")
129 self._track_for_cleanup = [] 119 self._track_for_cleanup = []
130 120
131 self.tearDownLocal() 121 self.tearDownLocal()
122 super(OESelftestTestCase, self).tearDown()
132 123
133 def tearDownLocal(self): 124 def tearDownLocal(self):
134 pass 125 pass
135 126
136 # add test specific commands to the tearDown method.
137 def add_command_to_tearDown(self, command): 127 def add_command_to_tearDown(self, command):
138 self.log.debug("Adding command '%s' to tearDown for this test." % command) 128 """Add test specific commands to the tearDown method"""
129 self.logger.debug("Adding command '%s' to tearDown for this test." % command)
139 self._extra_tear_down_commands.append(command) 130 self._extra_tear_down_commands.append(command)
140 # add test specific files or directories to be removed in the tearDown method 131
141 def track_for_cleanup(self, path): 132 def track_for_cleanup(self, path):
142 self.log.debug("Adding path '%s' to be cleaned up when test is over" % path) 133 """Add test specific files or directories to be removed in the tearDown method"""
134 self.logger.debug("Adding path '%s' to be cleaned up when test is over" % path)
143 self._track_for_cleanup.append(path) 135 self._track_for_cleanup.append(path)
144 136
145 # write to <builddir>/conf/selftest.inc
146 def write_config(self, data): 137 def write_config(self, data):
147 self.log.debug("Writing to: %s\n%s\n" % (self.testinc_path, data)) 138 """Write to <builddir>/conf/selftest.inc"""
139
140 self.logger.debug("Writing to: %s\n%s\n" % (self.testinc_path, data))
148 ftools.write_file(self.testinc_path, data) 141 ftools.write_file(self.testinc_path, data)
149 142
150 custommachine = os.getenv('CUSTOMMACHINE') 143 custommachine = os.getenv('CUSTOMMACHINE')
151 if custommachine and 'MACHINE' in data: 144 if custommachine and 'MACHINE' in data:
152 machine = get_bb_var('MACHINE') 145 machine = get_bb_var('MACHINE')
153 self.log.warning('MACHINE overridden: %s' % machine) 146 self.logger.warning('MACHINE overridden: %s' % machine)
154 147
155 # append to <builddir>/conf/selftest.inc
156 def append_config(self, data): 148 def append_config(self, data):
157 self.log.debug("Appending to: %s\n%s\n" % (self.testinc_path, data)) 149 """Append to <builddir>/conf/selftest.inc"""
150 self.logger.debug("Appending to: %s\n%s\n" % (self.testinc_path, data))
158 ftools.append_file(self.testinc_path, data) 151 ftools.append_file(self.testinc_path, data)
159 152
160 custommachine = os.getenv('CUSTOMMACHINE') 153 custommachine = os.getenv('CUSTOMMACHINE')
161 if custommachine and 'MACHINE' in data: 154 if custommachine and 'MACHINE' in data:
162 machine = get_bb_var('MACHINE') 155 machine = get_bb_var('MACHINE')
163 self.log.warning('MACHINE overridden: %s' % machine) 156 self.logger.warning('MACHINE overridden: %s' % machine)
164 157
165 # remove data from <builddir>/conf/selftest.inc
166 def remove_config(self, data): 158 def remove_config(self, data):
167 self.log.debug("Removing from: %s\n%s\n" % (self.testinc_path, data)) 159 """Remove data from <builddir>/conf/selftest.inc"""
160 self.logger.debug("Removing from: %s\n%s\n" % (self.testinc_path, data))
168 ftools.remove_from_file(self.testinc_path, data) 161 ftools.remove_from_file(self.testinc_path, data)
169 162
170 # write to meta-sefltest/recipes-test/<recipe>/test_recipe.inc
171 def write_recipeinc(self, recipe, data): 163 def write_recipeinc(self, recipe, data):
164 """Write to meta-sefltest/recipes-test/<recipe>/test_recipe.inc"""
172 inc_file = os.path.join(self.testlayer_path, 'recipes-test', recipe, 'test_recipe.inc') 165 inc_file = os.path.join(self.testlayer_path, 'recipes-test', recipe, 'test_recipe.inc')
173 self.log.debug("Writing to: %s\n%s\n" % (inc_file, data)) 166 self.logger.debug("Writing to: %s\n%s\n" % (inc_file, data))
174 ftools.write_file(inc_file, data) 167 ftools.write_file(inc_file, data)
175 168
176 # append data to meta-sefltest/recipes-test/<recipe>/test_recipe.inc
177 def append_recipeinc(self, recipe, data): 169 def append_recipeinc(self, recipe, data):
170 """Append data to meta-sefltest/recipes-test/<recipe>/test_recipe.inc"""
178 inc_file = os.path.join(self.testlayer_path, 'recipes-test', recipe, 'test_recipe.inc') 171 inc_file = os.path.join(self.testlayer_path, 'recipes-test', recipe, 'test_recipe.inc')
179 self.log.debug("Appending to: %s\n%s\n" % (inc_file, data)) 172 self.logger.debug("Appending to: %s\n%s\n" % (inc_file, data))
180 ftools.append_file(inc_file, data) 173 ftools.append_file(inc_file, data)
181 174
182 # remove data from meta-sefltest/recipes-test/<recipe>/test_recipe.inc
183 def remove_recipeinc(self, recipe, data): 175 def remove_recipeinc(self, recipe, data):
176 """Remove data from meta-sefltest/recipes-test/<recipe>/test_recipe.inc"""
184 inc_file = os.path.join(self.testlayer_path, 'recipes-test', recipe, 'test_recipe.inc') 177 inc_file = os.path.join(self.testlayer_path, 'recipes-test', recipe, 'test_recipe.inc')
185 self.log.debug("Removing from: %s\n%s\n" % (inc_file, data)) 178 self.logger.debug("Removing from: %s\n%s\n" % (inc_file, data))
186 ftools.remove_from_file(inc_file, data) 179 ftools.remove_from_file(inc_file, data)
187 180
188 # delete meta-sefltest/recipes-test/<recipe>/test_recipe.inc file
189 def delete_recipeinc(self, recipe): 181 def delete_recipeinc(self, recipe):
182 """Delete meta-sefltest/recipes-test/<recipe>/test_recipe.inc file"""
190 inc_file = os.path.join(self.testlayer_path, 'recipes-test', recipe, 'test_recipe.inc') 183 inc_file = os.path.join(self.testlayer_path, 'recipes-test', recipe, 'test_recipe.inc')
191 self.log.debug("Deleting file: %s" % inc_file) 184 self.logger.debug("Deleting file: %s" % inc_file)
192 try: 185 try:
193 os.remove(inc_file) 186 os.remove(inc_file)
194 except OSError as e: 187 except OSError as e:
195 if e.errno != errno.ENOENT: 188 if e.errno != errno.ENOENT:
196 raise 189 raise
197
198 # write to <builddir>/conf/bblayers.inc
199 def write_bblayers_config(self, data): 190 def write_bblayers_config(self, data):
200 self.log.debug("Writing to: %s\n%s\n" % (self.testinc_bblayers_path, data)) 191 """Write to <builddir>/conf/bblayers.inc"""
192 self.logger.debug("Writing to: %s\n%s\n" % (self.testinc_bblayers_path, data))
201 ftools.write_file(self.testinc_bblayers_path, data) 193 ftools.write_file(self.testinc_bblayers_path, data)
202 194
203 # append to <builddir>/conf/bblayers.inc
204 def append_bblayers_config(self, data): 195 def append_bblayers_config(self, data):
205 self.log.debug("Appending to: %s\n%s\n" % (self.testinc_bblayers_path, data)) 196 """Append to <builddir>/conf/bblayers.inc"""
197 self.logger.debug("Appending to: %s\n%s\n" % (self.testinc_bblayers_path, data))
206 ftools.append_file(self.testinc_bblayers_path, data) 198 ftools.append_file(self.testinc_bblayers_path, data)
207 199
208 # remove data from <builddir>/conf/bblayers.inc
209 def remove_bblayers_config(self, data): 200 def remove_bblayers_config(self, data):
210 self.log.debug("Removing from: %s\n%s\n" % (self.testinc_bblayers_path, data)) 201 """Remove data from <builddir>/conf/bblayers.inc"""
202 self.logger.debug("Removing from: %s\n%s\n" % (self.testinc_bblayers_path, data))
211 ftools.remove_from_file(self.testinc_bblayers_path, data) 203 ftools.remove_from_file(self.testinc_bblayers_path, data)
212 204
213 # write to <builddir>/conf/machine.inc
214 def set_machine_config(self, data): 205 def set_machine_config(self, data):
215 self.log.debug("Writing to: %s\n%s\n" % (self.machineinc_path, data)) 206 """Write to <builddir>/conf/machine.inc"""
207 self.logger.debug("Writing to: %s\n%s\n" % (self.machineinc_path, data))
216 ftools.write_file(self.machineinc_path, data) 208 ftools.write_file(self.machineinc_path, data)
217 209
218 # check does path exist 210 # check does path exist
@@ -227,7 +219,6 @@ be re-executed from a clean environment to ensure accurate results.")
227 msg = self._formatMessage(msg, "%s exists when it should not" % safe_repr(expr)) 219 msg = self._formatMessage(msg, "%s exists when it should not" % safe_repr(expr))
228 raise self.failureException(msg) 220 raise self.failureException(msg)
229 221
230
231def get_available_machines(): 222def get_available_machines():
232 # Get a list of all available machines 223 # Get a list of all available machines
233 bbpath = get_bb_var('BBPATH').split(':') 224 bbpath = get_bb_var('BBPATH').split(':')
@@ -237,7 +228,7 @@ def get_available_machines():
237 found_machines = glob.glob(os.path.join(path, 'conf', 'machine', '*.conf')) 228 found_machines = glob.glob(os.path.join(path, 'conf', 'machine', '*.conf'))
238 if found_machines: 229 if found_machines:
239 for i in found_machines: 230 for i in found_machines:
240 # eg: '/home/<user>/poky/meta-intel/conf/machine/intel-core2-32.conf' 231 # eg: '/home/<user>/poky/meta-intel/conf/machine/intel-core2-32.conf'
241 machines.append(os.path.splitext(os.path.basename(i))[0]) 232 machines.append(os.path.splitext(os.path.basename(i))[0])
242 233
243 return machines 234 return machines