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)
def main(): logging.basicConfig(level=logging.getLevelName(LOG_LEVEL), format=LOG_FORMAT) logger = logging.getLogger("alert") params = parse_params() log_level = params["log_level"].upper() if LOG_LEVEL != log_level: logger.setLevel(logging.getLevelName(log_level)) # urllib3 logging is too verbose, set it at WARNING or more loglevel_as_int = logging._nameToLevel[log_level.upper()] # `loglevel_as_int` is 50 for CRITICAL, 10 for DEBUG logging.getLogger("urllib3").setLevel(max(logging.WARNING, loglevel_as_int)) # after this line, the loglevel for urllib3 is CRITICAL, ERROR or WARNING if not params["url"].startswith("http://"): params["url"] = "{}{}".format("http://", params["url"]) moira = Moira( params["url"], auth_user=params["user"], auth_pass=params["password"], auth_custom={ "User-Agent": _make_user_agent(), }, retry_policy=RetryPolicy( max_tries=3, delay=1, backoff=1.5, ), ) redis = Redis.from_url(params["redis_token_storage"]) alert = MoiraAlert(moira=moira, redis=redis, token=params["token"]) data = config.read_from_file(params["config"], cluster_name=params.get("cluster")) defaults.apply_defaults(data, redis) alert.setup(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)
def test_call_setup_with_incorrect_argument_raise_AttributeError(self): alert = MoiraAlert(self.moira, self.redis, self.token) with self.assertRaises(AttributeError): alert.setup('')