Example #1
0
 def tearDown(self):
     """
     Cleans up the test environment
     """
     # Stop the framework
     FrameworkFactory.delete_framework()
     self.framework = None
Example #2
0
    def testBundleStart(self):
        """
        Tests if a bundle can be started before the framework itself
        """
        framework = FrameworkFactory.get_framework()
        context = framework.get_bundle_context()
        assert isinstance(context, BundleContext)

        # Install a bundle
        bundle = context.install_bundle(SIMPLE_BUNDLE)

        self.assertEqual(bundle.get_state(), Bundle.RESOLVED, "Bundle should be in RESOLVED state")

        # Starting the bundle now should fail
        self.assertRaises(BundleException, bundle.start)
        self.assertEqual(bundle.get_state(), Bundle.RESOLVED, "Bundle should be in RESOLVED state")

        # Start the framework
        framework.start()

        # Bundle should have been started now
        self.assertEqual(bundle.get_state(), Bundle.ACTIVE, "Bundle should be in ACTIVE state")

        # Stop the framework
        framework.stop()

        self.assertEqual(bundle.get_state(), Bundle.RESOLVED, "Bundle should be in RESOLVED state")

        # Try to start the bundle again (must fail)
        self.assertRaises(BundleException, bundle.start)
        self.assertEqual(bundle.get_state(), Bundle.RESOLVED, "Bundle should be in RESOLVED state")

        FrameworkFactory.delete_framework()
Example #3
0
    def testWaitForStopTimeout(self):
        """
        Tests the wait_for_stop() method
        """
        # Set up a framework
        framework = FrameworkFactory.get_framework()
        framework.start()

        # Start the framework killer
        threading.Thread(target=_framework_killer, args=(framework, 0.5)).start()

        # Wait for stop (timeout not raised)
        start = time.time()
        self.assertTrue(framework.wait_for_stop(1), "wait_for_stop() should return True")
        end = time.time()
        self.assertLess(end - start, 1, "Wait should be less than 1 sec")

        # Restart framework
        framework.start()

        # Start the framework killer
        threading.Thread(target=_framework_killer, args=(framework, 2)).start()

        # Wait for stop (timeout raised)
        start = time.time()
        self.assertFalse(framework.wait_for_stop(1), "wait_for_stop() should return False")
        end = time.time()
        self.assertLess(end - start, 1.2, "Wait should be less than 1.2 sec")

        # Wait for framework to really stop
        framework.wait_for_stop()

        FrameworkFactory.delete_framework()
 def tearDown(self):
     """
     Called after each test
     """
     if self.framework is not None:
         FrameworkFactory.delete_framework(self.framework)
         self.framework = None
    def testAddedProperty(self):
        """
        Tests the add_property method
        """
        pelix_test_name = "PELIX_TEST"
        pelix_test = "42"
        pelix_test_2 = "123"

        # Test without pre-set properties
        framework = FrameworkFactory.get_framework()

        self.assertIsNone(framework.get_property(pelix_test_name),
                          "Magic property value")

        # Add the property
        self.assertTrue(framework.add_property(pelix_test_name, pelix_test),
                        "add_property shouldn't fail on first call")

        self.assertEqual(framework.get_property(pelix_test_name), pelix_test,
                         "Invalid property value")

        # Update the property (must fail)
        self.assertFalse(framework.add_property(pelix_test_name, pelix_test_2),
                         "add_property must fail on second call")

        self.assertEqual(framework.get_property(pelix_test_name), pelix_test,
                         "Invalid property value")

        FrameworkFactory.delete_framework(framework)
    def testFrameworkRestart(self):
        """
        Tests call to Framework.update(), that restarts the framework
        """
        framework = FrameworkFactory.get_framework()
        context = framework.get_bundle_context()

        # Register the stop listener
        context.add_framework_stop_listener(self)

        # Calling update while the framework is stopped should do nothing
        framework.update()
        self.assertFalse(self.stopping, "Stop listener called")

        # Start and update the framework
        self.assertTrue(framework.start(), "Framework couldn't be started")
        framework.update()

        # The framework must have been stopped and must be active
        self.assertTrue(self.stopping, "Stop listener not called")
        self.assertEqual(framework.get_state(), Bundle.ACTIVE,
                         "Framework hasn't been restarted")

        framework.stop()
        FrameworkFactory.delete_framework(framework)
Example #7
0
 def tearDown(self):
     """
     Called after each test
     """
     self.framework.stop()
     FrameworkFactory.delete_framework()
     self.framework = None
    def testCreateFrameworkAutoStart(self):
        """
        Tests create_framework(), with specified bundles and auto-start
        """
        # Without bundles
        self.framework = pelix.create_framework([], auto_start=True)
        self.assertEqual(self.framework.get_state(), pelix.Bundle.ACTIVE,
                         "Framework hasn't been started")
        self.assertEqual(self.framework.get_bundles(), [],
                         'Framework is not empty')
        # Clean up
        FrameworkFactory.delete_framework(self.framework)

        # With bundles
        self.framework = pelix.create_framework([self.test_bundle_name],
                                                auto_start=True)
        self.assertEqual(self.framework.get_state(), pelix.Bundle.ACTIVE,
                         "Framework hasn't been started")
        self.assertEqual(len(self.framework.get_bundles()), 1,
                         'Framework should only have 1 bundle')

        bundle = self.framework.get_bundle_by_id(1)
        self.assertEqual(bundle.get_symbolic_name(), self.test_bundle_name,
                         "The test bundle hasn't been installed correctly")
        self.assertEqual(bundle.get_state(), pelix.Bundle.ACTIVE,
                         "Bundle hasn't been started")
    def testWaitForStop(self):
        """
        Tests the wait_for_stop() method
        """
        # Set up a framework
        framework = FrameworkFactory.get_framework()

        # No need to wait for the framework...
        self.assertTrue(framework.wait_for_stop(),
                        "wait_for_stop() must return True "
                        "on stopped framework")

        # Start the framework
        framework.start()

        # Start the framework killer
        threading.Thread(target=_framework_killer,
                         args=(framework, 0.5)).start()

        # Wait for stop
        start = time.time()
        self.assertTrue(framework.wait_for_stop(),
                        "wait_for_stop(None) should return True")
        end = time.time()
        self.assertLess(end - start, 1, "Wait should be less than 1 sec")

        FrameworkFactory.delete_framework(framework)
