Esempio n. 1
0
    def testProvides(self):
        """
        Tests the @Provides decorator
        """
        class DummyClass(object):
            pass

        def method():
            pass

        # Empty specification
        for empty in (None, "", "   "):
            self.assertRaises(ValueError, decorators.Provides, empty)

            # No error should be raised
            log_off()
            decorators.Provides("spec", empty)
            log_on()

        # Field name with a space
        self.assertRaises(ValueError, decorators.Provides, "spec", "a space")

        # Invalid specification type
        for invalid in ([1, 2, 3], tuple((1, 2, 3)), 123):
            self.assertRaises(ValueError, decorators.Provides, invalid)
            self.assertRaises(ValueError, decorators.Provides, "spec", invalid)

        # Invalid target
        for invalid in (None, method, 123):
            self.assertRaises(TypeError, decorators.Provides("spec", "field"),
                              invalid)
Esempio n. 2
0
    def test_install_fail_recursive(self):
        """
        Tries the recursive installation of a path
        """
        # List files in the package which will fail at first sight
        bad_root = os.path.join(self.test_root, "pkg_fail")
        expected = _list_modules(bad_root, False)

        # Install the package
        log_off()
        bundles, failed = self.context.install_package(bad_root)
        log_on()

        if not failed:
            self.fail("No failure detection")

        for fail_module_name in failed:
            parts = fail_module_name.split('.')
            self.assertEqual(parts[0], "pkg_fail", "No prefix set")
            self.assertEqual(parts[-1], "invalid", "Wrong module failed")

        # Excepted bundles
        to_remove = [name for name in expected if "invalid" in name]
        expected.difference_update(to_remove)
        for bundle in bundles:
            # Check results
            self.assertIsInstance(bundle, Bundle)
            expected.remove(
                os.path.splitext(os.path.abspath(bundle.get_location()))[0])

        if expected:
            self.fail("All bundles should have been installed. "
                      "Remaining: {}".format(expected))
Esempio n. 3
0
    def testProvides(self):
        """
        Tests the @Provides decorator
        """

        class DummyClass(object):
            pass

        def method():
            pass

        # Empty specification
        for empty in (None, "", "   "):
            self.assertRaises(ValueError, decorators.Provides, empty)

            # No error should be raised
            log_off()
            decorators.Provides("spec", empty)
            log_on()

        # Field name with a space
        self.assertRaises(ValueError, decorators.Provides, "spec", "a space")

        # Invalid specification type
        for invalid in ([1, 2, 3], tuple((1, 2, 3)), 123):
            self.assertRaises(ValueError, decorators.Provides, invalid)
            self.assertRaises(ValueError, decorators.Provides, "spec", invalid)

        # Invalid target
        for invalid in (None, method, 123):
            self.assertRaises(TypeError, decorators.Provides("spec", "field"), invalid)
Esempio n. 4
0
    def testUnbindingRaiser(self):
        """
        Tests the behavior of the HTTP service when a bound_to() method raises
        an exception
        """
        http_svc = instantiate_server(self.ipopo)

        # Make the servlet to not raise an exception
        servlet = self.servlets.SimpleServlet(False)

        # Register the servlet
        self.assertTrue(http_svc.register_servlet("/test", servlet),
                        "Servlet not registered")
        self.assertEqual(get_http_page(uri="/test", only_code=True), 200,
                         "Servlet not registered ?")

        # Make it raise an exception
        servlet.raiser = True

        # Unregister it (no exception should be propagated)
        log_off()
        http_svc.unregister("/test")
        log_on()

        # The servlet must have been unregistered
        self.assertEqual(get_http_page(uri="/test", only_code=True), 404,
                         "Servlet still registered")
Esempio n. 5
0
    def testUnbindingRaiser(self):
        """
        Tests the behavior of the HTTP service when a bound_to() method raises
        an exception
        """
        http_svc = instantiate_server(self.ipopo, self.http_bundle)

        # Make the servlet to not raise an exception
        servlet = self.servlets.SimpleServlet(False)

        # Register the servlet
        self.assertTrue(http_svc.register_servlet("/test", servlet),
                        "Servlet not registered")
        self.assertEqual(get_http_page(uri="/test", only_code=True), 200,
                         "Servlet not registered ?")

        # Make it raise an exception
        servlet.raiser = True

        # Unregister it (no exception should be propagated)
        log_off()
        http_svc.unregister("/test")
        log_on()

        # The servlet must have been unregistered
        self.assertEqual(get_http_page(uri="/test", only_code=True), 404,
                         "Servlet still registered")
