コード例 #1
0
    def setUp(self):
        patcher1 = patch('charlesbot_pagerduty_escalations.pagerdutyescalations.PagerdutyEscalations.load_config')  # NOQA
        self.addCleanup(patcher1.stop)
        self.mock_load_config = patcher1.start()

        patcher2 = patch('charlesbot_pagerduty_escalations.pagerdutyescalations.SlackUser')  # NOQA
        self.addCleanup(patcher2.stop)
        self.mock_slack_user = patcher2.start()

        patcher3 = patch('charlesbot_pagerduty_escalations.pagerdutyescalations.http_post_request')  # NOQA
        self.addCleanup(patcher3.stop)
        self.mock_http_post_request = patcher3.start()

        patcher4 = patch('charlesbot_pagerduty_escalations.pagerdutyescalations.PagerdutyEscalations.get_trigger_payload')  # NOQA
        self.addCleanup(patcher4.stop)
        self.mock_get_trigger_payload = patcher4.start()

        self.slack_message = SlackMessage(type="message",
                                          user="******",
                                          channel="C2147483705",
                                          text="This is a dummy message")

        from charlesbot_pagerduty_escalations.pagerdutyescalations import PagerdutyEscalations  # NOQA
        self.pd = PagerdutyEscalations()
        self.pd.slack = MagicMock()
        self.pd.slack.send_channel_message = MagicMock()
        self.pd.pd_service_mappings = {
            "service1": "pd_key_1"
        }
    def setUp(self):
        patcher1 = patch(
            'charlesbot_pagerduty_escalations.pagerdutyescalations.PagerdutyEscalations.load_config'
        )  # NOQA
        self.addCleanup(patcher1.stop)
        self.mock_load_config = patcher1.start()

        self.slack_message = SlackMessage(type="message",
                                          user="******",
                                          channel="C2147483705",
                                          text="This is a dummy message")

        from charlesbot_pagerduty_escalations.pagerdutyescalations import PagerdutyEscalations  # NOQA
        self.pd = PagerdutyEscalations()
        self.pd.slack.send_channel_message = MagicMock()
    def setUp(self):
        patcher1 = patch(
            'charlesbot_pagerduty_escalations.pagerdutyescalations.PagerdutyEscalations.load_config'
        )  # NOQA
        self.addCleanup(patcher1.stop)
        self.mock_load_config = patcher1.start()

        patcher2 = patch(
            'charlesbot_pagerduty_escalations.pagerdutyescalations.PagerdutyEscalations.print_service_list'
        )  # NOQA
        self.addCleanup(patcher2.stop)
        self.mock_print_service_list = patcher2.start()

        patcher3 = patch(
            'charlesbot_pagerduty_escalations.pagerdutyescalations.PagerdutyEscalations.trigger_escalation_incident'
        )  # NOQA
        self.addCleanup(patcher3.stop)
        self.mock_trigger_escalation_incident = patcher3.start()

        from charlesbot_pagerduty_escalations.pagerdutyescalations import PagerdutyEscalations  # NOQA
        self.pd = PagerdutyEscalations()
    def setUp(self):
        patcher1 = patch('charlesbot_pagerduty_escalations.pagerdutyescalations.PagerdutyEscalations.load_config')  # NOQA
        self.addCleanup(patcher1.stop)
        self.mock_load_config = patcher1.start()

        self.slack_message = SlackMessage(type="message",
                                          user="******",
                                          channel="C2147483705",
                                          text="This is a dummy message")

        from charlesbot_pagerduty_escalations.pagerdutyescalations import PagerdutyEscalations  # NOQA
        self.pd = PagerdutyEscalations()
        self.pd.slack.send_channel_message = MagicMock()
    def setUp(self):
        patcher1 = patch('charlesbot_pagerduty_escalations.pagerdutyescalations.PagerdutyEscalations.load_config')  # NOQA
        self.addCleanup(patcher1.stop)
        self.mock_load_config = patcher1.start()

        patcher2 = patch('charlesbot_pagerduty_escalations.pagerdutyescalations.PagerdutyEscalations.print_service_list')  # NOQA
        self.addCleanup(patcher2.stop)
        self.mock_print_service_list = patcher2.start()

        patcher3 = patch('charlesbot_pagerduty_escalations.pagerdutyescalations.PagerdutyEscalations.trigger_escalation_incident')  # NOQA
        self.addCleanup(patcher3.stop)
        self.mock_trigger_escalation_incident = patcher3.start()

        from charlesbot_pagerduty_escalations.pagerdutyescalations import PagerdutyEscalations  # NOQA
        self.pd = PagerdutyEscalations()
