def test_no_alerts(self): # pylint: disable=no-self-use tree = etree.XML( """ <cib> <configuration/> </cib> """ ) assert_xml_equal( '<alert id="my-alert" path="/test/path"/>', etree.tostring( alert.create_alert(tree, "my-alert", "/test/path") ).decode() ) assert_xml_equal( """ <cib> <configuration> <alerts> <alert id="my-alert" path="/test/path"/> </alerts> </configuration> </cib> """, etree.tostring(tree).decode() )
def create_alert( lib_env, alert_id, path, instance_attribute_dict, meta_attribute_dict, description=None ): """ Create new alert. Raises LibraryError if path is not specified, or any other failure. lib_env -- LibraryEnvironment alert_id -- id of alert to be created, if None it will be generated path -- path to script for alert instance_attribute_dict -- dictionary of instance attributes meta_attribute_dict -- dictionary of meta attributes description -- alert description description """ if not path: raise LibraryError(reports.required_option_is_missing(["path"])) alert_el = alert.create_alert( lib_env.get_cib(REQUIRED_CIB_VERSION), alert_id, path, description ) alert.update_instance_attributes(alert_el, instance_attribute_dict) alert.update_meta_attributes(alert_el, meta_attribute_dict) lib_env.push_cib()
def create_alert( lib_env, alert_id, path, instance_attribute_dict, meta_attribute_dict, description=None ): """ Create new alert. Raises LibraryError if path is not specified, or any other failure. lib_env -- LibraryEnvironment alert_id -- id of alert to be created, if None it will be generated path -- path to script for alert instance_attribute_dict -- dictionary of instance attributes meta_attribute_dict -- dictionary of meta attributes description -- alert description description """ if not path: raise LibraryError(reports.required_option_is_missing(["path"])) cib = lib_env.get_cib(REQUIRED_CIB_VERSION) alert_el = alert.create_alert(cib, alert_id, path, description) alert.update_instance_attributes(alert_el, instance_attribute_dict) alert.update_meta_attributes(alert_el, meta_attribute_dict) lib_env.push_cib(cib)
def create_alert( lib_env: LibraryEnvironment, alert_id, path, instance_attribute_dict, meta_attribute_dict, description=None, ): """ Create new alert. Raises LibraryError if path is not specified, or any other failure. lib_env -- LibraryEnvironment alert_id -- id of alert to be created, if None it will be generated path -- path to script for alert instance_attribute_dict -- dictionary of instance attributes meta_attribute_dict -- dictionary of meta attributes description -- alert description description """ if not path: raise LibraryError( ReportItem.error( reports.messages.RequiredOptionsAreMissing(["path"]))) cib = lib_env.get_cib(REQUIRED_CIB_VERSION) id_provider = IdProvider(cib) alert_el = alert.create_alert(cib, alert_id, path, description) arrange_first_instance_attributes(alert_el, instance_attribute_dict, id_provider) arrange_first_meta_attributes(alert_el, meta_attribute_dict, id_provider) lib_env.push_cib()
def test_alerts_exists_with_description(self): assert_xml_equal( '<alert id="my-alert" path="/test/path" description="nothing"/>', etree.tostring( alert.create_alert( self.tree, "my-alert", "/test/path", "nothing" ) ).decode(), ) assert_xml_equal( """ <cib> <configuration> <alerts> <alert id="alert"/> <alert id="my-alert" path="/test/path" description="nothing" /> </alerts> </configuration> </cib> """, etree.tostring(self.tree).decode(), )
def test_no_alerts(self): # pylint: disable=no-self-use tree = etree.XML( """ <cib> <configuration/> </cib> """ ) assert_xml_equal( '<alert id="my-alert" path="/test/path"/>', etree.tostring( alert.create_alert(tree, "my-alert", "/test/path") ).decode(), ) assert_xml_equal( """ <cib> <configuration> <alerts> <alert id="my-alert" path="/test/path"/> </alerts> </configuration> </cib> """, etree.tostring(tree).decode(), )
def test_id_exists(self): assert_raise_library_error( lambda: alert.create_alert(self.tree, "alert", "/path"), (severities.ERROR, report_codes.ID_ALREADY_EXISTS, { "id": "alert" }), )
def test_invalid_id(self): assert_raise_library_error( lambda: alert.create_alert(self.tree, "1alert", "/path"), (severities.ERROR, report_codes.INVALID_ID, { "id": "1alert", "id_description": "alert-id", "invalid_character": "1", "is_first_char": True, }))
def test_id_exists(self): assert_raise_library_error( lambda: alert.create_alert(self.tree, "alert", "/path"), ( severities.ERROR, report_codes.ID_ALREADY_EXISTS, {"id": "alert"} ) )
def test_invalid_id(self): assert_raise_library_error( lambda: alert.create_alert(self.tree, "1alert", "/path"), ( severities.ERROR, report_codes.INVALID_ID, { "id": "1alert", "id_description": "alert-id", "invalid_character": "1", "is_first_char": True, } ) )
def test_no_id(self): assert_xml_equal( '<alert id="alert-1" path="/test/path"/>', etree.tostring(alert.create_alert(self.tree, None, "/test/path")).decode()) assert_xml_equal( """ <cib> <configuration> <alerts> <alert id="alert"/> <alert id="alert-1" path="/test/path"/> </alerts> </configuration> </cib> """, etree.tostring(self.tree).decode())
def test_no_id(self): assert_xml_equal( '<alert id="alert-1" path="/test/path"/>', etree.tostring( alert.create_alert(self.tree, None, "/test/path") ).decode() ) assert_xml_equal( """ <cib> <configuration> <alerts> <alert id="alert"/> <alert id="alert-1" path="/test/path"/> </alerts> </configuration> </cib> """, etree.tostring(self.tree).decode() )
def test_alerts_exists_with_description(self): assert_xml_equal( '<alert id="my-alert" path="/test/path" description="nothing"/>', etree.tostring(alert.create_alert( self.tree, "my-alert", "/test/path", "nothing" )).decode() ) assert_xml_equal( """ <cib> <configuration> <alerts> <alert id="alert"/> <alert id="my-alert" path="/test/path" description="nothing" /> </alerts> </configuration> </cib> """, etree.tostring(self.tree).decode() )