Example #10
0
 def tearDown(self):
     """
     Called after each test
     """
     # Destroy the framework
     FrameworkFactory.delete_framework()
     self.framework = None
     self.waiting = None
Example #11
0
 def tearDown(self):
     """
     Cleans up the framework
     """
     self.framework.stop()
     FrameworkFactory.delete_framework()
     self.remote = None
     self.shell = None
     self.framework = None
Example #12
0
 def tearDown(self):
     """
     Cleans up the framework
     """
     self.framework.stop()
     FrameworkFactory.delete_framework()
     self.utility = None
     self.context = None
     self.framework = None
Example #13
0
    def tearDown(self):
        """
        Called after each test
        """
        self.framework.stop()
        FrameworkFactory.delete_framework()

        # Reset the environment variable
        os.environ['bundle.import.fail'] = "0"
Example #14
0
 def tearDown(self):
     """
     Cleans up the framework
     """
     self.framework.stop()
     FrameworkFactory.delete_framework()
     self.shell = None
     self.context = None
     self.framework = None
     self._flag = False
    def tearDown(self):
        """
        Called after each test
        """
        self.framework.stop()
        FrameworkFactory.delete_framework(self.framework)

        # Clean up
        self.ipopo = None
        self.framework = None
Example #16
0
    def tearDown(self):
        """
        Called after each test
        """
        self.framework.stop()
        FrameworkFactory.delete_framework()

        # Clean up
        self.ipopo = None
        self.framework = None
Example #17
0
 def tearDown(self):
     """
     Cleans up the framework
     """
     self.framework.stop()
     FrameworkFactory.delete_framework()
     self.shell = None
     self.context = None
     self.framework = None
     self._flag = False
    def testFrameworkStopper(self):
        """
        Tests FrameworkException stop flag handling
        """
        framework = FrameworkFactory.get_framework()
        context = framework.get_bundle_context()

        # Install the bundle
        bundle = context.install_bundle(SIMPLE_BUNDLE)
        module = bundle.get_module()

        # Set module in raiser stop mode
        module.fw_raiser = True
        module.fw_raiser_stop = True

        log_off()
        self.assertFalse(framework.start(), "Framework should be stopped")
        self.assertEqual(framework.get_state(), Bundle.RESOLVED,
                         "Framework should be stopped")
        self.assertEqual(bundle.get_state(), Bundle.RESOLVED,
                         "Bundle should be stopped")
        log_on()

        # Set module in raiser non-stop mode
        module.fw_raiser_stop = False

        log_off()
        self.assertTrue(framework.start(), "Framework should be stopped")
        self.assertEqual(framework.get_state(), Bundle.ACTIVE,
                         "Framework should be started")
        self.assertEqual(bundle.get_state(), Bundle.RESOLVED,
                         "Bundle should be stopped")
        log_on()

        # Start the module
        module.fw_raiser = False
        bundle.start()
        self.assertEqual(bundle.get_state(), Bundle.ACTIVE,
                         "Bundle should be active")

        # Set module in raiser mode
        module.fw_raiser = True
        module.fw_raiser_stop = True

        # Stop the framework
        log_off()
        self.assertTrue(framework.stop(), "Framework couldn't be stopped")
        self.assertEqual(framework.get_state(), Bundle.RESOLVED,
                         "Framework should be stopped")
        self.assertEqual(bundle.get_state(), Bundle.RESOLVED,
                         "Bundle should be stopped")
        log_on()

        FrameworkFactory.delete_framework(framework)
Example #19
0
    def testFrameworkStopper(self):
        """
        Tests FrameworkException stop flag handling
        """
        framework = FrameworkFactory.get_framework()
        context = framework.get_bundle_context()

        # Install the bundle
        bundle = context.install_bundle(SIMPLE_BUNDLE)
        module_ = bundle.get_module()

        # Set module in raiser stop mode
        module_.fw_raiser = True
        module_.fw_raiser_stop = True

        log_off()
        self.assertFalse(framework.start(), "Framework should be stopped")
        self.assertEqual(framework.get_state(), Bundle.RESOLVED,
                         "Framework should be stopped")
        self.assertEqual(bundle.get_state(), Bundle.RESOLVED,
                         "Bundle should be stopped")
        log_on()

        # Set module in raiser non-stop mode
        module_.fw_raiser_stop = False

        log_off()
        self.assertTrue(framework.start(), "Framework should be stopped")
        self.assertEqual(framework.get_state(), Bundle.ACTIVE,
                         "Framework should be started")
        self.assertEqual(bundle.get_state(), Bundle.RESOLVED,
                         "Bundle should be stopped")
        log_on()

        # Start the module
        module_.fw_raiser = False
        bundle.start()
        self.assertEqual(bundle.get_state(), Bundle.ACTIVE,
                         "Bundle should be active")

        # Set module in raiser mode
        module_.fw_raiser = True
        module_.fw_raiser_stop = True

        # Stop the framework
        log_off()
        self.assertTrue(framework.stop(), "Framework couldn't be stopped")
        self.assertEqual(framework.get_state(), Bundle.RESOLVED,
                         "Framework should be stopped")
        self.assertEqual(bundle.get_state(), Bundle.RESOLVED,
                         "Bundle should be stopped")
        log_on()

        FrameworkFactory.delete_framework()
