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 |
| 7 | # Base class inherited by test classes in meta/lib/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 | |
| 20 | @LogResults |
| 21 | class oeSelfTest(unittest.TestCase): |
| 22 | |
| 23 | log = logging.getLogger("selftest.base") |
| 24 | longMessage = True |
| 25 | |
| 26 | def __init__(self, methodName="runTest"): |
| 27 | self.builddir = os.environ.get("BUILDDIR") |
| 28 | self.localconf_path = os.path.join(self.builddir, "conf/local.conf") |
| 29 | self.testinc_path = os.path.join(self.builddir, "conf/selftest.inc") |
| 30 | self.local_bblayers_path = os.path.join(self.builddir, "conf/bblayers.conf") |
| 31 | self.testinc_bblayers_path = os.path.join(self.builddir, "conf/bblayers.inc") |
| 32 | self.testlayer_path = oeSelfTest.testlayer_path |
| 33 | self._extra_tear_down_commands = [] |
| 34 | self._track_for_cleanup = [] |
| 35 | super(oeSelfTest, self).__init__(methodName) |
| 36 | |
| 37 | def setUp(self): |
| 38 | os.chdir(self.builddir) |
| 39 | # we don't know what the previous test left around in config or inc files |
| 40 | # if it failed so we need a fresh start |
| 41 | try: |
| 42 | os.remove(self.testinc_path) |
| 43 | except OSError as e: |
| 44 | if e.errno != errno.ENOENT: |
| 45 | raise |
| 46 | for root, _, files in os.walk(self.testlayer_path): |
| 47 | for f in files: |
| 48 | if f == 'test_recipe.inc': |
| 49 | os.remove(os.path.join(root, f)) |
| 50 | try: |
| 51 | os.remove(self.testinc_bblayers_path) |
| 52 | except OSError as e: |
| 53 | if e.errno != errno.ENOENT: |
| 54 | raise |
| 55 | # tests might need their own setup |
| 56 | # but if they overwrite this one they have to call |
| 57 | # super each time, so let's give them an alternative |
| 58 | self.setUpLocal() |
| 59 | |
| 60 | def setUpLocal(self): |
| 61 | pass |
| 62 | |
| 63 | def tearDown(self): |
| 64 | if self._extra_tear_down_commands: |
| 65 | failed_extra_commands = [] |
| 66 | for command in self._extra_tear_down_commands: |
| 67 | result = runCmd(command, ignore_status=True) |
| 68 | if not result.status == 0: |
| 69 | failed_extra_commands.append(command) |
| 70 | if failed_extra_commands: |
| 71 | self.log.warning("tearDown commands have failed: %s" % ', '.join(map(str, failed_extra_commands))) |
| 72 | self.log.debug("Trying to move on.") |
| 73 | self._extra_tear_down_commands = [] |
| 74 | |
| 75 | if self._track_for_cleanup: |
| 76 | for path in self._track_for_cleanup: |
| 77 | if os.path.isdir(path): |
| 78 | shutil.rmtree(path) |
| 79 | if os.path.isfile(path): |
| 80 | os.remove(path) |
| 81 | self._track_for_cleanup = [] |
| 82 | |
| 83 | self.tearDownLocal() |
| 84 | |
| 85 | def tearDownLocal(self): |
| 86 | pass |
| 87 | |
| 88 | # add test specific commands to the tearDown method. |
| 89 | def add_command_to_tearDown(self, command): |
| 90 | self.log.debug("Adding command '%s' to tearDown for this test." % command) |
| 91 | self._extra_tear_down_commands.append(command) |
| 92 | # add test specific files or directories to be removed in the tearDown method |
| 93 | def track_for_cleanup(self, path): |
| 94 | self.log.debug("Adding path '%s' to be cleaned up when test is over" % path) |
| 95 | self._track_for_cleanup.append(path) |
| 96 | |
| 97 | # write to <builddir>/conf/selftest.inc |
| 98 | def write_config(self, data): |
| 99 | self.log.debug("Writing to: %s\n%s\n" % (self.testinc_path, data)) |
| 100 | ftools.write_file(self.testinc_path, data) |
| 101 | |
| 102 | # append to <builddir>/conf/selftest.inc |
| 103 | def append_config(self, data): |
| 104 | self.log.debug("Appending to: %s\n%s\n" % (self.testinc_path, data)) |
| 105 | ftools.append_file(self.testinc_path, data) |
| 106 | |
| 107 | # remove data from <builddir>/conf/selftest.inc |
| 108 | def remove_config(self, data): |
| 109 | self.log.debug("Removing from: %s\n\%s\n" % (self.testinc_path, data)) |
| 110 | ftools.remove_from_file(self.testinc_path, data) |
| 111 | |
| 112 | # write to meta-sefltest/recipes-test/<recipe>/test_recipe.inc |
| 113 | def write_recipeinc(self, recipe, data): |
| 114 | inc_file = os.path.join(self.testlayer_path, 'recipes-test', recipe, 'test_recipe.inc') |
| 115 | self.log.debug("Writing to: %s\n%s\n" % (inc_file, data)) |
| 116 | ftools.write_file(inc_file, data) |
| 117 | |
| 118 | # append data to meta-sefltest/recipes-test/<recipe>/test_recipe.inc |
| 119 | def append_recipeinc(self, recipe, data): |
| 120 | inc_file = os.path.join(self.testlayer_path, 'recipes-test', recipe, 'test_recipe.inc') |
| 121 | self.log.debug("Appending to: %s\n%s\n" % (inc_file, data)) |
| 122 | ftools.append_file(inc_file, data) |
| 123 | |
| 124 | # remove data from meta-sefltest/recipes-test/<recipe>/test_recipe.inc |
| 125 | def remove_recipeinc(self, recipe, data): |
| 126 | inc_file = os.path.join(self.testlayer_path, 'recipes-test', recipe, 'test_recipe.inc') |
| 127 | self.log.debug("Removing from: %s\n%s\n" % (inc_file, data)) |
| 128 | ftools.remove_from_file(inc_file, data) |
| 129 | |
| 130 | # delete meta-sefltest/recipes-test/<recipe>/test_recipe.inc file |
| 131 | def delete_recipeinc(self, recipe): |
| 132 | inc_file = os.path.join(self.testlayer_path, 'recipes-test', recipe, 'test_recipe.inc') |
| 133 | self.log.debug("Deleting file: %s" % inc_file) |
| 134 | try: |
| 135 | os.remove(inc_file) |
| 136 | except OSError as e: |
| 137 | if e.errno != errno.ENOENT: |
| 138 | raise |
| 139 | |
| 140 | # write to <builddir>/conf/bblayers.inc |
| 141 | def write_bblayers_config(self, data): |
| 142 | self.log.debug("Writing to: %s\n%s\n" % (self.testinc_bblayers_path, data)) |
| 143 | ftools.write_file(self.testinc_bblayers_path, data) |
| 144 | |
| 145 | # append to <builddir>/conf/bblayers.inc |
| 146 | def append_bblayers_config(self, data): |
| 147 | self.log.debug("Appending to: %s\n%s\n" % (self.testinc_bblayers_path, data)) |
| 148 | ftools.append_file(self.testinc_bblayers_path, data) |
| 149 | |
| 150 | # remove data from <builddir>/conf/bblayers.inc |
| 151 | def remove_bblayers_config(self, data): |
| 152 | self.log.debug("Removing from: %s\n\%s\n" % (self.testinc_bblayers_path, data)) |
| 153 | ftools.remove_from_file(self.testinc_bblayers_path, data) |