Esempio n. 1
0
    def test_fetch_all(self):
        client = Client(self.api_url)
        subscription_manager = SubscriptionManager(client)

        with patch.object(client, 'get', return_value={'list': []}) as get_mock:
            subscription_manager.fetch_all()

        self.assertTrue(get_mock.called)
        get_mock.assert_called_with('subscription')
Esempio n. 2
0
    def test_fetch_all_bad_response(self):
        client = Client(self.api_url)
        subscription_manager = SubscriptionManager(client)

        with patch.object(client, 'get', return_value={}) as get_mock:
            with self.assertRaises(ResponseStructureError):
                subscription_manager.fetch_all()

        self.assertTrue(get_mock.called)
        get_mock.assert_called_with('subscription') 
Esempio n. 3
0
    def test_test_fail(self):
        client = Client(self.api_url)
        subscription_manager = SubscriptionManager(client)

        subscription_id = '1'

        with patch.object(client, 'put') as put_mock:
            res = subscription_manager.test(subscription_id)

        self.assertTrue(put_mock.called)
        self.assertFalse(res)
        put_mock.assert_called_with('subscription/' + subscription_id + '/test')
Esempio n. 4
0
    def test_test(self):
        client = Client(self.api_url)
        subscription_manager = SubscriptionManager(client)

        subscription_id = '1'

        with patch.object(client, 'put', new=Mock(side_effect=InvalidJSONError(b''))) as put_mock:
            res = subscription_manager.test(subscription_id)

        self.assertTrue(put_mock.called)
        self.assertTrue(res)
        put_mock.assert_called_with('subscription/' + subscription_id + '/test')
Esempio n. 5
0
    def test_delete_fail(self):
        client = Client(self.api_url)
        subscription_manager = SubscriptionManager(client)

        subscription_id = '1'

        with patch.object(client, 'delete') as delete_mock:
            res = subscription_manager.delete(subscription_id)

        self.assertTrue(delete_mock.called)
        self.assertFalse(res)
        delete_mock.assert_called_with('subscription/' + subscription_id)
Esempio n. 6
0
    def test_delete(self):
        client = Client(self.api_url)
        subscription_manager = SubscriptionManager(client)

        subscription_id = '1'

        with patch.object(client, 'delete', new=Mock(side_effect=InvalidJSONError(b''))) as delete_mock:
            res = subscription_manager.delete(subscription_id)

        self.assertTrue(delete_mock.called)
        self.assertTrue(res)
        delete_mock.assert_called_with('subscription/' + subscription_id)
Esempio n. 7
0
    def test_create_fake_moira_trigger_and_alerting_prefix_exists_token_not_exists(
            self):
        valid_config = 'tests/valid_config_second.yaml'
        data = read_from_file(valid_config)
        trigger_count = len(data.triggers)
        client = Client(self.url)

        with patch.object(client, 'get',
                          return_value={'list':
                                        []}), patch.object(client,
                                                           'put',
                                                           return_value={}):
            trigger_manager = TriggerManager(client)
            tag_manager = TagManager(client)
            contact_manager = ContactManager(client)
            subscription_manager = SubscriptionManager(client)

            with patch.object(
                    self.moira, '_trigger', return_value=trigger_manager
            ) as _trigger_mock, patch.object(
                    self.moira, '_contact', return_value=contact_manager
            ) as _contact_mock, patch.object(
                    self.moira, '_tag',
                    return_value=tag_manager) as _tag_mock, patch.object(
                        self.moira,
                        '_subscription',
                        return_value=subscription_manager), patch.object(
                            self.redis, 'exists',
                            return_value=0) as _redis_exists, patch.object(
                                self.redis, 'sadd',
                                return_value=1) as _redis_sadd:
                _contact_mock.fetch_by_current_user.return_value = []
                _contact_mock.add = Mock(
                    side_effect=lambda value, contact_type: Contact(
                        id=str(uuid.uuid4()), type=contact_type, value=value))
                trigger_orig = MoiraAlert(self.moira, self.redis, self.token)
                trigger_orig.setup(data)

            args, kwargs = _trigger_mock.create.call_args_list[0]
            kwargs.pop('name')
            count_contacts_in_alerting_block = self._get_count_key_by_dict(
                data.alerting, 'contacts')
            count_contacts_in_alerting_block += 2  # + 2 contacts from escalations

            self.assertTrue(_tag_mock.fetch_assigned_triggers_by_tags.called)
            self.assertDictKeysEqual(
                {
                    k: v
                    for k, v in data.triggers[0].to_custom_dict().items()
                    if k not in ('name', )
                },
                kwargs,
            )
            self.assertTrue(_contact_mock.add.called)
            self.assertTrue(_trigger_mock.create.called)
            self.assertEqual(_contact_mock.add.call_count,
                             count_contacts_in_alerting_block)
            self.assertEqual(_trigger_mock.create.call_count, trigger_count)
            self.assertEqual(_redis_exists.call_count, 1)
            self.assertEqual(_redis_sadd.call_count, trigger_count)