Example #20
0
    def testFrameworkStartRaiser(self):
        """
        Tests framework start and stop with a bundle raising exception
        """
        framework = FrameworkFactory.get_framework()
        context = framework.get_bundle_context()

        # Register the stop listener
        context.add_framework_stop_listener(self)

        # Install the bundle
        bundle = context.install_bundle(SIMPLE_BUNDLE)
        module_ = bundle.get_module()

        # Set module in raiser mode
        module_.raiser = True

        # Framework can start...
        log_off()
        self.assertTrue(framework.start(), "Framework should be started")
        log_on()

        self.assertEqual(framework.get_state(), Bundle.ACTIVE,
                         "Framework should be in ACTIVE state")

        self.assertEqual(bundle.get_state(), Bundle.RESOLVED,
                         "Bundle should be in RESOLVED state")

        # Stop the framework
        self.assertTrue(framework.stop(), "Framework should be stopped")

        # Remove raiser mode
        module_.raiser = False

        # Framework can start
        self.assertTrue(framework.start(), "Framework couldn't be started")
        self.assertEqual(framework.get_state(), Bundle.ACTIVE,
                         "Framework should be in ACTIVE state")
        self.assertEqual(bundle.get_state(), Bundle.ACTIVE,
                         "Bundle should be in ACTIVE state")

        # Set module in raiser mode
        module_.raiser = True

        # Stop the framework
        log_off()
        self.assertTrue(framework.stop(), "Framework couldn't be stopped")
        log_on()

        self.assertTrue(self.stopping, "Stop listener not called")

        FrameworkFactory.delete_framework()
    def testFrameworkStartRaiser(self):
        """
        Tests framework start and stop with a bundle raising exception
        """
        framework = FrameworkFactory.get_framework()
        context = framework.get_bundle_context()

        # Register the stop listener
        context.add_framework_stop_listener(self)

        # Install the bundle
        bundle = context.install_bundle(SIMPLE_BUNDLE)
        module = bundle.get_module()

        # Set module in raiser mode
        module.raiser = True

        # Framework can start...
        log_off()
        self.assertTrue(framework.start(), "Framework should be started")
        log_on()

        self.assertEqual(framework.get_state(), Bundle.ACTIVE,
                         "Framework should be in ACTIVE state")

        self.assertEqual(bundle.get_state(), Bundle.RESOLVED,
                         "Bundle should be in RESOLVED state")

        # Stop the framework
        self.assertTrue(framework.stop(), "Framework should be stopped")

        # Remove raiser mode
        module.raiser = False

        # Framework can start
        self.assertTrue(framework.start(), "Framework couldn't be started")
        self.assertEqual(framework.get_state(), Bundle.ACTIVE,
                         "Framework should be in ACTIVE state")
        self.assertEqual(bundle.get_state(), Bundle.ACTIVE,
                         "Bundle should be in ACTIVE state")

        # Set module in raiser mode
        module.raiser = True

        # Stop the framework
        log_off()
        self.assertTrue(framework.stop(), "Framework couldn't be stopped")
        log_on()

        self.assertTrue(self.stopping, "Stop listener not called")

        FrameworkFactory.delete_framework(framework)
Example #22
0
    def testUninstall(self):
        """
        Tests if the framework raises an exception if uninstall() is called
        """
        # Set up a framework
        framework = FrameworkFactory.get_framework()
        self.assertRaises(BundleException, framework.uninstall)

        # Even once started...
        framework.start()
        self.assertRaises(BundleException, framework.uninstall)
        framework.stop()

        FrameworkFactory.delete_framework()
Example #23
0
    def testBundleZero(self):
        """
        Tests if bundle 0 is the framework
        """
        framework = FrameworkFactory.get_framework()

        self.assertIsNone(framework.get_bundle_by_name(None), "None name is not bundle 0")

        self.assertIs(framework, framework.get_bundle_by_id(0), "Invalid bundle 0")

        pelix_name = framework.get_symbolic_name()
        self.assertIs(framework, framework.get_bundle_by_name(pelix_name), "Invalid system bundle name")

        FrameworkFactory.delete_framework()
    def testUninstall(self):
        """
        Tests if the framework raises an exception if uninstall() is called
        """
        # Set up a framework
        framework = FrameworkFactory.get_framework()
        self.assertRaises(BundleException, framework.uninstall)

        # Even once started...
        framework.start()
        self.assertRaises(BundleException, framework.uninstall)
        framework.stop()

        FrameworkFactory.delete_framework(framework)
