def get(self, request):
        organization = Organization(slug="myorg")
        project = Project(id=30, slug="myproj")

        incident = Incident(identifier=123,
                            organization=organization,
                            title="Something broke")
        alert_rule = AlertRule(id=1,
                               organization=organization,
                               aggregation=1,
                               query="is:unresolved")
        alert_rule_trigger = AlertRuleTrigger(id=5,
                                              alert_rule=alert_rule,
                                              alert_threshold=100,
                                              resolve_threshold=50)
        action = AlertRuleTriggerAction(id=10,
                                        alert_rule_trigger=alert_rule_trigger)

        handler = EmailActionHandler(action, incident, project)
        email = handler.build_message(
            handler.generate_email_context(TriggerStatus.ACTIVE),
            TriggerStatus.ACTIVE, 1)
        return MailPreview(html_template=email.html_template,
                           text_template=email.template,
                           context=email.context).render(request)
예제 #2
0
    def test(self):
        # Full integration test to ensure that when a subscription receives an update
        # the `QuerySubscriptionConsumer` successfully retries the subscription and
        # calls the correct callback, which should result in an incident being created.

        callback = subscriber_registry[INCIDENTS_SNUBA_SUBSCRIPTION_TYPE]

        def exception_callback(*args, **kwargs):
            # We want to just error after the callback so that we can see the result of
            # processing. This means the offset won't be committed, but that's fine, we
            # can still check the results.
            callback(*args, **kwargs)
            raise KeyboardInterrupt()

        value_name = query_aggregation_to_snuba[QueryAggregations(
            self.subscription.aggregation)][2]

        subscriber_registry[
            INCIDENTS_SNUBA_SUBSCRIPTION_TYPE] = exception_callback
        message = {
            "version": 1,
            "payload": {
                "subscription_id": self.subscription.subscription_id,
                "values": {
                    value_name: self.trigger.alert_threshold + 1
                },
                "timestamp": 1235,
                "interval": 5,
                "partition": 50,
                "offset": 10,
            },
        }
        self.producer.produce(self.topic, json.dumps(message))
        self.producer.flush()

        def active_incident():
            return Incident.objects.filter(
                type=IncidentType.ALERT_TRIGGERED.value,
                status=IncidentStatus.OPEN.value,
                alert_rule=self.rule,
            )

        consumer = QuerySubscriptionConsumer("hi", topic=self.topic)
        with self.assertChanges(lambda: active_incident().exists(),
                                before=False,
                                after=True), self.tasks():
            consumer.run()

        assert len(mail.outbox) == 1
        handler = EmailActionHandler(self.action,
                                     active_incident().get(), self.project)
        message = handler.build_message(
            handler.generate_email_context(TriggerStatus.ACTIVE),
            TriggerStatus.ACTIVE, self.user.id)

        out = mail.outbox[0]
        assert out.to == [self.user.email]
        assert out.subject == message.subject
        built_message = message.build(self.user.email)
        assert out.body == built_message.body
예제 #3
0
파일: test_tasks.py 프로젝트: pasala91/test
    def test(self):
        # Full integration test to ensure that when a subscription receives an update
        # the `QuerySubscriptionConsumer` successfully retries the subscription and
        # calls the correct callback, which should result in an incident being created.

        message = {
            "version": 1,
            "payload": {
                "subscription_id": self.subscription.subscription_id,
                "values": {"data": [{"some_col": self.trigger.alert_threshold + 1}]},
                "timestamp": "2020-01-01T01:23:45.1234",
            },
        }
        self.producer.produce(self.topic, json.dumps(message))
        self.producer.flush()

        def active_incident():
            return Incident.objects.filter(
                type=IncidentType.ALERT_TRIGGERED.value, alert_rule=self.rule
            ).exclude(status=IncidentStatus.CLOSED.value)

        consumer = QuerySubscriptionConsumer("hi", topic=self.topic)

        original_callback = subscriber_registry[INCIDENTS_SNUBA_SUBSCRIPTION_TYPE]

        def shutdown_callback(*args, **kwargs):
            # We want to just exit after the callback so that we can see the result of
            # processing.
            original_callback(*args, **kwargs)
            consumer.shutdown()

        subscriber_registry[INCIDENTS_SNUBA_SUBSCRIPTION_TYPE] = shutdown_callback

        with self.feature(["organizations:incidents", "organizations:performance-view"]):
            with self.assertChanges(
                lambda: active_incident().exists(), before=False, after=True
            ), self.tasks(), self.capture_on_commit_callbacks(execute=True):
                consumer.run()

        assert len(mail.outbox) == 1
        handler = EmailActionHandler(self.action, active_incident().get(), self.project)
        message = handler.build_message(
            generate_incident_trigger_email_context(
                handler.project,
                handler.incident,
                handler.action.alert_rule_trigger,
                TriggerStatus.ACTIVE,
            ),
            TriggerStatus.ACTIVE,
            self.user.id,
        )

        out = mail.outbox[0]
        assert out.to == [self.user.email]
        assert out.subject == message.subject
        built_message = message.build(self.user.email)
        assert out.body == built_message.body