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