Example #25
0
    def tearDown(self):
        """
        Cleans up the framework
        """
        self.framework.stop()
        FrameworkFactory.delete_framework()

        # Remove the output file
        if os.path.exists(self.out_file):
            os.remove(self.out_file)

        self.report = None
        self.shell = None
        self.context = None
        self.framework = None
Example #26
0
    def testPropertiesWithoutPreset(self):
        """
        Test framework properties
        """
        pelix_test_name = "PELIX_TEST"
        pelix_test = "42"

        # Test without pre-set properties
        framework = FrameworkFactory.get_framework()

        self.assertIsNone(framework.get_property(pelix_test_name), "Magic property value")

        os.environ[pelix_test_name] = pelix_test
        self.assertEqual(framework.get_property(pelix_test_name), pelix_test, "Invalid property value")
        del os.environ[pelix_test_name]

        FrameworkFactory.delete_framework()
Example #27
0
    def testBundleZero(self):
        """
        Tests if bundle 0 is the framework
        """
        framework = FrameworkFactory.get_framework()

        self.assertIsNone(framework.get_bundle_by_name(None),
                          "None name is not bundle 0")

        self.assertIs(framework, framework.get_bundle_by_id(0),
                      "Invalid bundle 0")

        pelix_name = framework.get_symbolic_name()
        self.assertIs(framework, framework.get_bundle_by_name(pelix_name),
                      "Invalid system bundle name")

        FrameworkFactory.delete_framework()
Example #28
0
    def testPropertiesWithoutPreset(self):
        """
        Test framework properties
        """
        pelix_test_name = "PELIX_TEST"
        pelix_test = "42"

        # Test without pre-set properties
        framework = FrameworkFactory.get_framework()

        self.assertIsNone(framework.get_property(pelix_test_name),
                          "Magic property value")

        os.environ[pelix_test_name] = pelix_test
        self.assertEqual(framework.get_property(pelix_test_name), pelix_test,
                         "Invalid property value")
        del os.environ[pelix_test_name]

        FrameworkFactory.delete_framework()
    def testStopListener(self):
        """
        Test the framework stop event
        """
        # Set up a framework
        framework = FrameworkFactory.get_framework()
        framework.start()
        context = framework.get_bundle_context()

        # Assert initial state
        self.assertFalse(self.stopping, "Invalid initial state")

        # Register the stop listener
        self.assertTrue(context.add_framework_stop_listener(self),
                        "Can't register the stop listener")

        log_off()
        self.assertFalse(context.add_framework_stop_listener(self),
                         "Stop listener registered twice")
        log_on()

        # Assert running state
        self.assertFalse(self.stopping, "Invalid running state")

        # Stop the framework
        framework.stop()

        # Assert the listener has been called
        self.assertTrue(self.stopping, "Stop listener hasn't been called")

        # Unregister the listener
        self.assertTrue(context.remove_framework_stop_listener(self),
                        "Can't unregister the stop listener")

        log_off()
        self.assertFalse(context.remove_framework_stop_listener(self),
                         "Stop listener unregistered twice")
        log_on()

        FrameworkFactory.delete_framework(framework)
Example #30
0
    def testStopListener(self):
        """
        Test the framework stop event
        """
        # Set up a framework
        framework = FrameworkFactory.get_framework()
        framework.start()
        context = framework.get_bundle_context()

        # Assert initial state
        self.assertFalse(self.stopping, "Invalid initial state")

        # Register the stop listener
        self.assertTrue(context.add_framework_stop_listener(self),
                        "Can't register the stop listener")

        log_off()
        self.assertFalse(context.add_framework_stop_listener(self),
                         "Stop listener registered twice")
        log_on()

        # Assert running state
        self.assertFalse(self.stopping, "Invalid running state")

        # Stop the framework
        framework.stop()

        # Assert the listener has been called
        self.assertTrue(self.stopping, "Stop listener hasn't been called")

        # Unregister the listener
        self.assertTrue(context.remove_framework_stop_listener(self),
                        "Can't unregister the stop listener")

        log_off()
        self.assertFalse(context.remove_framework_stop_listener(self),
                         "Stop listener unregistered twice")
        log_on()

        FrameworkFactory.delete_framework()
