Exemplo n.º 1
0
    def test_send_alerts_stops_sending_if_an_alert_is_not_successfully_sent(
            self, mock_alert, mock_connection) -> None:
        mock_alert.return_value = RequestStatus.FAILED
        mock_connection.return_value.sleep.return_value = None
        test_alert_1 = OpenFileDescriptorsIncreasedAboveThresholdAlert(
            self.test_system_name, self.test_percentage_usage,
            self.test_panic_severity,
            datetime.now().timestamp(), self.test_panic_severity,
            self.test_parent_id, self.test_system_id)
        test_alert_2 = OpenFileDescriptorsIncreasedAboveThresholdAlert(
            self.test_system_name, self.test_percentage_usage,
            self.test_panic_severity,
            datetime.now().timestamp() + 1, self.test_panic_severity,
            self.test_parent_id, self.test_system_id)
        test_queue = Queue(4)
        self.test_opsgenie_alerts_handler._alerts_queue = test_queue
        test_queue.put(test_alert_1)
        test_queue.put(test_alert_2)

        self.test_opsgenie_alerts_handler._send_alerts()

        self.assertFalse(
            self.test_opsgenie_alerts_handler.alerts_queue.empty())
        self.assertEqual(
            2, self.test_opsgenie_alerts_handler.alerts_queue.qsize())
        self.assertEqual(
            test_alert_1,
            self.test_opsgenie_alerts_handler.alerts_queue.queue[0])
        self.assertEqual(
            test_alert_2,
            self.test_opsgenie_alerts_handler.alerts_queue.queue[1])
Exemplo n.º 2
0
    def setUp(self) -> None:
        self.test_channel_name = 'test_email'
        self.test_channel_id = 'test_email_id12345'
        self.dummy_logger = logging.getLogger('Dummy')
        self.dummy_logger.disabled = True
        self.test_emails_to = [
            '*****@*****.**', '*****@*****.**', '*****@*****.**'
        ]
        self.test_smtp = 'test smtp server'
        self.test_sender = 'test sender'
        self.test_username = '******'
        self.test_password = '******'
        self.test_port = 10

        self.test_email_api = EmailApi(self.test_smtp, self.test_sender,
                                       self.test_username, self.test_password,
                                       self.test_port)

        self.test_email_channel = EmailChannel(self.test_channel_name,
                                               self.test_channel_id,
                                               self.dummy_logger,
                                               self.test_emails_to,
                                               self.test_email_api)

        self.test_system_name = 'test_system'
        self.test_percentage_usage = 50
        self.test_panic_severity = 'WARNING'
        self.test_last_monitored = 45
        self.test_parent_id = 'parent_1234'
        self.test_system_id = 'system_id32423'
        self.test_alert = OpenFileDescriptorsIncreasedAboveThresholdAlert(
            self.test_system_name, self.test_percentage_usage,
            self.test_panic_severity, self.test_last_monitored,
            self.test_panic_severity, self.test_parent_id, self.test_system_id)
Exemplo n.º 3
0
    def test_alert_logs_alert_correctly(
            self, alert_severity, mock_logger_info, mock_logger_critical,
            mock_logger_warning, mock_logger_error) -> None:
        # This test will be performed by checking that the correct parameters
        # have been passed to the appropriate logger function.
        self.test_alert = OpenFileDescriptorsIncreasedAboveThresholdAlert(
            self.test_system_name, self.test_percentage_usage,
            alert_severity.value, self.test_last_monitored,
            alert_severity.value, self.test_parent_id, self.test_system_id
        )
        expected_msg = "PANIC {} - {}".format(alert_severity.value,
                                              self.test_alert.message)

        self.test_log_channel.alert(self.test_alert)

        if alert_severity == Severity.INFO:
            mock_logger_info.assert_has_calls([call(expected_msg)])
            assert_not_called_with(mock_logger_error, [call(expected_msg)])
            assert_not_called_with(mock_logger_warning, [call(expected_msg)])
            assert_not_called_with(mock_logger_critical, [call(expected_msg)])
        elif alert_severity == Severity.WARNING:
            mock_logger_warning.assert_has_calls([call(expected_msg)])
            assert_not_called_with(mock_logger_error, [call(expected_msg)])
            assert_not_called_with(mock_logger_info, [call(expected_msg)])
            assert_not_called_with(mock_logger_critical, [call(expected_msg)])
        elif alert_severity == Severity.CRITICAL:
            mock_logger_critical.assert_has_calls([call(expected_msg)])
            assert_not_called_with(mock_logger_error, [call(expected_msg)])
            assert_not_called_with(mock_logger_warning, [call(expected_msg)])
            assert_not_called_with(mock_logger_info, [call(expected_msg)])
        elif alert_severity == Severity.ERROR:
            mock_logger_error.assert_has_calls([call(expected_msg)])
            assert_not_called_with(mock_logger_info, [call(expected_msg)])
            assert_not_called_with(mock_logger_warning, [call(expected_msg)])
            assert_not_called_with(mock_logger_critical, [call(expected_msg)])
