blob: a4a9dd30fb59d01a6e70e790a4200c90295245d7 [file] [log] [blame]
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001# ex:ts=4:sw=4:sts=4:et
2# -*- tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*-
3#
4# BitBake Tests for the Data Store (data.py/data_smart.py)
5#
6# Copyright (C) 2010 Chris Larson
7# Copyright (C) 2012 Richard Purdie
8#
9# This program is free software; you can redistribute it and/or modify
10# it under the terms of the GNU General Public License version 2 as
11# published by the Free Software Foundation.
12#
13# This program is distributed in the hope that it will be useful,
14# but WITHOUT ANY WARRANTY; without even the implied warranty of
15# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16# GNU General Public License for more details.
17#
18# You should have received a copy of the GNU General Public License along
19# with this program; if not, write to the Free Software Foundation, Inc.,
20# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21#
22
23import unittest
24import bb
25import bb.data
26import bb.parse
27import logging
28
29class LogRecord():
30 def __enter__(self):
31 logs = []
32 class LogHandler(logging.Handler):
33 def emit(self, record):
34 logs.append(record)
35 logger = logging.getLogger("BitBake")
36 handler = LogHandler()
37 self.handler = handler
38 logger.addHandler(handler)
39 return logs
40 def __exit__(self, type, value, traceback):
41 logger = logging.getLogger("BitBake")
42 logger.removeHandler(self.handler)
43 return
44
45def logContains(item, logs):
46 for l in logs:
47 m = l.getMessage()
48 if item in m:
49 return True
50 return False
51
52class DataExpansions(unittest.TestCase):
53 def setUp(self):
54 self.d = bb.data.init()
55 self.d["foo"] = "value_of_foo"
56 self.d["bar"] = "value_of_bar"
57 self.d["value_of_foo"] = "value_of_'value_of_foo'"
58
59 def test_one_var(self):
60 val = self.d.expand("${foo}")
61 self.assertEqual(str(val), "value_of_foo")
62
63 def test_indirect_one_var(self):
64 val = self.d.expand("${${foo}}")
65 self.assertEqual(str(val), "value_of_'value_of_foo'")
66
67 def test_indirect_and_another(self):
68 val = self.d.expand("${${foo}} ${bar}")
69 self.assertEqual(str(val), "value_of_'value_of_foo' value_of_bar")
70
71 def test_python_snippet(self):
72 val = self.d.expand("${@5*12}")
73 self.assertEqual(str(val), "60")
74
75 def test_expand_in_python_snippet(self):
76 val = self.d.expand("${@'boo ' + '${foo}'}")
77 self.assertEqual(str(val), "boo value_of_foo")
78
79 def test_python_snippet_getvar(self):
Brad Bishop6e60e8b2018-02-01 10:27:11 -050080 val = self.d.expand("${@d.getVar('foo') + ' ${bar}'}")
Patrick Williamsc124f4f2015-09-15 14:41:29 -050081 self.assertEqual(str(val), "value_of_foo value_of_bar")
82
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050083 def test_python_unexpanded(self):
84 self.d.setVar("bar", "${unsetvar}")
Brad Bishop6e60e8b2018-02-01 10:27:11 -050085 val = self.d.expand("${@d.getVar('foo') + ' ${bar}'}")
86 self.assertEqual(str(val), "${@d.getVar('foo') + ' ${unsetvar}'}")
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050087
Patrick Williamsc124f4f2015-09-15 14:41:29 -050088 def test_python_snippet_syntax_error(self):
89 self.d.setVar("FOO", "${@foo = 5}")
90 self.assertRaises(bb.data_smart.ExpansionError, self.d.getVar, "FOO", True)
91
92 def test_python_snippet_runtime_error(self):
93 self.d.setVar("FOO", "${@int('test')}")
94 self.assertRaises(bb.data_smart.ExpansionError, self.d.getVar, "FOO", True)
95
96 def test_python_snippet_error_path(self):
97 self.d.setVar("FOO", "foo value ${BAR}")
98 self.d.setVar("BAR", "bar value ${@int('test')}")
99 self.assertRaises(bb.data_smart.ExpansionError, self.d.getVar, "FOO", True)
100
101 def test_value_containing_value(self):
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500102 val = self.d.expand("${@d.getVar('foo') + ' ${bar}'}")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500103 self.assertEqual(str(val), "value_of_foo value_of_bar")
104
105 def test_reference_undefined_var(self):
106 val = self.d.expand("${undefinedvar} meh")
107 self.assertEqual(str(val), "${undefinedvar} meh")
108
109 def test_double_reference(self):
110 self.d.setVar("BAR", "bar value")
111 self.d.setVar("FOO", "${BAR} foo ${BAR}")
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500112 val = self.d.getVar("FOO")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500113 self.assertEqual(str(val), "bar value foo bar value")
114
115 def test_direct_recursion(self):
116 self.d.setVar("FOO", "${FOO}")
117 self.assertRaises(bb.data_smart.ExpansionError, self.d.getVar, "FOO", True)
118
119 def test_indirect_recursion(self):
120 self.d.setVar("FOO", "${BAR}")
121 self.d.setVar("BAR", "${BAZ}")
122 self.d.setVar("BAZ", "${FOO}")
123 self.assertRaises(bb.data_smart.ExpansionError, self.d.getVar, "FOO", True)
124
125 def test_recursion_exception(self):
126 self.d.setVar("FOO", "${BAR}")
127 self.d.setVar("BAR", "${${@'FOO'}}")
128 self.assertRaises(bb.data_smart.ExpansionError, self.d.getVar, "FOO", True)
129
130 def test_incomplete_varexp_single_quotes(self):
131 self.d.setVar("FOO", "sed -i -e 's:IP{:I${:g' $pc")
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500132 val = self.d.getVar("FOO")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500133 self.assertEqual(str(val), "sed -i -e 's:IP{:I${:g' $pc")
134
135 def test_nonstring(self):
136 self.d.setVar("TEST", 5)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500137 val = self.d.getVar("TEST")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500138 self.assertEqual(str(val), "5")
139
140 def test_rename(self):
141 self.d.renameVar("foo", "newfoo")
142 self.assertEqual(self.d.getVar("newfoo", False), "value_of_foo")
143 self.assertEqual(self.d.getVar("foo", False), None)
144
145 def test_deletion(self):
146 self.d.delVar("foo")
147 self.assertEqual(self.d.getVar("foo", False), None)
148
149 def test_keys(self):
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600150 keys = list(self.d.keys())
151 self.assertCountEqual(keys, ['value_of_foo', 'foo', 'bar'])
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500152
153 def test_keys_deletion(self):
154 newd = bb.data.createCopy(self.d)
155 newd.delVar("bar")
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600156 keys = list(newd.keys())
157 self.assertCountEqual(keys, ['value_of_foo', 'foo'])
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500158
159class TestNestedExpansions(unittest.TestCase):
160 def setUp(self):
161 self.d = bb.data.init()
162 self.d["foo"] = "foo"
163 self.d["bar"] = "bar"
164 self.d["value_of_foobar"] = "187"
165
166 def test_refs(self):
167 val = self.d.expand("${value_of_${foo}${bar}}")
168 self.assertEqual(str(val), "187")
169
170 #def test_python_refs(self):
171 # val = self.d.expand("${@${@3}**2 + ${@4}**2}")
172 # self.assertEqual(str(val), "25")
173
174 def test_ref_in_python_ref(self):
175 val = self.d.expand("${@'${foo}' + 'bar'}")
176 self.assertEqual(str(val), "foobar")
177
178 def test_python_ref_in_ref(self):
179 val = self.d.expand("${${@'f'+'o'+'o'}}")
180 self.assertEqual(str(val), "foo")
181
182 def test_deep_nesting(self):
183 depth = 100
184 val = self.d.expand("${" * depth + "foo" + "}" * depth)
185 self.assertEqual(str(val), "foo")
186
187 #def test_deep_python_nesting(self):
188 # depth = 50
189 # val = self.d.expand("${@" * depth + "1" + "+1}" * depth)
190 # self.assertEqual(str(val), str(depth + 1))
191
192 def test_mixed(self):
193 val = self.d.expand("${value_of_${@('${foo}'+'bar')[0:3]}${${@'BAR'.lower()}}}")
194 self.assertEqual(str(val), "187")
195
196 def test_runtime(self):
197 val = self.d.expand("${${@'value_of' + '_f'+'o'+'o'+'b'+'a'+'r'}}")
198 self.assertEqual(str(val), "187")
199
200class TestMemoize(unittest.TestCase):
201 def test_memoized(self):
202 d = bb.data.init()
203 d.setVar("FOO", "bar")
204 self.assertTrue(d.getVar("FOO", False) is d.getVar("FOO", False))
205
206 def test_not_memoized(self):
207 d1 = bb.data.init()
208 d2 = bb.data.init()
209 d1.setVar("FOO", "bar")
210 d2.setVar("FOO", "bar2")
211 self.assertTrue(d1.getVar("FOO", False) is not d2.getVar("FOO", False))
212
213 def test_changed_after_memoized(self):
214 d = bb.data.init()
215 d.setVar("foo", "value of foo")
216 self.assertEqual(str(d.getVar("foo", False)), "value of foo")
217 d.setVar("foo", "second value of foo")
218 self.assertEqual(str(d.getVar("foo", False)), "second value of foo")
219
220 def test_same_value(self):
221 d = bb.data.init()
222 d.setVar("foo", "value of")
223 d.setVar("bar", "value of")
224 self.assertEqual(d.getVar("foo", False),
225 d.getVar("bar", False))
226
227class TestConcat(unittest.TestCase):
228 def setUp(self):
229 self.d = bb.data.init()
230 self.d.setVar("FOO", "foo")
231 self.d.setVar("VAL", "val")
232 self.d.setVar("BAR", "bar")
233
234 def test_prepend(self):
235 self.d.setVar("TEST", "${VAL}")
236 self.d.prependVar("TEST", "${FOO}:")
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500237 self.assertEqual(self.d.getVar("TEST"), "foo:val")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500238
239 def test_append(self):
240 self.d.setVar("TEST", "${VAL}")
241 self.d.appendVar("TEST", ":${BAR}")
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500242 self.assertEqual(self.d.getVar("TEST"), "val:bar")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500243
244 def test_multiple_append(self):
245 self.d.setVar("TEST", "${VAL}")
246 self.d.prependVar("TEST", "${FOO}:")
247 self.d.appendVar("TEST", ":val2")
248 self.d.appendVar("TEST", ":${BAR}")
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500249 self.assertEqual(self.d.getVar("TEST"), "foo:val:val2:bar")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500250
251class TestConcatOverride(unittest.TestCase):
252 def setUp(self):
253 self.d = bb.data.init()
254 self.d.setVar("FOO", "foo")
255 self.d.setVar("VAL", "val")
256 self.d.setVar("BAR", "bar")
257
258 def test_prepend(self):
259 self.d.setVar("TEST", "${VAL}")
260 self.d.setVar("TEST_prepend", "${FOO}:")
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500261 self.assertEqual(self.d.getVar("TEST"), "foo:val")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500262
263 def test_append(self):
264 self.d.setVar("TEST", "${VAL}")
265 self.d.setVar("TEST_append", ":${BAR}")
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500266 self.assertEqual(self.d.getVar("TEST"), "val:bar")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500267
268 def test_multiple_append(self):
269 self.d.setVar("TEST", "${VAL}")
270 self.d.setVar("TEST_prepend", "${FOO}:")
271 self.d.setVar("TEST_append", ":val2")
272 self.d.setVar("TEST_append", ":${BAR}")
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500273 self.assertEqual(self.d.getVar("TEST"), "foo:val:val2:bar")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500274
275 def test_append_unset(self):
276 self.d.setVar("TEST_prepend", "${FOO}:")
277 self.d.setVar("TEST_append", ":val2")
278 self.d.setVar("TEST_append", ":${BAR}")
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500279 self.assertEqual(self.d.getVar("TEST"), "foo::val2:bar")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500280
281 def test_remove(self):
282 self.d.setVar("TEST", "${VAL} ${BAR}")
283 self.d.setVar("TEST_remove", "val")
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500284 self.assertEqual(self.d.getVar("TEST"), "bar")
285
286 def test_remove_cleared(self):
287 self.d.setVar("TEST", "${VAL} ${BAR}")
288 self.d.setVar("TEST_remove", "val")
289 self.d.setVar("TEST", "${VAL} ${BAR}")
290 self.assertEqual(self.d.getVar("TEST"), "val bar")
291
292 # Ensure the value is unchanged if we have an inactive remove override
293 # (including that whitespace is preserved)
294 def test_remove_inactive_override(self):
295 self.d.setVar("TEST", "${VAL} ${BAR} 123")
296 self.d.setVar("TEST_remove_inactiveoverride", "val")
297 self.assertEqual(self.d.getVar("TEST"), "val bar 123")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500298
299 def test_doubleref_remove(self):
300 self.d.setVar("TEST", "${VAL} ${BAR}")
301 self.d.setVar("TEST_remove", "val")
302 self.d.setVar("TEST_TEST", "${TEST} ${TEST}")
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500303 self.assertEqual(self.d.getVar("TEST_TEST"), "bar bar")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500304
305 def test_empty_remove(self):
306 self.d.setVar("TEST", "")
307 self.d.setVar("TEST_remove", "val")
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500308 self.assertEqual(self.d.getVar("TEST"), "")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500309
310 def test_remove_expansion(self):
311 self.d.setVar("BAR", "Z")
312 self.d.setVar("TEST", "${BAR}/X Y")
313 self.d.setVar("TEST_remove", "${BAR}/X")
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500314 self.assertEqual(self.d.getVar("TEST"), "Y")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500315
316 def test_remove_expansion_items(self):
317 self.d.setVar("TEST", "A B C D")
318 self.d.setVar("BAR", "B D")
319 self.d.setVar("TEST_remove", "${BAR}")
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500320 self.assertEqual(self.d.getVar("TEST"), "A C")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500321
322class TestOverrides(unittest.TestCase):
323 def setUp(self):
324 self.d = bb.data.init()
325 self.d.setVar("OVERRIDES", "foo:bar:local")
326 self.d.setVar("TEST", "testvalue")
327
328 def test_no_override(self):
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500329 self.assertEqual(self.d.getVar("TEST"), "testvalue")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500330
331 def test_one_override(self):
332 self.d.setVar("TEST_bar", "testvalue2")
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500333 self.assertEqual(self.d.getVar("TEST"), "testvalue2")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500334
335 def test_one_override_unset(self):
336 self.d.setVar("TEST2_bar", "testvalue2")
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500337
338 self.assertEqual(self.d.getVar("TEST2"), "testvalue2")
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600339 self.assertCountEqual(list(self.d.keys()), ['TEST', 'TEST2', 'OVERRIDES', 'TEST2_bar'])
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500340
341 def test_multiple_override(self):
342 self.d.setVar("TEST_bar", "testvalue2")
343 self.d.setVar("TEST_local", "testvalue3")
344 self.d.setVar("TEST_foo", "testvalue4")
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500345 self.assertEqual(self.d.getVar("TEST"), "testvalue3")
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600346 self.assertCountEqual(list(self.d.keys()), ['TEST', 'TEST_foo', 'OVERRIDES', 'TEST_bar', 'TEST_local'])
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500347
348 def test_multiple_combined_overrides(self):
349 self.d.setVar("TEST_local_foo_bar", "testvalue3")
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500350 self.assertEqual(self.d.getVar("TEST"), "testvalue3")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500351
352 def test_multiple_overrides_unset(self):
353 self.d.setVar("TEST2_local_foo_bar", "testvalue3")
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500354 self.assertEqual(self.d.getVar("TEST2"), "testvalue3")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500355
356 def test_keyexpansion_override(self):
357 self.d.setVar("LOCAL", "local")
358 self.d.setVar("TEST_bar", "testvalue2")
359 self.d.setVar("TEST_${LOCAL}", "testvalue3")
360 self.d.setVar("TEST_foo", "testvalue4")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500361 bb.data.expandKeys(self.d)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500362 self.assertEqual(self.d.getVar("TEST"), "testvalue3")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500363
364 def test_rename_override(self):
365 self.d.setVar("ALTERNATIVE_ncurses-tools_class-target", "a")
366 self.d.setVar("OVERRIDES", "class-target")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500367 self.d.renameVar("ALTERNATIVE_ncurses-tools", "ALTERNATIVE_lib32-ncurses-tools")
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500368 self.assertEqual(self.d.getVar("ALTERNATIVE_lib32-ncurses-tools"), "a")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500369
370 def test_underscore_override(self):
371 self.d.setVar("TEST_bar", "testvalue2")
372 self.d.setVar("TEST_some_val", "testvalue3")
373 self.d.setVar("TEST_foo", "testvalue4")
374 self.d.setVar("OVERRIDES", "foo:bar:some_val")
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500375 self.assertEqual(self.d.getVar("TEST"), "testvalue3")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500376
377class TestKeyExpansion(unittest.TestCase):
378 def setUp(self):
379 self.d = bb.data.init()
380 self.d.setVar("FOO", "foo")
381 self.d.setVar("BAR", "foo")
382
383 def test_keyexpand(self):
384 self.d.setVar("VAL_${FOO}", "A")
385 self.d.setVar("VAL_${BAR}", "B")
386 with LogRecord() as logs:
387 bb.data.expandKeys(self.d)
388 self.assertTrue(logContains("Variable key VAL_${FOO} (A) replaces original key VAL_foo (B)", logs))
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500389 self.assertEqual(self.d.getVar("VAL_foo"), "A")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500390
391class TestFlags(unittest.TestCase):
392 def setUp(self):
393 self.d = bb.data.init()
394 self.d.setVar("foo", "value of foo")
395 self.d.setVarFlag("foo", "flag1", "value of flag1")
396 self.d.setVarFlag("foo", "flag2", "value of flag2")
397
398 def test_setflag(self):
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500399 self.assertEqual(self.d.getVarFlag("foo", "flag1", False), "value of flag1")
400 self.assertEqual(self.d.getVarFlag("foo", "flag2", False), "value of flag2")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500401
402 def test_delflag(self):
403 self.d.delVarFlag("foo", "flag2")
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500404 self.assertEqual(self.d.getVarFlag("foo", "flag1", False), "value of flag1")
405 self.assertEqual(self.d.getVarFlag("foo", "flag2", False), None)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500406
407
408class Contains(unittest.TestCase):
409 def setUp(self):
410 self.d = bb.data.init()
411 self.d.setVar("SOMEFLAG", "a b c")
412
413 def test_contains(self):
414 self.assertTrue(bb.utils.contains("SOMEFLAG", "a", True, False, self.d))
415 self.assertTrue(bb.utils.contains("SOMEFLAG", "b", True, False, self.d))
416 self.assertTrue(bb.utils.contains("SOMEFLAG", "c", True, False, self.d))
417
418 self.assertTrue(bb.utils.contains("SOMEFLAG", "a b", True, False, self.d))
419 self.assertTrue(bb.utils.contains("SOMEFLAG", "b c", True, False, self.d))
420 self.assertTrue(bb.utils.contains("SOMEFLAG", "c a", True, False, self.d))
421
422 self.assertTrue(bb.utils.contains("SOMEFLAG", "a b c", True, False, self.d))
423 self.assertTrue(bb.utils.contains("SOMEFLAG", "c b a", True, False, self.d))
424
425 self.assertFalse(bb.utils.contains("SOMEFLAG", "x", True, False, self.d))
426 self.assertFalse(bb.utils.contains("SOMEFLAG", "a x", True, False, self.d))
427 self.assertFalse(bb.utils.contains("SOMEFLAG", "x c b", True, False, self.d))
428 self.assertFalse(bb.utils.contains("SOMEFLAG", "x c b a", True, False, self.d))
429
430 def test_contains_any(self):
431 self.assertTrue(bb.utils.contains_any("SOMEFLAG", "a", True, False, self.d))
432 self.assertTrue(bb.utils.contains_any("SOMEFLAG", "b", True, False, self.d))
433 self.assertTrue(bb.utils.contains_any("SOMEFLAG", "c", True, False, self.d))
434
435 self.assertTrue(bb.utils.contains_any("SOMEFLAG", "a b", True, False, self.d))
436 self.assertTrue(bb.utils.contains_any("SOMEFLAG", "b c", True, False, self.d))
437 self.assertTrue(bb.utils.contains_any("SOMEFLAG", "c a", True, False, self.d))
438
439 self.assertTrue(bb.utils.contains_any("SOMEFLAG", "a x", True, False, self.d))
440 self.assertTrue(bb.utils.contains_any("SOMEFLAG", "x c", True, False, self.d))
441
442 self.assertFalse(bb.utils.contains_any("SOMEFLAG", "x", True, False, self.d))
443 self.assertFalse(bb.utils.contains_any("SOMEFLAG", "x y z", True, False, self.d))
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500444
445
446class Serialize(unittest.TestCase):
447
448 def test_serialize(self):
449 import tempfile
450 import pickle
451 d = bb.data.init()
452 d.enableTracking()
453 d.setVar('HELLO', 'world')
454 d.setVarFlag('HELLO', 'other', 'planet')
455 with tempfile.NamedTemporaryFile(delete=False) as tmpfile:
456 tmpfilename = tmpfile.name
457 pickle.dump(d, tmpfile)
458
459 with open(tmpfilename, 'rb') as f:
460 newd = pickle.load(f)
461
462 os.remove(tmpfilename)
463
464 self.assertEqual(d, newd)
465 self.assertEqual(newd.getVar('HELLO'), 'world')
466 self.assertEqual(newd.getVarFlag('HELLO', 'other'), 'planet')
467
468
469# Remote datastore tests
470# These really only test the interface, since in actual usage we have a
471# tinfoil connector that does everything over RPC, and this doesn't test
472# that.
473
474class TestConnector:
475 d = None
476 def __init__(self, d):
477 self.d = d
478 def getVar(self, name):
479 return self.d._findVar(name)
480 def getKeys(self):
481 return set(self.d.keys())
482 def getVarHistory(self, name):
483 return self.d.varhistory.variable(name)
484 def expandPythonRef(self, varname, expr, d):
485 localdata = self.d.createCopy()
486 for key in d.localkeys():
487 localdata.setVar(d.getVar(key))
488 varparse = bb.data_smart.VariableParse(varname, localdata)
489 return varparse.python_sub(expr)
490 def setVar(self, name, value):
491 self.d.setVar(name, value)
492 def setVarFlag(self, name, flag, value):
493 self.d.setVarFlag(name, flag, value)
494 def delVar(self, name):
495 self.d.delVar(name)
496 return False
497 def delVarFlag(self, name, flag):
498 self.d.delVarFlag(name, flag)
499 return False
500 def renameVar(self, name, newname):
501 self.d.renameVar(name, newname)
502 return False
503
504class Remote(unittest.TestCase):
505 def test_remote(self):
506
507 d1 = bb.data.init()
508 d1.enableTracking()
509 d2 = bb.data.init()
510 d2.enableTracking()
511 connector = TestConnector(d1)
512
513 d2.setVar('_remote_data', connector)
514
515 d1.setVar('HELLO', 'world')
516 d1.setVarFlag('OTHER', 'flagname', 'flagvalue')
517 self.assertEqual(d2.getVar('HELLO'), 'world')
518 self.assertEqual(d2.expand('${HELLO}'), 'world')
519 self.assertEqual(d2.expand('${@d.getVar("HELLO")}'), 'world')
520 self.assertIn('flagname', d2.getVarFlags('OTHER'))
521 self.assertEqual(d2.getVarFlag('OTHER', 'flagname'), 'flagvalue')
522 self.assertEqual(d1.varhistory.variable('HELLO'), d2.varhistory.variable('HELLO'))
523 # Test setVar on client side affects server
524 d2.setVar('HELLO', 'other-world')
525 self.assertEqual(d1.getVar('HELLO'), 'other-world')
526 # Test setVarFlag on client side affects server
527 d2.setVarFlag('HELLO', 'flagname', 'flagvalue')
528 self.assertEqual(d1.getVarFlag('HELLO', 'flagname'), 'flagvalue')
529 # Test client side data is incorporated in python expansion (which is done on server)
530 d2.setVar('FOO', 'bar')
531 self.assertEqual(d2.expand('${@d.getVar("FOO")}'), 'bar')
532 # Test overrides work
533 d1.setVar('FOO_test', 'baz')
534 d1.appendVar('OVERRIDES', ':test')
535 self.assertEqual(d2.getVar('FOO'), 'baz')
536
537
538# Remote equivalents of local test classes
539# Note that these aren't perfect since we only test in one direction
540
541class RemoteDataExpansions(DataExpansions):
542 def setUp(self):
543 self.d1 = bb.data.init()
544 self.d = bb.data.init()
545 self.d1["foo"] = "value_of_foo"
546 self.d1["bar"] = "value_of_bar"
547 self.d1["value_of_foo"] = "value_of_'value_of_foo'"
548 connector = TestConnector(self.d1)
549 self.d.setVar('_remote_data', connector)
550
551class TestRemoteNestedExpansions(TestNestedExpansions):
552 def setUp(self):
553 self.d1 = bb.data.init()
554 self.d = bb.data.init()
555 self.d1["foo"] = "foo"
556 self.d1["bar"] = "bar"
557 self.d1["value_of_foobar"] = "187"
558 connector = TestConnector(self.d1)
559 self.d.setVar('_remote_data', connector)
560
561class TestRemoteConcat(TestConcat):
562 def setUp(self):
563 self.d1 = bb.data.init()
564 self.d = bb.data.init()
565 self.d1.setVar("FOO", "foo")
566 self.d1.setVar("VAL", "val")
567 self.d1.setVar("BAR", "bar")
568 connector = TestConnector(self.d1)
569 self.d.setVar('_remote_data', connector)
570
571class TestRemoteConcatOverride(TestConcatOverride):
572 def setUp(self):
573 self.d1 = bb.data.init()
574 self.d = bb.data.init()
575 self.d1.setVar("FOO", "foo")
576 self.d1.setVar("VAL", "val")
577 self.d1.setVar("BAR", "bar")
578 connector = TestConnector(self.d1)
579 self.d.setVar('_remote_data', connector)
580
581class TestRemoteOverrides(TestOverrides):
582 def setUp(self):
583 self.d1 = bb.data.init()
584 self.d = bb.data.init()
585 self.d1.setVar("OVERRIDES", "foo:bar:local")
586 self.d1.setVar("TEST", "testvalue")
587 connector = TestConnector(self.d1)
588 self.d.setVar('_remote_data', connector)
589
590class TestRemoteKeyExpansion(TestKeyExpansion):
591 def setUp(self):
592 self.d1 = bb.data.init()
593 self.d = bb.data.init()
594 self.d1.setVar("FOO", "foo")
595 self.d1.setVar("BAR", "foo")
596 connector = TestConnector(self.d1)
597 self.d.setVar('_remote_data', connector)
598
599class TestRemoteFlags(TestFlags):
600 def setUp(self):
601 self.d1 = bb.data.init()
602 self.d = bb.data.init()
603 self.d1.setVar("foo", "value of foo")
604 self.d1.setVarFlag("foo", "flag1", "value of flag1")
605 self.d1.setVarFlag("foo", "flag2", "value of flag2")
606 connector = TestConnector(self.d1)
607 self.d.setVar('_remote_data', connector)