Example #31
0
    def testFrameworkStopRaiser(self):
        """
        Tests framework start and stop with a bundle raising exception
        """
        framework = FrameworkFactory.get_framework()
        context = framework.get_bundle_context()

        # Register the stop listener
        context.add_framework_stop_listener(self)

        # Install the bundle
        bundle = context.install_bundle(SIMPLE_BUNDLE)
        module_ = bundle.get_module()

        # Set module in non-raiser mode
        module_.raiser = False

        # Framework can start...
        log_off()
        self.assertTrue(framework.start(), "Framework should be started")
        self.assertEqual(framework.get_state(), Bundle.ACTIVE,
                         "Framework should be in ACTIVE state")
        self.assertEqual(bundle.get_state(), Bundle.ACTIVE,
                         "Bundle should be in ACTIVE state")
        log_on()

        # Set module in raiser mode
        module_.raiser = True

        # Bundle must raise the exception and stay active
        log_off()
        self.assertRaises(BundleException, bundle.stop)
        self.assertEqual(bundle.get_state(), Bundle.RESOLVED,
                         "Bundle should be in RESOLVED state")
        log_on()

        # Stop framework
        framework.stop()
        FrameworkFactory.delete_framework()
    def testFrameworkDoubleStart(self):
        """
        Tests double calls to start and stop
        """
        framework = FrameworkFactory.get_framework()
        context = framework.get_bundle_context()

        # Register the stop listener
        context.add_framework_stop_listener(self)

        self.assertTrue(framework.start(), "Framework couldn't be started")
        self.assertFalse(framework.start(), "Framework started twice")

        # Stop the framework
        self.assertTrue(framework.stop(), "Framework couldn't be stopped")
        self.assertTrue(self.stopping, "Stop listener not called")
        self.stopping = False

        self.assertFalse(framework.stop(), "Framework stopped twice")
        self.assertFalse(self.stopping, "Stop listener called twice")

        FrameworkFactory.delete_framework(framework)
    def testPropertiesWithPreset(self):
        """
        Test framework properties
        """
        pelix_test_name = "PELIX_TEST"
        pelix_test = "42"
        pelix_test_2 = "421"

        # Test with pre-set properties
        props = {pelix_test_name: pelix_test}
        framework = FrameworkFactory.get_framework(props)

        self.assertEqual(framework.get_property(pelix_test_name), pelix_test,
                         "Invalid property value (preset value not set)")

        # Pre-set property has priority
        os.environ[pelix_test_name] = pelix_test_2
        self.assertEqual(framework.get_property(pelix_test_name), pelix_test,
                         "Invalid property value (preset has priority)")
        del os.environ[pelix_test_name]

        FrameworkFactory.delete_framework(framework)
Example #34
0
    def testFrameworkDoubleStart(self):
        """
        Tests double calls to start and stop
        """
        framework = FrameworkFactory.get_framework()
        context = framework.get_bundle_context()

        # Register the stop listener
        context.add_framework_stop_listener(self)

        self.assertTrue(framework.start(), "Framework couldn't be started")
        self.assertFalse(framework.start(), "Framework started twice")

        # Stop the framework
        self.assertTrue(framework.stop(), "Framework couldn't be stopped")
        self.assertTrue(self.stopping, "Stop listener not called")
        self.stopping = False

        self.assertFalse(framework.stop(), "Framework stopped twice")
        self.assertFalse(self.stopping, "Stop listener called twice")

        FrameworkFactory.delete_framework()
    def testFrameworkStopRaiser(self):
        """
        Tests framework start and stop with a bundle raising exception
        """
        framework = FrameworkFactory.get_framework()
        context = framework.get_bundle_context()

        # Register the stop listener
        context.add_framework_stop_listener(self)

        # Install the bundle
        bundle = context.install_bundle(SIMPLE_BUNDLE)
        module = bundle.get_module()

        # Set module in non-raiser mode
        module.raiser = False

        # Framework can start...
        log_off()
        self.assertTrue(framework.start(), "Framework should be started")
        self.assertEqual(framework.get_state(), Bundle.ACTIVE,
                         "Framework should be in ACTIVE state")
        self.assertEqual(bundle.get_state(), Bundle.ACTIVE,
                         "Bundle should be in ACTIVE state")
        log_on()

        # Set module in raiser mode
        module.raiser = True

        # Bundle must raise the exception and stay active
        log_off()
        self.assertRaises(BundleException, bundle.stop)
        self.assertEqual(bundle.get_state(), Bundle.RESOLVED,
                         "Bundle should be in RESOLVED state")
        log_on()

        # Stop framework
        framework.stop()
        FrameworkFactory.delete_framework(framework)
Example #36
0
    def testPropertiesWithPreset(self):
        """
        Test framework properties
        """
        pelix_test_name = "PELIX_TEST"
        pelix_test = "42"
        pelix_test_2 = "421"

        # Test with pre-set properties
        props = {pelix_test_name: pelix_test}
        framework = FrameworkFactory.get_framework(props)

        self.assertEqual(framework.get_property(pelix_test_name), pelix_test,
                         "Invalid property value (preset value not set)")

        # Pre-set property has priority
        os.environ[pelix_test_name] = pelix_test_2
        self.assertEqual(framework.get_property(pelix_test_name), pelix_test,
                         "Invalid property value (preset has priority)")
        del os.environ[pelix_test_name]

        FrameworkFactory.delete_framework()
Example #37
0
    def testBundleStart(self):
        """
        Tests if a bundle can be started before the framework itself
        """
        framework = FrameworkFactory.get_framework()
        context = framework.get_bundle_context()
        assert isinstance(context, BundleContext)

        # Install a bundle
        bundle = context.install_bundle(SIMPLE_BUNDLE)

        self.assertEqual(bundle.get_state(), Bundle.RESOLVED,
                         "Bundle should be in RESOLVED state")

        # Starting the bundle now should fail
        self.assertRaises(BundleException, bundle.start)
        self.assertEqual(bundle.get_state(), Bundle.RESOLVED,
                         "Bundle should be in RESOLVED state")

        # Start the framework
        framework.start()

        # Bundle should have been started now
        self.assertEqual(bundle.get_state(), Bundle.ACTIVE,
                         "Bundle should be in ACTIVE state")

        # Stop the framework
        framework.stop()

        self.assertEqual(bundle.get_state(), Bundle.RESOLVED,
                         "Bundle should be in RESOLVED state")

        # Try to start the bundle again (must fail)
        self.assertRaises(BundleException, bundle.start)
        self.assertEqual(bundle.get_state(), Bundle.RESOLVED,
                         "Bundle should be in RESOLVED state")

        FrameworkFactory.delete_framework()