Exemplo n.º 4
0
    def setUp(self) -> None:
        self.test_api_key = 'test api key'
        self.test_eu_host_true = True
        self.test_eu_host_false = False
        self.test_system_name = 'test system'
        self.test_percentage_usage = 50
        self.test_panic_severity = 'WARNING'
        self.test_last_monitored = 45.5
        self.test_parent_id = 'chain123'
        self.test_system_id = 'system123'
        self.test_alert = OpenFileDescriptorsIncreasedAboveThresholdAlert(
            self.test_system_name, self.test_percentage_usage,
            self.test_panic_severity, self.test_last_monitored,
            self.test_panic_severity, self.test_parent_id, self.test_system_id
        )
        self.test_opsgenie_severity = OpsgenieSeverities.WARNING
        self.test_alias = self.test_alert.alert_code.value

        self.test_alert_create_payload = opsgenie_sdk.CreateAlertPayload(
            message=self.test_alert.alert_code.name,
            description="Message: {} \n Triggered at: {}".format(
                self.test_alert.message, datetime.fromtimestamp(
                    self.test_alert.timestamp)),
            priority=self.test_opsgenie_severity.value,
            source=self.test_alert.origin_id, alias=self.test_alias
        )

        self.test_opsgenie_api_eu = OpsgenieApi(self.test_api_key,
                                                self.test_eu_host_true)
        self.test_opsgenie_api_non_eu = OpsgenieApi(self.test_api_key,
                                                    self.test_eu_host_false)
Exemplo n.º 5
0
    def test_call_using_twilio_calls_all_if_validity_threshold_not_exceeded(
            self, exceeding_factor, mock_alert, mock_sleep) -> None:
        mock_alert.return_value = RequestStatus.SUCCESS
        mock_sleep.return_value = None
        test_alert = OpenFileDescriptorsIncreasedAboveThresholdAlert(
            self.test_system_name, self.test_percentage_usage,
            self.test_panic_severity,
            datetime.now().timestamp() - self.test_alert_validity_threshold +
            exceeding_factor, self.test_panic_severity, self.test_parent_id,
            self.test_system_id)

        ret = self.test_twilio_alerts_handler._call_using_twilio(test_alert)
        self.assertEqual(RequestStatus.SUCCESS, ret)
        expected_calls = [
            call(call_from=self.test_call_from,
                 call_to=self.test_call_to[0],
                 twiml=self.test_twiml,
                 twiml_is_url=self.test_twiml_is_url),
            call(call_from=self.test_call_from,
                 call_to=self.test_call_to[1],
                 twiml=self.test_twiml,
                 twiml_is_url=self.test_twiml_is_url),
            call(call_from=self.test_call_from,
                 call_to=self.test_call_to[2],
                 twiml=self.test_twiml,
                 twiml_is_url=self.test_twiml_is_url)
        ]
        actual_calls = mock_alert.call_args_list
        self.assertEqual(expected_calls, actual_calls)
