diff options
| author | Aníbal Limón <anibal.limon@linux.intel.com> | 2017-05-25 12:22:22 -0500 |
|---|---|---|
| committer | Richard Purdie <richard.purdie@linuxfoundation.org> | 2017-06-06 19:02:43 +0100 |
| commit | cb1b1eeb3f41bbb183694399b24c327b111a1421 (patch) | |
| tree | 54c3815cb0a83a6d7ff2339b34ee9727c4ea5c44 /meta/lib/oeqa/selftest/case.py | |
| parent | 7c8f3c398084573d01d70989bd0d35a94058a420 (diff) | |
| download | poky-cb1b1eeb3f41bbb183694399b24c327b111a1421.tar.gz | |
oeqa/selftest: Move base class to case module
To match the new structure of the OEQA framework.
(From OE-Core rev: 0d3d97414c8166d09065f6f7f45e3260ce405692)
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>
Diffstat (limited to 'meta/lib/oeqa/selftest/case.py')
| -rw-r--r-- | meta/lib/oeqa/selftest/case.py | 248 |
1 files changed, 248 insertions, 0 deletions
diff --git a/meta/lib/oeqa/selftest/case.py b/meta/lib/oeqa/selftest/case.py new file mode 100644 index 0000000000..43a1951be3 --- /dev/null +++ b/meta/lib/oeqa/selftest/case.py | |||
| @@ -0,0 +1,248 @@ | |||
| 1 | # Copyright (c) 2013 Intel Corporation | ||
| 2 | # | ||
| 3 | # Released under the MIT license (see COPYING.MIT) | ||
| 4 | |||
| 5 | |||
| 6 | # DESCRIPTION | ||
| 7 | # Base class inherited by test classes in meta/lib/oeqa/selftest | ||
| 8 | |||
| 9 | import unittest | ||
| 10 | import os | ||
| 11 | import sys | ||
| 12 | import shutil | ||
| 13 | import logging | ||
| 14 | import errno | ||
| 15 | |||
| 16 | import oeqa.utils.ftools as ftools | ||
| 17 | from oeqa.utils.commands import runCmd, bitbake, get_bb_var, get_test_layer | ||
| 18 | from oeqa.utils.decorators import LogResults | ||
| 19 | from random import choice | ||
| 20 | import glob | ||
| 21 | from unittest.util import safe_repr | ||
| 22 | |||
| 23 | @LogResults | ||
| 24 | class oeSelfTest(unittest.TestCase): | ||
| 25 | |||
| 26 | log = logging.getLogger("selftest.base") | ||
| 27 | longMessage = True | ||
| 28 | |||
| 29 | def __init__(self, methodName="runTest"): | ||
| 30 | self.builddir = os.environ.get("BUILDDIR") | ||
| 31 | self.localconf_path = os.path.join(self.builddir, "conf/local.conf") | ||
| 32 | self.localconf_backup = os.path.join(self.builddir, "conf/local.bk") | ||
| 33 | self.testinc_path = os.path.join(self.builddir, "conf/selftest.inc") | ||
| 34 | self.local_bblayers_path = os.path.join(self.builddir, "conf/bblayers.conf") | ||
| 35 | self.local_bblayers_backup = os.path.join(self.builddir, | ||
| 36 | "conf/bblayers.bk") | ||
| 37 | self.testinc_bblayers_path = os.path.join(self.builddir, "conf/bblayers.inc") | ||
| 38 | self.machineinc_path = os.path.join(self.builddir, "conf/machine.inc") | ||
| 39 | self.testlayer_path = oeSelfTest.testlayer_path | ||
| 40 | self._extra_tear_down_commands = [] | ||
| 41 | self._track_for_cleanup = [ | ||
| 42 | self.testinc_path, self.testinc_bblayers_path, | ||
| 43 | self.machineinc_path, self.localconf_backup, | ||
| 44 | self.local_bblayers_backup] | ||
| 45 | super(oeSelfTest, self).__init__(methodName) | ||
| 46 | |||
| 47 | def setUp(self): | ||
| 48 | os.chdir(self.builddir) | ||
| 49 | # Check if local.conf or bblayers.conf files backup exists | ||
| 50 | # from a previous failed test and restore them | ||
| 51 | if os.path.isfile(self.localconf_backup) or os.path.isfile( | ||
| 52 | self.local_bblayers_backup): | ||
| 53 | self.log.debug("Found a local.conf and/or bblayers.conf backup \ | ||
| 54 | from a previously aborted test. Restoring these files now, but tests should \ | ||
| 55 | be re-executed from a clean environment to ensure accurate results.") | ||
| 56 | try: | ||
| 57 | shutil.copyfile(self.localconf_backup, self.localconf_path) | ||
| 58 | except OSError as e: | ||
| 59 | if e.errno != errno.ENOENT: | ||
| 60 | raise | ||
| 61 | try: | ||
| 62 | shutil.copyfile(self.local_bblayers_backup, | ||
| 63 | self.local_bblayers_path) | ||
| 64 | except OSError as e: | ||
| 65 | if e.errno != errno.ENOENT: | ||
| 66 | raise | ||
| 67 | else: | ||
| 68 | # backup local.conf and bblayers.conf | ||
| 69 | shutil.copyfile(self.localconf_path, self.localconf_backup) | ||
| 70 | shutil.copyfile(self.local_bblayers_path, | ||
| 71 | self.local_bblayers_backup) | ||
| 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 | ||
| 74 | # if it failed so we need a fresh start | ||
| 75 | try: | ||
| 76 | os.remove(self.testinc_path) | ||
| 77 | except OSError as e: | ||
| 78 | if e.errno != errno.ENOENT: | ||
| 79 | raise | ||
| 80 | for root, _, files in os.walk(self.testlayer_path): | ||
| 81 | for f in files: | ||
| 82 | if f == 'test_recipe.inc': | ||
| 83 | os.remove(os.path.join(root, f)) | ||
| 84 | |||
| 85 | for incl_file in [self.testinc_bblayers_path, self.machineinc_path]: | ||
| 86 | try: | ||
| 87 | os.remove(incl_file) | ||
| 88 | except OSError as e: | ||
| 89 | if e.errno != errno.ENOENT: | ||
| 90 | raise | ||
| 91 | |||
| 92 | # Get CUSTOMMACHINE from env (set by --machine argument to oe-selftest) | ||
| 93 | custommachine = os.getenv('CUSTOMMACHINE') | ||
| 94 | if custommachine: | ||
| 95 | if custommachine == 'random': | ||
| 96 | machine = get_random_machine() | ||
| 97 | else: | ||
| 98 | machine = custommachine | ||
| 99 | machine_conf = 'MACHINE ??= "%s"\n' % machine | ||
| 100 | self.set_machine_config(machine_conf) | ||
| 101 | print('MACHINE: %s' % machine) | ||
| 102 | |||
| 103 | # tests might need their own setup | ||
| 104 | # but if they overwrite this one they have to call | ||
| 105 | # super each time, so let's give them an alternative | ||
| 106 | self.setUpLocal() | ||
| 107 | |||
| 108 | def setUpLocal(self): | ||
| 109 | pass | ||
| 110 | |||
| 111 | def tearDown(self): | ||
| 112 | if self._extra_tear_down_commands: | ||
| 113 | failed_extra_commands = [] | ||
| 114 | for command in self._extra_tear_down_commands: | ||
| 115 | result = runCmd(command, ignore_status=True) | ||
| 116 | if not result.status == 0: | ||
| 117 | failed_extra_commands.append(command) | ||
| 118 | if failed_extra_commands: | ||
| 119 | self.log.warning("tearDown commands have failed: %s" % ', '.join(map(str, failed_extra_commands))) | ||
| 120 | self.log.debug("Trying to move on.") | ||
| 121 | self._extra_tear_down_commands = [] | ||
| 122 | |||
| 123 | if self._track_for_cleanup: | ||
| 124 | for path in self._track_for_cleanup: | ||
| 125 | if os.path.isdir(path): | ||
| 126 | shutil.rmtree(path) | ||
| 127 | if os.path.isfile(path): | ||
| 128 | os.remove(path) | ||
| 129 | self._track_for_cleanup = [] | ||
| 130 | |||
| 131 | self.tearDownLocal() | ||
| 132 | |||
| 133 | def tearDownLocal(self): | ||
| 134 | pass | ||
| 135 | |||
| 136 | # add test specific commands to the tearDown method. | ||
| 137 | def add_command_to_tearDown(self, command): | ||
| 138 | self.log.debug("Adding command '%s' to tearDown for this test." % command) | ||
| 139 | self._extra_tear_down_commands.append(command) | ||
| 140 | # add test specific files or directories to be removed in the tearDown method | ||
| 141 | def track_for_cleanup(self, path): | ||
| 142 | self.log.debug("Adding path '%s' to be cleaned up when test is over" % path) | ||
| 143 | self._track_for_cleanup.append(path) | ||
| 144 | |||
| 145 | # write to <builddir>/conf/selftest.inc | ||
| 146 | def write_config(self, data): | ||
| 147 | self.log.debug("Writing to: %s\n%s\n" % (self.testinc_path, data)) | ||
| 148 | ftools.write_file(self.testinc_path, data) | ||
| 149 | |||
| 150 | custommachine = os.getenv('CUSTOMMACHINE') | ||
| 151 | if custommachine and 'MACHINE' in data: | ||
| 152 | machine = get_bb_var('MACHINE') | ||
| 153 | self.log.warning('MACHINE overridden: %s' % machine) | ||
| 154 | |||
| 155 | # append to <builddir>/conf/selftest.inc | ||
| 156 | def append_config(self, data): | ||
| 157 | self.log.debug("Appending to: %s\n%s\n" % (self.testinc_path, data)) | ||
| 158 | ftools.append_file(self.testinc_path, data) | ||
| 159 | |||
| 160 | custommachine = os.getenv('CUSTOMMACHINE') | ||
| 161 | if custommachine and 'MACHINE' in data: | ||
| 162 | machine = get_bb_var('MACHINE') | ||
| 163 | self.log.warning('MACHINE overridden: %s' % machine) | ||
| 164 | |||
| 165 | # remove data from <builddir>/conf/selftest.inc | ||
| 166 | def remove_config(self, data): | ||
| 167 | self.log.debug("Removing from: %s\n%s\n" % (self.testinc_path, data)) | ||
| 168 | ftools.remove_from_file(self.testinc_path, data) | ||
| 169 | |||
| 170 | # write to meta-sefltest/recipes-test/<recipe>/test_recipe.inc | ||
| 171 | def write_recipeinc(self, recipe, data): | ||
| 172 | 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)) | ||
| 174 | ftools.write_file(inc_file, data) | ||
| 175 | |||
| 176 | # append data to meta-sefltest/recipes-test/<recipe>/test_recipe.inc | ||
| 177 | def append_recipeinc(self, recipe, data): | ||
| 178 | 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)) | ||
| 180 | ftools.append_file(inc_file, data) | ||
| 181 | |||
| 182 | # remove data from meta-sefltest/recipes-test/<recipe>/test_recipe.inc | ||
| 183 | def remove_recipeinc(self, recipe, data): | ||
| 184 | 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)) | ||
| 186 | ftools.remove_from_file(inc_file, data) | ||
| 187 | |||
| 188 | # delete meta-sefltest/recipes-test/<recipe>/test_recipe.inc file | ||
| 189 | def delete_recipeinc(self, recipe): | ||
| 190 | inc_file = os.path.join(self.testlayer_path, 'recipes-test', recipe, 'test_recipe.inc') | ||
| 191 | self.log.debug("Deleting file: %s" % inc_file) | ||
| 192 | try: | ||
| 193 | os.remove(inc_file) | ||
| 194 | except OSError as e: | ||
| 195 | if e.errno != errno.ENOENT: | ||
| 196 | raise | ||
| 197 | |||
| 198 | # write to <builddir>/conf/bblayers.inc | ||
| 199 | def write_bblayers_config(self, data): | ||
| 200 | self.log.debug("Writing to: %s\n%s\n" % (self.testinc_bblayers_path, data)) | ||
| 201 | ftools.write_file(self.testinc_bblayers_path, data) | ||
| 202 | |||
| 203 | # append to <builddir>/conf/bblayers.inc | ||
| 204 | def append_bblayers_config(self, data): | ||
| 205 | self.log.debug("Appending to: %s\n%s\n" % (self.testinc_bblayers_path, data)) | ||
| 206 | ftools.append_file(self.testinc_bblayers_path, data) | ||
| 207 | |||
| 208 | # remove data from <builddir>/conf/bblayers.inc | ||
| 209 | def remove_bblayers_config(self, data): | ||
| 210 | self.log.debug("Removing from: %s\n%s\n" % (self.testinc_bblayers_path, data)) | ||
| 211 | ftools.remove_from_file(self.testinc_bblayers_path, data) | ||
| 212 | |||
| 213 | # write to <builddir>/conf/machine.inc | ||
| 214 | def set_machine_config(self, data): | ||
| 215 | self.log.debug("Writing to: %s\n%s\n" % (self.machineinc_path, data)) | ||
| 216 | ftools.write_file(self.machineinc_path, data) | ||
| 217 | |||
| 218 | # check does path exist | ||
| 219 | def assertExists(self, expr, msg=None): | ||
| 220 | if not os.path.exists(expr): | ||
| 221 | msg = self._formatMessage(msg, "%s does not exist" % safe_repr(expr)) | ||
| 222 | raise self.failureException(msg) | ||
| 223 | |||
| 224 | # check does path not exist | ||
| 225 | def assertNotExists(self, expr, msg=None): | ||
| 226 | if os.path.exists(expr): | ||
| 227 | msg = self._formatMessage(msg, "%s exists when it should not" % safe_repr(expr)) | ||
| 228 | raise self.failureException(msg) | ||
| 229 | |||
| 230 | |||
| 231 | def get_available_machines(): | ||
| 232 | # Get a list of all available machines | ||
| 233 | bbpath = get_bb_var('BBPATH').split(':') | ||
| 234 | machines = [] | ||
| 235 | |||
| 236 | for path in bbpath: | ||
| 237 | found_machines = glob.glob(os.path.join(path, 'conf', 'machine', '*.conf')) | ||
| 238 | if found_machines: | ||
| 239 | for i in found_machines: | ||
| 240 | # eg: '/home/<user>/poky/meta-intel/conf/machine/intel-core2-32.conf' | ||
| 241 | machines.append(os.path.splitext(os.path.basename(i))[0]) | ||
| 242 | |||
| 243 | return machines | ||
| 244 | |||
| 245 | |||
| 246 | def get_random_machine(): | ||
| 247 | # Get a random machine | ||
| 248 | return choice(get_available_machines()) | ||