Example #38
0
    def testWaitForStopTimeout(self):
        """
        Tests the wait_for_stop() method
        """
        # Set up a framework
        framework = FrameworkFactory.get_framework()
        framework.start()

        # Start the framework killer
        threading.Thread(target=_framework_killer,
                         args=(framework, 0.5)).start()

        # Wait for stop (timeout not raised)
        start = time.time()
        self.assertTrue(framework.wait_for_stop(1),
                        "wait_for_stop() should return True")
        end = time.time()
        self.assertLess(end - start, 1, "Wait should be less than 1 sec")

        # Restart framework
        framework.start()

        # Start the framework killer
        threading.Thread(target=_framework_killer,
                         args=(framework, 2)).start()

        # Wait for stop (timeout raised)
        start = time.time()
        self.assertFalse(framework.wait_for_stop(1),
                         "wait_for_stop() should return False")
        end = time.time()
        self.assertLess(end - start, 1.2, "Wait should be less than 1.2 sec")

        # Wait for framework to really stop
        framework.wait_for_stop()

        FrameworkFactory.delete_framework()
Example #39
0
 def tearDown(self):
     """
     Called after each test
     """
     self.framework.stop()
     FrameworkFactory.delete_framework()
 def tearDown(self):
     """
     Called after each test
     """
     if self.framework is not None:
         FrameworkFactory.delete_framework(self.framework)
Example #41
0
    def __run_test(self, transport_bundle, exporter_factory, importer_factory,
                   test_kwargs=True):
        """
        Runs a remote service call test

        :param transport_bundle: Transport implementation bundle to use
        :param exporter_factory: Name of the RS exporter factory
        :param importer_factory: Name of the RS importer factory
        :param test_kwargs: Test keyword arguments
        :raise queue.Empty: Peer took to long to answer
        :raise ValueError: Test failed
        """
        # Define components
        components = [(exporter_factory, "rs-exporter"),
                      (importer_factory, "rs-importer")]

        # Start the remote framework
        print("Starting...")
        status_queue = Queue()
        peer = WrappedProcess(
            target=export_framework,
            args=(status_queue, APP_ID, transport_bundle, components))
        peer.start()

        try:
            # Wait for the ready state
            state = status_queue.get(5)
            self.assertEqual(state, "ready")

            # Load the local framework (after the fork)
            framework = load_framework(APP_ID, transport_bundle, components)
            context = framework.get_bundle_context()

            # Look for the remote service
            for _ in range(30):
                svc_ref = context.get_service_reference(SVC_SPEC)
                if svc_ref is not None:
                    break
                time.sleep(.5)
            else:
                self.fail("Remote Service not found")

            # Get it
            svc = context.get_service(svc_ref)

            # Dummy call
            result = svc.dummy()
            state = status_queue.get(10)
            self.assertEqual(state, "call-dummy")
            self.assertIsNone(result, "Dummy didn't returned None: {0}"
                              .format(result))

            # Echo call
            for value in (None, "Test", 42, [1, 2, 3], {"a": "b"}):
                result = svc.echo(value)

                # Check state
                state = status_queue.get(10)
                self.assertEqual(state, "call-echo")

                # Check result
                self.assertEqual(result, value)

            if test_kwargs:
                # Keyword arguments
                sample_text = "SomeSampleText"

                # Test as-is with default arguments
                result = svc.keywords(text=sample_text)
                state = status_queue.get(10)
                self.assertEqual(state, "call-keyword")
                self.assertEqual(result, sample_text.upper())

                # Test with keywords in the same order as positional arguments
                result = svc.keywords(text=sample_text, to_lower=True)
                state = status_queue.get(10)
                self.assertEqual(state, "call-keyword")
                self.assertEqual(result, sample_text.lower())

                result = svc.keywords(text=sample_text, to_lower=False)
                state = status_queue.get(10)
                self.assertEqual(state, "call-keyword")
                self.assertEqual(result, sample_text.upper())

                # Test with keywords in a different order
                # than positional arguments
                result = svc.keywords(to_lower=True, text=sample_text)
                state = status_queue.get(10)
                self.assertEqual(state, "call-keyword")
                self.assertEqual(result, sample_text.lower())

            # Exception handling
            try:
                svc.error()
            except pelix.remote.RemoteServiceError:
                # The error has been propagated
                state = status_queue.get(10)
                self.assertEqual(state, "call-error")
            else:
                self.fail("No exception raised calling 'error'")

            # Call undefined method
            self.assertRaises(Exception, svc.undefined)

            try:
                # Stop the peer
                svc.stop()
            except pelix.remote.RemoteServiceError:
                # Exception can occur because the peer is disconnected from
                # MQTT before the call result is received
                pass

            # Wait for the peer to stop
            state = status_queue.get(10)
            self.assertEqual(state, "stopping")

            # Wait a bit more, to let coverage save its files
            time.sleep(.1)
        finally:
            # Stop everything (and delete the framework in any case
            FrameworkFactory.delete_framework()
            peer.terminate()
            status_queue.close()