Esempio n. 6
0
    def test_install_fail_recursive(self):
        """
        Tries the recursive installation of a path
        """
        # List files in the package which will fail at first sight
        bad_root = os.path.join(self.test_root, "pkg_fail")
        expected = _list_modules(bad_root, False)

        # Install the package
        log_off()
        bundles, failed = self.context.install_package(bad_root)
        log_on()

        if not failed:
            self.fail("No failure detection")

        for fail_module_name in failed:
            parts = fail_module_name.split('.')
            self.assertEqual(parts[0], "pkg_fail", "No prefix set")
            self.assertEqual(parts[-1], "invalid", "Wrong module failed")

        # Excepted bundles
        to_remove = [name for name in expected if "invalid" in name]
        expected.difference_update(to_remove)
        for bundle in bundles:
            # Check results
            self.assertIsInstance(bundle, Bundle)
            expected.remove(os.path.splitext(
                os.path.abspath(bundle.get_location()))[0])

        if expected:
            self.fail("All bundles should have been installed. "
                      "Remaining: {}".format(expected))
    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)
Esempio n. 8
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()
Esempio n. 9
0
    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()
Esempio n. 10
0
    def testInvalidFilterListener(self):
        """
        Tests invalid filter listener registration
        """
        context = self.framework.get_bundle_context()
        assert isinstance(context, BundleContext)

        log_off()
        self.assertRaises(BundleException, context.add_service_listener, self,
                          "Invalid")
        log_on()

        self.assertFalse(context.remove_service_listener(self),
                         "Invalid filter was registered anyway")
Esempio n. 11
0
    def testInvalidFilterListener(self):
        """
        Tests invalid filter listener registration
        """
        context = self.framework.get_bundle_context()
        assert isinstance(context, BundleContext)

        log_off()
        self.assertRaises(BundleException, context.add_service_listener, self,
                          "Invalid")
        log_on()

        self.assertFalse(context.remove_service_listener(self),
                         "Invalid filter was registered anyway")
Esempio n. 12
0
    def testAcceptBinding(self):
        """
        Tests the behavior of the HTTP service when a bound_to() method raises
        an exception
        """
        http_svc = instantiate_server(self.ipopo, self.http_bundle)

        # Make the first servlet
        servlet = self.servlets.SimpleServlet(False)

        # Make the second servlet
        servlet_2 = self.servlets.SimpleServlet(False)

        # Register the first servlet
        self.assertTrue(http_svc.register_servlet("/test", servlet),
                        "Servlet not registered")
        self.assertEqual(get_http_page(uri="/test", only_code=True), 200,
                         "Servlet not registered ?")

        # Second registration must work
        self.assertTrue(http_svc.register_servlet("/test", servlet),
                        "Servlet not registered")

        # Try to register the second servlet, accepting the server
        servlet_2.accept = True
        self.assertRaises(ValueError, http_svc.register_servlet, "/test",
                          servlet_2)

        # Ensure that our first servlet is still there
        self.assertEqual(get_http_page(uri="/test", only_code=True), 200,
                         "Servlet not registered ?")

        # Try to register the second servlet, rejecting the server
        servlet_2.accept = False
        self.assertFalse(http_svc.register_servlet("/test", servlet_2),
                         "Non-accepted server -> must return False")

        # Ensure that our first servlet is still there
        self.assertEqual(get_http_page(uri="/test", only_code=True), 200,
                         "Servlet not registered ?")

        # Unregister it (no exception should be propagated)
        log_off()
        http_svc.unregister("/test")
        log_on()

        # The servlet must have been unregistered
        self.assertEqual(get_http_page(uri="/test", only_code=True), 404,
                         "Servlet still registered")
