blob: e10455edc15d2a3e27cfe3aeefde058177247c17 [file] [log] [blame]
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001# Copyright (c) 2013 Intel Corporation
2#
3# Released under the MIT license (see COPYING.MIT)
4
5
6# DESCRIPTION
Patrick Williamsd8c66bc2016-06-20 12:57:21 -05007# Base class inherited by test classes in meta/lib/oeqa/selftest
Patrick Williamsc124f4f2015-09-15 14:41:29 -05008
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
18from oeqa.utils.decorators import LogResults
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050019from random import choice
20import glob
Patrick Williamsc124f4f2015-09-15 14:41:29 -050021
22@LogResults
23class oeSelfTest(unittest.TestCase):
24
25 log = logging.getLogger("selftest.base")
26 longMessage = True
27
28 def __init__(self, methodName="runTest"):
29 self.builddir = os.environ.get("BUILDDIR")
30 self.localconf_path = os.path.join(self.builddir, "conf/local.conf")
31 self.testinc_path = os.path.join(self.builddir, "conf/selftest.inc")
32 self.local_bblayers_path = os.path.join(self.builddir, "conf/bblayers.conf")
33 self.testinc_bblayers_path = os.path.join(self.builddir, "conf/bblayers.inc")
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050034 self.machineinc_path = os.path.join(self.builddir, "conf/machine.inc")
Patrick Williamsc124f4f2015-09-15 14:41:29 -050035 self.testlayer_path = oeSelfTest.testlayer_path
36 self._extra_tear_down_commands = []
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050037 self._track_for_cleanup = [self.testinc_path, self.testinc_bblayers_path, self.machineinc_path]
Patrick Williamsc124f4f2015-09-15 14:41:29 -050038 super(oeSelfTest, self).__init__(methodName)
39
40 def setUp(self):
41 os.chdir(self.builddir)
42 # we don't know what the previous test left around in config or inc files
43 # if it failed so we need a fresh start
44 try:
45 os.remove(self.testinc_path)
46 except OSError as e:
47 if e.errno != errno.ENOENT:
48 raise
49 for root, _, files in os.walk(self.testlayer_path):
50 for f in files:
51 if f == 'test_recipe.inc':
52 os.remove(os.path.join(root, f))
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050053
54 for incl_file in [self.testinc_bblayers_path, self.machineinc_path]:
55 try:
56 os.remove(incl_file)
57 except OSError as e:
58 if e.errno != errno.ENOENT:
59 raise
60
61 # Get CUSTOMMACHINE from env (set by --machine argument to oe-selftest)
62 custommachine = os.getenv('CUSTOMMACHINE')
63 if custommachine:
64 if custommachine == 'random':
65 machine = get_random_machine()
66 else:
67 machine = custommachine
68 machine_conf = 'MACHINE ??= "%s"\n' % machine
69 self.set_machine_config(machine_conf)
70 print 'MACHINE: %s' % machine
71
Patrick Williamsc124f4f2015-09-15 14:41:29 -050072 # tests might need their own setup
73 # but if they overwrite this one they have to call
74 # super each time, so let's give them an alternative
75 self.setUpLocal()
76
77 def setUpLocal(self):
78 pass
79
80 def tearDown(self):
81 if self._extra_tear_down_commands:
82 failed_extra_commands = []
83 for command in self._extra_tear_down_commands:
84 result = runCmd(command, ignore_status=True)
85 if not result.status == 0:
86 failed_extra_commands.append(command)
87 if failed_extra_commands:
88 self.log.warning("tearDown commands have failed: %s" % ', '.join(map(str, failed_extra_commands)))
89 self.log.debug("Trying to move on.")
90 self._extra_tear_down_commands = []
91
92 if self._track_for_cleanup:
93 for path in self._track_for_cleanup:
94 if os.path.isdir(path):
95 shutil.rmtree(path)
96 if os.path.isfile(path):
97 os.remove(path)
98 self._track_for_cleanup = []
99
100 self.tearDownLocal()
101
102 def tearDownLocal(self):
103 pass
104
105 # add test specific commands to the tearDown method.
106 def add_command_to_tearDown(self, command):
107 self.log.debug("Adding command '%s' to tearDown for this test." % command)
108 self._extra_tear_down_commands.append(command)
109 # add test specific files or directories to be removed in the tearDown method
110 def track_for_cleanup(self, path):
111 self.log.debug("Adding path '%s' to be cleaned up when test is over" % path)
112 self._track_for_cleanup.append(path)
113
114 # write to <builddir>/conf/selftest.inc
115 def write_config(self, data):
116 self.log.debug("Writing to: %s\n%s\n" % (self.testinc_path, data))
117 ftools.write_file(self.testinc_path, data)
118
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500119 custommachine = os.getenv('CUSTOMMACHINE')
120 if custommachine and 'MACHINE' in data:
121 machine = get_bb_var('MACHINE')
122 self.log.warning('MACHINE overridden: %s' % machine)
123
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500124 # append to <builddir>/conf/selftest.inc
125 def append_config(self, data):
126 self.log.debug("Appending to: %s\n%s\n" % (self.testinc_path, data))
127 ftools.append_file(self.testinc_path, data)
128
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500129 custommachine = os.getenv('CUSTOMMACHINE')
130 if custommachine and 'MACHINE' in data:
131 machine = get_bb_var('MACHINE')
132 self.log.warning('MACHINE overridden: %s' % machine)
133
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500134 # remove data from <builddir>/conf/selftest.inc
135 def remove_config(self, data):
136 self.log.debug("Removing from: %s\n\%s\n" % (self.testinc_path, data))
137 ftools.remove_from_file(self.testinc_path, data)
138
139 # write to meta-sefltest/recipes-test/<recipe>/test_recipe.inc
140 def write_recipeinc(self, recipe, data):
141 inc_file = os.path.join(self.testlayer_path, 'recipes-test', recipe, 'test_recipe.inc')
142 self.log.debug("Writing to: %s\n%s\n" % (inc_file, data))
143 ftools.write_file(inc_file, data)
144
145 # append data to meta-sefltest/recipes-test/<recipe>/test_recipe.inc
146 def append_recipeinc(self, recipe, data):
147 inc_file = os.path.join(self.testlayer_path, 'recipes-test', recipe, 'test_recipe.inc')
148 self.log.debug("Appending to: %s\n%s\n" % (inc_file, data))
149 ftools.append_file(inc_file, data)
150
151 # remove data from meta-sefltest/recipes-test/<recipe>/test_recipe.inc
152 def remove_recipeinc(self, recipe, data):
153 inc_file = os.path.join(self.testlayer_path, 'recipes-test', recipe, 'test_recipe.inc')
154 self.log.debug("Removing from: %s\n%s\n" % (inc_file, data))
155 ftools.remove_from_file(inc_file, data)
156
157 # delete meta-sefltest/recipes-test/<recipe>/test_recipe.inc file
158 def delete_recipeinc(self, recipe):
159 inc_file = os.path.join(self.testlayer_path, 'recipes-test', recipe, 'test_recipe.inc')
160 self.log.debug("Deleting file: %s" % inc_file)
161 try:
162 os.remove(inc_file)
163 except OSError as e:
164 if e.errno != errno.ENOENT:
165 raise
166
167 # write to <builddir>/conf/bblayers.inc
168 def write_bblayers_config(self, data):
169 self.log.debug("Writing to: %s\n%s\n" % (self.testinc_bblayers_path, data))
170 ftools.write_file(self.testinc_bblayers_path, data)
171
172 # append to <builddir>/conf/bblayers.inc
173 def append_bblayers_config(self, data):
174 self.log.debug("Appending to: %s\n%s\n" % (self.testinc_bblayers_path, data))
175 ftools.append_file(self.testinc_bblayers_path, data)
176
177 # remove data from <builddir>/conf/bblayers.inc
178 def remove_bblayers_config(self, data):
179 self.log.debug("Removing from: %s\n\%s\n" % (self.testinc_bblayers_path, data))
180 ftools.remove_from_file(self.testinc_bblayers_path, data)
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500181
182 # write to <builddir>/conf/machine.inc
183 def set_machine_config(self, data):
184 self.log.debug("Writing to: %s\n%s\n" % (self.machineinc_path, data))
185 ftools.write_file(self.machineinc_path, data)
186
187
188def get_available_machines():
189 # Get a list of all available machines
190 bbpath = get_bb_var('BBPATH').split(':')
191 machines = []
192
193 for path in bbpath:
194 found_machines = glob.glob(os.path.join(path, 'conf', 'machine', '*.conf'))
195 if found_machines:
196 for i in found_machines:
197 # eg: '/home/<user>/poky/meta-intel/conf/machine/intel-core2-32.conf'
198 machines.append(os.path.splitext(os.path.basename(i))[0])
199
200 return machines
201
202
203def get_random_machine():
204 # Get a random machine
205 return choice(get_available_machines())