diff options
Diffstat (limited to 'meta/lib/oeqa/oetest.py')
-rw-r--r-- | meta/lib/oeqa/oetest.py | 120 |
1 files changed, 120 insertions, 0 deletions
diff --git a/meta/lib/oeqa/oetest.py b/meta/lib/oeqa/oetest.py new file mode 100644 index 0000000000..529abdc19a --- /dev/null +++ b/meta/lib/oeqa/oetest.py | |||
@@ -0,0 +1,120 @@ | |||
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 bb | ||
14 | from oeqa.utils.sshcontrol import SSHControl | ||
15 | |||
16 | |||
17 | def runTests(tc): | ||
18 | |||
19 | # set the context object passed from the test class | ||
20 | setattr(oeRuntimeTest, "tc", tc) | ||
21 | # set ps command to use | ||
22 | setattr(oeRuntimeTest, "pscmd", "ps -ef" if oeRuntimeTest.hasPackage("procps") else "ps") | ||
23 | # prepare test suite, loader and runner | ||
24 | suite = unittest.TestSuite() | ||
25 | testloader = unittest.TestLoader() | ||
26 | testloader.sortTestMethodsUsing = None | ||
27 | runner = unittest.TextTestRunner(verbosity=2) | ||
28 | |||
29 | bb.note("Test modules %s" % tc.testslist) | ||
30 | suite = testloader.loadTestsFromNames(tc.testslist) | ||
31 | bb.note("Found %s tests" % suite.countTestCases()) | ||
32 | |||
33 | result = runner.run(suite) | ||
34 | |||
35 | return result | ||
36 | |||
37 | |||
38 | |||
39 | class oeRuntimeTest(unittest.TestCase): | ||
40 | |||
41 | longMessage = True | ||
42 | testFailures = [] | ||
43 | testSkipped = [] | ||
44 | testErrors = [] | ||
45 | |||
46 | def __init__(self, methodName='runTest'): | ||
47 | self.target = oeRuntimeTest.tc.target | ||
48 | super(oeRuntimeTest, self).__init__(methodName) | ||
49 | |||
50 | |||
51 | def run(self, result=None): | ||
52 | super(oeRuntimeTest, self).run(result) | ||
53 | |||
54 | # we add to our own lists the results, we use those for decorators | ||
55 | if len(result.failures) > len(oeRuntimeTest.testFailures): | ||
56 | oeRuntimeTest.testFailures.append(str(result.failures[-1][0]).split()[0]) | ||
57 | if len(result.skipped) > len(oeRuntimeTest.testSkipped): | ||
58 | oeRuntimeTest.testSkipped.append(str(result.skipped[-1][0]).split()[0]) | ||
59 | if len(result.errors) > len(oeRuntimeTest.testErrors): | ||
60 | oeRuntimeTest.testErrors.append(str(result.errors[-1][0]).split()[0]) | ||
61 | |||
62 | @classmethod | ||
63 | def hasPackage(self, pkg): | ||
64 | |||
65 | pkgfile = os.path.join(oeRuntimeTest.tc.d.getVar("WORKDIR", True), "installed_pkgs.txt") | ||
66 | |||
67 | with open(pkgfile) as f: | ||
68 | data = mmap.mmap(f.fileno(), 0, access=mmap.ACCESS_READ) | ||
69 | match = re.search(pkg, data) | ||
70 | data.close() | ||
71 | |||
72 | if match: | ||
73 | return True | ||
74 | |||
75 | return False | ||
76 | |||
77 | @classmethod | ||
78 | def hasFeature(self,feature): | ||
79 | |||
80 | if feature in oeRuntimeTest.tc.d.getVar("IMAGE_FEATURES", True).split() or \ | ||
81 | feature in oeRuntimeTest.tc.d.getVar("DISTRO_FEATURES", True).split(): | ||
82 | return True | ||
83 | else: | ||
84 | return False | ||
85 | |||
86 | @classmethod | ||
87 | def restartTarget(self,params=None): | ||
88 | |||
89 | if oeRuntimeTest.tc.qemu.restart(params): | ||
90 | oeRuntimeTest.tc.target.host = oeRuntimeTest.tc.qemu.ip | ||
91 | else: | ||
92 | raise Exception("Restarting target failed") | ||
93 | |||
94 | |||
95 | def getmodule(pos=2): | ||
96 | # stack returns a list of tuples containg frame information | ||
97 | # First element of the list the is current frame, caller is 1 | ||
98 | frameinfo = inspect.stack()[pos] | ||
99 | modname = inspect.getmodulename(frameinfo[1]) | ||
100 | #modname = inspect.getmodule(frameinfo[0]).__name__ | ||
101 | return modname | ||
102 | |||
103 | def skipModule(reason, pos=2): | ||
104 | modname = getmodule(pos) | ||
105 | if modname not in oeRuntimeTest.tc.testsrequired: | ||
106 | raise unittest.SkipTest("%s: %s" % (modname, reason)) | ||
107 | else: | ||
108 | raise Exception("\nTest %s wants to be skipped.\nReason is: %s" \ | ||
109 | "\nTest was required in TEST_SUITES, so either the condition for skipping is wrong" \ | ||
110 | "\nor the image really doesn't have the requred feature/package when it should." % (modname, reason)) | ||
111 | |||
112 | def skipModuleIf(cond, reason): | ||
113 | |||
114 | if cond: | ||
115 | skipModule(reason, 3) | ||
116 | |||
117 | def skipModuleUnless(cond, reason): | ||
118 | |||
119 | if not cond: | ||
120 | skipModule(reason, 3) | ||