1# 2# SPDX-License-Identifier: MIT 3# 4 5import os 6import re 7import time 8import logging 9import bb.tinfoil 10 11from oeqa.selftest.case import OESelftestTestCase 12 13class TinfoilTests(OESelftestTestCase): 14 """ Basic tests for the tinfoil API """ 15 16 def test_getvar(self): 17 with bb.tinfoil.Tinfoil() as tinfoil: 18 tinfoil.prepare(True) 19 machine = tinfoil.config_data.getVar('MACHINE') 20 if not machine: 21 self.fail('Unable to get MACHINE value - returned %s' % machine) 22 23 def test_expand(self): 24 with bb.tinfoil.Tinfoil() as tinfoil: 25 tinfoil.prepare(True) 26 expr = '${@os.getpid()}' 27 pid = tinfoil.config_data.expand(expr) 28 if not pid: 29 self.fail('Unable to expand "%s" - returned %s' % (expr, pid)) 30 31 def test_getvar_bb_origenv(self): 32 with bb.tinfoil.Tinfoil() as tinfoil: 33 tinfoil.prepare(True) 34 origenv = tinfoil.config_data.getVar('BB_ORIGENV', False) 35 if not origenv: 36 self.fail('Unable to get BB_ORIGENV value - returned %s' % origenv) 37 self.assertEqual(origenv.getVar('HOME', False), os.environ['HOME']) 38 39 def test_parse_recipe(self): 40 with bb.tinfoil.Tinfoil() as tinfoil: 41 tinfoil.prepare(config_only=False, quiet=2) 42 testrecipe = 'mdadm' 43 best = tinfoil.find_best_provider(testrecipe) 44 if not best: 45 self.fail('Unable to find recipe providing %s' % testrecipe) 46 rd = tinfoil.parse_recipe_file(best[3]) 47 self.assertEqual(testrecipe, rd.getVar('PN')) 48 49 def test_parse_recipe_copy_expand(self): 50 with bb.tinfoil.Tinfoil() as tinfoil: 51 tinfoil.prepare(config_only=False, quiet=2) 52 testrecipe = 'mdadm' 53 best = tinfoil.find_best_provider(testrecipe) 54 if not best: 55 self.fail('Unable to find recipe providing %s' % testrecipe) 56 rd = tinfoil.parse_recipe_file(best[3]) 57 # Check we can get variable values 58 self.assertEqual(testrecipe, rd.getVar('PN')) 59 # Check that expanding a value that includes a variable reference works 60 self.assertEqual(testrecipe, rd.getVar('BPN')) 61 # Now check that changing the referenced variable's value in a copy gives that 62 # value when expanding 63 localdata = bb.data.createCopy(rd) 64 localdata.setVar('PN', 'hello') 65 self.assertEqual('hello', localdata.getVar('BPN')) 66 67 # The config_data API tp parse_recipe_file is used by: 68 # layerindex-web layerindex/update_layer.py 69 def test_parse_recipe_custom_data(self): 70 with bb.tinfoil.Tinfoil() as tinfoil: 71 tinfoil.prepare(config_only=False, quiet=2) 72 localdata = bb.data.createCopy(tinfoil.config_data) 73 localdata.setVar("TESTVAR", "testval") 74 testrecipe = 'mdadm' 75 best = tinfoil.find_best_provider(testrecipe) 76 if not best: 77 self.fail('Unable to find recipe providing %s' % testrecipe) 78 rd = tinfoil.parse_recipe_file(best[3], config_data=localdata) 79 self.assertEqual("testval", rd.getVar('TESTVAR')) 80 81 def test_list_recipes(self): 82 with bb.tinfoil.Tinfoil() as tinfoil: 83 tinfoil.prepare(config_only=False, quiet=2) 84 # Check pkg_pn 85 checkpns = ['tar', 'automake', 'coreutils', 'm4-native', 'nativesdk-gcc'] 86 pkg_pn = tinfoil.cooker.recipecaches[''].pkg_pn 87 for pn in checkpns: 88 self.assertIn(pn, pkg_pn) 89 # Check pkg_fn 90 checkfns = {'nativesdk-gcc': '^virtual:nativesdk:.*', 'coreutils': '.*/coreutils_.*.bb'} 91 for fn, pn in tinfoil.cooker.recipecaches[''].pkg_fn.items(): 92 if pn in checkpns: 93 if pn in checkfns: 94 self.assertTrue(re.match(checkfns[pn], fn), 'Entry for %s: %s did not match %s' % (pn, fn, checkfns[pn])) 95 checkpns.remove(pn) 96 if checkpns: 97 self.fail('Unable to find pkg_fn entries for: %s' % ', '.join(checkpns)) 98 99 def test_wait_event(self): 100 with bb.tinfoil.Tinfoil() as tinfoil: 101 tinfoil.prepare(config_only=True) 102 103 tinfoil.set_event_mask(['bb.event.FilesMatchingFound', 'bb.command.CommandCompleted', 'bb.command.CommandFailed', 'bb.command.CommandExit']) 104 105 # Need to drain events otherwise events that were masked may still be in the queue 106 while tinfoil.wait_event(): 107 pass 108 109 pattern = 'conf' 110 res = tinfoil.run_command('testCookerCommandEvent', pattern, handle_events=False) 111 self.assertTrue(res) 112 113 eventreceived = False 114 commandcomplete = False 115 start = time.time() 116 # Wait for maximum 60s in total so we'd detect spurious heartbeat events for example 117 while (not (eventreceived == True and commandcomplete == True) 118 and (time.time() - start < 60)): 119 # if we received both events (on let's say a good day), we are done 120 event = tinfoil.wait_event(1) 121 if event: 122 if isinstance(event, bb.command.CommandCompleted): 123 commandcomplete = True 124 elif isinstance(event, bb.event.FilesMatchingFound): 125 self.assertEqual(pattern, event._pattern) 126 self.assertIn('A', event._matches) 127 self.assertIn('B', event._matches) 128 eventreceived = True 129 elif isinstance(event, logging.LogRecord): 130 continue 131 else: 132 self.fail('Unexpected event: %s' % event) 133 134 self.assertTrue(commandcomplete, 'Timed out waiting for CommandCompleted event from bitbake server (Matching event received: %s)' % str(eventreceived)) 135 self.assertTrue(eventreceived, 'Did not receive FilesMatchingFound event from bitbake server') 136 137 def test_setvariable_clean(self): 138 # First check that setVariable affects the datastore 139 with bb.tinfoil.Tinfoil() as tinfoil: 140 tinfoil.prepare(config_only=True) 141 tinfoil.run_command('setVariable', 'TESTVAR', 'specialvalue') 142 self.assertEqual(tinfoil.config_data.getVar('TESTVAR'), 'specialvalue', 'Value set using setVariable is not reflected in client-side getVar()') 143 144 # Now check that the setVariable's effects are no longer present 145 # (this may legitimately break in future if we stop reinitialising 146 # the datastore, in which case we'll have to reconsider use of 147 # setVariable entirely) 148 with bb.tinfoil.Tinfoil() as tinfoil: 149 tinfoil.prepare(config_only=True) 150 self.assertNotEqual(tinfoil.config_data.getVar('TESTVAR'), 'specialvalue', 'Value set using setVariable is still present!') 151 152 # Now check that setVar on the main datastore works (uses setVariable internally) 153 with bb.tinfoil.Tinfoil() as tinfoil: 154 tinfoil.prepare(config_only=True) 155 tinfoil.config_data.setVar('TESTVAR', 'specialvalue') 156 value = tinfoil.run_command('getVariable', 'TESTVAR') 157 self.assertEqual(value, 'specialvalue', 'Value set using config_data.setVar() is not reflected in config_data.getVar()') 158 159 def test_datastore_operations(self): 160 with bb.tinfoil.Tinfoil() as tinfoil: 161 tinfoil.prepare(config_only=True) 162 # Test setVarFlag() / getVarFlag() 163 tinfoil.config_data.setVarFlag('TESTVAR', 'flagname', 'flagval') 164 value = tinfoil.config_data.getVarFlag('TESTVAR', 'flagname') 165 self.assertEqual(value, 'flagval', 'Value set using config_data.setVarFlag() is not reflected in config_data.getVarFlag()') 166 # Test delVarFlag() 167 tinfoil.config_data.setVarFlag('TESTVAR', 'otherflag', 'othervalue') 168 tinfoil.config_data.delVarFlag('TESTVAR', 'flagname') 169 value = tinfoil.config_data.getVarFlag('TESTVAR', 'flagname') 170 self.assertEqual(value, None, 'Varflag deleted using config_data.delVarFlag() is not reflected in config_data.getVarFlag()') 171 value = tinfoil.config_data.getVarFlag('TESTVAR', 'otherflag') 172 self.assertEqual(value, 'othervalue', 'Varflag deleted using config_data.delVarFlag() caused unrelated flag to be removed') 173 # Test delVar() 174 tinfoil.config_data.setVar('TESTVAR', 'varvalue') 175 value = tinfoil.config_data.getVar('TESTVAR') 176 self.assertEqual(value, 'varvalue', 'Value set using config_data.setVar() is not reflected in config_data.getVar()') 177 tinfoil.config_data.delVar('TESTVAR') 178 value = tinfoil.config_data.getVar('TESTVAR') 179 self.assertEqual(value, None, 'Variable deleted using config_data.delVar() appears to still have a value') 180 # Test renameVar() 181 tinfoil.config_data.setVar('TESTVAROLD', 'origvalue') 182 tinfoil.config_data.renameVar('TESTVAROLD', 'TESTVARNEW') 183 value = tinfoil.config_data.getVar('TESTVAROLD') 184 self.assertEqual(value, None, 'Variable renamed using config_data.renameVar() still seems to exist') 185 value = tinfoil.config_data.getVar('TESTVARNEW') 186 self.assertEqual(value, 'origvalue', 'Variable renamed using config_data.renameVar() does not appear with new name') 187 # Test overrides 188 tinfoil.config_data.setVar('TESTVAR', 'original') 189 tinfoil.config_data.setVar('TESTVAR:overrideone', 'one') 190 tinfoil.config_data.setVar('TESTVAR:overridetwo', 'two') 191 tinfoil.config_data.appendVar('OVERRIDES', ':overrideone') 192 value = tinfoil.config_data.getVar('TESTVAR') 193 self.assertEqual(value, 'one', 'Variable overrides not functioning correctly') 194 195 def test_variable_history(self): 196 # Basic test to ensure that variable history works when tracking=True 197 with bb.tinfoil.Tinfoil(tracking=True) as tinfoil: 198 tinfoil.prepare(config_only=False, quiet=2) 199 # Note that _tracking for any datastore we get will be 200 # false here, that's currently expected - so we can't check 201 # for that 202 history = tinfoil.config_data.varhistory.variable('DL_DIR') 203 for entry in history: 204 if entry['file'].endswith('/bitbake.conf'): 205 if entry['op'] in ['set', 'set?']: 206 break 207 else: 208 self.fail('Did not find history entry setting DL_DIR in bitbake.conf. History: %s' % history) 209 # Check it works for recipes as well 210 testrecipe = 'zlib' 211 rd = tinfoil.parse_recipe(testrecipe) 212 history = rd.varhistory.variable('LICENSE') 213 bbfound = -1 214 recipefound = -1 215 for i, entry in enumerate(history): 216 if entry['file'].endswith('/bitbake.conf'): 217 if entry['detail'] == 'INVALID' and entry['op'] in ['set', 'set?']: 218 bbfound = i 219 elif entry['file'].endswith('.bb'): 220 if entry['op'] == 'set': 221 recipefound = i 222 if bbfound == -1: 223 self.fail('Did not find history entry setting LICENSE in bitbake.conf parsing %s recipe. History: %s' % (testrecipe, history)) 224 if recipefound == -1: 225 self.fail('Did not find history entry setting LICENSE in %s recipe. History: %s' % (testrecipe, history)) 226 if bbfound > recipefound: 227 self.fail('History entry setting LICENSE in %s recipe and in bitbake.conf in wrong order. History: %s' % (testrecipe, history)) 228