Esempio n. 13
0
    def testAcceptBinding(self):
        """
        Tests the behavior of the HTTP service when a bound_to() method raises
        an exception
        """
        http_svc = instantiate_server(self.ipopo)

        # Make the first servlet
        servlet = self.servlets.SimpleServlet(False)

        # Make the second servlet
        servlet_2 = self.servlets.SimpleServlet(False)

        # Register the first servlet
        self.assertTrue(http_svc.register_servlet("/test", servlet),
                        "Servlet not registered")
        self.assertEqual(get_http_page(uri="/test", only_code=True), 200,
                         "Servlet not registered ?")

        # Second registration must work
        self.assertTrue(http_svc.register_servlet("/test", servlet),
                        "Servlet not registered")

        # Try to register the second servlet, accepting the server
        servlet_2.accept = True
        self.assertRaises(ValueError, http_svc.register_servlet,
                          "/test", servlet_2)

        # Ensure that our first servlet is still there
        self.assertEqual(get_http_page(uri="/test", only_code=True), 200,
                         "Servlet not registered ?")

        # Try to register the second servlet, rejecting the server
        servlet_2.accept = False
        self.assertFalse(http_svc.register_servlet("/test", servlet_2),
                         "Non-accepted server -> must return False")

        # Ensure that our first servlet is still there
        self.assertEqual(get_http_page(uri="/test", only_code=True), 200,
                         "Servlet not registered ?")

        # Unregister it (no exception should be propagated)
        log_off()
        http_svc.unregister("/test")
        log_on()

        # The servlet must have been unregistered
        self.assertEqual(get_http_page(uri="/test", only_code=True), 404,
                         "Servlet still registered")
Esempio n. 14
0
    def testInstantiate(self):
        """
        Tests the instantiate method
        """
        FACTORY = "dummy-factory"
        FACTORY_2 = "dummy-factory-2"
        INSTANCE = "dummy-instance"
        context = self.framework.get_bundle_context()

        @decorators.ComponentFactory(FACTORY)
        class TestComponent(object):
            pass

        # Invalid name
        for invalid in (None, "", [1]):
            self.assertRaises(ValueError, self.ipopo.instantiate,
                              invalid, INSTANCE)

            self.assertRaises(ValueError, self.ipopo.instantiate,
                              FACTORY, invalid)

        # Unknown factory -> Type Error
        self.assertRaises(TypeError, self.ipopo.instantiate, FACTORY, INSTANCE)

        # Register factory
        self.ipopo.register_factory(context, TestComponent)
        self.ipopo.instantiate(FACTORY, INSTANCE)

        # Already running -> Value Error
        self.assertRaises(
            ValueError, self.ipopo.instantiate, FACTORY, INSTANCE)
        self.ipopo.kill(INSTANCE)

        # Exception on instantiate -> Type Error
        @decorators.ComponentFactory(FACTORY_2)
        class TestComponent2(object):

            def __init__(self):
                raise NotImplementedError

        self.ipopo.register_factory(context, TestComponent2)

        log_off()
        self.assertRaises(TypeError, self.ipopo.instantiate, FACTORY_2,
                          INSTANCE)
        log_on()
Esempio n. 15
0
    def test_first_install_fail(self):
        """
        Tests the installation of a package with failures
        """
        # List files in the "ok" package, avoiding cache
        bad_root = os.path.join(self.test_root, "pkg_invalid")

        # Install the package
        log_off()
        bundles, failed = self.context.install_package(bad_root)
        log_on()

        if bundles:
            self.fail("Some bundles were installed anyway")

        self.assertEqual(len(failed), 1)
        self.assertEqual(failed.pop(), "pkg_invalid")
Esempio n. 16
0
    def test_instantiate(self):
        """
        Tests the instantiate method
        """
        FACTORY = "dummy-factory"
        FACTORY_2 = "dummy-factory-2"
        INSTANCE = "dummy-instance"
        context = self.framework.get_bundle_context()

        @decorators.ComponentFactory(FACTORY)
        class TestComponent(object):
            pass

        # Invalid name
        for invalid in (None, "", [1]):
            self.assertRaises(ValueError, self.ipopo.instantiate,
                              invalid, INSTANCE)

            self.assertRaises(ValueError, self.ipopo.instantiate,
                              FACTORY, invalid)

        # Unknown factory -> Type Error
        self.assertRaises(TypeError, self.ipopo.instantiate, FACTORY, INSTANCE)

        # Register factory
        self.ipopo.register_factory(context, TestComponent)
        self.ipopo.instantiate(FACTORY, INSTANCE)

        # Already running -> Value Error
        self.assertRaises(
            ValueError, self.ipopo.instantiate, FACTORY, INSTANCE)
        self.ipopo.kill(INSTANCE)

        # Exception on instantiate -> Type Error
        @decorators.ComponentFactory(FACTORY_2)
        class TestComponent2(object):

            def __init__(self):
                raise NotImplementedError

        self.ipopo.register_factory(context, TestComponent2)

        log_off()
        self.assertRaises(TypeError, self.ipopo.instantiate, FACTORY_2,
                          INSTANCE)
        log_on()