class TestStringFunctions(asynctest.TestCase):

    def setUp(self):
        patcher1 = patch('charlesbot_pagerduty_escalations.pagerdutyescalations.PagerdutyEscalations.load_config')  # NOQA
        self.addCleanup(patcher1.stop)
        self.mock_load_config = patcher1.start()

        self.slack_message = SlackMessage(type="message",
                                          user="******",
                                          channel="C2147483705",
                                          text="This is a dummy message")

        from charlesbot_pagerduty_escalations.pagerdutyescalations import PagerdutyEscalations  # NOQA
        self.pd = PagerdutyEscalations()
        self.pd.slack.send_channel_message = MagicMock()

    @asynctest.ignore_loop
    def test_help_msg_two_entries(self):
        help_msg = self.pd.get_help_message()
        self.assertEqual(help_msg.count('\n'), 1)

    def test_print_service_empty_list(self):
        self.pd.pd_service_mappings = {}
        yield from self.pd.print_service_list(self.slack_message)
        expected_call = call("C2147483705", "```\n\n```")
        self.pd.slack.send_channel_message.assert_has_calls([expected_call])

    def test_print_service_one_item(self):
        self.pd.pd_service_mappings = {
            "service1": "key1"
        }
        yield from self.pd.print_service_list(self.slack_message)
        expected_call = call("C2147483705", "```\nservice1\n```")
        self.pd.slack.send_channel_message.assert_has_calls([expected_call])

    def test_print_service_two_items(self):
        self.pd.pd_service_mappings = {
            "service1": "key1",
            "service2": "key2"
        }
        yield from self.pd.print_service_list(self.slack_message)
        expected_call = call("C2147483705", "```\nservice1\nservice2\n```")
        self.pd.slack.send_channel_message.assert_has_calls([expected_call])

    def test_print_service_two_items_sorted(self):
        self.pd.pd_service_mappings = {
            "service2": "key1",
            "service1": "key2"
        }
        yield from self.pd.print_service_list(self.slack_message)
        expected_call = call("C2147483705", "```\nservice1\nservice2\n```")
        self.pd.slack.send_channel_message.assert_has_calls([expected_call])

    @asynctest.ignore_loop
    def test_get_trigger_payload_no_custom_message(self):
        expected_output = {
            "service_key": "key1",
            "event_type": "trigger",
            "description": "Service escalation for website (triggered by marvin)",  # NOQA
            "incident_key": "website-service-escalation",
            "client": "charlesbot",
        }
        received_output = self.pd.get_trigger_payload(
            service_key="key1",
            service_name="website",
            escalating_user="******"
        )
        self.assertEqual(received_output, expected_output)

    @asynctest.ignore_loop
    def test_get_trigger_payload_with_custom_message(self):
        expected_output = {
            "service_key": "key1",
            "event_type": "trigger",
            "description": "Service escalation for website (triggered by marvin) -- halp plz",  # NOQA
            "incident_key": "website-service-escalation",
            "client": "charlesbot",
        }
        received_output = self.pd.get_trigger_payload(
            service_key="key1",
            service_name="website",
            escalating_user="******",
            custom_message="halp plz"
        )
        self.assertEqual(received_output, expected_output)