Exemplo n.º 6
0
    def setUp(self) -> None:
        self.test_smtp = 'test smtp server'
        self.test_sender = 'test sender'
        self.test_username = '******'
        self.test_password = '******'
        self.test_port = 10
        self.test_message = 'This is a test alert'
        self.test_subject = 'Test Alert'
        self.test_sender = 'PANIC alerter'
        self.test_receiver = '*****@*****.**'

        self.test_email_api = EmailApi(self.test_smtp, self.test_sender,
                                       self.test_username, self.test_password,
                                       self.test_port)

        self.test_system_name = 'test system'
        self.test_percentage_usage = 50
        self.test_severity = 'WARNING'
        self.test_last_monitored = 45.5
        self.test_parent_id = 'chain123'
        self.test_system_id = 'system123'
        self.test_alert = OpenFileDescriptorsIncreasedAboveThresholdAlert(
            self.test_system_name, self.test_percentage_usage,
            self.test_severity, self.test_last_monitored, self.test_severity,
            self.test_parent_id, self.test_system_id)

        self.test_msg = EmailMessage()
        self.test_msg.set_content("{}\nDate - {}".format(
            self.test_message, datetime.now()))
        self.test_msg['Subject'] = self.test_subject
        self.test_msg['From'] = self.test_sender
        self.test_msg['To'] = self.test_receiver

        # No host and port are given to avoid creating an smtp session
        self.test_smtp_interface = smtplib.SMTP()
Exemplo n.º 7
0
    def setUp(self) -> None:
        self.test_channel_name = 'test_telegram_channel'
        self.test_channel_id = 'test_telegram_id12345'
        self.dummy_logger = logging.getLogger('Dummy')
        self.dummy_logger.disabled = True
        self.test_bot_token = 'test_bot_token'
        self.test_bot_chat_id = 'test_bot_chat_id'
        self.test_base_url = \
            "https://api.telegram.org/bot" + self.test_bot_token

        self.test_telegram_bot_api = TelegramBotApi(self.test_bot_token,
                                                    self.test_bot_chat_id)
        self.test_telegram_channel = TelegramChannel(
            self.test_channel_name, self.test_channel_id, self.dummy_logger,
            self.test_telegram_bot_api)

        self.test_system_name = 'test_system'
        self.test_percentage_usage = 50
        self.test_panic_severity = 'WARNING'
        self.test_last_monitored = 45
        self.test_parent_id = 'parent_1234'
        self.test_system_id = 'system_id32423'
        self.test_alert = OpenFileDescriptorsIncreasedAboveThresholdAlert(
            self.test_system_name, self.test_percentage_usage,
            self.test_panic_severity, self.test_last_monitored,
            self.test_panic_severity, self.test_parent_id, self.test_system_id
        )
Exemplo n.º 8
0
    def test_send_alerts_discards_old_alerts_and_sends_the_recent(
            self, mock_alert) -> None:
        mock_alert.return_value = RequestStatus.SUCCESS
        test_alert_old1 = OpenFileDescriptorsIncreasedAboveThresholdAlert(
            self.test_system_name, self.test_percentage_usage,
            self.test_panic_severity,
            datetime.now().timestamp() - self.test_alert_validity_threshold -
            1, self.test_panic_severity, self.test_parent_id,
            self.test_system_id)
        test_alert_recent1 = OpenFileDescriptorsIncreasedAboveThresholdAlert(
            self.test_system_name, self.test_percentage_usage,
            self.test_panic_severity,
            datetime.now().timestamp() - self.test_alert_validity_threshold,
            self.test_panic_severity, self.test_parent_id, self.test_system_id)
        test_alert_recent2 = OpenFileDescriptorsIncreasedAboveThresholdAlert(
            self.test_system_name, self.test_percentage_usage,
            self.test_panic_severity,
            datetime.now().timestamp(), self.test_panic_severity,
            self.test_parent_id, self.test_system_id)
        test_alert_recent3 = OpenFileDescriptorsIncreasedAboveThresholdAlert(
            self.test_system_name, self.test_percentage_usage,
            self.test_panic_severity,
            datetime.now().timestamp() - self.test_alert_validity_threshold +
            4, self.test_panic_severity, self.test_parent_id,
            self.test_system_id)
        test_queue = Queue(4)
        self.test_pagerduty_alerts_handler._alerts_queue = test_queue
        test_queue.put(test_alert_old1)
        test_queue.put(test_alert_recent1)
        test_queue.put(test_alert_recent2)
        test_queue.put(test_alert_recent3)

        self.test_pagerduty_alerts_handler._send_alerts()

        self.assertTrue(
            self.test_pagerduty_alerts_handler.alerts_queue.empty())
        expected_calls = [
            call(test_alert_recent1),
            call(test_alert_recent2),
            call(test_alert_recent3)
        ]
        actual_calls = mock_alert.call_args_list
        self.assertEqual(expected_calls, actual_calls)
