common.py 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. # Copyright (C) 2017 Intel Corporation
  2. #
  3. # SPDX-License-Identifier: MIT
  4. #
  5. import glob
  6. import os
  7. import unittest
  8. import re
  9. from checklayer import get_signatures, LayerType, check_command, compare_signatures, get_git_toplevel
  10. from checklayer.case import OECheckLayerTestCase
  11. class CommonCheckLayer(OECheckLayerTestCase):
  12. def test_readme(self):
  13. if self.tc.layer['type'] == LayerType.CORE:
  14. raise unittest.SkipTest("Core layer's README is top level")
  15. # The top-level README file may have a suffix (like README.rst or README.txt).
  16. readme_files = glob.glob(os.path.join(self.tc.layer['path'], '[Rr][Ee][Aa][Dd][Mm][Ee]*'))
  17. self.assertTrue(len(readme_files) > 0,
  18. msg="Layer doesn't contain a README file.")
  19. # There might be more than one file matching the file pattern above
  20. # (for example, README.rst and README-COPYING.rst). The one with the shortest
  21. # name is considered the "main" one.
  22. readme_file = sorted(readme_files)[0]
  23. data = ''
  24. with open(readme_file, 'r') as f:
  25. data = f.read()
  26. self.assertTrue(data,
  27. msg="Layer contains a README file but it is empty.")
  28. # If a layer's README references another README, then the checks below are not valid
  29. if re.search('README', data, re.IGNORECASE):
  30. return
  31. self.assertIn('maintainer', data.lower())
  32. self.assertIn('patch', data.lower())
  33. # Check that there is an email address in the README
  34. email_regex = re.compile(r"[^@]+@[^@]+")
  35. self.assertTrue(email_regex.match(data))
  36. def find_file_by_name(self, globs):
  37. """
  38. Utility function to find a file that matches the specified list of
  39. globs, in either the layer directory itself or the repository top-level
  40. directory.
  41. """
  42. directories = [self.tc.layer["path"]]
  43. toplevel = get_git_toplevel(directories[0])
  44. if toplevel:
  45. directories.append(toplevel)
  46. for path in directories:
  47. for name in globs:
  48. files = glob.glob(os.path.join(path, name))
  49. if files:
  50. return sorted(files)[0]
  51. return None
  52. def test_security(self):
  53. """
  54. Test that the layer has a SECURITY.md (or similar) file, either in the
  55. layer itself or at the top of the containing git repository.
  56. """
  57. if self.tc.layer["type"] == LayerType.CORE:
  58. raise unittest.SkipTest("Core layer's SECURITY is top level")
  59. filename = self.find_file_by_name(("SECURITY", "SECURITY.*"))
  60. self.assertTrue(filename, msg="Layer doesn't contain a SECURITY.md file.")
  61. size = os.path.getsize(filename)
  62. self.assertGreater(size, 0, msg=f"{filename} has no content.")
  63. def test_parse(self):
  64. check_command('Layer %s failed to parse.' % self.tc.layer['name'],
  65. 'bitbake -p')
  66. def test_show_environment(self):
  67. check_command('Layer %s failed to show environment.' % self.tc.layer['name'],
  68. 'bitbake -e')
  69. def test_world(self):
  70. '''
  71. "bitbake world" is expected to work. test_signatures does not cover that
  72. because it is more lenient and ignores recipes in a world build that
  73. are not actually buildable, so here we fail when "bitbake -S none world"
  74. fails.
  75. '''
  76. get_signatures(self.td['builddir'], failsafe=False)
  77. def test_world_inherit_class(self):
  78. '''
  79. This also does "bitbake -S none world" along with inheriting "yocto-check-layer"
  80. class, which can do additional per-recipe test cases.
  81. '''
  82. msg = []
  83. try:
  84. get_signatures(self.td['builddir'], failsafe=False, machine=None, extravars='BB_ENV_PASSTHROUGH_ADDITIONS="$BB_ENV_PASSTHROUGH_ADDITIONS INHERIT" INHERIT="yocto-check-layer"')
  85. except RuntimeError as ex:
  86. msg.append(str(ex))
  87. if msg:
  88. msg.insert(0, 'Layer %s failed additional checks from yocto-check-layer.bbclass\nSee below log for specific recipe parsing errors:\n' % \
  89. self.tc.layer['name'])
  90. self.fail('\n'.join(msg))
  91. def test_patches_upstream_status(self):
  92. import sys
  93. sys.path.append(os.path.join(sys.path[0], '../../../../meta/lib/'))
  94. import oe.qa
  95. patches = []
  96. for dirpath, dirs, files in os.walk(self.tc.layer['path']):
  97. for filename in files:
  98. if filename.endswith(".patch"):
  99. ppath = os.path.join(dirpath, filename)
  100. if oe.qa.check_upstream_status(ppath):
  101. patches.append(ppath)
  102. self.assertEqual(len(patches), 0 , \
  103. msg="Found following patches with malformed or missing upstream status:\n%s" % '\n'.join([str(patch) for patch in patches]))
  104. def test_signatures(self):
  105. if self.tc.layer['type'] == LayerType.SOFTWARE and \
  106. not self.tc.test_software_layer_signatures:
  107. raise unittest.SkipTest("Not testing for signature changes in a software layer %s." \
  108. % self.tc.layer['name'])
  109. curr_sigs, _ = get_signatures(self.td['builddir'], failsafe=True)
  110. msg = compare_signatures(self.td['sigs'], curr_sigs)
  111. if msg is not None:
  112. self.fail('Adding layer %s changed signatures.\n%s' % (self.tc.layer['name'], msg))
  113. def test_layerseries_compat(self):
  114. for collection_name, collection_data in self.tc.layer['collections'].items():
  115. self.assertTrue(collection_data['compat'], "Collection %s from layer %s does not set compatible oe-core versions via LAYERSERIES_COMPAT_collection." \
  116. % (collection_name, self.tc.layer['name']))