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 | # Main unittest module used by testimage.bbclass |
| 6 | # This provides the oeRuntimeTest base class which is inherited by all tests in meta/lib/oeqa/runtime. |
| 7 | |
| 8 | # It also has some helper functions and it's responsible for actually starting the tests |
| 9 | |
| 10 | import os, re, mmap |
| 11 | import unittest |
| 12 | import inspect |
| 13 | import subprocess |
| 14 | import bb |
| 15 | from oeqa.utils.decorators import LogResults, gettag |
| 16 | from sys import exc_info, exc_clear |
| 17 | |
| 18 | def getVar(obj): |
| 19 | #extend form dict, if a variable didn't exists, need find it in testcase |
| 20 | class VarDict(dict): |
| 21 | def __getitem__(self, key): |
| 22 | return gettag(obj, key) |
| 23 | return VarDict() |
| 24 | |
| 25 | def checkTags(tc, tagexp): |
| 26 | return eval(tagexp, None, getVar(tc)) |
| 27 | |
| 28 | |
| 29 | def filterByTagExp(testsuite, tagexp): |
| 30 | if not tagexp: |
| 31 | return testsuite |
| 32 | caseList = [] |
| 33 | for each in testsuite: |
| 34 | if not isinstance(each, unittest.BaseTestSuite): |
| 35 | if checkTags(each, tagexp): |
| 36 | caseList.append(each) |
| 37 | else: |
| 38 | caseList.append(filterByTagExp(each, tagexp)) |
| 39 | return testsuite.__class__(caseList) |
| 40 | |
| 41 | def loadTests(tc, type="runtime"): |
| 42 | if type == "runtime": |
| 43 | # set the context object passed from the test class |
| 44 | setattr(oeTest, "tc", tc) |
| 45 | # set ps command to use |
| 46 | setattr(oeRuntimeTest, "pscmd", "ps -ef" if oeTest.hasPackage("procps") else "ps") |
| 47 | # prepare test suite, loader and runner |
| 48 | suite = unittest.TestSuite() |
| 49 | elif type == "sdk": |
| 50 | # set the context object passed from the test class |
| 51 | setattr(oeTest, "tc", tc) |
| 52 | testloader = unittest.TestLoader() |
| 53 | testloader.sortTestMethodsUsing = None |
| 54 | suites = [testloader.loadTestsFromName(name) for name in tc.testslist] |
| 55 | suites = filterByTagExp(suites, getattr(tc, "tagexp", None)) |
| 56 | |
| 57 | def getTests(test): |
| 58 | '''Return all individual tests executed when running the suite.''' |
| 59 | # Unfortunately unittest does not have an API for this, so we have |
| 60 | # to rely on implementation details. This only needs to work |
| 61 | # for TestSuite containing TestCase. |
| 62 | method = getattr(test, '_testMethodName', None) |
| 63 | if method: |
| 64 | # leaf case: a TestCase |
| 65 | yield test |
| 66 | else: |
| 67 | # Look into TestSuite. |
| 68 | tests = getattr(test, '_tests', []) |
| 69 | for t1 in tests: |
| 70 | for t2 in getTests(t1): |
| 71 | yield t2 |
| 72 | |
| 73 | # Determine dependencies between suites by looking for @skipUnlessPassed |
| 74 | # method annotations. Suite A depends on suite B if any method in A |
| 75 | # depends on a method on B. |
| 76 | for suite in suites: |
| 77 | suite.dependencies = [] |
| 78 | suite.depth = 0 |
| 79 | for test in getTests(suite): |
| 80 | methodname = getattr(test, '_testMethodName', None) |
| 81 | if methodname: |
| 82 | method = getattr(test, methodname) |
| 83 | depends_on = getattr(method, '_depends_on', None) |
| 84 | if depends_on: |
| 85 | for dep_suite in suites: |
| 86 | if depends_on in [getattr(t, '_testMethodName', None) for t in getTests(dep_suite)]: |
| 87 | if dep_suite not in suite.dependencies and \ |
| 88 | dep_suite is not suite: |
| 89 | suite.dependencies.append(dep_suite) |
| 90 | break |
| 91 | else: |
| 92 | bb.warn("Test %s was declared as @skipUnlessPassed('%s') but that test is either not defined or not active. Will run the test anyway." % |
| 93 | (test, depends_on)) |
| 94 | # Use brute-force topological sort to determine ordering. Sort by |
| 95 | # depth (higher depth = must run later), with original ordering to |
| 96 | # break ties. |
| 97 | def set_suite_depth(suite): |
| 98 | for dep in suite.dependencies: |
| 99 | new_depth = set_suite_depth(dep) + 1 |
| 100 | if new_depth > suite.depth: |
| 101 | suite.depth = new_depth |
| 102 | return suite.depth |
| 103 | for index, suite in enumerate(suites): |
| 104 | set_suite_depth(suite) |
| 105 | suite.index = index |
| 106 | suites.sort(cmp=lambda a,b: cmp((a.depth, a.index), (b.depth, b.index))) |
| 107 | return testloader.suiteClass(suites) |
| 108 | |
| 109 | def runTests(tc, type="runtime"): |
| 110 | |
| 111 | suite = loadTests(tc, type) |
| 112 | bb.note("Test modules %s" % tc.testslist) |
| 113 | if hasattr(tc, "tagexp") and tc.tagexp: |
| 114 | bb.note("Filter test cases by tags: %s" % tc.tagexp) |
| 115 | bb.note("Found %s tests" % suite.countTestCases()) |
| 116 | runner = unittest.TextTestRunner(verbosity=2) |
| 117 | result = runner.run(suite) |
| 118 | |
| 119 | return result |
| 120 | |
| 121 | @LogResults |
| 122 | class oeTest(unittest.TestCase): |
| 123 | |
| 124 | longMessage = True |
| 125 | |
| 126 | @classmethod |
| 127 | def hasPackage(self, pkg): |
| 128 | for item in oeTest.tc.pkgmanifest.split('\n'): |
| 129 | if re.match(pkg, item): |
| 130 | return True |
| 131 | return False |
| 132 | |
| 133 | @classmethod |
| 134 | def hasFeature(self,feature): |
| 135 | |
| 136 | if feature in oeTest.tc.imagefeatures or \ |
| 137 | feature in oeTest.tc.distrofeatures: |
| 138 | return True |
| 139 | else: |
| 140 | return False |
| 141 | |
| 142 | class oeRuntimeTest(oeTest): |
| 143 | def __init__(self, methodName='runTest'): |
| 144 | self.target = oeRuntimeTest.tc.target |
| 145 | super(oeRuntimeTest, self).__init__(methodName) |
| 146 | |
| 147 | def setUp(self): |
| 148 | # Check if test needs to run |
| 149 | if self.tc.sigterm: |
| 150 | self.fail("Got SIGTERM") |
| 151 | elif (type(self.target).__name__ == "QemuTarget"): |
| 152 | self.assertTrue(self.target.check(), msg = "Qemu not running?") |
| 153 | |
Patrick Williams | d7e9631 | 2015-09-22 08:09:05 -0500 | [diff] [blame] | 154 | self.setUpLocal() |
| 155 | |
| 156 | # a setup method before tests but after the class instantiation |
| 157 | def setUpLocal(self): |
| 158 | pass |
| 159 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 160 | def tearDown(self): |
| 161 | # If a test fails or there is an exception |
| 162 | if not exc_info() == (None, None, None): |
| 163 | exc_clear() |
| 164 | #Only dump for QemuTarget |
| 165 | if (type(self.target).__name__ == "QemuTarget"): |
| 166 | self.tc.host_dumper.create_dir(self._testMethodName) |
| 167 | self.tc.host_dumper.dump_host() |
| 168 | self.target.target_dumper.dump_target( |
| 169 | self.tc.host_dumper.dump_dir) |
| 170 | print ("%s dump data stored in %s" % (self._testMethodName, |
| 171 | self.tc.host_dumper.dump_dir)) |
| 172 | |
| 173 | #TODO: use package_manager.py to install packages on any type of image |
| 174 | def install_packages(self, packagelist): |
| 175 | for package in packagelist: |
| 176 | (status, result) = self.target.run("smart install -y "+package) |
| 177 | if status != 0: |
| 178 | return status |
| 179 | |
| 180 | class oeSDKTest(oeTest): |
| 181 | def __init__(self, methodName='runTest'): |
| 182 | self.sdktestdir = oeSDKTest.tc.sdktestdir |
| 183 | super(oeSDKTest, self).__init__(methodName) |
| 184 | |
| 185 | @classmethod |
| 186 | def hasHostPackage(self, pkg): |
| 187 | |
| 188 | if re.search(pkg, oeTest.tc.hostpkgmanifest): |
| 189 | return True |
| 190 | return False |
| 191 | |
| 192 | def _run(self, cmd): |
| 193 | return subprocess.check_output(cmd, shell=True) |
| 194 | |
| 195 | def getmodule(pos=2): |
| 196 | # stack returns a list of tuples containg frame information |
| 197 | # First element of the list the is current frame, caller is 1 |
| 198 | frameinfo = inspect.stack()[pos] |
| 199 | modname = inspect.getmodulename(frameinfo[1]) |
| 200 | #modname = inspect.getmodule(frameinfo[0]).__name__ |
| 201 | return modname |
| 202 | |
| 203 | def skipModule(reason, pos=2): |
| 204 | modname = getmodule(pos) |
| 205 | if modname not in oeTest.tc.testsrequired: |
| 206 | raise unittest.SkipTest("%s: %s" % (modname, reason)) |
| 207 | else: |
| 208 | raise Exception("\nTest %s wants to be skipped.\nReason is: %s" \ |
| 209 | "\nTest was required in TEST_SUITES, so either the condition for skipping is wrong" \ |
| 210 | "\nor the image really doesn't have the required feature/package when it should." % (modname, reason)) |
| 211 | |
| 212 | def skipModuleIf(cond, reason): |
| 213 | |
| 214 | if cond: |
| 215 | skipModule(reason, 3) |
| 216 | |
| 217 | def skipModuleUnless(cond, reason): |
| 218 | |
| 219 | if not cond: |
| 220 | skipModule(reason, 3) |