def test_skipped_alert(self): definition_json = self._get_script_alert_definition() # normally set by AlertSchedulerHandler definition_json['source']['stacks_directory'] = os.path.join('ambari_agent', 'dummy_files') definition_json['source']['common_services_directory'] = os.path.join('ambari_agent', 'common-services') definition_json['source']['host_scripts_directory'] = os.path.join('ambari_agent', 'host_scripts') configuration = {'foo-site' : { 'skip': 'true' } } collector = AlertCollector() cluster_configuration = self.__get_cluster_configuration() self.__update_cluster_configuration(cluster_configuration, configuration) alert = ScriptAlert(definition_json, definition_json['source'], None) # instruct the test alert script to be skipped alert.set_helpers(collector, cluster_configuration ) alert.set_cluster("c1", "c6401.ambari.apache.org") self.assertEquals(definition_json['source']['path'], alert.path) self.assertEquals(definition_json['source']['stacks_directory'], alert.stacks_dir) self.assertEquals(definition_json['source']['common_services_directory'], alert.common_services_dir) self.assertEquals(definition_json['source']['host_scripts_directory'], alert.host_scripts_dir) # ensure that it was skipped self.assertEquals(0,len(collector.alerts()))
def test_configuration_updates(self): definition_json = self._get_script_alert_definition() # normally set by AlertSchedulerHandler definition_json['source']['stacks_directory'] = os.path.join( 'ambari_agent', 'dummy_files') definition_json['source']['common_services_directory'] = os.path.join( 'ambari_agent', 'common-services') definition_json['source']['host_scripts_directory'] = os.path.join( 'ambari_agent', 'host_scripts') configuration = { 'foo-site': { 'bar': 'rendered-bar', 'baz': 'rendered-baz' } } # populate the configuration cache with the initial configs collector = AlertCollector() cluster_configuration = self.__get_cluster_configuration() self.__update_cluster_configuration(cluster_configuration, configuration) # run the alert and verify the output alert = ScriptAlert(definition_json, definition_json['source'], MagicMock()) alert.set_helpers(collector, cluster_configuration) alert.set_cluster("c1", "c6401.ambari.apache.org") alert.collect() alerts = collector.alerts() self.assertEquals(0, len(collector.alerts())) self.assertEquals('WARNING', alerts[0]['state']) self.assertEquals('bar is rendered-bar, baz is rendered-baz', alerts[0]['text']) # now update only the configs and run the same alert again and check # for different output configuration = { 'foo-site': { 'bar': 'rendered-bar2', 'baz': 'rendered-baz2' } } # populate the configuration cache with the initial configs self.__update_cluster_configuration(cluster_configuration, configuration) alert.collect() alerts = collector.alerts() self.assertEquals(0, len(collector.alerts())) self.assertEquals('WARNING', alerts[0]['state']) self.assertEquals('bar is rendered-bar2, baz is rendered-baz2', alerts[0]['text'])
def test_script_alert(self): definition_json = self._get_script_alert_definition() # normally set by AlertSchedulerHandler definition_json['source']['stacks_directory'] = os.path.join('ambari_agent', 'dummy_files') definition_json['source']['common_services_directory'] = os.path.join('ambari_agent', 'common-services') definition_json['source']['host_scripts_directory'] = os.path.join('ambari_agent', 'host_scripts') configuration = {'foo-site' : { 'bar': 'rendered-bar', 'baz' : 'rendered-baz' } } collector = AlertCollector() cluster_configuration = self.__get_cluster_configuration() self.__update_cluster_configuration(cluster_configuration, configuration) alert = ScriptAlert(definition_json, definition_json['source'], MagicMock()) alert.set_helpers(collector, cluster_configuration ) alert.set_cluster("c1", "c6401.ambari.apache.org") self.assertEquals(definition_json['source']['path'], alert.path) self.assertEquals(definition_json['source']['stacks_directory'], alert.stacks_dir) self.assertEquals(definition_json['source']['common_services_directory'], alert.common_services_dir) self.assertEquals(definition_json['source']['host_scripts_directory'], alert.host_scripts_dir) alert.collect() alerts = collector.alerts() self.assertEquals(0, len(collector.alerts())) self.assertEquals('WARNING', alerts[0]['state']) self.assertEquals('bar is rendered-bar, baz is rendered-baz', alerts[0]['text'])
def test_skipped_alert(self): definition_json = self._get_script_alert_definition() # normally set by AlertSchedulerHandler definition_json['source']['stacks_directory'] = os.path.join( 'ambari_agent', 'dummy_files') definition_json['source']['common_services_directory'] = os.path.join( 'ambari_agent', 'common-services') definition_json['source']['host_scripts_directory'] = os.path.join( 'ambari_agent', 'host_scripts') configuration = {'foo-site': {'skip': 'true'}} collector = AlertCollector() cluster_configuration = self.__get_cluster_configuration() self.__update_cluster_configuration(cluster_configuration, configuration) alert = ScriptAlert(definition_json, definition_json['source'], None) # instruct the test alert script to be skipped alert.set_helpers(collector, cluster_configuration) alert.set_cluster("c1", "c6401.ambari.apache.org") self.assertEquals(definition_json['source']['path'], alert.path) self.assertEquals(definition_json['source']['stacks_directory'], alert.stacks_dir) self.assertEquals( definition_json['source']['common_services_directory'], alert.common_services_dir) self.assertEquals(definition_json['source']['host_scripts_directory'], alert.host_scripts_dir) # ensure that it was skipped self.assertEquals(0, len(collector.alerts()))
def test_configuration_updates(self): definition_json = self._get_script_alert_definition() # normally set by AlertSchedulerHandler definition_json['source']['stacks_directory'] = os.path.join('ambari_agent', 'dummy_files') definition_json['source']['common_services_directory'] = os.path.join('ambari_agent', 'common-services') definition_json['source']['host_scripts_directory'] = os.path.join('ambari_agent', 'host_scripts') configuration = {'foo-site' : { 'bar': 'rendered-bar', 'baz' : 'rendered-baz' } } # populate the configuration cache with the initial configs collector = AlertCollector() cluster_configuration = self.__get_cluster_configuration() self.__update_cluster_configuration(cluster_configuration, configuration) # run the alert and verify the output alert = ScriptAlert(definition_json, definition_json['source'], MagicMock()) alert.set_helpers(collector, cluster_configuration ) alert.set_cluster("c1", "c6401.ambari.apache.org") alert.collect() alerts = collector.alerts() self.assertEquals(0, len(collector.alerts())) self.assertEquals('WARNING', alerts[0]['state']) self.assertEquals('bar is rendered-bar, baz is rendered-baz', alerts[0]['text']) # now update only the configs and run the same alert again and check # for different output configuration = {'foo-site' : { 'bar': 'rendered-bar2', 'baz' : 'rendered-baz2' } } # populate the configuration cache with the initial configs self.__update_cluster_configuration(cluster_configuration, configuration) alert.collect() alerts = collector.alerts() self.assertEquals(0, len(collector.alerts())) self.assertEquals('WARNING', alerts[0]['state']) self.assertEquals('bar is rendered-bar2, baz is rendered-baz2', alerts[0]['text'])
def test_skipped_alert(self): json = { "name": "namenode_process", "service": "HDFS", "component": "NAMENODE", "label": "NameNode process", "interval": 6, "scope": "host", "enabled": True, "uuid": "c1f73191-4481-4435-8dae-fd380e4c0be1", "source": { "type": "SCRIPT", "path": "test_script.py", } } # normally set by AlertSchedulerHandler json['source']['stacks_directory'] = os.path.join( 'ambari_agent', 'dummy_files') json['source']['common_services_directory'] = os.path.join( 'ambari_agent', 'common-services') json['source']['host_scripts_directory'] = os.path.join( 'ambari_agent', 'host_scripts') collector = AlertCollector() sa = ScriptAlert(json, json['source'], None) # instruct the test alert script to be skipped sa.set_helpers(collector, {'foo-site/skip': 'true'}) self.assertEquals(json['source']['path'], sa.path) self.assertEquals(json['source']['stacks_directory'], sa.stacks_dir) self.assertEquals(json['source']['common_services_directory'], sa.common_services_dir) self.assertEquals(json['source']['host_scripts_directory'], sa.host_scripts_dir) # ensure that it was skipped self.assertEquals(0, len(collector.alerts()))
def test_script_alert(self): json = { "name": "namenode_process", "service": "HDFS", "component": "NAMENODE", "label": "NameNode process", "interval": 6, "scope": "host", "enabled": True, "uuid": "c1f73191-4481-4435-8dae-fd380e4c0be1", "source": { "type": "SCRIPT", "path": "test_script.py", } } # normally set by AlertSchedulerHandler json['source']['stacks_directory'] = os.path.join( 'ambari_agent', 'dummy_files') json['source']['common_services_directory'] = os.path.join( 'ambari_agent', 'common-services') json['source']['host_scripts_directory'] = os.path.join( 'ambari_agent', 'host_scripts') collector = AlertCollector() sa = ScriptAlert(json, json['source'], MagicMock()) sa.set_helpers(collector, { 'foo-site/bar': 'rendered-bar', 'foo-site/baz': 'rendered-baz' }) self.assertEquals(json['source']['path'], sa.path) self.assertEquals(json['source']['stacks_directory'], sa.stacks_dir) self.assertEquals(json['source']['common_services_directory'], sa.common_services_dir) self.assertEquals(json['source']['host_scripts_directory'], sa.host_scripts_dir) sa.collect() alerts = collector.alerts() self.assertEquals(0, len(collector.alerts())) self.assertEquals('WARNING', alerts[0]['state']) self.assertEquals('bar is rendered-bar, baz is rendered-baz', alerts[0]['text'])
def test_script_alert(self): json = { "name": "namenode_process", "service": "HDFS", "component": "NAMENODE", "label": "NameNode process", "interval": 6, "scope": "host", "enabled": True, "uuid": "c1f73191-4481-4435-8dae-fd380e4c0be1", "source": { "type": "SCRIPT", "path": "test_script.py", "reporting": { "ok": { "text": "TCP OK - {0:.4f} response time on port {1}" }, "critical": { "text": "Could not load process info: {0}" } } } } # normally set by AlertSchedulerHandler json['source']['stacks_dir'] = os.path.join('ambari_agent', 'dummy_files') collector = AlertCollector() sa = ScriptAlert(json, json['source']) sa.set_helpers(collector, '') self.assertEquals(json['source']['path'], sa.path) self.assertEquals(json['source']['stacks_dir'], sa.stacks_dir) sa.collect() self.assertEquals('WARNING', collector.alerts()[0]['state']) self.assertEquals('all is not well', collector.alerts()[0]['text'])
def test_default_reporting_text(self): definition_json = self._get_script_alert_definition() alert = ScriptAlert(definition_json, definition_json['source'], None) self.assertEquals(alert._get_reporting_text(alert.RESULT_OK), '{0}') self.assertEquals(alert._get_reporting_text(alert.RESULT_WARNING), '{0}') self.assertEquals(alert._get_reporting_text(alert.RESULT_CRITICAL), '{0}') definition_json['source']['type'] = 'PORT' alert = PortAlert(definition_json, definition_json['source']) self.assertEquals(alert._get_reporting_text(alert.RESULT_OK), 'TCP OK - {0:.4f} response on port {1}') self.assertEquals(alert._get_reporting_text(alert.RESULT_WARNING), 'TCP OK - {0:.4f} response on port {1}') self.assertEquals(alert._get_reporting_text(alert.RESULT_CRITICAL), 'Connection failed: {0} to {1}:{2}') definition_json['source']['type'] = 'WEB' alert = WebAlert(definition_json, definition_json['source'], None) self.assertEquals(alert._get_reporting_text(alert.RESULT_OK), 'HTTP {0} response in {2:.4f} seconds') self.assertEquals(alert._get_reporting_text(alert.RESULT_WARNING), 'HTTP {0} response in {2:.4f} seconds') self.assertEquals(alert._get_reporting_text(alert.RESULT_CRITICAL), 'Connection failed to {1}') definition_json['source']['type'] = 'METRIC' alert = MetricAlert(definition_json, definition_json['source']) self.assertEquals(alert._get_reporting_text(alert.RESULT_OK), '{0}') self.assertEquals(alert._get_reporting_text(alert.RESULT_WARNING), '{0}') self.assertEquals(alert._get_reporting_text(alert.RESULT_CRITICAL), '{0}')
def test_default_reporting_text(self): json = { "name": "namenode_process", "service": "HDFS", "component": "NAMENODE", "label": "NameNode process", "interval": 6, "scope": "host", "enabled": True, "uuid": "c1f73191-4481-4435-8dae-fd380e4c0be1", "source": { "type": "SCRIPT", "path": "test_script.py", } } alert = ScriptAlert(json, json['source'], None) self.assertEquals(alert._get_reporting_text(alert.RESULT_OK), '{0}') self.assertEquals(alert._get_reporting_text(alert.RESULT_WARNING), '{0}') self.assertEquals(alert._get_reporting_text(alert.RESULT_CRITICAL), '{0}') json['source']['type'] = 'PORT' alert = PortAlert(json, json['source']) self.assertEquals(alert._get_reporting_text(alert.RESULT_OK), 'TCP OK - {0:.4f} response on port {1}') self.assertEquals(alert._get_reporting_text(alert.RESULT_WARNING), 'TCP OK - {0:.4f} response on port {1}') self.assertEquals(alert._get_reporting_text(alert.RESULT_CRITICAL), 'Connection failed: {0} to {1}:{2}') json['source']['type'] = 'WEB' alert = WebAlert(json, json['source']) self.assertEquals(alert._get_reporting_text(alert.RESULT_OK), 'HTTP {0} response in {2:.4f} seconds') self.assertEquals(alert._get_reporting_text(alert.RESULT_WARNING), 'HTTP {0} response in {2:.4f} seconds') self.assertEquals(alert._get_reporting_text(alert.RESULT_CRITICAL), 'Connection failed to {1}') json['source']['type'] = 'METRIC' alert = MetricAlert(json, json['source']) self.assertEquals(alert._get_reporting_text(alert.RESULT_OK), '{0}') self.assertEquals(alert._get_reporting_text(alert.RESULT_WARNING), '{0}') self.assertEquals(alert._get_reporting_text(alert.RESULT_CRITICAL), '{0}')