Exemplo n.º 9
0
 def setUp(self) -> None:
     self.test_handler_name = 'test_email_alerts_handler'
     self.dummy_logger = logging.getLogger('Dummy')
     self.dummy_logger.disabled = True
     self.connection_check_time_interval = timedelta(seconds=0)
     self.rabbit_ip = env.RABBIT_IP
     self.rabbitmq = RabbitMQApi(
         self.dummy_logger, self.rabbit_ip,
         connection_check_time_interval=self.connection_check_time_interval)
     self.test_channel_name = 'test_email_channel'
     self.test_channel_id = 'test_email1234'
     self.test_channel_logger = self.dummy_logger.getChild('dummy_channel')
     self.test_emails_to = ['*****@*****.**', '*****@*****.**',
                            '*****@*****.**']
     self.test_smtp = 'test smtp server'
     self.test_sender = 'test sender'
     self.test_username = '******'
     self.test_password = '******'
     self.test_port = 10
     self.test_api = EmailApi(self.test_smtp, self.test_sender,
                              self.test_username, self.test_password,
                              self.test_port)
     self.test_channel = EmailChannel(self.test_channel_name,
                                      self.test_channel_id,
                                      self.test_channel_logger,
                                      self.test_emails_to,
                                      self.test_api)
     self.test_queue_size = 1000
     self.test_max_attempts = 5
     self.test_alert_validity_threshold = 300
     self.test_email_alerts_handler = EmailAlertsHandler(
         self.test_handler_name, self.dummy_logger, self.rabbitmq,
         self.test_channel, self.test_queue_size,
         self.test_max_attempts, self.test_alert_validity_threshold)
     self.test_data_str = "this is a test string"
     self.test_rabbit_queue_name = 'Test Queue'
     self.test_timestamp = 45676565.556
     self.test_heartbeat = {
         'component_name': 'Test Component',
         'is_alive': True,
         'timestamp': self.test_timestamp,
     }
     self.test_system_name = 'test_system'
     self.test_percentage_usage = 50
     self.test_panic_severity = 'WARNING'
     self.test_parent_id = 'parent_1234'
     self.test_system_id = 'system_id32423'
     self.test_alert = OpenFileDescriptorsIncreasedAboveThresholdAlert(
         self.test_system_name, self.test_percentage_usage,
         self.test_panic_severity, self.test_timestamp,
         self.test_panic_severity, self.test_parent_id, self.test_system_id
     )
     self.test_alerts_queue = Queue(self.test_queue_size)
Exemplo n.º 10
0
    def test_call_using_twilio_does_not_call_if_validity_threshold_exceeded(
            self, exceeding_factor, mock_alert, mock_sleep) -> None:
        mock_alert.return_value = None
        mock_sleep.return_value = None
        test_alert = OpenFileDescriptorsIncreasedAboveThresholdAlert(
            self.test_system_name, self.test_percentage_usage,
            self.test_panic_severity,
            datetime.now().timestamp() - self.test_alert_validity_threshold -
            exceeding_factor, self.test_panic_severity, self.test_parent_id,
            self.test_system_id)

        ret = self.test_twilio_alerts_handler._call_using_twilio(test_alert)
        self.assertEqual(RequestStatus.FAILED, ret)
        mock_alert.assert_not_called()