class TestPagerdutyEscalations(asynctest.TestCase):

    def setUp(self):
        patcher1 = patch('charlesbot_pagerduty_escalations.pagerdutyescalations.PagerdutyEscalations.load_config')  # NOQA
        self.addCleanup(patcher1.stop)
        self.mock_load_config = patcher1.start()

        patcher2 = patch('charlesbot_pagerduty_escalations.pagerdutyescalations.PagerdutyEscalations.print_service_list')  # NOQA
        self.addCleanup(patcher2.stop)
        self.mock_print_service_list = patcher2.start()

        patcher3 = patch('charlesbot_pagerduty_escalations.pagerdutyescalations.PagerdutyEscalations.trigger_escalation_incident')  # NOQA
        self.addCleanup(patcher3.stop)
        self.mock_trigger_escalation_incident = patcher3.start()

        from charlesbot_pagerduty_escalations.pagerdutyescalations import PagerdutyEscalations  # NOQA
        self.pd = PagerdutyEscalations()

    def test_process_message_non_slack_message(self):
        message = "!lassie services this is my message to you ooh ooh"
        yield from self.pd.process_message(message)
        self.assertEqual(self.mock_print_service_list.mock_calls, [])
        self.assertEqual(self.mock_trigger_escalation_incident.mock_calls, [])

    def test_process_message_non_lassie_message(self):
        message = SlackMessage(type="message",
                               user="******",
                               channel="C2147483705",
                               text="This is my message to you ooh ooh")
        yield from self.pd.process_message(message)
        self.assertEqual(self.mock_print_service_list.mock_calls, [])
        self.assertEqual(self.mock_trigger_escalation_incident.mock_calls, [])

    def test_process_message_invalid_lassie_message(self):
        message = SlackMessage(type="message",
                               user="******",
                               channel="C2147483705",
                               text="!lassie")
        yield from self.pd.process_message(message)
        self.assertEqual(self.mock_print_service_list.mock_calls, [])
        self.assertEqual(self.mock_trigger_escalation_incident.mock_calls, [])

    def test_process_message_services_message_one(self):
        message = SlackMessage(type="message",
                               user="******",
                               channel="C2147483705",
                               text="!lassie services")
        yield from self.pd.process_message(message)
        expected_call = call(message)
        self.assertEqual(self.mock_print_service_list.mock_calls,
                         [expected_call])
        self.assertEqual(self.mock_trigger_escalation_incident.mock_calls, [])

    def test_process_message_services_message_two(self):
        message = SlackMessage(type="message",
                               user="******",
                               channel="C2147483705",
                               text="!lassie services some filler we ignore")
        yield from self.pd.process_message(message)
        expected_call = call(message)
        self.assertEqual(self.mock_print_service_list.mock_calls,
                         [expected_call])
        self.assertEqual(self.mock_trigger_escalation_incident.mock_calls, [])

    def test_process_message_escalate_one(self):
        message = SlackMessage(type="message",
                               user="******",
                               channel="C2147483705",
                               text="!lassie website")
        yield from self.pd.process_message(message)
        expected_call = call("website", message)
        self.assertEqual(self.mock_print_service_list.mock_calls, [])
        self.assertEqual(self.mock_trigger_escalation_incident.mock_calls,
                         [expected_call])

    def test_process_message_escalate_two(self):
        message = SlackMessage(type="message",
                               user="******",
                               channel="C2147483705",
                               text="!lassie website halp plz")
        yield from self.pd.process_message(message)
        expected_call = call("website halp plz", message)
        self.assertEqual(self.mock_print_service_list.mock_calls, [])
        self.assertEqual(self.mock_trigger_escalation_incident.mock_calls,
                         [expected_call])
class TestPagerdutyEscalations(asynctest.TestCase):
    def setUp(self):
        patcher1 = patch(
            'charlesbot_pagerduty_escalations.pagerdutyescalations.PagerdutyEscalations.load_config'
        )  # NOQA
        self.addCleanup(patcher1.stop)
        self.mock_load_config = patcher1.start()

        patcher2 = patch(
            'charlesbot_pagerduty_escalations.pagerdutyescalations.PagerdutyEscalations.print_service_list'
        )  # NOQA
        self.addCleanup(patcher2.stop)
        self.mock_print_service_list = patcher2.start()

        patcher3 = patch(
            'charlesbot_pagerduty_escalations.pagerdutyescalations.PagerdutyEscalations.trigger_escalation_incident'
        )  # NOQA
        self.addCleanup(patcher3.stop)
        self.mock_trigger_escalation_incident = patcher3.start()

        from charlesbot_pagerduty_escalations.pagerdutyescalations import PagerdutyEscalations  # NOQA
        self.pd = PagerdutyEscalations()

    def test_process_message_non_slack_message(self):
        message = "!lassie services this is my message to you ooh ooh"
        yield from self.pd.process_message(message)
        self.assertEqual(self.mock_print_service_list.mock_calls, [])
        self.assertEqual(self.mock_trigger_escalation_incident.mock_calls, [])

    def test_process_message_non_lassie_message(self):
        message = SlackMessage(type="message",
                               user="******",
                               channel="C2147483705",
                               text="This is my message to you ooh ooh")
        yield from self.pd.process_message(message)
        self.assertEqual(self.mock_print_service_list.mock_calls, [])
        self.assertEqual(self.mock_trigger_escalation_incident.mock_calls, [])

    def test_process_message_invalid_lassie_message(self):
        message = SlackMessage(type="message",
                               user="******",
                               channel="C2147483705",
                               text="!lassie")
        yield from self.pd.process_message(message)
        self.assertEqual(self.mock_print_service_list.mock_calls, [])
        self.assertEqual(self.mock_trigger_escalation_incident.mock_calls, [])

    def test_process_message_services_message_one(self):
        message = SlackMessage(type="message",
                               user="******",
                               channel="C2147483705",
                               text="!lassie services")
        yield from self.pd.process_message(message)
        expected_call = call(message)
        self.assertEqual(self.mock_print_service_list.mock_calls,
                         [expected_call])
        self.assertEqual(self.mock_trigger_escalation_incident.mock_calls, [])

    def test_process_message_services_message_two(self):
        message = SlackMessage(type="message",
                               user="******",
                               channel="C2147483705",
                               text="!lassie services some filler we ignore")
        yield from self.pd.process_message(message)
        expected_call = call(message)
        self.assertEqual(self.mock_print_service_list.mock_calls,
                         [expected_call])
        self.assertEqual(self.mock_trigger_escalation_incident.mock_calls, [])

    def test_process_message_escalate_one(self):
        message = SlackMessage(type="message",
                               user="******",
                               channel="C2147483705",
                               text="!lassie website")
        yield from self.pd.process_message(message)
        expected_call = call("website", message)
        self.assertEqual(self.mock_print_service_list.mock_calls, [])
        self.assertEqual(self.mock_trigger_escalation_incident.mock_calls,
                         [expected_call])

    def test_process_message_escalate_two(self):
        message = SlackMessage(type="message",
                               user="******",
                               channel="C2147483705",
                               text="!lassie website halp plz")
        yield from self.pd.process_message(message)
        expected_call = call("website halp plz", message)
        self.assertEqual(self.mock_print_service_list.mock_calls, [])
        self.assertEqual(self.mock_trigger_escalation_incident.mock_calls,
                         [expected_call])
