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)
Beispiel #2
0
    def test_fetch_all(self):
        client = Client(self.api_url)
        contact_manager = ContactManager(client)

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

        self.assertTrue(get_mock.called)
        get_mock.assert_called_with('contact')
Beispiel #3
0
    def test_fetch_all_bad_response(self):
        client = Client(self.api_url)
        contact_manager = ContactManager(client)

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

        self.assertTrue(get_mock.called)
        get_mock.assert_called_with('contact')
Beispiel #4
0
    def test_delete_fail(self):
        client = Client(self.api_url)
        contact_manager = ContactManager(client)

        contact_id = '1'

        with patch.object(client, 'delete') as delete_mock:
            res = contact_manager.delete(contact_id)

        self.assertTrue(delete_mock.called)
        self.assertFalse(res)
        delete_mock.assert_called_with('contact/' + contact_id)
Beispiel #5
0
    def test_delete(self):
        client = Client(self.api_url)
        contact_manager = ContactManager(client)

        contact_id = '1'

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

        self.assertTrue(delete_mock.called)
        self.assertTrue(res)
        delete_mock.assert_called_with('contact/' + contact_id)
    def test_add_bad_response(self):
        client = Client(self.api_url)
        contact_manager = ContactManager(client)

        contact_value = '#channel'

        with patch.object(client, 'put', return_value={}) as put_mock, \
                patch.object(client, 'get', return_value={'list': []}) as get_mock:
            with self.assertRaises(ResponseStructureError):
                contact_manager.add(contact_value, CONTACT_SLACK)

        self.assertTrue(put_mock.called)
        self.assertTrue(get_mock.called)
        expected_request_data = {'value': contact_value, 'type': CONTACT_SLACK}
        put_mock.assert_called_with('contact/', json=expected_request_data)
    def test_add(self):
        client = Client(self.api_url)
        contact_manager = ContactManager(client)

        contact_value = '#channel'
        contact_id = 1

        with patch.object(client, 'put', return_value={'id': contact_id}) as put_mock, \
                patch.object(client, 'get', return_value={'list': []}) as get_mock:
            res_contact = contact_manager.add(contact_value, CONTACT_SLACK)

        self.assertTrue(put_mock.called)
        self.assertTrue(get_mock.called)

        expected_request_data = {'value': contact_value, 'type': CONTACT_SLACK}

        expected_contact = Contact(id=contact_id, **expected_request_data)

        self.assertEqual(expected_contact, res_contact)

        put_mock.assert_called_with('contact/', json=expected_request_data)
    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)