Exemplo n.º 11
0
 def setUp(self) -> None:
     self.test_handler_name = 'test_telegram_alerts_handler'
     self.dummy_logger = logging.getLogger('Dummy')
     self.dummy_logger.disabled = True
     self.connection_check_time_interval = timedelta(seconds=0)
     self.rabbit_ip = env.RABBIT_IP
     self.rabbitmq = RabbitMQApi(
         self.dummy_logger,
         self.rabbit_ip,
         connection_check_time_interval=self.connection_check_time_interval)
     self.test_channel_name = 'test_telegram_channel'
     self.test_channel_id = 'test_telegram_id12345'
     self.test_channel_logger = self.dummy_logger.getChild('dummy_channel')
     self.test_bot_token = 'test_bot_token'
     self.test_bot_chat_id = 'test_bot_chat_id'
     self.test_base_url = \
         "https://api.telegram.org/bot" + self.test_bot_token
     self.test_api = TelegramBotApi(self.test_bot_token,
                                    self.test_bot_chat_id)
     self.test_channel = TelegramChannel(self.test_channel_name,
                                         self.test_channel_id,
                                         self.test_channel_logger,
                                         self.test_api)
     self.test_queue_size = 1000
     self.test_max_attempts = 5
     self.test_alert_validity_threshold = 300
     self.test_telegram_alerts_handler = TelegramAlertsHandler(
         self.test_handler_name, self.dummy_logger, self.rabbitmq,
         self.test_channel, self.test_queue_size, self.test_max_attempts,
         self.test_alert_validity_threshold)
     self.test_data_str = "this is a test string"
     self.test_rabbit_queue_name = 'Test Queue'
     self.test_timestamp = 45676565.556
     self.test_heartbeat = {
         'component_name': 'Test Component',
         'is_alive': True,
         'timestamp': self.test_timestamp,
     }
     self.test_system_name = 'test_system'
     self.test_percentage_usage = 50
     self.test_panic_severity = 'WARNING'
     self.test_parent_id = 'parent_1234'
     self.test_system_id = 'system_id32423'
     self.test_alert = OpenFileDescriptorsIncreasedAboveThresholdAlert(
         self.test_system_name, self.test_percentage_usage,
         self.test_panic_severity, self.test_timestamp,
         self.test_panic_severity, self.test_parent_id, self.test_system_id)
     self.test_alerts_queue = Queue(self.test_queue_size)
Exemplo n.º 12
0
 def setUp(self) -> None:
     self.test_handler_name = 'test_twilio_alerts_handler'
     self.dummy_logger = logging.getLogger('Dummy')
     self.dummy_logger.disabled = True
     self.connection_check_time_interval = timedelta(seconds=0)
     self.rabbit_ip = env.RABBIT_IP
     self.rabbitmq = RabbitMQApi(
         self.dummy_logger,
         self.rabbit_ip,
         connection_check_time_interval=self.connection_check_time_interval)
     self.test_channel_name = 'test_twilio_channel'
     self.test_channel_id = 'test_twilio_id12345'
     self.test_channel_logger = self.dummy_logger.getChild('twilio_channel')
     self.test_account_sid = 'test_account_sid'
     self.test_auth_token = 'test_auth_token'
     self.test_call_from = '+35699999999'
     self.test_call_to = ['+35611111111', '+35644545454', '+35634343434']
     self.test_twiml = '<Response><Reject/></Response>'
     self.test_twiml_is_url = False
     self.test_api = TwilioApi(self.test_account_sid, self.test_auth_token)
     self.test_channel = TwilioChannel(self.test_channel_name,
                                       self.test_channel_id,
                                       self.test_channel_logger,
                                       self.test_api)
     self.test_max_attempts = 3
     self.test_alert_validity_threshold = 300
     self.test_twilio_alerts_handler = TwilioAlertsHandler(
         self.test_handler_name, self.dummy_logger, self.rabbitmq,
         self.test_channel, self.test_call_from, self.test_call_to,
         self.test_twiml, self.test_twiml_is_url, self.test_max_attempts,
         self.test_alert_validity_threshold)
     self.test_data_str = "this is a test string"
     self.test_rabbit_queue_name = 'Test Queue'
     self.test_timestamp = 45676565.556
     self.test_heartbeat = {
         'component_name': 'Test Component',
         'is_alive': True,
         'timestamp': self.test_timestamp,
     }
     self.test_system_name = 'test_system'
     self.test_percentage_usage = 50
     self.test_panic_severity = 'WARNING'
     self.test_parent_id = 'parent_1234'
     self.test_system_id = 'system_id32423'
     self.test_alert = OpenFileDescriptorsIncreasedAboveThresholdAlert(
         self.test_system_name, self.test_percentage_usage,
         self.test_panic_severity, self.test_timestamp,
         self.test_panic_severity, self.test_parent_id, self.test_system_id)
