blob: 42a1b6b4f4d7a1de40a92910a3948df9127850a4 [file] [log] [blame]
Brad Bishopc342db32019-05-15 21:57:59 -04001#
2# SPDX-License-Identifier: MIT
3#
4
Brad Bishopd7bf8c12018-02-25 22:55:05 -05005import os
6import re
7import time
8import logging
9import bb.tinfoil
10
11from oeqa.selftest.case import OESelftestTestCase
12from oeqa.utils.commands import runCmd
Brad Bishopd7bf8c12018-02-25 22:55:05 -050013
14class TinfoilTests(OESelftestTestCase):
15 """ Basic tests for the tinfoil API """
16
Brad Bishopd7bf8c12018-02-25 22:55:05 -050017 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
Brad Bishopd7bf8c12018-02-25 22:55:05 -050024 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
Brad Bishopd7bf8c12018-02-25 22:55:05 -050032 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
Brad Bishopd7bf8c12018-02-25 22:55:05 -050040 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
Brad Bishopd7bf8c12018-02-25 22:55:05 -050050 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
Brad Bishopd7bf8c12018-02-25 22:55:05 -050068 def test_parse_recipe_initial_datastore(self):
69 with bb.tinfoil.Tinfoil() as tinfoil:
70 tinfoil.prepare(config_only=False, quiet=2)
71 testrecipe = 'mdadm'
72 best = tinfoil.find_best_provider(testrecipe)
73 if not best:
74 self.fail('Unable to find recipe providing %s' % testrecipe)
75 dcopy = bb.data.createCopy(tinfoil.config_data)
76 dcopy.setVar('MYVARIABLE', 'somevalue')
77 rd = tinfoil.parse_recipe_file(best[3], config_data=dcopy)
78 # Check we can get variable values
79 self.assertEqual('somevalue', rd.getVar('MYVARIABLE'))
80
Brad Bishopd7bf8c12018-02-25 22:55:05 -050081 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
Brad Bishopd7bf8c12018-02-25 22:55:05 -050099 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'])
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('findFilesMatchingInDir', pattern, 'conf/machine')
111 self.assertTrue(res)
112
113 eventreceived = False
114 commandcomplete = False
115 start = time.time()
116 # Wait for 5s in total so we'd detect spurious heartbeat events for example
117 while time.time() - start < 5:
118 event = tinfoil.wait_event(1)
119 if event:
120 if isinstance(event, bb.command.CommandCompleted):
121 commandcomplete = True
122 elif isinstance(event, bb.event.FilesMatchingFound):
123 self.assertEqual(pattern, event._pattern)
124 self.assertIn('qemuarm.conf', event._matches)
125 eventreceived = True
126 elif isinstance(event, logging.LogRecord):
127 continue
128 else:
129 self.fail('Unexpected event: %s' % event)
130
131 self.assertTrue(commandcomplete, 'Timed out waiting for CommandCompleted event from bitbake server')
132 self.assertTrue(eventreceived, 'Did not receive FilesMatchingFound event from bitbake server')
133
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500134 def test_setvariable_clean(self):
135 # First check that setVariable affects the datastore
136 with bb.tinfoil.Tinfoil() as tinfoil:
137 tinfoil.prepare(config_only=True)
138 tinfoil.run_command('setVariable', 'TESTVAR', 'specialvalue')
139 self.assertEqual(tinfoil.config_data.getVar('TESTVAR'), 'specialvalue', 'Value set using setVariable is not reflected in client-side getVar()')
140
141 # Now check that the setVariable's effects are no longer present
142 # (this may legitimately break in future if we stop reinitialising
143 # the datastore, in which case we'll have to reconsider use of
144 # setVariable entirely)
145 with bb.tinfoil.Tinfoil() as tinfoil:
146 tinfoil.prepare(config_only=True)
147 self.assertNotEqual(tinfoil.config_data.getVar('TESTVAR'), 'specialvalue', 'Value set using setVariable is still present!')
148
149 # Now check that setVar on the main datastore works (uses setVariable internally)
150 with bb.tinfoil.Tinfoil() as tinfoil:
151 tinfoil.prepare(config_only=True)
152 tinfoil.config_data.setVar('TESTVAR', 'specialvalue')
153 value = tinfoil.run_command('getVariable', 'TESTVAR')
154 self.assertEqual(value, 'specialvalue', 'Value set using config_data.setVar() is not reflected in config_data.getVar()')
155
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500156 def test_datastore_operations(self):
157 with bb.tinfoil.Tinfoil() as tinfoil:
158 tinfoil.prepare(config_only=True)
159 # Test setVarFlag() / getVarFlag()
160 tinfoil.config_data.setVarFlag('TESTVAR', 'flagname', 'flagval')
161 value = tinfoil.config_data.getVarFlag('TESTVAR', 'flagname')
162 self.assertEqual(value, 'flagval', 'Value set using config_data.setVarFlag() is not reflected in config_data.getVarFlag()')
163 # Test delVarFlag()
164 tinfoil.config_data.setVarFlag('TESTVAR', 'otherflag', 'othervalue')
165 tinfoil.config_data.delVarFlag('TESTVAR', 'flagname')
166 value = tinfoil.config_data.getVarFlag('TESTVAR', 'flagname')
167 self.assertEqual(value, None, 'Varflag deleted using config_data.delVarFlag() is not reflected in config_data.getVarFlag()')
168 value = tinfoil.config_data.getVarFlag('TESTVAR', 'otherflag')
169 self.assertEqual(value, 'othervalue', 'Varflag deleted using config_data.delVarFlag() caused unrelated flag to be removed')
170 # Test delVar()
171 tinfoil.config_data.setVar('TESTVAR', 'varvalue')
172 value = tinfoil.config_data.getVar('TESTVAR')
173 self.assertEqual(value, 'varvalue', 'Value set using config_data.setVar() is not reflected in config_data.getVar()')
174 tinfoil.config_data.delVar('TESTVAR')
175 value = tinfoil.config_data.getVar('TESTVAR')
176 self.assertEqual(value, None, 'Variable deleted using config_data.delVar() appears to still have a value')
177 # Test renameVar()
178 tinfoil.config_data.setVar('TESTVAROLD', 'origvalue')
179 tinfoil.config_data.renameVar('TESTVAROLD', 'TESTVARNEW')
180 value = tinfoil.config_data.getVar('TESTVAROLD')
181 self.assertEqual(value, None, 'Variable renamed using config_data.renameVar() still seems to exist')
182 value = tinfoil.config_data.getVar('TESTVARNEW')
183 self.assertEqual(value, 'origvalue', 'Variable renamed using config_data.renameVar() does not appear with new name')
184 # Test overrides
185 tinfoil.config_data.setVar('TESTVAR', 'original')
186 tinfoil.config_data.setVar('TESTVAR_overrideone', 'one')
187 tinfoil.config_data.setVar('TESTVAR_overridetwo', 'two')
188 tinfoil.config_data.appendVar('OVERRIDES', ':overrideone')
189 value = tinfoil.config_data.getVar('TESTVAR')
190 self.assertEqual(value, 'one', 'Variable overrides not functioning correctly')
191
192 def test_variable_history(self):
193 # Basic test to ensure that variable history works when tracking=True
194 with bb.tinfoil.Tinfoil(tracking=True) as tinfoil:
195 tinfoil.prepare(config_only=False, quiet=2)
196 # Note that _tracking for any datastore we get will be
197 # false here, that's currently expected - so we can't check
198 # for that
199 history = tinfoil.config_data.varhistory.variable('DL_DIR')
200 for entry in history:
201 if entry['file'].endswith('/bitbake.conf'):
202 if entry['op'] in ['set', 'set?']:
203 break
204 else:
205 self.fail('Did not find history entry setting DL_DIR in bitbake.conf. History: %s' % history)
206 # Check it works for recipes as well
207 testrecipe = 'zlib'
208 rd = tinfoil.parse_recipe(testrecipe)
209 history = rd.varhistory.variable('LICENSE')
210 bbfound = -1
211 recipefound = -1
212 for i, entry in enumerate(history):
213 if entry['file'].endswith('/bitbake.conf'):
214 if entry['detail'] == 'INVALID' and entry['op'] in ['set', 'set?']:
215 bbfound = i
216 elif entry['file'].endswith('.bb'):
217 if entry['op'] == 'set':
218 recipefound = i
219 if bbfound == -1:
220 self.fail('Did not find history entry setting LICENSE in bitbake.conf parsing %s recipe. History: %s' % (testrecipe, history))
221 if recipefound == -1:
222 self.fail('Did not find history entry setting LICENSE in %s recipe. History: %s' % (testrecipe, history))
223 if bbfound > recipefound:
224 self.fail('History entry setting LICENSE in %s recipe and in bitbake.conf in wrong order. History: %s' % (testrecipe, history))