Example #42
0
    def _run_test(self, discovery_bundle, discovery_factory,
                  discovery_opts=None):
        """
        Runs a remote service call test

        :param discovery_bundle: Discovery implementation bundle to use
        :param discovery_factory: Name of the discovery factory
        :param discovery_opts: Initial parameters of the discovery component
        :raise queue.Empty: Peer took to long to answer
        :raise ValueError: Test failed
        """
        transport_bundle = "pelix.remote.json_rpc"

        # Define components
        components = [
            (pelix.remote.FACTORY_TRANSPORT_JSONRPC_EXPORTER, "rs-exporter"),
            (pelix.remote.FACTORY_TRANSPORT_JSONRPC_IMPORTER, "rs-importer"),
            (discovery_factory, "discovery", discovery_opts)]

        # Start the remote framework
        status_queue = Queue()
        peer = WrappedProcess(target=self._export_framework,
                              args=(status_queue, transport_bundle,
                                    discovery_bundle, components))
        peer.start()

        try:
            # Wait for the ready state
            state = status_queue.get(4)
            self.assertEqual(state, "ready")

            # Load the local framework (after the fork)
            framework = self._load_framework(
                transport_bundle, discovery_bundle, components)
            context = framework.get_bundle_context()

            # Look for the remote service
            for _ in range(10):
                svc_ref = context.get_service_reference(SVC_SPEC)
                if svc_ref is not None:
                    break
                time.sleep(.5)
            else:
                self.fail("Remote Service not found")

            # Get it
            svc = context.get_service(svc_ref)

            # Echo call
            for value in (None, "Test", 42, [1, 2, 3], {"a": "b"}):
                result = svc.echo(value)
                # Check state
                state = status_queue.get(2)
                self.assertEqual(state, "call-echo")
                # Check result
                self.assertEqual(result, value)

            # Stop the peer
            svc.stop()

            # Wait for the peer to stop
            state = status_queue.get(2)
            self.assertEqual(state, "stopping")

            # Wait a bit more, to let coverage save its files
            peer.join(1)

            # Check the remote service
            # Look for the remote service
            for _ in range(10):
                svc_ref = context.get_service_reference(SVC_SPEC)
                if svc_ref is None:
                    break
                time.sleep(2)
            else:
                self.fail("Remote Service still registered")
        finally:
            # Stop everything (and delete the framework in any case
            try:
                FrameworkFactory.delete_framework()
            except:
                pass
            peer.terminate()
            status_queue.close()
Example #43
0
    def _run_test(self,
                  discovery_bundle,
                  discovery_factory,
                  discovery_opts=None):
        """
        Runs a remote service call test

        :param discovery_bundle: Discovery implementation bundle to use
        :param discovery_factory: Name of the discovery factory
        :param discovery_opts: Initial parameters of the discovery component
        :raise queue.Empty: Peer took to long to answer
        :raise ValueError: Test failed
        """
        transport_bundle = "pelix.remote.json_rpc"

        # Define components
        components = [
            (pelix.remote.FACTORY_TRANSPORT_JSONRPC_EXPORTER, "rs-exporter"),
            (pelix.remote.FACTORY_TRANSPORT_JSONRPC_IMPORTER, "rs-importer"),
            (discovery_factory, "discovery", discovery_opts)
        ]

        # Start the remote framework
        status_queue = Queue()
        peer = WrappedProcess(target=self._export_framework,
                              args=(status_queue, transport_bundle,
                                    discovery_bundle, components))
        peer.start()

        try:
            # Wait for the ready state
            state = status_queue.get(4)
            self.assertEqual(state, "ready")

            # Load the local framework (after the fork)
            framework = self._load_framework(transport_bundle,
                                             discovery_bundle, components)
            context = framework.get_bundle_context()

            # Look for the remote service
            for _ in range(10):
                svc_ref = context.get_service_reference(SVC_SPEC)
                if svc_ref is not None:
                    break
                time.sleep(.5)
            else:
                self.fail("Remote Service not found")

            # Get it
            svc = context.get_service(svc_ref)

            # Echo call
            for value in (None, "Test", 42, [1, 2, 3], {"a": "b"}):
                result = svc.echo(value)
                # Check state
                state = status_queue.get(2)
                self.assertEqual(state, "call-echo")
                # Check result
                self.assertEqual(result, value)

            # Stop the peer
            svc.stop()

            # Wait for the peer to stop
            state = status_queue.get(2)
            self.assertEqual(state, "stopping")

            # Wait a bit more, to let coverage save its files
            peer.join(1)

            # Check the remote service
            # Look for the remote service
            for _ in range(10):
                svc_ref = context.get_service_reference(SVC_SPEC)
                if svc_ref is None:
                    break
                time.sleep(2)
            else:
                self.fail("Remote Service still registered")
        finally:
            # Stop everything (and delete the framework in any case
            try:
                FrameworkFactory.delete_framework()
            except:
                pass
            peer.terminate()
            status_queue.close()
