def test_collect_empty_topology(self): """ Testing AWS Topology check should not produce any topology (apis_to_run set to empty array) """ instance = InstanceInfo({ "role_arn": "arn:aws:iam::123456789012:role/RoleName", "regions": ["eu-west-1"], "apis_to_run": [] }) self.check.check(instance) test_topology = topology.get_snapshot(self.check.check_id) self.assertEqual(test_topology["instance_key"], { "type": "aws-v2", "url": "123456789012" }) self.assertEqual(test_topology["components"], []) self.assertEqual(test_topology["relations"], []) service_checks = aggregator.service_checks( self.check.SERVICE_CHECK_CONNECT_NAME) self.assertGreater(len(service_checks), 0) self.assertEqual(service_checks[0].status, AgentCheck.OK) service_checks = aggregator.service_checks( self.check.SERVICE_CHECK_EXECUTE_NAME) self.assertGreater(len(service_checks), 0) self.assertEqual(service_checks[0].status, AgentCheck.OK)
def test_creating_event_from_change_request_when_field_has_null_value( self): """ SNOW CR Field can have null for display_value "category": { "display_value": null, "value": "" } """ self.check._collect_relation_types = mock_collect_process self.check._batch_collect_components = mock_collect_process self.check._batch_collect_relations = mock_collect_process self.check._collect_change_requests = mock.MagicMock() self.check._collect_change_requests.return_value = self._read_data( 'CHG0000002.json') self.check.run() topology_events = telemetry._topology_events service_checks = aggregator.service_checks( 'servicenow.cmdb.topology_information') self.assertEqual(AgentCheck.OK, service_checks[0].status) self.assertEqual(1, len(topology_events)) self.assertEqual('CHG0000002: Rollback Oracle Version', topology_events[0]['msg_title']) category_tag = [ e for e in topology_events[0]['tags'] if 'category' in e ][0] self.assertEqual('category:None', category_tag) self.check.commit_state(None)
def test_ignore_saved_search_run_error(self): instance = { 'url': 'http://localhost:8089', 'authentication': { 'basic_auth': { 'username': "******", 'password': "******" } }, 'component_saved_searches': [{ "name": "dispatch_exception", "parameters": {} }], 'relation_saved_searches': [], 'ignore_saved_search_errors': True, 'tags': ['mytag', 'mytag2'] } self.check = MockedSplunkTopology(self.CHECK_NAME, {}, {}, [instance]) assert self.check.run() == '' service_checks = aggregator.service_checks( SplunkTopology.SERVICE_CHECK_NAME) self.assertEqual(service_checks[0].status, 2)
def test_execute_failure(self): """ Testing execution failure """ class s3(object): API = "s3" def __init__(self, location_info, client, agent): pass def process_all(self, filter=None): raise Exception("error") registry = {"regional": {"s3": s3}, "global": {}} self.check.APIS = {"regional": {"s3": {}}} with patch( "stackstate_checks.aws_topology.resources.ResourceRegistry.get_registry", return_value=registry): self.check.run() service_checks = aggregator.service_checks( self.check.SERVICE_CHECK_EXECUTE_NAME) self.assertGreater(len(service_checks), 0) self.assertIn("topology collection failed", service_checks[0].message)
def test_partially_incomplete(self): instance = { 'url': 'http://localhost:8089', 'authentication': { 'basic_auth': { 'username': "******", 'password': "******" } }, 'snapshot': True, 'component_saved_searches': [{ "name": "partially_incomplete_components", "parameters": {} }], 'relation_saved_searches': [{ "name": "partially_incomplete_relations", "parameters": {} }], 'tags': ['mytag', 'mytag2'] } self.check = MockedSplunkTopology(self.CHECK_NAME, {}, {}, [instance]) assert self.check.run() == '' topology.assert_snapshot( self.check.check_id, self.instance_key, True, True, [{ "id": u"vm_2_1", "type": u"vm", "data": { u"tags": [ 'integration-type:splunk', 'integration-url:http://localhost:8089', 'mytag', 'mytag2' ] } }], [{ "type": u"HOSTED_ON", "source_id": u"vm_2_1", "target_id": u"server_2", "data": { "tags": ['mytag', 'mytag2'] } }]) service_checks = aggregator.service_checks( SplunkTopology.SERVICE_CHECK_NAME) self.assertEqual(service_checks[0].status, 1) self.assertEqual( service_checks[0].message, "The saved search 'partially_incomplete_components' contained 1 incomplete records" ) self.assertEqual(service_checks[1].status, 1) self.assertEqual( service_checks[1].message, "The saved search 'partially_incomplete_relations' contained 1 incomplete records" )
def test_no_snapshot(self): instance = { 'url': 'http://localhost:8089', 'authentication': { 'basic_auth': { 'username': "******", 'password': "******" } }, 'snapshot': False, 'component_saved_searches': [{ "name": "components", "parameters": {} }], 'relation_saved_searches': [], 'tags': ['mytag', 'mytag2'] } self.check = MockedSplunkTopology(self.CHECK_NAME, {}, {}, [instance]) assert self.check.run() == '' topology.assert_snapshot( self.check.check_id, self.instance_key, False, False, [{ 'id': u'vm_2_1', 'type': u'vm', 'data': { u"running": True, u"_time": "2017-03-06T14:55:54.000+00:00", u"label.label1Key": "label1Value", u"tags": [ 'integration-type:splunk', 'integration-url:http://localhost:8089', 'mytag', 'mytag2', 'result_tag1' ] } }, { "id": u"server_2", "type": u"server", "data": { u"description": "My important server 2", u"_time": "2017-03-06T14:55:54.000+00:00", u"label.label2Key": "label2Value", u"tags": [ 'integration-type:splunk', 'integration-url:http://localhost:8089', 'mytag', 'mytag2', 'result_tag2' ] } }], []) service_checks = aggregator.service_checks( SplunkTopology.SERVICE_CHECK_NAME) self.assertEqual(service_checks[0].status, 0)
def test_missing_relation_type_field(self, mock): # TODO this is needed because the topology retains data across tests aggregator.reset() self.check.run() service_checks = aggregator.service_checks("StaticTopology") self.assertEqual(service_checks[0].status, 2) self.assertEqual(service_checks[0].message, "CSV header type not found in relation csv.")
def test_zabbix_topology_multiple_host_groups(self): """ Zabbix hosts can be placed in multiple host groups. When there is only one host group we place the host component in the StackState domain with the host group's name. However, when there are multiple host groups we use StackState domain 'Zabbix' """ # TODO this is needed because the topology retains data across tests topology.reset() def _mocked_method_request(url, name, auth=None, params={}, request_id=1): if name == "apiinfo.version": return self._apiinfo_response() elif name == "host.get": response = self._zabbix_host_response() response['result'][0]['groups'].append( { "groupid": "5", "name": "MyHostGroup" } ) return response else: self.fail("TEST FAILED on making invalid request") self.check.method_request = _mocked_method_request self.check.login = lambda url, user, password: "******" self.check.retrieve_problems = lambda url, auth: [] self.check.retrieve_events = lambda url, auth, event_ids: [] self.check.check(self.instance) topo_instances = topology.get_snapshot(self.check.check_id) self.assertEqual(len(topo_instances['components']), 1) self.assertEqual(len(topo_instances['relations']), 0) # Start and Stop Snapshot should be True self.assertEqual(topo_instances.get("start_snapshot"), True) self.assertEqual(topo_instances.get("stop_snapshot"), True) component = topo_instances['components'][0] self.assertEqual(component['data']['domain'], 'Zabbix') self.assertEqual(component['data']['layer'], 'machines') labels = component['data']['labels'] for label in ['zabbix', 'host group:Zabbix servers', 'host group:MyHostGroup']: if label not in labels: self.fail("Component does not have label '%s'." % label) # check if OK service check generated service_checks = aggregator.service_checks('Zabbix') self.assertEqual(AgentCheck.OK, service_checks[0].status)
def test_get_json_timeout(self, mock_request_get): """ Test timeout exception exception gets critical service check """ mock_request_get.side_effect = requests.exceptions.Timeout check = ServicenowCheck('servicenow', {}, {}, [mock_instance]) check.run() service_checks = aggregator.service_checks( self.check.SERVICE_CHECK_NAME) self.assertEqual(1, len(service_checks)) self.assertEqual(self.check.SERVICE_CHECK_NAME, service_checks[0].name) self.assertEqual(AgentCheck.CRITICAL, service_checks[0].status) self.assertEqual('Timeout: ', service_checks[0].message) self.check.commit_state(None)
def test_check_raise_exception(self, m): """ Test to raise a check exception when collecting components and snapshot should be False """ m.get("/api/v1/entity", exc=Exception("Exception occured")) self.assertRaises(Exception, self.check.run()) # since the check raised exception, the topology snapshot is not completed topo_instance = topology.get_snapshot(self.check.check_id) self.assertEqual(topo_instance.get("start_snapshot"), True) self.assertEqual(topo_instance.get("stop_snapshot"), False) # Service Checks should be generated service_checks = aggregator.service_checks("dynatrace") self.assertEqual(len(service_checks), 1) self.assertEqual(service_checks[0].name, self.SERVICE_CHECK_NAME) self.assertEqual(service_checks[0].status, 2)
def test_empty_relation_file(self): config = { 'init_config': {}, 'instances': [{ 'type': 'csv', 'components_file': '/ignored/', 'relations_file': '/dev/null', 'delimiter': ';' }] } # TODO this is needed because the topology retains data across tests aggregator.reset() self.check.check(config["instances"][0]) service_checks = aggregator.service_checks("StaticTopology") self.assertEqual(service_checks[0].status, 2)
def test_zabbix_disabled_triggers(self): """ When there are no triggers enabled and return empty list for `trigger.get` call, then we are expecting no problems and host will become green if it was red because there will be an event without any severity/problems. """ # TODO this is needed because the aggregator retains events data across tests aggregator.reset() def _mocked_method_request(url, name, auth=None, params={}, request_id=1): if name == "apiinfo.version": return self._apiinfo_response() elif name == "host.get": return self._zabbix_host_response() elif name == "problem.get": response = self._zabbix_problem() return response elif name == "trigger.get": response = self._zabbix_trigger() response['result'] = [] return response else: self.fail("TEST FAILED on making invalid request") self.check.method_request = _mocked_method_request self.check.login = lambda url, user, password: "******" self.check.check(self.instance) events = aggregator.events self.assertEqual(len(events), 1) tags = events[0]['tags'] for tag in [ 'host_id:10084', "host:zabbix01.example.com", "host_name:Zabbix server", "severity:0", "triggers:[]" ]: if tag not in tags: self.fail("Event does not have tag '%s', got: %s." % (tag, tags)) self.assertEqual(len(tags), 5) # check if OK service check generated service_checks = aggregator.service_checks('Zabbix') self.assertEqual(AgentCheck.OK, service_checks[0].status)
def test_when_check_has_exception_stop_snapshot_is_false(self): """ Test to raise a check exception when collecting components """ self.check._get_json = mock.MagicMock() self.check._get_json.side_effect = Exception("Test exception occurred") self.check.run() # since the check raised exception, the topology snapshot is not completed topo_instance = topology.get_snapshot(self.check.check_id) self.assertEqual(topo_instance.get("start_snapshot"), True) self.assertEqual(topo_instance.get("stop_snapshot"), False) # Service Checks should be generated service_checks = aggregator.service_checks( self.check.SERVICE_CHECK_NAME) self.assertEqual(len(service_checks), 1) self.assertEqual(service_checks[0].name, self.check.SERVICE_CHECK_NAME) self.assertEqual(service_checks[0].status, AgentCheck.CRITICAL)
def test_creating_event_from_change_request(self): """ Test creating event from SNOW Change Request """ self.check._collect_relation_types = mock_collect_process self.check._batch_collect_components = mock_collect_process self.check._batch_collect_relations = mock_collect_process self.check._collect_change_requests = mock.MagicMock() self.check._collect_change_requests.return_value = self._read_data( 'CHG0000001.json') self.check.run() topology_events = telemetry._topology_events service_checks = aggregator.service_checks( 'servicenow.cmdb.topology_information') self.assertEqual(AgentCheck.OK, service_checks[0].status) self.assertEqual(1, len(topology_events)) self.assertEqual(to_string('CHG0000001: Rollback Oracle ® Version'), topology_events[0]['msg_title']) self.check.commit_state(None)
def test_custom_cmdb_ci_field(self): """ Read from the custom field. """ custom_field_instance = { 'url': "https://instance.service-now.com", 'user': '******', 'password': '******', 'custom_cmdb_ci_field': 'u_configuration_item' } check = ServicenowCheck('servicenow', {}, {}, [custom_field_instance]) check._collect_relation_types = mock_collect_process check._batch_collect_components = mock_collect_process check._batch_collect_relations = mock_collect_process check._collect_change_requests = mock.MagicMock() response = self._read_data('CHG0000003.json') self.assertEqual( to_string('Sales © Force Automation'), to_string(response['result'][0]['u_configuration_item'] ['display_value'])) self.assertEqual( to_string( 'Service Management Tools Portal - AXA WINTERTHUR - Production - Standard' ), response['result'][0]['cmdb_ci']['display_value']) check._collect_change_requests.return_value = response check.run() topology_events = telemetry._topology_events service_checks = aggregator.service_checks( 'servicenow.cmdb.topology_information') self.assertEqual(AgentCheck.OK, service_checks[0].status) self.assertEqual(1, len(topology_events)) self.assertEqual(to_string('CHG0000003: Rollback Oracle ® Version'), topology_events[0]['msg_title']) host_identifier = [ e for e in topology_events[0]['context']['element_identifiers'] if 'urn:host:/' in e ][0] self.assertEqual(to_string('urn:host:/Sales © Force Automation'), host_identifier) self.check.commit_state(None)
def test_check_invalid_initial_token(self): """ Splunk check should not work with invalid initial token and stop the check """ instance = { 'url': 'http://localhost:8089', 'authentication': { 'token_auth': { 'name': "admin", 'initial_token': "dsfdgfhgjhkjuyr567uhfe345ythu7y6tre456sdx", 'audience': "search", 'renewal_days': 10 } }, 'component_saved_searches': [{ "name": "components", "parameters": {} }], 'relation_saved_searches': [], 'tags': ['mytag', 'mytag2'] } self.check = MockedSplunkTopology(self.CHECK_NAME, {}, {}, [instance]) # Run once to initialize assert self.check.run() == '' aggregator.reset() self.check.instance_data.splunk_client.invalid_token = True assert self.check.run() == '' msg = "Current in use authentication token is expired. Please provide a valid token in the YAML and restart " \ "the Agent" service_checks = aggregator.service_checks( SplunkTopology.SERVICE_CHECK_NAME) self.assertEqual(service_checks[0].status, 2) self.assertEqual(service_checks[0].message, msg)
def test_metadata(self): self.api_results.update( {"ListBuckets": { "Buckets": [{ "Name": "testname" }] }}) self.check.run() service_checks = aggregator.service_checks( self.check.SERVICE_CHECK_EXECUTE_NAME) self.assertEquals(service_checks[0].status, AgentCheck.OK) test_topology = topology.get_snapshot(self.check.check_id) self.assertEqual(len(test_topology["components"]), 1) self.assertIsNotNone(test_topology["components"][0]["data"]) self.assertEqual(test_topology["components"][0]["data"]["Tags"], {}) self.assertEqual(test_topology["components"][0]["data"]["Location"], { "AwsAccount": "123456789012", "AwsRegion": "eu-west-1" }) self.assertEqual( test_topology["components"][0]["data"]["tags"], ["integration-type:aws-v2", "integration-url:123456789012"]) self.assertGreater(len(service_checks), 0)
def assert_executed_ok(self): service_checks = aggregator.service_checks( self.check.SERVICE_CHECK_EXECUTE_NAME) self.assertGreater(len(service_checks), 0) self.assertEqual(service_checks[0].status, AgentCheck.OK, service_checks[0].message)