Esempio n. 17
0
    def test_first_install_fail(self):
        """
        Tests the installation of a package with failures
        """
        # List files in the "ok" package, avoiding cache
        bad_root = os.path.join(self.test_root, "pkg_invalid")

        # Install the package
        log_off()
        bundles, failed = self.context.install_package(bad_root)
        log_on()

        if bundles:
            self.fail("Some bundles were installed anyway")

        self.assertEqual(len(failed), 1)
        self.assertEqual(failed.pop(), "pkg_invalid")
Esempio n. 18
0
    def testBindingRaiser(self):
        """
        Tests the behavior of the HTTP service when a bound_to() method raises
        an exception
        """
        http_svc = instantiate_server(self.ipopo)

        # Make the servlet raise an exception
        servlet = self.servlets.SimpleServlet(True)

        # Register the servlet
        log_off()
        self.assertFalse(http_svc.register_servlet("/test", servlet),
                         "Servlet registered even raising an exception")
        log_on()

        self.assertEqual(get_http_page(uri="/test", only_code=True), 404,
                         "Servlet registered even raising an exception")
Esempio n. 19
0
    def testBindingRaiser(self):
        """
        Tests the behavior of the HTTP service when a bound_to() method raises
        an exception
        """
        http_svc = instantiate_server(self.ipopo, self.http_bundle)

        # Make the servlet raise an exception
        servlet = self.servlets.SimpleServlet(True)

        # Register the servlet
        log_off()
        self.assertFalse(http_svc.register_servlet("/test", servlet),
                         "Servlet registered even raising an exception")
        log_on()

        self.assertEqual(get_http_page(uri="/test", only_code=True), 404,
                         "Servlet registered even raising an exception")
Esempio n. 20
0
    def testCallbackRaiser(self):
        """
        Tests exception handling during a callback
        """
        module = install_bundle(self.framework)

        # Instantiate B (no requirement present)
        compoB = self.ipopo.instantiate(module.FACTORY_B, NAME_B)
        self.assertEqual([IPopoEvent.INSTANTIATED], compoB.states,
                         "Invalid component states: {0}".format(compoB.states))
        compoB.reset()

        # Set B in raiser mode
        compoB.raiser = True

        # Instantiate A (validated)
        log_off()
        compoA = self.ipopo.instantiate(module.FACTORY_A, NAME_A)
        log_on()

        self.assertEqual([IPopoEvent.INSTANTIATED, IPopoEvent.VALIDATED],
                         compoA.states,
                         "Invalid component states: {0}".format(compoA.states))
        compoA.reset()

        # B must have been validated
        self.assertEqual([IPopoEvent.BOUND, IPopoEvent.VALIDATED],
                         compoB.states,
                         "Invalid component states: {0}".format(compoB.states))
        compoB.reset()

        # Uninstantiate A
        log_off()
        self.ipopo.kill(NAME_A)
        log_on()

        self.assertEqual([IPopoEvent.INVALIDATED], compoA.states,
                         "Invalid component states: {0}".format(compoA.states))

        # Uninstantiate B
        self.assertEqual([IPopoEvent.INVALIDATED, IPopoEvent.UNBOUND],
                         compoB.states,
                         "Invalid component states: {0}".format(compoB.states))
Esempio n. 21
0
    def testCallbackRaiser(self):
        """
        Tests exception handling during a callback
        """
        module = install_bundle(self.framework)

        # Instantiate B (no requirement present)
        compoB = self.ipopo.instantiate(module.FACTORY_B, NAME_B)
        self.assertEqual([IPopoEvent.INSTANTIATED], compoB.states,
                         "Invalid component states: {0}".format(compoB.states))
        compoB.reset()

        # Set B in raiser mode
        compoB.raiser = True

        # Instantiate A (validated)
        log_off()
        compoA = self.ipopo.instantiate(module.FACTORY_A, NAME_A)
        log_on()

        self.assertEqual([IPopoEvent.INSTANTIATED, IPopoEvent.VALIDATED],
                         compoA.states,
                         "Invalid component states: {0}".format(compoA.states))
        compoA.reset()

        # B must have been validated
        self.assertEqual([IPopoEvent.BOUND, IPopoEvent.VALIDATED],
                         compoB.states,
                         "Invalid component states: {0}".format(compoB.states))
        compoB.reset()

        # Uninstantiate A
        log_off()
        self.ipopo.kill(NAME_A)
        log_on()

        self.assertEqual([IPopoEvent.INVALIDATED], compoA.states,
                         "Invalid component states: {0}".format(compoA.states))

        # Uninstantiate B
        self.assertEqual([IPopoEvent.INVALIDATED, IPopoEvent.UNBOUND],
                         compoB.states,
                         "Invalid component states: {0}".format(compoB.states))
