base.py 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. # Copyright (c) 2013 Intel Corporation
  2. #
  3. # Released under the MIT license (see COPYING.MIT)
  4. # DESCRIPTION
  5. # Base class inherited by test classes in meta/lib/selftest
  6. import unittest
  7. import os
  8. import sys
  9. import logging
  10. import errno
  11. import oeqa.utils.ftools as ftools
  12. class oeSelfTest(unittest.TestCase):
  13. log = logging.getLogger("selftest.base")
  14. longMessage = True
  15. def __init__(self, methodName="runTest"):
  16. self.builddir = os.environ.get("BUILDDIR")
  17. self.localconf_path = os.path.join(self.builddir, "conf/local.conf")
  18. self.testinc_path = os.path.join(self.builddir, "conf/selftest.inc")
  19. self.testlayer_path = oeSelfTest.testlayer_path
  20. super(oeSelfTest, self).__init__(methodName)
  21. def setUp(self):
  22. os.chdir(self.builddir)
  23. # we don't know what the previous test left around in config or inc files
  24. # if it failed so we need a fresh start
  25. try:
  26. os.remove(self.testinc_path)
  27. except OSError as e:
  28. if e.errno != errno.ENOENT:
  29. raise
  30. for root, _, files in os.walk(self.testlayer_path):
  31. for f in files:
  32. if f == 'test_recipe.inc':
  33. os.remove(os.path.join(root, f))
  34. # tests might need their own setup
  35. # but if they overwrite this one they have to call
  36. # super each time, so let's give them an alternative
  37. self.setUpLocal()
  38. def setUpLocal(self):
  39. pass
  40. def tearDown(self):
  41. self.tearDownLocal()
  42. def tearDownLocal(self):
  43. pass
  44. # write to <builddir>/conf/selftest.inc
  45. def write_config(self, data):
  46. self.log.debug("Writing to: %s\n%s\n" % (self.testinc_path, data))
  47. ftools.write_file(self.testinc_path, data)
  48. # append to <builddir>/conf/selftest.inc
  49. def append_config(self, data):
  50. self.log.debug("Appending to: %s\n%s\n" % (self.testinc_path, data))
  51. ftools.append_file(self.testinc_path, data)
  52. # remove data from <builddir>/conf/selftest.inc
  53. def remove_config(self, data):
  54. self.log.debug("Removing from: %s\n\%s\n" % (self.testinc_path, data))
  55. ftools.remove_from_file(self.testinc_path, data)
  56. # write to meta-sefltest/recipes-test/<recipe>/test_recipe.inc
  57. def write_recipeinc(self, recipe, data):
  58. inc_file = os.path.join(self.testlayer_path, 'recipes-test', recipe, 'test_recipe.inc')
  59. self.log.debug("Writing to: %s\n%s\n" % (inc_file, data))
  60. ftools.write_file(inc_file, data)
  61. # append data to meta-sefltest/recipes-test/<recipe>/test_recipe.inc
  62. def append_recipeinc(self, recipe, data):
  63. inc_file = os.path.join(self.testlayer_path, 'recipes-test', recipe, 'test_recipe.inc')
  64. self.log.debug("Appending to: %s\n%s\n" % (inc_file, data))
  65. ftools.append_file(inc_file, data)
  66. # remove data from meta-sefltest/recipes-test/<recipe>/test_recipe.inc
  67. def remove_recipeinc(self, recipe, data):
  68. inc_file = os.path.join(self.testlayer_path, 'recipes-test', recipe, 'test_recipe.inc')
  69. self.log.debug("Removing from: %s\n%s\n" % (inc_file, data))
  70. ftools.remove_from_file(inc_file, data)
  71. # delete meta-sefltest/recipes-test/<recipe>/test_recipe.inc file
  72. def delete_recipeinc(self, recipe):
  73. inc_file = os.path.join(self.testlayer_path, 'recipes-test', recipe, 'test_recipe.inc')
  74. self.log.debug("Deleting file: %s" % inc_file)
  75. try:
  76. os.remove(self.testinc_path)
  77. except OSError as e:
  78. if e.errno != errno.ENOENT:
  79. raise