コード例 #9
0
class TestTriggerEscalationIncident(asynctest.TestCase):

    def setUp(self):
        patcher1 = patch('charlesbot_pagerduty_escalations.pagerdutyescalations.PagerdutyEscalations.load_config')  # NOQA
        self.addCleanup(patcher1.stop)
        self.mock_load_config = patcher1.start()

        patcher2 = patch('charlesbot_pagerduty_escalations.pagerdutyescalations.SlackUser')  # NOQA
        self.addCleanup(patcher2.stop)
        self.mock_slack_user = patcher2.start()

        patcher3 = patch('charlesbot_pagerduty_escalations.pagerdutyescalations.http_post_request')  # NOQA
        self.addCleanup(patcher3.stop)
        self.mock_http_post_request = patcher3.start()

        patcher4 = patch('charlesbot_pagerduty_escalations.pagerdutyescalations.PagerdutyEscalations.get_trigger_payload')  # NOQA
        self.addCleanup(patcher4.stop)
        self.mock_get_trigger_payload = patcher4.start()

        self.slack_message = SlackMessage(type="message",
                                          user="******",
                                          channel="C2147483705",
                                          text="This is a dummy message")

        from charlesbot_pagerduty_escalations.pagerdutyescalations import PagerdutyEscalations  # NOQA
        self.pd = PagerdutyEscalations()
        self.pd.slack = MagicMock()
        self.pd.slack.send_channel_message = MagicMock()
        self.pd.pd_service_mappings = {
            "service1": "pd_key_1"
        }

    def test_service_key_not_found_no_message(self):
        yield from self.pd.trigger_escalation_incident("website",
                                                       self.slack_message)
        expected_call = call(
            "C2147483705",
            "Could not find key for service 'website', check that the service exists in the config file!"  # NOQA
        )
        self.pd.slack.send_channel_message.assert_has_calls([expected_call])
        self.assertEqual(len(self.mock_slack_user.mock_calls), 0)
        self.assertEqual(len(self.mock_http_post_request.mock_calls), 0)

    def test_service_key_not_found_with_message(self):
        yield from self.pd.trigger_escalation_incident("website halp plz",
                                                       self.slack_message)
        expected_call = call(
            "C2147483705",
            "Could not find key for service 'website', check that the service exists in the config file!"  # NOQA
        )
        self.pd.slack.send_channel_message.assert_has_calls([expected_call])
        self.assertEqual(len(self.mock_slack_user.mock_calls), 0)
        self.assertEqual(len(self.mock_http_post_request.mock_calls), 0)

    def test_failed_http_post(self):
        self.mock_http_post_request.side_effect = [False]
        self.mock_get_trigger_payload.return_value = {
            "payload": "test payload"
        }

        yield from self.pd.trigger_escalation_incident("service1",
                                                       self.slack_message)
        expected_slack_user_call = call(self.pd.slack, "U2147483697")
        self.mock_slack_user.assert_has_calls([expected_slack_user_call])

        expected_http_post_call = call(
            url="https://events.pagerduty.com/generic/2010-04-15/create_event.json",  # NOQA
            payload='{"payload": "test payload"}'
        )
        self.mock_http_post_request.assert_has_calls([expected_http_post_call])

        expected_send_channel_message_call = call(
            "C2147483705",
            "Could not escalate incident for service1. Check the logs for details."  # NOQA
        )
        self.pd.slack.send_channel_message.assert_has_calls(
            [expected_send_channel_message_call]
        )
        self.assertEqual(len(self.pd.slack.send_channel_message.mock_calls), 2)

    def test_successful_http_post(self):
        self.mock_http_post_request.side_effect = [True]
        self.mock_get_trigger_payload.return_value = {
            "payload": "test payload"
        }

        yield from self.pd.trigger_escalation_incident("service1",
                                                       self.slack_message)
        expected_slack_user_call = call(self.pd.slack, "U2147483697")
        self.mock_slack_user.assert_has_calls([expected_slack_user_call])

        expected_http_post_call = call(
            url="https://events.pagerduty.com/generic/2010-04-15/create_event.json",  # NOQA
            payload='{"payload": "test payload"}'
        )
        self.mock_http_post_request.assert_has_calls([expected_http_post_call])

        expected_send_channel_message_call = call(
            "C2147483705",
            "Successfully escalated this service1 incident!"  # NOQA
        )
        self.pd.slack.send_channel_message.assert_has_calls(
            [expected_send_channel_message_call]
        )
        self.assertEqual(len(self.pd.slack.send_channel_message.mock_calls), 2)