Example #44
0
    def _run_test(self,
                  transport_bundle,
                  exporter_factory,
                  importer_factory,
                  test_kwargs=True):
        """
        Runs a remote service call test

        :param transport_bundle: Transport implementation bundle to use
        :param exporter_factory: Name of the RS exporter factory
        :param importer_factory: Name of the RS importer factory
        :param test_kwargs: Test keyword arguments
        :raise queue.Empty: Peer took to long to answer
        :raise ValueError: Test failed
        """
        # Define components
        components = [(exporter_factory, "rs-exporter"),
                      (importer_factory, "rs-importer")]

        # Start the remote framework
        status_queue = Queue()
        peer = WrappedProcess(target=self._export_framework,
                              args=(status_queue, transport_bundle,
                                    components))
        peer.start()

        try:
            # Wait for the ready state
            state = status_queue.get(4)
            self.assertEqual(state, "ready")

            # Load the local framework (after the fork)
            framework = self._load_framework(transport_bundle, components)
            context = framework.get_bundle_context()

            # Look for the remote service
            for _ in range(10):
                svc_ref = context.get_service_reference(SVC_SPEC)
                if svc_ref is not None:
                    break
                time.sleep(.5)
            else:
                self.fail("Remote Service not found")

            # Get it
            svc = context.get_service(svc_ref)

            # Dummy call
            result = svc.dummy()
            state = status_queue.get(2)
            self.assertEqual(state, "call-dummy")
            self.assertIsNone(result,
                              "Dummy didn't returned None: {0}".format(result))

            # Echo call
            for value in (None, "Test", 42, [1, 2, 3], {"a": "b"}):
                result = svc.echo(value)

                # Check state
                state = status_queue.get(2)
                self.assertEqual(state, "call-echo")

                # Check result
                self.assertEqual(result, value)

            if test_kwargs:
                # Keyword arguments
                sample_text = "SomeSampleText"

                # Test as-is with default arguments
                result = svc.keywords(text=sample_text)
                state = status_queue.get(2)
                self.assertEqual(state, "call-keyword")
                self.assertEqual(result, sample_text.upper())

                # Test with keywords in the same order as positional arguments
                result = svc.keywords(text=sample_text, to_lower=True)
                state = status_queue.get(2)
                self.assertEqual(state, "call-keyword")
                self.assertEqual(result, sample_text.lower())

                result = svc.keywords(text=sample_text, to_lower=False)
                state = status_queue.get(2)
                self.assertEqual(state, "call-keyword")
                self.assertEqual(result, sample_text.upper())

                # Test with keywords in a different order
                # than positional arguments
                result = svc.keywords(to_lower=True, text=sample_text)
                state = status_queue.get(2)
                self.assertEqual(state, "call-keyword")
                self.assertEqual(result, sample_text.lower())

            # Exception handling
            try:
                svc.error()
            except:
                # The error has been propagated
                state = status_queue.get(2)
                self.assertEqual(state, "call-error")
            else:
                self.fail("No exception raised calling 'error'")

            # Call undefined method
            try:
                svc.undefined()
            except:
                # The error has been propagated: OK
                pass
            else:
                self.fail("No exception raised calling an undefined method")

            # Stop the peer
            svc.stop()

            # Wait for the peer to stop
            state = status_queue.get(2)
            self.assertEqual(state, "stopping")

            # Wait a bit more, to let coverage save its files
            time.sleep(.1)
        finally:
            # Stop everything (and delete the framework in any case
            FrameworkFactory.delete_framework()
            peer.terminate()
            status_queue.close()
Example #45
0
 def tearDown(self):
     """
     Called after each test
     """
     FrameworkFactory.delete_framework()
    def __run_test(self, transport_bundle, exporter_factory, importer_factory):
        """
        Runs a remote service call test

        :param transport_bundle: Transport implementation bundle to use
        :param exporter_factory: Name of the RS exporter factory
        :param importer_factory: Name of the RS importer factory
        :raise queue.Empty: Peer took to long to answer
        :raise ValueError: Test failed
        """
        # Define components
        components = [(exporter_factory, "rs-exporter"),
                      (importer_factory, "rs-importer")]

        # Start the remote framework
        status_queue = Queue()
        peer = WrappedProcess(target=export_framework,
                              args=(status_queue, transport_bundle,
                                    components))
        peer.start()

        try:
            # Wait for the ready state
            state = status_queue.get(4)
            self.assertEqual(state, "ready")

            # Load the local framework (after the fork)
            framework = load_framework(transport_bundle, components)
            context = framework.get_bundle_context()

            # Look for the remote service
            for _ in range(10):
                svc_ref = context.get_service_reference(SVC_SPEC)
                if svc_ref is not None:
                    break
                time.sleep(.5)
            else:
                self.fail("Remote Service not found")

            # Get it
            svc = context.get_service(svc_ref)

            # Dummy call
            result = svc.dummy()
            state = status_queue.get(2)
            self.assertEqual(state, "call-dummy")
            self.assertIsNone(result, "Dummy didn't returned None: {0}"
                              .format(result))

            # Echo call
            for value in (None, "Test", 42, [1, 2, 3], {"a": "b"}):
                result = svc.echo(value)

                # Check state
                state = status_queue.get(2)
                self.assertEqual(state, "call-echo")

                # Check result
                self.assertEqual(result, value)

            # Exception handling
            try:
                svc.error()
            except:
                # The error has been propagated
                state = status_queue.get(2)
                self.assertEqual(state, "call-error")
            else:
                self.fail("No exception raised calling 'error'")

            # Call undefined method
            try:
                svc.undefined()
            except:
                # The error has been propagated: OK
                pass
            else:
                self.fail("No exception raised calling an undefined method")

            # Stop the peer
            svc.stop()

            # Wait for the peer to stop
            state = status_queue.get(2)
            self.assertEqual(state, "stopping")

            # Wait a bit more, to let coverage save its files
            time.sleep(.1)
        finally:
            # Stop everything (and delete the framework in any case
            FrameworkFactory.delete_framework(FrameworkFactory.get_framework())
            peer.terminate()
            status_queue.close()