def test_notify_subscribe_both_not_exception(self):
     f = mock.Mock()
     notifications.subscribe(f, event=NotifyEvents.BUNDLE,
                             when=NotifyType.BOTH)
     notifications.notify(NotifyEvents.BUNDLE, "hello",
                          when=NotifyType.EXCEPTION, thing="thing")
     f.assert_not_called()
 def test_subscribe_one_event_both_when(self):
     notifications.subscribe(self.f,
                             event=NotifyEvents.BUNDLE,
                             when=NotifyType.BOTH)
     self._add_pattern(NotifyEvents.BUNDLE, NotifyType.BEFORE)
     self._add_pattern(NotifyEvents.BUNDLE, NotifyType.AFTER)
     self._asserts(self.f)
 def test_notify_single_different_event(self):
     f = mock.Mock()
     notifications.subscribe(f, event=NotifyEvents.BUNDLE,
                             when=NotifyType.BEFORE)
     notifications.notify(NotifyEvents.ENV_DEPLOYMENT, "hello",
                          when=NotifyType.BEFORE, thing="thing")
     f.assert_not_called()
 def test_notify_single(self):
     f = mock.Mock()
     notifications.subscribe(f, event=NotifyEvents.BUNDLE,
                             when=NotifyType.BEFORE)
     notifications.notify(NotifyEvents.BUNDLE, "hello",
                          when=NotifyType.BEFORE, thing="thing")
     f.assert_called_once_with(
         NotifyEvents.BUNDLE, NotifyType.BEFORE, "hello", thing="thing")
 def test_unsubscribe_one_event_all_when(self):
     notifications.subscribe(self.f,
                             event=NotifyEvents.BUNDLE,
                             when=NotifyType.ALL)
     notifications.unsubscribe(self.f,
                               event=NotifyEvents.BUNDLE,
                               when=NotifyType.ALL)
     self._asserts(self.f)
 def test_unsubscribe_one_event_multiple_when(self):
     notifications.subscribe(self.f,
                             event=NotifyEvents.BUNDLE,
                             when=(NotifyType.BEFORE, NotifyType.AFTER))
     notifications.unsubscribe(self.f,
                               event=NotifyEvents.BUNDLE,
                               when=(NotifyType.BEFORE, NotifyType.AFTER))
     self._asserts(self.f)
 def test_subscribe_multiple_event_one_when(self):
     notifications.subscribe(self.f,
                             event=(NotifyEvents.BUNDLE,
                                    NotifyEvents.ENV_DEPLOYMENT),
                             when=NotifyType.BEFORE)
     self._add_pattern(NotifyEvents.BUNDLE, NotifyType.BEFORE)
     self._add_pattern(NotifyEvents.ENV_DEPLOYMENT, NotifyType.BEFORE)
     self._asserts(self.f)
    def test_noitify_around(self):
        f = mock.Mock()
        notifications.subscribe(f, event=NotifyEvents.BUNDLE,
                                when=NotifyType.BOTH)

        @notifications.notify_around(NotifyEvents.BUNDLE)
        def target():
            pass

        target()

        f.assert_has_calls((
            mock.call(NotifyEvents.BUNDLE, NotifyType.BEFORE, uuid=mock.ANY),
            mock.call(NotifyEvents.BUNDLE, NotifyType.AFTER, uuid=mock.ANY)))
 def test_notify_subscribe_all_events(self):
     f = mock.Mock()
     notifications.subscribe(f, when=NotifyType.ALL)
     notifications.notify(NotifyEvents.BUNDLE, "hello",
                          when=NotifyType.BEFORE, thing="thing")
     f.assert_called_once_with(
         NotifyEvents.BUNDLE, NotifyType.BEFORE, "hello", thing="thing")
     f.reset_mock()
     notifications.notify(NotifyEvents.BUNDLE, "hello",
                          when=NotifyType.AFTER, thing="thing")
     f.assert_called_once_with(
         NotifyEvents.BUNDLE, NotifyType.AFTER, "hello", thing="thing")
     f.reset_mock()
     notifications.notify(NotifyEvents.BUNDLE, "hello",
                          when=NotifyType.EXCEPTION, thing="thing")
     f.assert_called_once_with(
         NotifyEvents.BUNDLE, NotifyType.EXCEPTION, "hello", thing="thing")
    def test_notify_function_raises(self):
        f = mock.Mock()

        class ExceptionTest(Exception):
            pass

        def raise_(*args, **kwargs):
            raise ExceptionTest("bang")

        f.side_effect = raise_

        notifications.subscribe(f, event=NotifyEvents.BUNDLE,
                                when=NotifyType.BEFORE)

        with self.assertRaises(ExceptionTest):
            notifications.notify(NotifyEvents.BUNDLE, "hello",
                                 when=NotifyType.BEFORE, thing="thing")
        f.assert_called_once_with(
            NotifyEvents.BUNDLE, NotifyType.BEFORE, "hello", thing="thing")
    def test_noitify_around_with_exception(self):
        f = mock.Mock()

        notifications.subscribe(f, event=NotifyEvents.BUNDLE,
                                when=NotifyType.ALL)

        class ExceptionTest(Exception):
            pass

        @notifications.notify_around(NotifyEvents.BUNDLE, "hello", this="that")
        def target():
            raise ExceptionTest('bang')

        with self.assertRaises(ExceptionTest):
            target()

        f.assert_has_calls((
            mock.call(NotifyEvents.BUNDLE, NotifyType.BEFORE, "hello",
                      this="that", uuid=mock.ANY),
            mock.call(NotifyEvents.BUNDLE, NotifyType.EXCEPTION, "hello",
                      this="that", uuid=mock.ANY)))