Esempio n. 22
0
    def testInstantiate(self):
        """
        Tests the @Instantiate decorator
        """
        class DummyClass(object):
            pass

        def method():
            pass

        # Empty name
        for empty in ("", "   "):
            self.assertRaises(ValueError, decorators.Instantiate, empty)

        # Invalid name type
        for invalid in (None, [], tuple(), 123):
            self.assertRaises(TypeError, decorators.Instantiate, invalid)

        # Invalid properties type
        for invalid in ("props", [1, 2], tuple((1, 2, 3)), 123):
            self.assertRaises(TypeError, decorators.Instantiate, "test",
                              invalid)

        # Invalid target
        for invalid in (None, method, 123):
            self.assertRaises(TypeError, decorators.Instantiate("test"),
                              invalid)

        # 1st injection
        decorators.Instantiate("test", {"id": 1})(DummyClass)

        # 2nd injection: nothing happens
        log_off()
        decorators.Instantiate("test", {"id": 2})(DummyClass)
        log_on()

        # Get the factory context
        context = decorators.get_factory_context(DummyClass)
        instances = context.get_instances()
        self.assertEqual(instances["test"]["id"], 1,
                         "Instance properties have been overridden")
    def testInstantiate(self):
        """
        Tests the @Instantiate decorator
        """
        class DummyClass(object):
            pass

        def method():
            pass

        # Empty name
        for empty in ("", "   "):
            self.assertRaises(ValueError, decorators.Instantiate, empty)

        # Invalid name type
        for invalid in (None, [], tuple(), 123):
            self.assertRaises(TypeError, decorators.Instantiate, invalid)

        # Invalid properties type
        for invalid in ("props", [1, 2], tuple((1, 2, 3)), 123):
            self.assertRaises(TypeError, decorators.Instantiate, "test",
                              invalid)

        # Invalid target
        for invalid in (None, method, 123):
            self.assertRaises(TypeError, decorators.Instantiate("test"),
                              invalid)

        # 1st injection
        decorators.Instantiate("test", {"id": 1})(DummyClass)

        # 2nd injection: nothing happens
        log_off()
        decorators.Instantiate("test", {"id": 2})(DummyClass)
        log_on()

        # Get the factory context
        context = decorators.get_factory_context(DummyClass)
        instances = context.get_instances()
        self.assertEqual(instances["test"]["id"], 1,
                         "Instance properties have been overridden")
    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)
Esempio n. 25
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()
Esempio n. 26
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 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)
Esempio n. 28
0
    def testDoubleListener(self):
        """
        Tests double registration / unregistration
        """
        context = self.framework.get_bundle_context()
        assert isinstance(context, BundleContext)

        # Double registration
        self.assertTrue(context.add_service_listener(self),
                        "Can't register the service listener")

        log_off()
        self.assertFalse(context.add_service_listener(self),
                         "Service listener registered twice")
        log_on()

        # Double unregistration
        self.assertTrue(context.remove_service_listener(self),
                        "Can't unregister the service listener")

        log_off()
        self.assertFalse(context.remove_service_listener(self),
                         "Service listener unregistered twice")
        log_on()
Esempio n. 29
0
    def testDoubleListener(self):
        """
        Tests double registration / unregistration
        """
        context = self.framework.get_bundle_context()
        assert isinstance(context, BundleContext)

        # Double registration
        self.assertTrue(context.add_service_listener(self),
                        "Can't register the service listener")

        log_off()
        self.assertFalse(context.add_service_listener(self),
                         "Service listener registered twice")
        log_on()

        # Double unregistration
        self.assertTrue(context.remove_service_listener(self),
                        "Can't unregister the service listener")

        log_off()
        self.assertFalse(context.remove_service_listener(self),
                         "Service listener unregistered twice")
        log_on()
Esempio n. 30
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()
    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)