Esempio n. 8
0
    def _get_remote_subscriptions(
            self, alerts: List[Subscription],
            subscription_manager: SubscriptionManager
    ) -> List[MoiraSubscription]:
        """
        Возвращает список всех подписок, которые принадлежат пользователю и имеют тег из конфиг файла
        :param alerts: список оповещателей
        :return:
        """
        tags = {t for s in alerts for t in s.tags}
        excludes = [
            "ERROR",
            "OK",
            "NODATA",
            "CRITICAL",
            "WARN",
            "Critical",
            "critical",
            "MONAD",
        ]

        subscriptions = subscription_manager.fetch_all()

        def filter_by_tags(s):
            return (tags & set(s.tags)) - set(excludes)

        return list(filter(filter_by_tags, subscriptions))
Esempio n. 9
0
    def test_create_fake_moira_trigger_and_alerting_token_exists_and_not_empty(
            self):
        valid_config = 'tests/valid_config.yaml'
        data = read_from_file(valid_config)
        current_triggers = self.createCustomTriggers()
        moira_triggers = dict([current_triggers.popitem()])
        client = Client(self.url)

        redis_trigger_ids = set(
            i.encode('utf-8') for i in moira_triggers.keys())
        redis_trigger_ids = redis_trigger_ids | {
            str(uuid.uuid4()).encode('utf-8')
        }

        with patch.object(client, 'get',
                          return_value={'list':
                                        []}), patch.object(client,
                                                           'put',
                                                           return_value={}):
            trigger_manager = TriggerManager(client)
            tag_manager = TagManager(client)
            contact_manager = ContactManager(client)
            subscription_manager = SubscriptionManager(client)

            with patch.object(
                    self.moira, '_trigger', return_value=trigger_manager
            ) as _trigger_mock, patch.object(
                    self.moira, '_contact', return_value=contact_manager
            ) as _contact_mock, patch.object(
                    self.moira, '_tag',
                    return_value=tag_manager) as _tag_mock, patch.object(
                        self.moira,
                        '_subscription',
                        return_value=subscription_manager), patch.object(
                            self.redis, 'exists', return_value=1
                        ) as _redis_exists_mock, patch.object(
                            self.redis,
                            'smembers',
                            return_value=redis_trigger_ids
                        ) as _redis_smembers_mock, patch.object(
                            self.redis, 'sadd',
                            return_value=1) as _redis_sadd_mock, patch.object(
                                self.redis, 'srem',
                                return_value=1) as _redis_srem_mock:
                _trigger_mock.fetch_by_id = Mock(
                    side_effect=lambda id: moira_triggers.get(id, None))
                _contact_mock.fetch_by_current_user.return_value = []
                _contact_mock.add = Mock(
                    side_effect=lambda value, contact_type: Contact(
                        id=str(uuid.uuid4()), type=contact_type, value=value))
                trigger_orig = MoiraAlert(self.moira, self.redis, self.token)
                trigger_orig.setup(data)

            args, kwargs = _trigger_mock.create.call_args_list[0]
            kwargs.pop('name')
            count_contacts_in_alerting_block = self._get_count_key_by_dict(
                data.alerting, 'contacts')
            count_contacts_in_alerting_block += 2  # + 2 contacts from escalations

            self.assertFalse(_tag_mock.fetch_assigned_triggers_by_tags.called)
            self.assertDictKeysEqual(
                {
                    k: v
                    for k, v in data.triggers[0].to_custom_dict().items()
                    if k not in ('name', )
                },
                kwargs,
            )
            self.assertTrue(_contact_mock.add.called)
            self.assertEqual(_trigger_mock.fetch_by_id.call_count, 2)
            self.assertTrue(_trigger_mock.create.called)
            self.assertEqual(_contact_mock.add.call_count,
                             count_contacts_in_alerting_block)
            self.assertEqual(_trigger_mock.create.call_count, 1)
            self.assertTrue(_redis_exists_mock.call_count)
            self.assertTrue(_redis_smembers_mock.call_count)
            self.assertEqual(_redis_sadd_mock.call_count, 1)
            self.assertEqual(_redis_srem_mock.call_count, 1)