Exemplo n.º 13
0
    def test_send_alerts_removes_alert_if_it_was_successfully_sent(
            self, alert_request_status_list, mock_alert,
            mock_connection) -> None:
        mock_alert.side_effect = alert_request_status_list
        mock_connection.return_value.sleep.return_value = None
        test_alert = OpenFileDescriptorsIncreasedAboveThresholdAlert(
            self.test_system_name, self.test_percentage_usage,
            self.test_panic_severity,
            datetime.now().timestamp(), self.test_panic_severity,
            self.test_parent_id, self.test_system_id)
        test_queue = Queue(4)
        self.test_opsgenie_alerts_handler._alerts_queue = test_queue
        test_queue.put(test_alert)

        self.test_opsgenie_alerts_handler._send_alerts()

        self.assertTrue(self.test_opsgenie_alerts_handler.alerts_queue.empty())
Exemplo n.º 14
0
    def setUp(self) -> None:
        self.test_channel_name = 'test_console'
        self.test_channel_id = 'test_console_id12345'
        self.dummy_logger = logging.getLogger('Dummy')
        self.dummy_logger.disabled = True

        self.test_console_channel = ConsoleChannel(self.test_channel_name,
                                                   self.test_channel_id,
                                                   self.dummy_logger)

        self.test_system_name = 'test_system'
        self.test_percentage_usage = 50
        self.test_panic_severity = 'WARNING'
        self.test_last_monitored = 45
        self.test_parent_id = 'parent_1234'
        self.test_system_id = 'system_id32423'
        self.test_alert = OpenFileDescriptorsIncreasedAboveThresholdAlert(
            self.test_system_name, self.test_percentage_usage,
            self.test_panic_severity, self.test_last_monitored,
            self.test_panic_severity, self.test_parent_id, self.test_system_id)
Exemplo n.º 15
0
    def setUp(self) -> None:
        self.test_integration_key = 'test_integration_key'

        self.test_pagerduty_api = PagerDutyApi(self.test_integration_key)

        self.test_system_name = 'test system'
        self.test_percentage_usage = 50
        self.test_panic_severity = 'WARNING'
        self.test_last_monitored = 45.5
        self.test_parent_id = 'chain123'
        self.test_system_id = 'system123'
        self.test_alert = OpenFileDescriptorsIncreasedAboveThresholdAlert(
            self.test_system_name, self.test_percentage_usage,
            self.test_panic_severity, self.test_last_monitored,
            self.test_panic_severity, self.test_parent_id, self.test_system_id)
        self.test_dedup_key = 'test_dedup_key'
        self.test_pagerduty_severity = PagerDutySeverities.WARNING
        self.test_alert_payload = {
            'timestamp':
            datetime.fromtimestamp(self.test_alert.timestamp).isoformat()
        }
Exemplo n.º 16
0
    def test_send_alerts_attempts_to_send_alert_for_up_to_max_attempts_times(
            self, alert_request_status_list, expected_no_calls, mock_alert,
            mock_connection) -> None:
        mock_alert.side_effect = alert_request_status_list
        mock_connection.return_value.sleep.return_value = None
        test_alert = OpenFileDescriptorsIncreasedAboveThresholdAlert(
            self.test_system_name, self.test_percentage_usage,
            self.test_panic_severity,
            datetime.now().timestamp(), self.test_panic_severity,
            self.test_parent_id, self.test_system_id)
        test_queue = Queue(4)
        self.test_opsgenie_alerts_handler._alerts_queue = test_queue
        test_queue.put(test_alert)

        self.test_opsgenie_alerts_handler._send_alerts()

        expected_calls = []
        for _ in range(expected_no_calls):
            expected_calls.append(call(test_alert))
        actual_calls = mock_alert.call_args_list
        self.assertEqual(expected_calls, actual_calls)
