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