-
Notifications
You must be signed in to change notification settings - Fork 27
/
test_bundles.py
469 lines (361 loc) · 15.5 KB
/
test_bundles.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
#!/usr/bin/env python
# -- Content-Encoding: UTF-8 --
"""
Tests the bundles handling.
:author: Thomas Calmant
"""
# Tests
from tests import log_on, log_off
# Pelix
from pelix.framework import FrameworkFactory, Bundle, BundleException, \
BundleContext
# Standard library
import os
try:
import unittest2 as unittest
except ImportError:
import unittest
# ------------------------------------------------------------------------------
__version__ = "1.0.0"
SERVICE_BUNDLE = "tests.framework.service_bundle"
SIMPLE_BUNDLE = "tests.framework.simple_bundle"
# ------------------------------------------------------------------------------
class BundlesTest(unittest.TestCase):
"""
Pelix bundle registry tests
"""
def setUp(self):
"""
Called before each test. Initiates a framework.
"""
self.framework = FrameworkFactory.get_framework()
self.framework.start()
self.context = self.framework.get_bundle_context()
self.test_bundle_name = SIMPLE_BUNDLE
# File path, without extension
self.test_bundle_loc = os.path.join(
os.path.dirname(__file__), self.test_bundle_name.rsplit('.', 1)[1])
def tearDown(self):
"""
Called after each test
"""
self.framework.stop()
FrameworkFactory.delete_framework()
def testImportError(self):
"""
Tries to install an invalid bundle
"""
# Try to install the bundle
self.assertRaises(BundleException, self.context.install_bundle,
"//Invalid Name\\\\")
def testCompatibility(self, test_bundle_id=False):
"""
Tests a bundle installation + start + stop
@param test_bundle_id: If True, also tests if the test bundle ID is 1
"""
# Install the bundle
bundle_id = self.context.install_bundle(self.test_bundle_name)
bundle = self.context.get_bundle(bundle_id)
assert isinstance(bundle, Bundle)
if test_bundle_id:
self.assertEqual(bundle.get_bundle_id(), 1,
"Not the first bundle in framework")
# Get the internal module
module_ = bundle.get_module()
# Assert initial state
self.assertFalse(module_.started, "Bundle should not be started yet")
self.assertFalse(module_.stopped, "Bundle should not be stopped yet")
# Activator
bundle.start()
self.assertTrue(module_.started, "Bundle should be started now")
self.assertFalse(module_.stopped, "Bundle should not be stopped yet")
# De-activate
bundle.stop()
self.assertTrue(module_.started, "Bundle should be changed")
self.assertTrue(module_.stopped, "Bundle should be stopped now")
# Uninstall (validated in another test)
bundle.uninstall()
def testLifeCycle(self, test_bundle_id=False):
"""
Tests a bundle installation + start + stop
@param test_bundle_id: If True, also tests if the test bundle ID is 1
"""
# Install the bundle
bundle = self.context.install_bundle(self.test_bundle_name)
assert isinstance(bundle, Bundle)
if test_bundle_id:
self.assertEqual(bundle.get_bundle_id(), 1,
"Not the first bundle in framework")
# Get the internal module
module_ = bundle.get_module()
# Assert initial state
self.assertFalse(module_.started, "Bundle should not be started yet")
self.assertFalse(module_.stopped, "Bundle should not be stopped yet")
# Activator
bundle.start()
self.assertTrue(module_.started, "Bundle should be started now")
self.assertFalse(module_.stopped, "Bundle should not be stopped yet")
# De-activate
bundle.stop()
self.assertTrue(module_.started, "Bundle should be changed")
self.assertTrue(module_.stopped, "Bundle should be stopped now")
# Uninstall (validated in another test)
bundle.uninstall()
def testLifeCycleRecalls(self):
"""
Tests a bundle installation + start + stop
"""
# Install the bundle
bundle = self.context.install_bundle(self.test_bundle_name)
assert isinstance(bundle, Bundle)
# Get the internal module
module_ = bundle.get_module()
# Assert initial state
self.assertFalse(module_.started, "Bundle should not be started yet")
self.assertFalse(module_.stopped, "Bundle should not be stopped yet")
# Activator
bundle.start()
self.assertEqual(bundle.get_state(), Bundle.ACTIVE,
"Bundle should be considered active")
self.assertTrue(module_.started, "Bundle should be started now")
self.assertFalse(module_.stopped, "Bundle should not be stopped yet")
# Recall activator
module_.started = False
bundle.start()
self.assertFalse(module_.started, "Bundle shouldn't be started twice")
# Reset to previous state
module_.started = True
# De-activate
bundle.stop()
self.assertNotEqual(bundle.get_state(), Bundle.ACTIVE,
"Bundle shouldn't be considered active")
self.assertTrue(module_.started, "Bundle should be changed")
self.assertTrue(module_.stopped, "Bundle should be stopped now")
# Recall activator
module_.stopped = False
bundle.stop()
self.assertFalse(module_.stopped, "Bundle shouldn't be stopped twice")
# Uninstall (validated in another test)
bundle.uninstall()
self.assertEqual(bundle.get_state(), Bundle.UNINSTALLED,
"Bundle should be considered uninstalled")
def testLifeCycleExceptions(self):
"""
Tests a bundle installation + start + stop
"""
# Install the bundle
bundle = self.context.install_bundle(self.test_bundle_name)
assert isinstance(bundle, Bundle)
# Get the internal module
module_ = bundle.get_module()
# Assert initial state
self.assertFalse(module_.started, "Bundle should not be started yet")
self.assertFalse(module_.stopped, "Bundle should not be stopped yet")
# Activator with exception
module_.raiser = True
log_off()
self.assertRaises(BundleException, bundle.start)
log_on()
# Assert post-exception state
self.assertNotEqual(bundle.get_state(), Bundle.ACTIVE,
"Bundle shouldn't be considered active")
self.assertFalse(module_.started, "Bundle should not be started yet")
self.assertFalse(module_.stopped, "Bundle should not be stopped yet")
# Activator, without exception
module_.raiser = False
bundle.start()
self.assertEqual(bundle.get_state(), Bundle.ACTIVE,
"Bundle should be considered active")
self.assertTrue(module_.started, "Bundle should be started now")
self.assertFalse(module_.stopped, "Bundle should not be stopped yet")
# De-activate with exception
module_.raiser = True
log_off()
self.assertRaises(BundleException, bundle.stop)
log_on()
self.assertNotEqual(bundle.get_state(), Bundle.ACTIVE,
"Bundle shouldn't be considered active")
self.assertTrue(module_.started, "Bundle should be changed")
self.assertFalse(module_.stopped, "Bundle should be stopped now")
# Uninstall (validated in another test)
bundle.uninstall()
def testUninstallInstall(self):
"""
Runs the life-cycle test twice.
The bundle is installed then un-installed twice. started and stopped
values of the bundle should be reset to False.
Keeping two separate calls instead of using a loop allows to see at
which pass the test have failed
"""
# Pass 1: normal test
self.testLifeCycle(True)
# Pass 2: refresh test
self.testLifeCycle(False)
def testUninstallWithStartStop(self):
"""
Tests if a bundle is correctly uninstalled and if it is really
unaccessible after its uninstallation.
"""
# Install the bundle
bundle = self.context.install_bundle(self.test_bundle_name)
assert isinstance(bundle, Bundle)
bid = bundle.get_bundle_id()
self.assertEqual(bid, 1, "Invalid first bundle ID '{0:d}'".format(bid))
# Test state
self.assertEqual(bundle.get_state(), Bundle.RESOLVED,
"Invalid fresh install state {0:d}"
.format(bundle.get_state()))
# Start
bundle.start()
self.assertEqual(bundle.get_state(), Bundle.ACTIVE,
"Invalid fresh start state {0:d}"
.format(bundle.get_state()))
# Stop
bundle.stop()
self.assertEqual(bundle.get_state(), Bundle.RESOLVED,
"Invalid fresh stop state {0:d}"
.format(bundle.get_state()))
# Uninstall
bundle.uninstall()
self.assertEqual(bundle.get_state(), Bundle.UNINSTALLED,
"Invalid fresh stop state {0:d}"
.format(bundle.get_state()))
# The bundle must not be accessible through the framework
self.assertRaises(BundleException, self.context.get_bundle, bid)
self.assertRaises(
BundleException, self.framework.get_bundle_by_id, bid)
found_bundle = self.framework.get_bundle_by_name(self.test_bundle_name)
self.assertIsNone(found_bundle, "Bundle is still accessible by name "
"through the framework")
def testUpdate(self):
"""
Tests a bundle update
"""
bundle_content = """#!/usr/bin/python
# -- Content-Encoding: UTF-8 --
# Auto-generated bundle, for Pelix tests
__version__ = "{version}"
test_var = {test}
def test_fct():
return {test}
"""
# Compute the bundle full path
simple_name = "generated_bundle"
bundle_name = '{0}.{1}'.format(
__name__.rsplit('.', 1)[0], simple_name)
bundle_fullname = os.path.join(
os.path.dirname(__file__), "{0}.py".format(simple_name))
# 0/ Clean up existing files
for suffix in ('', 'c', 'o'):
path = "{0}{1}".format(bundle_fullname, suffix)
if os.path.exists(path):
os.remove(path)
# 1/ Prepare the bundle, test variable is set to False
with open(bundle_fullname, "w") as f:
f.write(bundle_content.format(version="1.0.0", test=False))
# 2/ Install the bundle and get its variable
bundle = self.context.install_bundle(bundle_name)
module_ = bundle.get_module()
# Also start the bundle
bundle.start()
self.assertFalse(module_.test_var, "Test variable should be False")
# 3/ Change the bundle file
with open(bundle_fullname, "w") as f:
f.write(bundle_content.format(version="1.0.1", test=True))
# 4/ Update, keeping the module reference
bundle.update()
self.assertIs(module_, bundle.get_module(), "Module has changed")
self.assertTrue(module_.test_var, "Test variable should be True")
# 5/ Change the bundle file, make it erroneous
with open(bundle_fullname, "w") as f:
f.write(bundle_content.format(version="1.0.2", test="\n"))
# No error must be raised...
log_off()
bundle.update()
log_on()
# ... but the state of the module shouldn't have changed
self.assertTrue(module_.test_var, "Test variable should still be True")
# Finally, change the test file to be a valid module
# -> Used by coverage for its report
with open(bundle_fullname, "w") as f:
f.write(bundle_content.format(version="1.0.0", test=False))
def testVersion(self):
"""
Tests if the version is correctly read from the bundle
"""
# Install the bundle
bundle = self.framework.install_bundle(self.test_bundle_name)
assert isinstance(bundle, Bundle)
bid = bundle.get_bundle_id()
self.assertEqual(bid, 1, "Invalid first bundle ID '{0:d}'".format(bid))
# Get the internal module
module_ = bundle.get_module()
# Validate the bundle name
self.assertEqual(bundle.get_symbolic_name(), self.test_bundle_name,
"Names are different ({0} / {1})"
.format(bundle.get_symbolic_name(),
self.test_bundle_name))
# Validate get_location()
bundle_without_ext = os.path.splitext(bundle.get_location())[0]
full_bundle_path = os.path.abspath(bundle_without_ext)
self.assertIn(self.test_bundle_loc,
(bundle_without_ext, full_bundle_path))
# Validate the version number
self.assertEqual(bundle.get_version(), module_.__version__,
"Different versions found ({0} / {1})"
.format(bundle.get_version(), module_.__version__))
# Remove the bundle
bundle.uninstall()
# ------------------------------------------------------------------------------
class LocalBundleTest(unittest.TestCase):
"""
Tests the installation of the __main__ bundle
"""
def setUp(self):
"""
Called before each test. Initiates a framework.
"""
self.framework = FrameworkFactory.get_framework()
self.framework.start()
def tearDown(self):
"""
Called after each test
"""
self.framework.stop()
FrameworkFactory.delete_framework()
def testLocalBundle(self):
"""
Tests the correctness of the __main__ bundle objects in the framework
"""
fw_context = self.framework.get_bundle_context()
assert isinstance(fw_context, BundleContext)
# Install local bundle in framework (for service installation & co)
bundle = fw_context.install_bundle(__name__)
# Get a reference to the bundle, by name
bundle_2 = fw_context.get_framework().get_bundle_by_name(__name__)
self.assertIs(bundle, bundle_2,
"Different bundle returned by ID and by name")
# Validate the symbolic name
self.assertEqual(bundle.get_symbolic_name(), __name__,
"Bundle ({0}) and module ({1}) are different"
.format(bundle.get_symbolic_name(), __name__))
# Validate get_bundle() via bundle context
context_bundle = bundle.get_bundle_context().get_bundle()
self.assertIs(bundle, context_bundle,
"Not the same bundle:\n{0:d} / {1}\n{2:d} / {3}"
.format(id(bundle), bundle,
id(context_bundle), context_bundle))
# Validate get_version()
self.assertEqual(bundle.get_version(), __version__,
"Not the same version {0} -> {1}"
.format(__version__, bundle.get_version()))
# Validate get_location()
self.assertEqual(bundle.get_location(), __file__,
"Not the same location {0} -> {1}"
.format(__file__, bundle.get_location()))
# ------------------------------------------------------------------------------
if __name__ == "__main__":
# Set logging level
import logging
logging.basicConfig(level=logging.DEBUG)
unittest.main()