Exemplo n.º 17
0
 def setUp(self) -> None:
     self.test_handler_name = 'test_log_alerts_handler'
     self.dummy_logger = logging.getLogger('Dummy')
     self.dummy_logger.disabled = True
     self.connection_check_time_interval = timedelta(seconds=0)
     self.rabbit_ip = env.RABBIT_IP
     self.rabbitmq = RabbitMQApi(
         self.dummy_logger,
         self.rabbit_ip,
         connection_check_time_interval=self.connection_check_time_interval)
     self.test_channel_name = 'test_logger_channel'
     self.test_channel_id = 'test_logger1234'
     self.test_channel_logger = self.dummy_logger.getChild('dummy_channel')
     self.alerts_logger = self.dummy_logger.getChild('alerts_logger')
     self.test_channel = LogChannel(self.test_channel_name,
                                    self.test_channel_id,
                                    self.test_channel_logger,
                                    self.alerts_logger)
     self.test_log_alerts_handler = LogAlertsHandler(
         self.test_handler_name, self.dummy_logger, self.rabbitmq,
         self.test_channel)
     self.test_data_str = "this is a test string"
     self.test_rabbit_queue_name = 'Test Queue'
     self.test_timestamp = 45676565.556
     self.test_heartbeat = {
         'component_name': 'Test Component',
         'is_alive': True,
         'timestamp': self.test_timestamp,
     }
     self.test_system_name = 'test_system'
     self.test_percentage_usage = 50
     self.test_panic_severity = 'WARNING'
     self.test_parent_id = 'parent_1234'
     self.test_system_id = 'system_id32423'
     self.test_alert = OpenFileDescriptorsIncreasedAboveThresholdAlert(
         self.test_system_name, self.test_percentage_usage,
         self.test_panic_severity, self.test_timestamp,
         self.test_panic_severity, self.test_parent_id, self.test_system_id)
Exemplo n.º 18
0
    def setUp(self) -> None:
        self.test_channel_name = 'test_pagerduty_channel'
        self.test_channel_id = 'test_pagerduty_id12345'
        self.dummy_logger = logging.getLogger('Dummy')
        self.dummy_logger.disabled = True
        self.test_integration_key = 'test_integration_key'

        self.test_pagerduty_api = PagerDutyApi(self.test_integration_key)
        self.test_pagerduty_channel = PagerDutyChannel(self.test_channel_name,
                                                       self.test_channel_id,
                                                       self.dummy_logger,
                                                       self.test_pagerduty_api)

        self.test_system_name = 'test_system'
        self.test_percentage_usage = 50
        self.test_panic_severity = 'WARNING'
        self.test_last_monitored = 45
        self.test_parent_id = 'parent_1234'
        self.test_system_id = 'system_id32423'
        self.test_alert = OpenFileDescriptorsIncreasedAboveThresholdAlert(
            self.test_system_name, self.test_percentage_usage,
            self.test_panic_severity, self.test_last_monitored,
            self.test_panic_severity, self.test_parent_id, self.test_system_id)
Exemplo n.º 19
0
    def test_call_using_twilio_attempts_calling_max_attempts_times_for_everyone(
            self, alert_request_status, failed_callee_index, amount_of_calls,
            expected_ret, mock_alert, mock_connection) -> None:
        # Here we will assume that the self._alert_validity_threshold is not
        # exceeded
        mock_alert.side_effect = alert_request_status
        mock_connection.return_value.sleep.return_value = None
        successful_callee_index = [
            i for i in range(len(self.test_call_to))
            if i not in failed_callee_index
        ]
        expected_calls = []
        test_alert = OpenFileDescriptorsIncreasedAboveThresholdAlert(
            self.test_system_name, self.test_percentage_usage,
            self.test_panic_severity,
            datetime.now().timestamp(), self.test_panic_severity,
            self.test_parent_id, self.test_system_id)
        for index in range(len(self.test_call_to)):
            if index in failed_callee_index:
                for _ in range(amount_of_calls[index]):
                    expected_calls.append(
                        call(call_from=self.test_call_from,
                             call_to=self.test_call_to[index],
                             twiml=self.test_twiml,
                             twiml_is_url=self.test_twiml_is_url))
            elif index in successful_callee_index:
                expected_calls.append(
                    call(call_from=self.test_call_from,
                         call_to=self.test_call_to[index],
                         twiml=self.test_twiml,
                         twiml_is_url=self.test_twiml_is_url))

        ret = self.test_twilio_alerts_handler._call_using_twilio(test_alert)
        self.assertEqual(expected_ret, ret)
        actual_calls = mock_alert.call_args_list
        self.assertEqual(expected_calls, actual_calls)