class TestStringFunctions(asynctest.TestCase):
    def setUp(self):
        patcher1 = patch(
            'charlesbot_pagerduty_escalations.pagerdutyescalations.PagerdutyEscalations.load_config'
        )  # NOQA
        self.addCleanup(patcher1.stop)
        self.mock_load_config = patcher1.start()

        self.slack_message = SlackMessage(type="message",
                                          user="******",
                                          channel="C2147483705",
                                          text="This is a dummy message")

        from charlesbot_pagerduty_escalations.pagerdutyescalations import PagerdutyEscalations  # NOQA
        self.pd = PagerdutyEscalations()
        self.pd.slack.send_channel_message = MagicMock()

    @asynctest.ignore_loop
    def test_help_msg_two_entries(self):
        help_msg = self.pd.get_help_message()
        self.assertEqual(help_msg.count('\n'), 1)

    def test_print_service_empty_list(self):
        self.pd.pd_service_mappings = {}
        yield from self.pd.print_service_list(self.slack_message)
        expected_call = call("C2147483705", "```\n\n```")
        self.pd.slack.send_channel_message.assert_has_calls([expected_call])

    def test_print_service_one_item(self):
        self.pd.pd_service_mappings = {"service1": "key1"}
        yield from self.pd.print_service_list(self.slack_message)
        expected_call = call("C2147483705", "```\nservice1\n```")
        self.pd.slack.send_channel_message.assert_has_calls([expected_call])

    def test_print_service_two_items(self):
        self.pd.pd_service_mappings = {"service1": "key1", "service2": "key2"}
        yield from self.pd.print_service_list(self.slack_message)
        expected_call = call("C2147483705", "```\nservice1\nservice2\n```")
        self.pd.slack.send_channel_message.assert_has_calls([expected_call])

    def test_print_service_two_items_sorted(self):
        self.pd.pd_service_mappings = {"service2": "key1", "service1": "key2"}
        yield from self.pd.print_service_list(self.slack_message)
        expected_call = call("C2147483705", "```\nservice1\nservice2\n```")
        self.pd.slack.send_channel_message.assert_has_calls([expected_call])

    @asynctest.ignore_loop
    def test_get_trigger_payload_no_custom_message(self):
        expected_output = {
            "service_key": "key1",
            "event_type": "trigger",
            "description":
            "Service escalation for website (triggered by marvin)",  # NOQA
            "incident_key": "website-service-escalation",
            "client": "charlesbot",
        }
        received_output = self.pd.get_trigger_payload(service_key="key1",
                                                      service_name="website",
                                                      escalating_user="******")
        self.assertEqual(received_output, expected_output)

    @asynctest.ignore_loop
    def test_get_trigger_payload_with_custom_message(self):
        expected_output = {
            "service_key": "key1",
            "event_type": "trigger",
            "description":
            "Service escalation for website (triggered by marvin) -- halp plz",  # NOQA
            "incident_key": "website-service-escalation",
            "client": "charlesbot",
        }
        received_output = self.pd.get_trigger_payload(
            service_key="key1",
            service_name="website",
            escalating_user="******",
            custom_message="halp plz")
        self.assertEqual(received_output, expected_output)