Example #12
0
    def __init__(self, env_deployments):
        """Initialise the Events Plugin.

        :param env_deployments: the deployments/tests that will happen.
        :type env_deployments: List[EnvironmentDeploy]
        """
        self.env_deployments = env_deployments

        keep_logs = get_option('zaza-events.keep-logs', False)
        if keep_logs:
            self.logs_dir_base = os.path.join(tempfile.gettempdir(),
                                              "zaza-events")
            Path(self.logs_dir_base).mkdir(parents=True, exist_ok=True)
            logger.debug("Ensuring logs base dir as: %s", self.logs_dir_base)
        else:
            # get a directory that will disappear at the end of the program.
            self.logs_dir_base = tempfile.TemporaryDirectory("-zaza-events")

        # Handle all notifications and turn them into zaza.events for the
        # timeseries event logging.
        subscribe(self.handle_notifications, event=None, when=NotifyType.BOTH)

        # Handle the BEFORE and AFTER bundle notifications to actually create
        # and finalise the events logger after each deployment.
        subscribe(self.handle_before_bundle,
                  event=NotifyEvents.BUNDLE,
                  when=NotifyType.BEFORE)
        subscribe(self.handle_after_bundle,
                  event=NotifyEvents.BUNDLE,
                  when=NotifyType.AFTER)
        logger.info("Configured EventsPlugin.")
 def test_subscribe_all_BEFORE(self):
     notifications.subscribe(self.f, when=NotifyType.BEFORE)
     # same as _all above)
     self._add_pattern(NotifyEvents, NotifyType.BEFORE)
     self._asserts(self.f)
 def test_subscribe_all(self):
     notifications.subscribe(self.f)
     self._add_pattern(NotifyEvents, NotifyType.BEFORE)
     self._asserts(self.f)
 def test_unsubscribe_all_BEFORE(self):
     notifications.subscribe(self.f, when=NotifyType.BEFORE)
     notifications.unsubscribe(self.f, when=NotifyType.BEFORE)
     self._asserts(self.f)
 def test_unsubscribe_all(self):
     notifications.subscribe(self.f)
     notifications.unsubscribe(self.f)
     self._asserts(self.f)