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