Esempio n. 32
0
    def testCallbackInstantiateStopper(self):
        """
        Tests exception handling during a callback
        """
        module = install_bundle(self.framework)

        # Instantiate B (no requirement present)
        compoB = self.ipopo.instantiate(module.FACTORY_B, NAME_B)
        self.assertEqual([IPopoEvent.INSTANTIATED], compoB.states,
                         "Invalid component states: {0}".format(compoB.states))
        compoB.reset()

        # Set B in raiser mode
        compoB.fw_raiser = True
        compoB.fw_raiser_stop = False

        # Instantiate A (validated)
        log_off()
        compoA = self.ipopo.instantiate(module.FACTORY_A, NAME_A)
        log_on()

        self.assertEqual([IPopoEvent.INSTANTIATED, IPopoEvent.VALIDATED],
                         compoA.states,
                         "Invalid component states: {0}".format(compoA.states))
        compoA.reset()

        # B must have failed to start
        self.assertEqual([IPopoEvent.BOUND, IPopoEvent.UNBOUND],
                         compoB.states,
                         "Invalid component states: {0}".format(compoB.states))
        compoB.reset()

        # Framework must still be active
        self.assertEqual(self.framework.get_state(), Bundle.ACTIVE,
                         "Framework has stopped")

        # B must have been automatically killed
        instances = [info[0] for info in self.ipopo.get_instances()]
        self.assertIn(NAME_A, instances, "Component A should still be there")
        self.assertNotIn(NAME_B, instances, "Component B still in instances")

        # Kill A
        self.ipopo.kill(NAME_A)

        # Instantiate B (no requirement present)
        compoB = self.ipopo.instantiate(module.FACTORY_B, NAME_B)
        self.assertEqual([IPopoEvent.INSTANTIATED], compoB.states,
                         "Invalid component states: {0}".format(compoB.states))
        compoB.reset()

        # Set B in raiser mode
        compoB.fw_raiser = True
        compoB.fw_raiser_stop = True

        # Instantiate A (validated)
        log_off()
        compoA = self.ipopo.instantiate(module.FACTORY_A, NAME_A)
        log_on()

        # Framework must have stopped now
        self.assertEqual(self.framework.get_state(), Bundle.RESOLVED,
                         "Framework hasn't stopped")
Esempio n. 33
0
    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))
Esempio n. 34
0
    def testCallbackInstantiateStopper(self):
        """
        Tests exception handling during a callback
        """
        module = install_bundle(self.framework)

        # Instantiate B (no requirement present)
        compoB = self.ipopo.instantiate(module.FACTORY_B, NAME_B)
        self.assertEqual([IPopoEvent.INSTANTIATED], compoB.states,
                         "Invalid component states: {0}".format(compoB.states))
        compoB.reset()

        # Set B in raiser mode
        compoB.fw_raiser = True
        compoB.fw_raiser_stop = False

        # Instantiate A (validated)
        log_off()
        compoA = self.ipopo.instantiate(module.FACTORY_A, NAME_A)
        log_on()

        self.assertEqual([IPopoEvent.INSTANTIATED, IPopoEvent.VALIDATED],
                         compoA.states,
                         "Invalid component states: {0}".format(compoA.states))
        compoA.reset()

        # B must have failed to start
        self.assertEqual([IPopoEvent.BOUND, IPopoEvent.UNBOUND], compoB.states,
                         "Invalid component states: {0}".format(compoB.states))
        compoB.reset()

        # Framework must still be active
        self.assertEqual(self.framework.get_state(), Bundle.ACTIVE,
                         "Framework has stopped")

        # B must have been automatically killed
        instances = [info[0] for info in self.ipopo.get_instances()]
        self.assertIn(NAME_A, instances, "Component A should still be there")
        self.assertNotIn(NAME_B, instances, "Component B still in instances")

        # Kill A
        self.ipopo.kill(NAME_A)

        # Instantiate B (no requirement present)
        compoB = self.ipopo.instantiate(module.FACTORY_B, NAME_B)
        self.assertEqual([IPopoEvent.INSTANTIATED], compoB.states,
                         "Invalid component states: {0}".format(compoB.states))
        compoB.reset()

        # Set B in raiser mode
        compoB.fw_raiser = True
        compoB.fw_raiser_stop = True

        # Instantiate A (validated)
        log_off()
        compoA = self.ipopo.instantiate(module.FACTORY_A, NAME_A)
        log_on()

        # Framework must have stopped now
        self.assertEqual(self.framework.get_state(), Bundle.RESOLVED,
                         "Framework hasn't stopped")