Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1 | # Copyright (c) 2013 Intel Corporation |
| 2 | # |
| 3 | # Released under the MIT license (see COPYING.MIT) |
| 4 | |
| 5 | |
| 6 | # DESCRIPTION |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 7 | # Base class inherited by test classes in meta/lib/oeqa/selftest |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 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 |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 19 | from random import choice |
| 20 | import glob |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 21 | |
| 22 | @LogResults |
| 23 | class oeSelfTest(unittest.TestCase): |
| 24 | |
| 25 | log = logging.getLogger("selftest.base") |
| 26 | longMessage = True |
| 27 | |
| 28 | def __init__(self, methodName="runTest"): |
| 29 | self.builddir = os.environ.get("BUILDDIR") |
| 30 | self.localconf_path = os.path.join(self.builddir, "conf/local.conf") |
| 31 | self.testinc_path = os.path.join(self.builddir, "conf/selftest.inc") |
| 32 | self.local_bblayers_path = os.path.join(self.builddir, "conf/bblayers.conf") |
| 33 | self.testinc_bblayers_path = os.path.join(self.builddir, "conf/bblayers.inc") |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 34 | self.machineinc_path = os.path.join(self.builddir, "conf/machine.inc") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 35 | self.testlayer_path = oeSelfTest.testlayer_path |
| 36 | self._extra_tear_down_commands = [] |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 37 | self._track_for_cleanup = [self.testinc_path, self.testinc_bblayers_path, self.machineinc_path] |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 38 | super(oeSelfTest, self).__init__(methodName) |
| 39 | |
| 40 | def setUp(self): |
| 41 | os.chdir(self.builddir) |
| 42 | # we don't know what the previous test left around in config or inc files |
| 43 | # if it failed so we need a fresh start |
| 44 | try: |
| 45 | os.remove(self.testinc_path) |
| 46 | except OSError as e: |
| 47 | if e.errno != errno.ENOENT: |
| 48 | raise |
| 49 | for root, _, files in os.walk(self.testlayer_path): |
| 50 | for f in files: |
| 51 | if f == 'test_recipe.inc': |
| 52 | os.remove(os.path.join(root, f)) |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 53 | |
| 54 | for incl_file in [self.testinc_bblayers_path, self.machineinc_path]: |
| 55 | try: |
| 56 | os.remove(incl_file) |
| 57 | except OSError as e: |
| 58 | if e.errno != errno.ENOENT: |
| 59 | raise |
| 60 | |
| 61 | # Get CUSTOMMACHINE from env (set by --machine argument to oe-selftest) |
| 62 | custommachine = os.getenv('CUSTOMMACHINE') |
| 63 | if custommachine: |
| 64 | if custommachine == 'random': |
| 65 | machine = get_random_machine() |
| 66 | else: |
| 67 | machine = custommachine |
| 68 | machine_conf = 'MACHINE ??= "%s"\n' % machine |
| 69 | self.set_machine_config(machine_conf) |
| 70 | print 'MACHINE: %s' % machine |
| 71 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 72 | # tests might need their own setup |
| 73 | # but if they overwrite this one they have to call |
| 74 | # super each time, so let's give them an alternative |
| 75 | self.setUpLocal() |
| 76 | |
| 77 | def setUpLocal(self): |
| 78 | pass |
| 79 | |
| 80 | def tearDown(self): |
| 81 | if self._extra_tear_down_commands: |
| 82 | failed_extra_commands = [] |
| 83 | for command in self._extra_tear_down_commands: |
| 84 | result = runCmd(command, ignore_status=True) |
| 85 | if not result.status == 0: |
| 86 | failed_extra_commands.append(command) |
| 87 | if failed_extra_commands: |
| 88 | self.log.warning("tearDown commands have failed: %s" % ', '.join(map(str, failed_extra_commands))) |
| 89 | self.log.debug("Trying to move on.") |
| 90 | self._extra_tear_down_commands = [] |
| 91 | |
| 92 | if self._track_for_cleanup: |
| 93 | for path in self._track_for_cleanup: |
| 94 | if os.path.isdir(path): |
| 95 | shutil.rmtree(path) |
| 96 | if os.path.isfile(path): |
| 97 | os.remove(path) |
| 98 | self._track_for_cleanup = [] |
| 99 | |
| 100 | self.tearDownLocal() |
| 101 | |
| 102 | def tearDownLocal(self): |
| 103 | pass |
| 104 | |
| 105 | # add test specific commands to the tearDown method. |
| 106 | def add_command_to_tearDown(self, command): |
| 107 | self.log.debug("Adding command '%s' to tearDown for this test." % command) |
| 108 | self._extra_tear_down_commands.append(command) |
| 109 | # add test specific files or directories to be removed in the tearDown method |
| 110 | def track_for_cleanup(self, path): |
| 111 | self.log.debug("Adding path '%s' to be cleaned up when test is over" % path) |
| 112 | self._track_for_cleanup.append(path) |
| 113 | |
| 114 | # write to <builddir>/conf/selftest.inc |
| 115 | def write_config(self, data): |
| 116 | self.log.debug("Writing to: %s\n%s\n" % (self.testinc_path, data)) |
| 117 | ftools.write_file(self.testinc_path, data) |
| 118 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 119 | custommachine = os.getenv('CUSTOMMACHINE') |
| 120 | if custommachine and 'MACHINE' in data: |
| 121 | machine = get_bb_var('MACHINE') |
| 122 | self.log.warning('MACHINE overridden: %s' % machine) |
| 123 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 124 | # append to <builddir>/conf/selftest.inc |
| 125 | def append_config(self, data): |
| 126 | self.log.debug("Appending to: %s\n%s\n" % (self.testinc_path, data)) |
| 127 | ftools.append_file(self.testinc_path, data) |
| 128 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 129 | custommachine = os.getenv('CUSTOMMACHINE') |
| 130 | if custommachine and 'MACHINE' in data: |
| 131 | machine = get_bb_var('MACHINE') |
| 132 | self.log.warning('MACHINE overridden: %s' % machine) |
| 133 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 134 | # remove data from <builddir>/conf/selftest.inc |
| 135 | def remove_config(self, data): |
| 136 | self.log.debug("Removing from: %s\n\%s\n" % (self.testinc_path, data)) |
| 137 | ftools.remove_from_file(self.testinc_path, data) |
| 138 | |
| 139 | # write to meta-sefltest/recipes-test/<recipe>/test_recipe.inc |
| 140 | def write_recipeinc(self, recipe, data): |
| 141 | inc_file = os.path.join(self.testlayer_path, 'recipes-test', recipe, 'test_recipe.inc') |
| 142 | self.log.debug("Writing to: %s\n%s\n" % (inc_file, data)) |
| 143 | ftools.write_file(inc_file, data) |
| 144 | |
| 145 | # append data to meta-sefltest/recipes-test/<recipe>/test_recipe.inc |
| 146 | def append_recipeinc(self, recipe, data): |
| 147 | inc_file = os.path.join(self.testlayer_path, 'recipes-test', recipe, 'test_recipe.inc') |
| 148 | self.log.debug("Appending to: %s\n%s\n" % (inc_file, data)) |
| 149 | ftools.append_file(inc_file, data) |
| 150 | |
| 151 | # remove data from meta-sefltest/recipes-test/<recipe>/test_recipe.inc |
| 152 | def remove_recipeinc(self, recipe, data): |
| 153 | inc_file = os.path.join(self.testlayer_path, 'recipes-test', recipe, 'test_recipe.inc') |
| 154 | self.log.debug("Removing from: %s\n%s\n" % (inc_file, data)) |
| 155 | ftools.remove_from_file(inc_file, data) |
| 156 | |
| 157 | # delete meta-sefltest/recipes-test/<recipe>/test_recipe.inc file |
| 158 | def delete_recipeinc(self, recipe): |
| 159 | inc_file = os.path.join(self.testlayer_path, 'recipes-test', recipe, 'test_recipe.inc') |
| 160 | self.log.debug("Deleting file: %s" % inc_file) |
| 161 | try: |
| 162 | os.remove(inc_file) |
| 163 | except OSError as e: |
| 164 | if e.errno != errno.ENOENT: |
| 165 | raise |
| 166 | |
| 167 | # write to <builddir>/conf/bblayers.inc |
| 168 | def write_bblayers_config(self, data): |
| 169 | self.log.debug("Writing to: %s\n%s\n" % (self.testinc_bblayers_path, data)) |
| 170 | ftools.write_file(self.testinc_bblayers_path, data) |
| 171 | |
| 172 | # append to <builddir>/conf/bblayers.inc |
| 173 | def append_bblayers_config(self, data): |
| 174 | self.log.debug("Appending to: %s\n%s\n" % (self.testinc_bblayers_path, data)) |
| 175 | ftools.append_file(self.testinc_bblayers_path, data) |
| 176 | |
| 177 | # remove data from <builddir>/conf/bblayers.inc |
| 178 | def remove_bblayers_config(self, data): |
| 179 | self.log.debug("Removing from: %s\n\%s\n" % (self.testinc_bblayers_path, data)) |
| 180 | ftools.remove_from_file(self.testinc_bblayers_path, data) |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 181 | |
| 182 | # write to <builddir>/conf/machine.inc |
| 183 | def set_machine_config(self, data): |
| 184 | self.log.debug("Writing to: %s\n%s\n" % (self.machineinc_path, data)) |
| 185 | ftools.write_file(self.machineinc_path, data) |
| 186 | |
| 187 | |
| 188 | def get_available_machines(): |
| 189 | # Get a list of all available machines |
| 190 | bbpath = get_bb_var('BBPATH').split(':') |
| 191 | machines = [] |
| 192 | |
| 193 | for path in bbpath: |
| 194 | found_machines = glob.glob(os.path.join(path, 'conf', 'machine', '*.conf')) |
| 195 | if found_machines: |
| 196 | for i in found_machines: |
| 197 | # eg: '/home/<user>/poky/meta-intel/conf/machine/intel-core2-32.conf' |
| 198 | machines.append(os.path.splitext(os.path.basename(i))[0]) |
| 199 | |
| 200 | return machines |
| 201 | |
| 202 | |
| 203 | def get_random_machine(): |
| 204 | # Get a random machine |
| 205 | return choice(get_available_machines()) |