1
0
mirror of https://git.yoctoproject.org/poky synced 2026-05-07 16:59:22 +00:00

oeqa/selftest/case: Migrate case class to the new OEQA framework

Summary of the changes:

- Use OETestCase as base class instead of unittest.TestCase
- Remove LogResults decorator the new framework provides logging into
  the core functionality.
- Logger is now self.logger instead of self.log
- Move comments into docstrings in several help methods
- Use get_test_layer() method instead of access monkey patched variable
  in old oeSelfTest case class.

(From OE-Core rev: c38cab77893f9d8fd505f050cc880a15677b73db)

Signed-off-by: Leonardo Sandoval <leonardo.sandoval.gonzalez@linux.intel.com>
Signed-off-by: Aníbal Limón <anibal.limon@linux.intel.com>
Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
This commit is contained in:
Leonardo Sandoval
2017-05-25 12:23:42 -05:00
committed by Richard Purdie
parent cb1b1eeb3f
commit a80aa4c025
+51 -60
View File
@@ -1,31 +1,19 @@
# Copyright (c) 2013 Intel Corporation
#
# Copyright (C) 2013-2017 Intel Corporation
# Released under the MIT license (see COPYING.MIT)
# DESCRIPTION
# Base class inherited by test classes in meta/lib/oeqa/selftest
import unittest
import os
import sys
import os
import shutil
import logging
import glob
import errno
from random import choice
from unittest.util import safe_repr
import oeqa.utils.ftools as ftools
from oeqa.utils.commands import runCmd, bitbake, get_bb_var, get_test_layer
from oeqa.utils.decorators import LogResults
from random import choice
import glob
from unittest.util import safe_repr
@LogResults
class oeSelfTest(unittest.TestCase):
log = logging.getLogger("selftest.base")
longMessage = True
from oeqa.core.case import OETestCase
class OESelftestTestCase(OETestCase):
def __init__(self, methodName="runTest"):
self.builddir = os.environ.get("BUILDDIR")
self.localconf_path = os.path.join(self.builddir, "conf/local.conf")
@@ -36,23 +24,26 @@ class oeSelfTest(unittest.TestCase):
"conf/bblayers.bk")
self.testinc_bblayers_path = os.path.join(self.builddir, "conf/bblayers.inc")
self.machineinc_path = os.path.join(self.builddir, "conf/machine.inc")
self.testlayer_path = oeSelfTest.testlayer_path
self.testlayer_path = get_test_layer()
self._extra_tear_down_commands = []
self._track_for_cleanup = [
self.testinc_path, self.testinc_bblayers_path,
self.machineinc_path, self.localconf_backup,
self.local_bblayers_backup]
super(oeSelfTest, self).__init__(methodName)
super(OESelftestTestCase, self).__init__(methodName)
def setUp(self):
super(OESelftestTestCase, self).setUp()
os.chdir(self.builddir)
# Check if local.conf or bblayers.conf files backup exists
# from a previous failed test and restore them
if os.path.isfile(self.localconf_backup) or os.path.isfile(
self.local_bblayers_backup):
self.log.debug("Found a local.conf and/or bblayers.conf backup \
from a previously aborted test. Restoring these files now, but tests should \
be re-executed from a clean environment to ensure accurate results.")
self.logger.debug("\
Found a local.conf and/or bblayers.conf backup from a previously aborted test.\
Restoring these files now, but tests should be re-executed from a clean environment\
to ensure accurate results.")
try:
shutil.copyfile(self.localconf_backup, self.localconf_path)
except OSError as e:
@@ -67,9 +58,8 @@ be re-executed from a clean environment to ensure accurate results.")
else:
# backup local.conf and bblayers.conf
shutil.copyfile(self.localconf_path, self.localconf_backup)
shutil.copyfile(self.local_bblayers_path,
self.local_bblayers_backup)
self.log.debug("Creating local.conf and bblayers.conf backups.")
shutil.copyfile(self.local_bblayers_path, self.local_bblayers_backup)
self.logger.debug("Creating local.conf and bblayers.conf backups.")
# we don't know what the previous test left around in config or inc files
# if it failed so we need a fresh start
try:
@@ -116,8 +106,8 @@ be re-executed from a clean environment to ensure accurate results.")
if not result.status == 0:
failed_extra_commands.append(command)
if failed_extra_commands:
self.log.warning("tearDown commands have failed: %s" % ', '.join(map(str, failed_extra_commands)))
self.log.debug("Trying to move on.")
self.logger.warning("tearDown commands have failed: %s" % ', '.join(map(str, failed_extra_commands)))
self.logger.debug("Trying to move on.")
self._extra_tear_down_commands = []
if self._track_for_cleanup:
@@ -129,90 +119,92 @@ be re-executed from a clean environment to ensure accurate results.")
self._track_for_cleanup = []
self.tearDownLocal()
super(OESelftestTestCase, self).tearDown()
def tearDownLocal(self):
pass
# add test specific commands to the tearDown method.
def add_command_to_tearDown(self, command):
self.log.debug("Adding command '%s' to tearDown for this test." % command)
"""Add test specific commands to the tearDown method"""
self.logger.debug("Adding command '%s' to tearDown for this test." % command)
self._extra_tear_down_commands.append(command)
# add test specific files or directories to be removed in the tearDown method
def track_for_cleanup(self, path):
self.log.debug("Adding path '%s' to be cleaned up when test is over" % path)
"""Add test specific files or directories to be removed in the tearDown method"""
self.logger.debug("Adding path '%s' to be cleaned up when test is over" % path)
self._track_for_cleanup.append(path)
# write to <builddir>/conf/selftest.inc
def write_config(self, data):
self.log.debug("Writing to: %s\n%s\n" % (self.testinc_path, data))
"""Write to <builddir>/conf/selftest.inc"""
self.logger.debug("Writing to: %s\n%s\n" % (self.testinc_path, data))
ftools.write_file(self.testinc_path, data)
custommachine = os.getenv('CUSTOMMACHINE')
if custommachine and 'MACHINE' in data:
machine = get_bb_var('MACHINE')
self.log.warning('MACHINE overridden: %s' % machine)
self.logger.warning('MACHINE overridden: %s' % machine)
# append to <builddir>/conf/selftest.inc
def append_config(self, data):
self.log.debug("Appending to: %s\n%s\n" % (self.testinc_path, data))
"""Append to <builddir>/conf/selftest.inc"""
self.logger.debug("Appending to: %s\n%s\n" % (self.testinc_path, data))
ftools.append_file(self.testinc_path, data)
custommachine = os.getenv('CUSTOMMACHINE')
if custommachine and 'MACHINE' in data:
machine = get_bb_var('MACHINE')
self.log.warning('MACHINE overridden: %s' % machine)
self.logger.warning('MACHINE overridden: %s' % machine)
# remove data from <builddir>/conf/selftest.inc
def remove_config(self, data):
self.log.debug("Removing from: %s\n%s\n" % (self.testinc_path, data))
"""Remove data from <builddir>/conf/selftest.inc"""
self.logger.debug("Removing from: %s\n%s\n" % (self.testinc_path, data))
ftools.remove_from_file(self.testinc_path, data)
# write to meta-sefltest/recipes-test/<recipe>/test_recipe.inc
def write_recipeinc(self, recipe, data):
"""Write to meta-sefltest/recipes-test/<recipe>/test_recipe.inc"""
inc_file = os.path.join(self.testlayer_path, 'recipes-test', recipe, 'test_recipe.inc')
self.log.debug("Writing to: %s\n%s\n" % (inc_file, data))
self.logger.debug("Writing to: %s\n%s\n" % (inc_file, data))
ftools.write_file(inc_file, data)
# append data to meta-sefltest/recipes-test/<recipe>/test_recipe.inc
def append_recipeinc(self, recipe, data):
"""Append data to meta-sefltest/recipes-test/<recipe>/test_recipe.inc"""
inc_file = os.path.join(self.testlayer_path, 'recipes-test', recipe, 'test_recipe.inc')
self.log.debug("Appending to: %s\n%s\n" % (inc_file, data))
self.logger.debug("Appending to: %s\n%s\n" % (inc_file, data))
ftools.append_file(inc_file, data)
# remove data from meta-sefltest/recipes-test/<recipe>/test_recipe.inc
def remove_recipeinc(self, recipe, data):
"""Remove data from meta-sefltest/recipes-test/<recipe>/test_recipe.inc"""
inc_file = os.path.join(self.testlayer_path, 'recipes-test', recipe, 'test_recipe.inc')
self.log.debug("Removing from: %s\n%s\n" % (inc_file, data))
self.logger.debug("Removing from: %s\n%s\n" % (inc_file, data))
ftools.remove_from_file(inc_file, data)
# delete meta-sefltest/recipes-test/<recipe>/test_recipe.inc file
def delete_recipeinc(self, recipe):
"""Delete meta-sefltest/recipes-test/<recipe>/test_recipe.inc file"""
inc_file = os.path.join(self.testlayer_path, 'recipes-test', recipe, 'test_recipe.inc')
self.log.debug("Deleting file: %s" % inc_file)
self.logger.debug("Deleting file: %s" % inc_file)
try:
os.remove(inc_file)
except OSError as e:
if e.errno != errno.ENOENT:
raise
# write to <builddir>/conf/bblayers.inc
def write_bblayers_config(self, data):
self.log.debug("Writing to: %s\n%s\n" % (self.testinc_bblayers_path, data))
"""Write to <builddir>/conf/bblayers.inc"""
self.logger.debug("Writing to: %s\n%s\n" % (self.testinc_bblayers_path, data))
ftools.write_file(self.testinc_bblayers_path, data)
# append to <builddir>/conf/bblayers.inc
def append_bblayers_config(self, data):
self.log.debug("Appending to: %s\n%s\n" % (self.testinc_bblayers_path, data))
"""Append to <builddir>/conf/bblayers.inc"""
self.logger.debug("Appending to: %s\n%s\n" % (self.testinc_bblayers_path, data))
ftools.append_file(self.testinc_bblayers_path, data)
# remove data from <builddir>/conf/bblayers.inc
def remove_bblayers_config(self, data):
self.log.debug("Removing from: %s\n%s\n" % (self.testinc_bblayers_path, data))
"""Remove data from <builddir>/conf/bblayers.inc"""
self.logger.debug("Removing from: %s\n%s\n" % (self.testinc_bblayers_path, data))
ftools.remove_from_file(self.testinc_bblayers_path, data)
# write to <builddir>/conf/machine.inc
def set_machine_config(self, data):
self.log.debug("Writing to: %s\n%s\n" % (self.machineinc_path, data))
"""Write to <builddir>/conf/machine.inc"""
self.logger.debug("Writing to: %s\n%s\n" % (self.machineinc_path, data))
ftools.write_file(self.machineinc_path, data)
# check does path exist
@@ -227,7 +219,6 @@ be re-executed from a clean environment to ensure accurate results.")
msg = self._formatMessage(msg, "%s exists when it should not" % safe_repr(expr))
raise self.failureException(msg)
def get_available_machines():
# Get a list of all available machines
bbpath = get_bb_var('BBPATH').split(':')
@@ -237,7 +228,7 @@ def get_available_machines():
found_machines = glob.glob(os.path.join(path, 'conf', 'machine', '*.conf'))
if found_machines:
for i in found_machines:
# eg: '/home/<user>/poky/meta-intel/conf/machine/intel-core2-32.conf'
# eg: '/home/<user>/poky/meta-intel/conf/machine/intel-core2-32.conf'
machines.append(os.path.splitext(os.path.basename(i))[0])
return machines