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