Exemplo n.º 20
0
 def setUp(self) -> None:
     self.test_handler_name = 'test_telegram_commands_handler'
     self.dummy_logger = logging.getLogger('Dummy')
     self.dummy_logger.disabled = True
     self.connection_check_time_interval = timedelta(seconds=0)
     self.rabbit_ip = env.RABBIT_IP
     self.rabbitmq = RabbitMQApi(
         self.dummy_logger,
         self.rabbit_ip,
         connection_check_time_interval=self.connection_check_time_interval)
     self.test_channel_name = 'test_telegram_channel'
     self.test_channel_id = 'test_telegram_id12345'
     self.test_channel_logger = self.dummy_logger.getChild('dummy_channel')
     self.test_bot_token = '1234567891:ABC-67ABCrfZFdddqRT5Gh837T2rtUFHgTY'
     self.test_bot_chat_id = 'test_bot_chat_id'
     self.test_base_url = \
         "https://api.telegram.org/bot" + self.test_bot_token
     self.test_api = TelegramBotApi(self.test_bot_token,
                                    self.test_bot_chat_id)
     self.test_channel = TelegramChannel(self.test_channel_name,
                                         self.test_channel_id,
                                         self.test_channel_logger,
                                         self.test_api)
     self.cmd_handlers_rabbit = RabbitMQApi(
         logger=self.dummy_logger.getChild(RabbitMQApi.__name__),
         host=self.rabbit_ip,
         connection_check_time_interval=self.connection_check_time_interval)
     self.redis = RedisApi(logger=self.dummy_logger.getChild(
         RedisApi.__name__),
                           host=env.REDIS_IP,
                           db=env.REDIS_DB,
                           port=env.REDIS_PORT,
                           namespace=env.UNIQUE_ALERTER_IDENTIFIER)
     self.mongo = MongoApi(logger=self.dummy_logger.getChild(
         MongoApi.__name__),
                           host=env.DB_IP,
                           db_name=env.DB_NAME,
                           port=env.DB_PORT)
     self.test_command_handlers_logger = self.dummy_logger.getChild(
         'command_handlers')
     self.test_chain_1 = 'Kusama'
     self.test_chain_2 = 'Cosmos'
     self.test_chain_3 = 'Test_Chain'
     self.test_chain1_id = 'kusama1234'
     self.test_chain2_id = 'cosmos1234'
     self.test_chain3_id = 'test_chain11123'
     self.test_associated_chains = {
         self.test_chain1_id: self.test_chain_1,
         self.test_chain2_id: self.test_chain_2,
         self.test_chain3_id: self.test_chain_3
     }
     self.test_telegram_command_handlers = TelegramCommandHandlers(
         self.test_handler_name, self.test_command_handlers_logger,
         self.test_associated_chains, self.test_channel,
         self.cmd_handlers_rabbit, self.redis, self.mongo)
     self.test_telegram_commands_handler = TelegramCommandsHandler(
         self.test_handler_name, self.dummy_logger, self.rabbitmq,
         self.test_channel, self.test_telegram_command_handlers)
     self.test_data_str = "this is a test string"
     self.test_rabbit_queue_name = 'Test Queue'
     self.test_timestamp = 45676565.556
     self.test_heartbeat = {
         'component_name': 'Test Component',
         'timestamp': self.test_timestamp,
     }
     self.test_system_name = 'test_system'
     self.test_percentage_usage = 50
     self.test_panic_severity = 'WARNING'
     self.test_parent_id = 'parent_1234'
     self.test_system_id = 'system_id32423'
     self.test_alert = OpenFileDescriptorsIncreasedAboveThresholdAlert(
         self.test_system_name, self.test_percentage_usage,
         self.test_panic_severity, self.test_timestamp,
         self.test_panic_severity, self.test_parent_id, self.test_system_id)