def test_with_description(self): assert_xml_equal( """ <recipient id="alert-recipient-1" value="value1" description="desc" /> """, etree.tostring( alert.add_recipient(self.mock_reporter, self.tree, "alert", "value1", description="desc")).decode()) assert_xml_equal( """ <cib> <configuration> <alerts> <alert id="alert" path="/path"> <recipient id="alert-recipient" value="test_val"/> <recipient id="alert-recipient-1" value="value1" description="desc" /> </alert> </alerts> </configuration> </cib> """, etree.tostring(self.tree).decode()) self.assertEqual([], self.mock_reporter.report_item_list)
def test_duplicity_of_value_allowed(self): assert_xml_equal( '<recipient id="alert-recipient-1" value="test_val"/>', etree.tostring( alert.add_recipient(self.mock_reporter, self.tree, "alert", "test_val", allow_same_value=True)).decode()) assert_xml_equal( """ <cib> <configuration> <alerts> <alert id="alert" path="/path"> <recipient id="alert-recipient" value="test_val"/> <recipient id="alert-recipient-1" value="test_val"/> </alert> </alerts> </configuration> </cib> """, etree.tostring(self.tree).decode()) assert_report_item_list_equal( self.mock_reporter.report_item_list, [(severities.WARNING, report_codes.CIB_ALERT_RECIPIENT_ALREADY_EXISTS, { "alert": "alert", "recipient": "test_val" })])
def test_alert_not_exist(self): assert_raise_library_error( lambda: alert.add_recipient(self.mock_reporter, self.tree, "alert1", "test_val"), (severities.ERROR, report_codes.CIB_ALERT_NOT_FOUND, { "alert": "alert1" }))
def test_with_description(self): assert_xml_equal( """ <recipient id="alert-recipient-1" value="value1" description="desc" /> """, etree.tostring(alert.add_recipient( self.mock_reporter, self.tree, "alert", "value1", description="desc" )).decode() ) assert_xml_equal( """ <cib> <configuration> <alerts> <alert id="alert" path="/path"> <recipient id="alert-recipient" value="test_val"/> <recipient id="alert-recipient-1" value="value1" description="desc" /> </alert> </alerts> </configuration> </cib> """, etree.tostring(self.tree).decode() ) self.assertEqual([], self.mock_reporter.report_item_list)
def add_recipient(lib_env, alert_id, recipient_value, instance_attribute_dict, meta_attribute_dict, recipient_id=None, description=None, allow_same_value=False): """ Add new recipient to alert witch id alert_id. lib_env -- LibraryEnvironment alert_id -- id of alert to which new recipient should be added recipient_value -- value of new recipient instance_attribute_dict -- dictionary of instance attributes to update meta_attribute_dict -- dictionary of meta attributes to update recipient_id -- id of new recipient, if None it will be generated description -- recipient description allow_same_value -- if True unique recipient value is not required """ if not recipient_value: raise LibraryError(reports.required_option_is_missing(["value"])) cib = lib_env.get_cib(REQUIRED_CIB_VERSION) recipient = alert.add_recipient(lib_env.report_processor, cib, alert_id, recipient_value, recipient_id=recipient_id, description=description, allow_same_value=allow_same_value) alert.update_instance_attributes(recipient, instance_attribute_dict) alert.update_meta_attributes(recipient, meta_attribute_dict) lib_env.push_cib(cib)
def test_id_exists(self): assert_raise_library_error( lambda: alert.add_recipient(self.mock_reporter, self.tree, "alert", "value1", "alert-recipient"), (severities.ERROR, report_codes.ID_ALREADY_EXISTS, { "id": "alert-recipient" })) self.assertEqual([], self.mock_reporter.report_item_list)
def test_alert_not_exist(self): assert_raise_library_error( lambda: alert.add_recipient(self.mock_reporter, self.tree, "alert1", "test_val"), (severities.ERROR, report_codes.ID_NOT_FOUND, { "id": "alert1", "context_type": "alerts", "context_id": "", "id_description": "alert" }))
def test_duplicity_of_value_not_allowed(self): report_item = (severities.ERROR, report_codes.CIB_ALERT_RECIPIENT_ALREADY_EXISTS, { "alert": "alert", "recipient": "test_val" }, report_codes.FORCE_ALERT_RECIPIENT_VALUE_NOT_UNIQUE) assert_raise_library_error(lambda: alert.add_recipient( self.mock_reporter, self.tree, "alert", "test_val")) assert_report_item_list_equal(self.mock_reporter.report_item_list, [report_item])
def test_alert_not_exist(self): assert_raise_library_error( lambda: alert.add_recipient( self.mock_reporter, self.tree, "alert1", "test_val" ), ( severities.ERROR, report_codes.CIB_ALERT_NOT_FOUND, {"alert": "alert1"} ) )
def test_id_exists(self): assert_raise_library_error( lambda: alert.add_recipient( self.mock_reporter, self.tree, "alert", "value1", "alert-recipient" ), ( severities.ERROR, report_codes.ID_ALREADY_EXISTS, {"id": "alert-recipient"} ) ) self.assertEqual([], self.mock_reporter.report_item_list)
def test_alert_not_exist(self): assert_raise_library_error( lambda: alert.add_recipient(self.mock_reporter, self.tree, "alert1", "test_val"), ( severities.ERROR, report_codes.ID_NOT_FOUND, { "id": "alert1", "expected_types": ["alert"], "context_type": "alerts", "context_id": "", }, None, ), )
def test_alert_not_exist(self): assert_raise_library_error( lambda: alert.add_recipient( self.mock_reporter, self.tree, "alert1", "test_val" ), ( severities.ERROR, report_codes.ID_NOT_FOUND, { "id": "alert1", "context_type": "alerts", "context_id": "", "id_description": "alert" } ) )
def add_recipient( lib_env, alert_id, recipient_value, instance_attribute_dict, meta_attribute_dict, recipient_id=None, description=None, allow_same_value=False ): """ Add new recipient to alert witch id alert_id. lib_env -- LibraryEnvironment alert_id -- id of alert to which new recipient should be added recipient_value -- value of new recipient instance_attribute_dict -- dictionary of instance attributes to update meta_attribute_dict -- dictionary of meta attributes to update recipient_id -- id of new recipient, if None it will be generated description -- recipient description allow_same_value -- if True unique recipient value is not required """ if not recipient_value: raise LibraryError( reports.required_option_is_missing(["value"]) ) cib = lib_env.get_cib(REQUIRED_CIB_VERSION) id_provider = IdProvider(cib) recipient = alert.add_recipient( lib_env.report_processor, cib, alert_id, recipient_value, recipient_id=recipient_id, description=description, allow_same_value=allow_same_value ) arrange_first_instance_attributes( recipient, instance_attribute_dict, id_provider ) arrange_first_meta_attributes( recipient, meta_attribute_dict, id_provider ) lib_env.push_cib()
def add_recipient( lib_env: LibraryEnvironment, alert_id, recipient_value, instance_attribute_dict, meta_attribute_dict, recipient_id=None, description=None, allow_same_value=False, ): """ Add new recipient to alert witch id alert_id. lib_env -- LibraryEnvironment alert_id -- id of alert to which new recipient should be added recipient_value -- value of new recipient instance_attribute_dict -- dictionary of instance attributes to update meta_attribute_dict -- dictionary of meta attributes to update recipient_id -- id of new recipient, if None it will be generated description -- recipient description allow_same_value -- if True unique recipient value is not required """ if not recipient_value: raise LibraryError( ReportItem.error( reports.messages.RequiredOptionsAreMissing(["value"]))) cib = lib_env.get_cib(REQUIRED_CIB_VERSION) id_provider = IdProvider(cib) recipient = alert.add_recipient( lib_env.report_processor, cib, alert_id, recipient_value, recipient_id=recipient_id, description=description, allow_same_value=allow_same_value, ) arrange_first_instance_attributes(recipient, instance_attribute_dict, id_provider) arrange_first_meta_attributes(recipient, meta_attribute_dict, id_provider) lib_env.push_cib()
def test_duplicity_of_value_not_allowed(self): report_item = ( severities.ERROR, report_codes.CIB_ALERT_RECIPIENT_ALREADY_EXISTS, { "alert": "alert", "recipient": "test_val" }, report_codes.FORCE_ALERT_RECIPIENT_VALUE_NOT_UNIQUE ) assert_raise_library_error( lambda: alert.add_recipient( self.mock_reporter, self.tree, "alert", "test_val" ), report_item ) assert_report_item_list_equal( self.mock_reporter.report_item_list, [report_item] )
def test_duplicity_of_value_allowed(self): assert_xml_equal( '<recipient id="alert-recipient-1" value="test_val"/>', etree.tostring( alert.add_recipient( self.mock_reporter, self.tree, "alert", "test_val", allow_same_value=True ) ).decode() ) assert_xml_equal( """ <cib> <configuration> <alerts> <alert id="alert" path="/path"> <recipient id="alert-recipient" value="test_val"/> <recipient id="alert-recipient-1" value="test_val"/> </alert> </alerts> </configuration> </cib> """, etree.tostring(self.tree).decode() ) assert_report_item_list_equal( self.mock_reporter.report_item_list, [( severities.WARNING, report_codes.CIB_ALERT_RECIPIENT_ALREADY_EXISTS, { "alert": "alert", "recipient": "test_val" } )] )