diff options
Diffstat (limited to 'meta/lib/oeqa/selftest/base.py')
-rw-r--r-- | meta/lib/oeqa/selftest/base.py | 131 |
1 files changed, 131 insertions, 0 deletions
diff --git a/meta/lib/oeqa/selftest/base.py b/meta/lib/oeqa/selftest/base.py new file mode 100644 index 0000000000..80b9b4b312 --- /dev/null +++ b/meta/lib/oeqa/selftest/base.py | |||
@@ -0,0 +1,131 @@ | |||
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.testlayer_path = oeSelfTest.testlayer_path | ||
31 | self._extra_tear_down_commands = [] | ||
32 | self._track_for_cleanup = [] | ||
33 | super(oeSelfTest, self).__init__(methodName) | ||
34 | |||
35 | def setUp(self): | ||
36 | os.chdir(self.builddir) | ||
37 | # we don't know what the previous test left around in config or inc files | ||
38 | # if it failed so we need a fresh start | ||
39 | try: | ||
40 | os.remove(self.testinc_path) | ||
41 | except OSError as e: | ||
42 | if e.errno != errno.ENOENT: | ||
43 | raise | ||
44 | for root, _, files in os.walk(self.testlayer_path): | ||
45 | for f in files: | ||
46 | if f == 'test_recipe.inc': | ||
47 | os.remove(os.path.join(root, f)) | ||
48 | # tests might need their own setup | ||
49 | # but if they overwrite this one they have to call | ||
50 | # super each time, so let's give them an alternative | ||
51 | self.setUpLocal() | ||
52 | |||
53 | def setUpLocal(self): | ||
54 | pass | ||
55 | |||
56 | def tearDown(self): | ||
57 | if self._extra_tear_down_commands: | ||
58 | failed_extra_commands = [] | ||
59 | for command in self._extra_tear_down_commands: | ||
60 | result = runCmd(command, ignore_status=True) | ||
61 | if not result.status == 0: | ||
62 | failed_extra_commands.append(command) | ||
63 | if failed_extra_commands: | ||
64 | self.log.warning("tearDown commands have failed: %s" % ', '.join(map(str, failed_extra_commands))) | ||
65 | self.log.debug("Trying to move on.") | ||
66 | self._extra_tear_down_commands = [] | ||
67 | |||
68 | if self._track_for_cleanup: | ||
69 | for path in self._track_for_cleanup: | ||
70 | if os.path.isdir(path): | ||
71 | shutil.rmtree(path) | ||
72 | if os.path.isfile(path): | ||
73 | os.remove(path) | ||
74 | self._track_for_cleanup = [] | ||
75 | |||
76 | self.tearDownLocal() | ||
77 | |||
78 | def tearDownLocal(self): | ||
79 | pass | ||
80 | |||
81 | # add test specific commands to the tearDown method. | ||
82 | def add_command_to_tearDown(self, command): | ||
83 | self.log.debug("Adding command '%s' to tearDown for this test." % command) | ||
84 | self._extra_tear_down_commands.append(command) | ||
85 | # add test specific files or directories to be removed in the tearDown method | ||
86 | def track_for_cleanup(self, path): | ||
87 | self.log.debug("Adding path '%s' to be cleaned up when test is over" % path) | ||
88 | self._track_for_cleanup.append(path) | ||
89 | |||
90 | # write to <builddir>/conf/selftest.inc | ||
91 | def write_config(self, data): | ||
92 | self.log.debug("Writing to: %s\n%s\n" % (self.testinc_path, data)) | ||
93 | ftools.write_file(self.testinc_path, data) | ||
94 | |||
95 | # append to <builddir>/conf/selftest.inc | ||
96 | def append_config(self, data): | ||
97 | self.log.debug("Appending to: %s\n%s\n" % (self.testinc_path, data)) | ||
98 | ftools.append_file(self.testinc_path, data) | ||
99 | |||
100 | # remove data from <builddir>/conf/selftest.inc | ||
101 | def remove_config(self, data): | ||
102 | self.log.debug("Removing from: %s\n\%s\n" % (self.testinc_path, data)) | ||
103 | ftools.remove_from_file(self.testinc_path, data) | ||
104 | |||
105 | # write to meta-sefltest/recipes-test/<recipe>/test_recipe.inc | ||
106 | def write_recipeinc(self, recipe, data): | ||
107 | inc_file = os.path.join(self.testlayer_path, 'recipes-test', recipe, 'test_recipe.inc') | ||
108 | self.log.debug("Writing to: %s\n%s\n" % (inc_file, data)) | ||
109 | ftools.write_file(inc_file, data) | ||
110 | |||
111 | # append data to meta-sefltest/recipes-test/<recipe>/test_recipe.inc | ||
112 | def append_recipeinc(self, recipe, data): | ||
113 | inc_file = os.path.join(self.testlayer_path, 'recipes-test', recipe, 'test_recipe.inc') | ||
114 | self.log.debug("Appending to: %s\n%s\n" % (inc_file, data)) | ||
115 | ftools.append_file(inc_file, data) | ||
116 | |||
117 | # remove data from meta-sefltest/recipes-test/<recipe>/test_recipe.inc | ||
118 | def remove_recipeinc(self, recipe, data): | ||
119 | inc_file = os.path.join(self.testlayer_path, 'recipes-test', recipe, 'test_recipe.inc') | ||
120 | self.log.debug("Removing from: %s\n%s\n" % (inc_file, data)) | ||
121 | ftools.remove_from_file(inc_file, data) | ||
122 | |||
123 | # delete meta-sefltest/recipes-test/<recipe>/test_recipe.inc file | ||
124 | def delete_recipeinc(self, recipe): | ||
125 | inc_file = os.path.join(self.testlayer_path, 'recipes-test', recipe, 'test_recipe.inc') | ||
126 | self.log.debug("Deleting file: %s" % inc_file) | ||
127 | try: | ||
128 | os.remove(inc_file) | ||
129 | except OSError as e: | ||
130 | if e.errno != errno.ENOENT: | ||
131 | raise | ||