def dynatrace_check(test_instance):
    check = DynatraceCheck('dynatrace', {}, instances=[test_instance])
    yield check
    aggregator.reset()
    telemetry.reset()
    topology.reset()
    check.commit_state(None)
    def setUp(self):
        """
        Initialize and patch the check, i.e.
        """
        config = InitConfig({
            "aws_access_key_id":
            "some_key",
            "aws_secret_access_key":
            "some_secret",
            "external_id":
            "disable_external_id_this_is_unsafe",
        })
        self.patcher = patch("botocore.client.BaseClient._make_api_call")
        self.mock_object = self.patcher.start()
        self.api_results = deepcopy(API_RESULTS)
        topology.reset()
        aggregator.reset()
        self.check = AwsTopologyCheck(self.CHECK_NAME, config, [self.instance])
        state_descriptor = self.check._get_state_descriptor()
        # clear the state
        self.check.state_manager.clear(state_descriptor)

        def results(operation_name, kwarg):
            return self.api_results.get(operation_name) or {}

        self.mock_object.side_effect = results
示例#3
0
    def test_topology_with_multiple_environments(self, mock):

        # TODO this is needed because the topology retains data across tests
        topology.reset()

        self.check.run()
        instances = topology.get_snapshot(self.check.check_id)
        self.assertEqual(len(instances['components']), 2)
        self.assertEqual(len(instances['components'][0]['data']['labels']), 2)
        self.assertEqual(len(instances['components'][1]['data']['labels']), 2)
        self.assertIn("csv.component:component.csv",
                      instances['components'][0]['data']['labels'])
        self.assertIn("csv.relation:relation.csv",
                      instances['components'][0]['data']['labels'])
        self.assertEqual(
            len(instances['components'][0]['data']['environments']), 2)
        self.assertEqual(
            len(instances['components'][1]['data']['environments']), 1)
        self.assertIn("env1",
                      instances['components'][0]['data']['environments'])
        self.assertIn("env2",
                      instances['components'][0]['data']['environments'])
        self.assertIn("Production",
                      instances['components'][1]['data']['environments'])

        self.assertEqual(len(instances['relations']), 1)
        self.assertNotIn('labels', instances['relations'][0]['data'])

        AgentIntegrationTestUtil.assert_integration_snapshot(
            self.check, 'StaticTopology:component.csv')
示例#4
0
    def test_topology_with_labels_and_instance_tags(self, mock):

        # TODO this is needed because the topology retains data across tests
        topology.reset()

        config = {
            'init_config': {},
            'instances': [{
                'type': 'csv',
                'components_file': 'component.csv',
                'relations_file': 'relation.csv',
                'delimiter': ',',
                'tags': ['tag1', 'tag2']
            }]
        }
        self.check.check(config["instances"][0])
        instances = topology.get_snapshot(self.check.check_id)
        self.assertEqual(len(instances['components']), 2)
        self.assertEqual(len(instances['components'][0]['data']['labels']), 5)
        self.assertEqual(len(instances['components'][1]['data']['labels']), 4)
        self.assertIn("csv.component:component.csv",
                      instances['components'][0]['data']['labels'])
        self.assertIn("csv.relation:relation.csv",
                      instances['components'][0]['data']['labels'])
        self.assertIn("csv.component:component.csv",
                      instances['components'][1]['data']['labels'])
        self.assertIn("csv.relation:relation.csv",
                      instances['components'][1]['data']['labels'])

        self.assertEqual(len(instances['relations']), 1)
        self.assertNotIn('labels', instances['relations'][0]['data'])
示例#5
0
    def setUp(self):
        """
        Initialize and patch the check, i.e.
        """
        global account_id
        with open(relative_path("../stackstate_checks/aws_topology.yaml"),
                  "r") as stream:
            data_loaded = yaml.safe_load(stream)
        top.reset()
        aggregator.reset()
        init_config = InitConfig({
            "aws_access_key_id":
            data_loaded["init_config"]["aws_access_key_id"],
            "aws_secret_access_key":
            data_loaded["init_config"]["aws_secret_access_key"],
            "external_id":
            data_loaded["init_config"]["external_id"],
        })
        role = data_loaded["instances"][0]["role_arn"]
        account_id = role.split(":")[4]
        instance = {
            "role_arn": role,
            "regions": regions,
        }
        instance.update({"apis_to_run": ["redshift"]})

        self.check = AwsTopologyCheck(self.CHECK_NAME, InitConfig(init_config),
                                      [instance])
示例#6
0
    def setUp(self):
        """
        Initialize and patch the check, i.e.
        """
        config = InitConfig({
            "aws_access_key_id": "some_key",
            "aws_secret_access_key": "some_secret",
            "external_id": "secret_string"
        })
        self.patcher = patch("botocore.client.BaseClient._make_api_call")
        self.mock_object = self.patcher.start()
        self.api_results = deepcopy(API_RESULTS)
        topology.reset()
        aggregator.reset()
        self.check = AwsTopologyCheck(self.CHECK_NAME, config, [self.instance])

        def results(operation_name, api_params):
            if operation_name == "AssumeRole" and "ExternalId" not in api_params:
                raise ClientError({"Error": {
                    "Code": "AccessDenied"
                }}, operation_name)
            else:
                return self.api_results.get(operation_name) or {}

        self.mock_object.side_effect = results
    def test_collect_topology_component(self):
        """
        Test the component collection from the topology for VirtualMachine
        """
        # TODO this is needed because the topology retains data across tests
        topology.reset()

        self.check._is_excluded = MagicMock(return_value=True)

        instance = {'name': 'vsphere_mock', 'host': 'test-esxi'}
        topo_items = {
            'datastores': [],
            'clustercomputeresource': [],
            'computeresource': [],
            'hosts': [],
            'datacenters': [],
            'vms': [{
                'hostname': 'Ubuntu',
                'topo_tags': {
                    'topo_type': 'vsphere-VirtualMachine',
                    'name': 'Ubuntu',
                    'datastore': '54183927-04f91918-a72a-6805ca147c55'
                },
                'mor_type': 'vm'
            }]
        }
        self.check.get_topologyitems_sync = MagicMock(return_value=topo_items)
        self.check.collect_topology(instance)
        snapshot = topology.get_snapshot(self.check.check_id)

        # Check if the returned topology contains 1 component
        self.assertEqual(len(snapshot['components']), 1)
        self.assertEqual(
            snapshot['components'][0]['id'],
            'urn:vsphere:/test-esxi/vsphere-VirtualMachine/Ubuntu')
 def setUp(self):
     """
     Initialize and patch the check, i.e.
     """
     self.check = ServicenowCheck('servicenow', {}, {}, [self.instance])
     topology.reset()
     aggregator.reset()
     telemetry.reset()
     self.check.commit_state(None)
    def setUp(self):
        """
        Initialize and patch the check, i.e.
        """
        config = {}
        self.check = DynatraceTopologyCheck(self.CHECK_NAME, config, instances=[self.instance])

        # this is needed because the topology retains data across tests
        topology.reset()
 def setUp(self):
     """
     Initialize and patch the check, i.e.
     """
     self.check = MockedSplunkTopology(self.CHECK_NAME, {}, {},
                                       [self.instance])
     topology.reset()
     aggregator.reset()
     self.check.commit_state(None)
示例#11
0
    def test_snapshot(self, mock):

        # TODO this is needed because the topology retains data across tests
        topology.reset()

        self.check.run()
        instances = topology.get_snapshot(self.check.check_id)
        self.assertTrue(instances['start_snapshot'],
                        msg='start_snapshot was not set to True')
        self.assertTrue(instances['stop_snapshot'],
                        msg='stop_snapshot was not set to True')
示例#12
0
    def test_handle_incomplete_relation_line(self, mock):
        # TODO this is needed because the topology retains data across tests
        topology.reset()

        self.check.run()
        instances = topology.get_snapshot(self.check.check_id)
        self.assertEqual(len(instances['components']), 2)
        self.assertEqual(len(instances['relations']), 1)

        AgentIntegrationTestUtil.assert_integration_snapshot(
            self.check, 'StaticTopology:component.csv')
 def setUp(self):
     """
     Initialize and patch the check, i.e.
     """
     # Initialize
     config = {}
     self.check = StaticHealthCheck(self.CHECK_NAME, config, instances=[self.instance])
     # We clear these guys here. We'd like to use these as fixtures, but fixture do not play nice with unittest,
     # see https://stackoverflow.com/questions/22677654/why-cant-unittest-testcases-see-my-py-test-fixtures
     health.reset()
     topology.reset()
    def test_collect_topology_comp_relations(self):
        """
        Test the collection of components and relations from the topology for Datastore
        """
        # TODO this is needed because the topology retains data across tests
        topology.reset()

        self.check._is_excluded = MagicMock(return_value=True)

        topo_items = {
            "datastores": [{
                'mor_type': 'datastore',
                'topo_tags': {
                    'accessible':
                    True,
                    'topo_type':
                    'vsphere-Datastore',
                    'capacity':
                    999922073600,
                    'name':
                    'WDC1TB',
                    'url':
                    '/vmfs/volumes/54183927-04f91918-a72a-6805ca147c55',
                    'type':
                    'VMFS',
                    'vms': [
                        'UBUNTU_SECURE', 'W-NodeBox', 'NAT',
                        'Z_CONTROL_MONITORING (.151)', 'LEXX (.40)', 'parrot'
                    ]
                }
            }],
            "vms": [],
            'clustercomputeresource': [],
            'computeresource': [],
            'hosts': [],
            'datacenters': []
        }

        instance = {'name': 'vsphere_mock', 'host': 'test-esxi'}
        self.check.get_topologyitems_sync = MagicMock(return_value=topo_items)
        self.check.collect_topology(instance)
        snapshot = topology.get_snapshot(self.check.check_id)
        print(snapshot)

        # Check if the returned topology contains 1 component
        self.assertEqual(len(snapshot['components']), 1)
        self.assertEqual(snapshot['components'][0]['id'],
                         'urn:vsphere:/test-esxi/vsphere-Datastore/WDC1TB')

        # Check if the returned topology contains 6 relations for 6 VMs
        self.assertEqual(len(snapshot['relations']), 6)
        self.assertEqual(snapshot['relations'][0]['type'],
                         'vsphere-vm-uses-datastore')
示例#15
0
    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 setUp(self):
     """
     Initialize and patch the check, i.e.
     """
     config = {}
     self.check = AgentIntegrationSampleCheck(self.CHECK_NAME,
                                              config,
                                              instances=[self.instance])
     # TODO this is needed because the topology retains data across tests
     topology.reset()
     aggregator.reset()
     health.reset()
     telemetry.reset()
    def test_check_error_handling(self, check_name, expected_unique_topology_types):
        with patch(
            "stackstate_checks.aws_topology.resources.ResourceRegistry.get_registry", wraps=get_wrapper(check_name)
        ):
            top.reset()
            self.check.run()
            topology = [top.get_snapshot(self.check.check_id)]
            self.assertEqual(len(topology), 1)
            events = aggregator.events

            unique_types = self.unique_topology_types(topology)
            self.assertEqual(len(unique_types), expected_unique_topology_types)
            # TODO I can't return an error when running an API partly (error handling is WIP)
            if "|" not in check_name:
                aws_agent_check_errors = list(filter(lambda x: x["event_type"] == "aws_agent_check_error", events))
                self.assertEqual(len(aws_agent_check_errors), 1)
def test_check_run_no_sap_instances(aggregator, instance):
    # TODO this is needed because the topology retains data across tests
    topology.reset()

    host_control_url = "http://localhost:1128/SAPHostControl"
    with requests_mock.mock() as m:
        m.get(host_control_url + "/?wsdl",
              text=_read_test_file("wsdl/SAPHostAgent.wsdl"))
        m.post(host_control_url + ".cgi",
               text=_read_test_file("samples/GetCIMObject-NoResult.xml"))

        sap_check = SapCheck(CHECK_NAME, {}, instances=[instance])
        sap_check.run()
        topology.get_snapshot(sap_check.check_id)

        topology.assert_snapshot(
            check_id=sap_check.check_id,
            start_snapshot=True,
            stop_snapshot=True,
            instance_key=TopologyInstance("sap", "LAB-SAP-001"),
            components=[{
                "id": "urn:host:/LAB-SAP-001",
                "type": "sap-host",
                "data": {
                    "host": "LAB-SAP-001",
                    'tags':
                    ['integration-type:sap', 'integration-url:LAB-SAP-001'],
                    "domain": "sap",
                    "environment": "sap-prod"
                }
            }],
            relations=[],
        )

        AgentIntegrationTestUtil.assert_integration_snapshot(
            sap_check, 'sap:LAB-SAP-001')

        aggregator.assert_event(
            msg_text="Host control 'LAB-SAP-001' status update.",
            tags=["status:sap-host-control-success", "host:LAB-SAP-001"])

        aggregator.all_metrics_asserted()

        aggregator.assert_service_check(name=SapCheck.SERVICE_CHECK_NAME,
                                        status=AgentCheck.OK,
                                        message="OK",
                                        tags=[])
    def test_topology_items_from_config_check(self):
        # TODO this is needed because the topology retains data across tests
        topology.reset()

        instance_config = {
            "stackstate-layer": "layer-conf-a",
            "stackstate-environment": "environment-conf-a",
            "stackstate-domain": "domain-conf-a"
        }
        self.check = AgentIntegrationSampleCheck(self.CHECK_NAME, {},
                                                 instances=[instance_config])
        result = self.check.run()
        assert result == ''
        topo_instances = topology.get_snapshot(self.check.check_id)

        assert topo_instances == self._read_data(
            'expected_topology_instance_topology_config.json')
示例#20
0
    def test_topology_with_identifier(self, mock):

        # TODO this is needed because the topology retains data across tests
        topology.reset()

        self.check.run()
        instances = topology.get_snapshot(self.check.check_id)
        self.assertEqual(len(instances['components']), 2)
        self.assertEqual(
            len(instances['components'][0]['data']['identifiers']), 1)
        self.assertEqual(
            len(instances['components'][1]['data']['identifiers']), 0)
        self.assertIn("id1", instances['components'][0]['data']['identifiers'])

        self.assertEqual(len(instances['relations']), 1)
        self.assertNotIn('labels', instances['relations'][0]['data'])

        AgentIntegrationTestUtil.assert_integration_snapshot(
            self.check, 'StaticTopology:component.csv')
示例#21
0
    def test_zabbix_topology_hosts(self):

        # 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":
                return self._zabbix_host_response(tag=True)
            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)

        component = topo_instances['components'][0]
        expected_identifier = ["urn:host:/zabbix01.example.com", "zabbix01.example.com", "common-identifier"]
        self.assertEqual(component['id'], 'urn:host:/zabbix01.example.com')
        self.assertEqual(component['type'], 'zabbix_host')
        self.assertEqual(component['data']['name'], 'Zabbix server')
        self.assertEqual(component['data']['host_id'], '10084')
        self.assertEqual(component['data']['host'], 'zabbix01.example.com')
        self.assertEqual(component['data']['layer'], 'stackstate')
        self.assertEqual(component['data']['domain'], 'Zabbix servers')
        self.assertEqual(component['data']['identifiers'], expected_identifier)
        self.assertEqual(component['data']['environment'], 'Production')
        self.assertEqual(component['data']['host_groups'], ['Zabbix servers'])

        labels = component['data']['labels']
        for label in ['zabbix', 'host group:Zabbix servers']:
            if label not in labels:
                self.fail("Component does not have label '%s'." % label)
def test_check_run_no_sap_instances(aggregator, instance):
    # TODO this is needed because the topology retains data across tests
    topology.reset()

    host_control_url = "http://localhost:1128/SAPHostControl"
    with requests_mock.mock() as m:
        m.get(host_control_url + "/?wsdl",
              text=_read_test_file("wsdl/HostControl.wsdl"))
        m.post(host_control_url + ".cgi",
               text=_read_test_file("samples/GetCIMObject-NoResult.xml"))

        sap_check = SapCheck(CHECK_NAME, {}, instances=[instance])
        sap_check.check(instance)

        topology.assert_snapshot(check_id=sap_check.check_id,
                                 start_snapshot=True,
                                 stop_snapshot=True,
                                 instance_key=TopologyInstance(
                                     "sap", "LAB-SAP-001"),
                                 components=[{
                                     "id": "urn:host:/LAB-SAP-001",
                                     "type": "sap-host",
                                     "data": {
                                         "host": "LAB-SAP-001"
                                     }
                                 }])

        aggregator.assert_event(
            msg_text="",
            tags=["status:sap-host-control-success", "host:LAB-SAP-001"])

        aggregator.all_metrics_asserted()

        aggregator.assert_service_check(name=SapCheck.SERVICE_CHECK_NAME,
                                        status=AgentCheck.OK,
                                        message="OK",
                                        tags=[])
def test_collect_only_hosts(aggregator, instance):
    # TODO this is needed because the topology retains data across tests
    topology.reset()

    host_control_url = "http://localhost:1128/SAPHostControl"
    with requests_mock.mock() as m:
        m.get(host_control_url + "/?wsdl",
              text=_read_test_file("wsdl/HostControl.wsdl"))
        m.post(host_control_url + ".cgi",
               text=_read_test_file("samples/GetCIMObject.xml"))

        sap_check = SapCheck(CHECK_NAME, {}, instances=[instance])
        sap_check._get_config(instance)
        sap_check._collect_hosts()

        topology.assert_snapshot(
            check_id=sap_check.check_id,
            start_snapshot=False,
            stop_snapshot=False,
            instance_key=TopologyInstance("sap", "LAB-SAP-001"),
            components=[{
                "id": "urn:host:/LAB-SAP-001",
                "type": "sap-host",
                "data": {
                    "host": "LAB-SAP-001"
                }
            }, {
                "id": "urn:sap:/instance:LAB-SAP-001:67",
                "type": "sap-instance",
                "data": {
                    "host": "LAB-SAP-001",
                    "labels": [],
                    "name": "CDA",
                    "sid": "CDA",
                    "system_number": "67",
                    "type": "Solution Manager Diagnostic Agent",
                    "version": "753, patch 200, changelist 1844229"
                }
            }, {
                "id": "urn:sap:/instance:LAB-SAP-001:00",
                "type": "sap-instance",
                "data": {
                    "host": "LAB-SAP-001",
                    "labels": [],
                    "name": "DON",
                    "sid": "DON",
                    "system_number": "00",
                    "type": "ABAP Instance",
                    "version": "753, patch 401, changelist 1927964"
                }
            }, {
                "id": "urn:sap:/instance:LAB-SAP-001:01",
                "type": "sap-instance",
                "data": {
                    "host": "LAB-SAP-001",
                    "labels": [],
                    "name": "DON",
                    "sid": "DON",
                    "system_number": "01",
                    "type": "Central Services Instance",
                    "version": "753, patch 401, changelist 1927964"
                }
            }],
            relations=[{
                'data': {},
                'source_id': 'urn:sap:/instance:LAB-SAP-001:67',
                'target_id': 'urn:host:/LAB-SAP-001',
                'type': 'is hosted on'
            }, {
                'data': {},
                'source_id': 'urn:sap:/instance:LAB-SAP-001:00',
                'target_id': 'urn:host:/LAB-SAP-001',
                'type': 'is hosted on'
            }, {
                'data': {},
                'source_id': 'urn:sap:/instance:LAB-SAP-001:01',
                'target_id': 'urn:host:/LAB-SAP-001',
                'type': 'is hosted on'
            }])

        aggregator.assert_event(
            msg_text="",
            tags=["status:sap-host-control-success", "host:LAB-SAP-001"])

        aggregator.all_metrics_asserted()
def topology():
    from stackstate_checks.base.stubs import topology

    topology.reset()
    return topology
    def setUp(self):
        """
        Initialize and patch the check, i.e.
        """
        method = getattr(self, self._testMethodName)
        not_authorized = []
        if hasattr(method, "not_authorized"):
            not_authorized = method.not_authorized
        cloudtrail_event = None
        if hasattr(method, "cloudtrail_event"):
            cloudtrail_event = method.cloudtrail_event
        eventbridge_event = None
        if hasattr(method, "eventbridge_event"):
            eventbridge_event = method.eventbridge_event
        filter = ""
        if hasattr(method, "filter"):
            filter = method.filter
        subdirectory = ""
        if hasattr(method, "subdirectory"):
            subdirectory = method.subdirectory
        self.patcher = patch("botocore.client.BaseClient._make_api_call",
                             autospec=True)
        self.mock_object = self.patcher.start()
        top.reset()
        aggregator.reset()
        init_config = InitConfig({
            "aws_access_key_id":
            "some_key",
            "aws_secret_access_key":
            "some_secret",
            "external_id":
            "disable_external_id_this_is_unsafe",
        })
        regions = self.get_region()
        if not isinstance(regions, list):
            regions = [regions]
        instance = {
            "role_arn":
            "arn:aws:iam::{}:role/RoleName".format(self.get_account_id()),
            "regions":
            regions
        }
        api = self.get_api()
        apis = None
        if api:
            if filter:
                apis = [api + "|" + filter]
            else:
                apis = [api]
        if cloudtrail_event:
            apis = []
        instance.update({"apis_to_run": apis})

        self.check = AwsTopologyCheck(self.CHECK_NAME, InitConfig(init_config),
                                      [instance])
        self.check.last_full_topology = datetime(2021, 5, 1, 0, 0,
                                                 0).replace(tzinfo=pytz.utc)

        def ignore_callback(self, *args, **kwargs):
            return

        self.check.get_flowlog_update = ignore_callback
        if cloudtrail_event is None and eventbridge_event is None:
            self.check.get_topology_update = ignore_callback
        self.mock_object.side_effect = wrapper(
            api,
            not_authorized,
            subdirectory,
            event_name=cloudtrail_event,
            eventbridge_event_name=eventbridge_event)
        self.components_checked = 0
        self.relations_checked = 0
示例#26
0
 def topology():
     """This fixture returns a mocked Agent topology with state cleared."""
     __topology.reset()
     return __topology
def test_collect_processes(aggregator, instance):
    # TODO this is needed because the topology retains data across tests
    topology.reset()

    instance_id = "00"
    host_agent_url = "http://localhost:50013/"
    with requests_mock.mock() as m:
        m.get(host_agent_url + "SAPHostAgent/?wsdl",
              text=_read_test_file("wsdl/HostAgent.wsdl"))
        m.post(host_agent_url,
               text=_read_test_file("samples/GetProcessList.xml"))

        sap_check = SapCheck(CHECK_NAME, {}, instances=[instance])
        sap_check._get_config(instance)
        sap_check._collect_processes(instance_id,
                                     sap_check._get_proxy(instance_id))

        topology.assert_snapshot(
            check_id=sap_check.check_id,
            start_snapshot=False,
            stop_snapshot=False,
            instance_key=TopologyInstance("sap", "LAB-SAP-001"),
            components=[{
                'data': {
                    'description': 'Dispatcher',
                    'elapsedtime': '119:16:01',
                    'host': 'LAB-SAP-001',
                    'labels': [],
                    'name': 'disp+work.EXE',
                    'pid': 4392,
                    'starttime': '2020 01 22 12:52:29'
                },
                'id': 'urn:process:/LAB-SAP-001:00:4392',
                'type': 'sap-process'
            }, {
                'data': {
                    'description': 'IGS Watchdog',
                    'elapsedtime': '119:16:01',
                    'host': 'LAB-SAP-001',
                    'labels': [],
                    'name': 'igswd.EXE',
                    'pid': 11088,
                    'starttime': '2020 01 22 12:52:29'
                },
                'id': 'urn:process:/LAB-SAP-001:00:11088',
                'type': 'sap-process'
            }, {
                'data': {
                    'description': 'Gateway',
                    'elapsedtime': '119:16:01',
                    'host': 'LAB-SAP-001',
                    'labels': [],
                    'name': 'gwrd',
                    'pid': 9512,
                    'starttime': '2020 01 22 12:52:29'
                },
                'id': 'urn:process:/LAB-SAP-001:00:9512',
                'type': 'sap-process'
            }, {
                'data': {
                    'description': 'ICM',
                    'elapsedtime': '119:16:01',
                    'host': 'LAB-SAP-001',
                    'labels': [],
                    'name': 'icman',
                    'pid': 6584,
                    'starttime': '2020 01 22 12:52:29'
                },
                'id': 'urn:process:/LAB-SAP-001:00:6584',
                'type': 'sap-process'
            }],
            relations=[{
                'data': {},
                'source_id': 'urn:process:/LAB-SAP-001:00:4392',
                'target_id': 'urn:sap:/instance:LAB-SAP-001:00',
                'type': 'runs on'
            }, {
                'data': {},
                'source_id': 'urn:process:/LAB-SAP-001:00:11088',
                'target_id': 'urn:sap:/instance:LAB-SAP-001:00',
                'type': 'runs on'
            }, {
                'data': {},
                'source_id': 'urn:process:/LAB-SAP-001:00:9512',
                'target_id': 'urn:sap:/instance:LAB-SAP-001:00',
                'type': 'runs on'
            }, {
                'data': {},
                'source_id': 'urn:process:/LAB-SAP-001:00:6584',
                'target_id': 'urn:sap:/instance:LAB-SAP-001:00',
                'type': 'runs on'
            }])

        aggregator.assert_event(msg_text="Running",
                                tags=[
                                    "status:SAPControl-GREEN",
                                    "pid:4392",
                                    "instance_id:" + instance_id,
                                    "starttime:2020 01 22 12:52:29",
                                ])
        aggregator.assert_event(msg_text="Running",
                                tags=[
                                    "status:SAPControl-GREEN",
                                    "pid:11088",
                                    "instance_id:" + instance_id,
                                    "starttime:2020 01 22 12:52:29",
                                ])
        aggregator.assert_event(msg_text="Running",
                                tags=[
                                    "status:SAPControl-GREEN",
                                    "pid:9512",
                                    "instance_id:" + instance_id,
                                    "starttime:2020 01 22 12:52:29",
                                ])
        aggregator.assert_event(msg_text="Running",
                                tags=[
                                    "status:SAPControl-GRAY",
                                    "pid:6584",
                                    "instance_id:" + instance_id,
                                    "starttime:2020 01 22 12:52:29",
                                ])

        aggregator.all_metrics_asserted()
    def setUp(self):
        """
        Initialize and patch the check, i.e.
        """
        method = getattr(self, self._testMethodName)
        not_authorized = []
        if hasattr(method, "not_authorized"):
            not_authorized = method.not_authorized
        cloudtrail_event = None
        if hasattr(method, "cloudtrail_event"):
            cloudtrail_event = method.cloudtrail_event
        eventbridge_event = None
        if hasattr(method, "eventbridge_event"):
            eventbridge_event = method.eventbridge_event
        filter = ""
        if hasattr(method, "filter"):
            filter = method.filter
        subdirectory = ""
        if hasattr(method, "subdirectory"):
            subdirectory = method.subdirectory
        self.patcher = patch("botocore.client.BaseClient._make_api_call",
                             autospec=True)
        self.mock_object = self.patcher.start()
        top.reset()
        aggregator.reset()
        init_config = InitConfig({
            "aws_access_key_id":
            "some_key",
            "aws_secret_access_key":
            "some_secret",
            "external_id":
            "disable_external_id_this_is_unsafe",
        })
        regions = self.get_region()
        if not isinstance(regions, list):
            regions = [regions]
        instance = {
            "role_arn":
            "arn:aws:iam::{}:role/RoleName".format(self.get_account_id()),
            "regions":
            regions,
            "state": {
                "last_full_topology": "2021-05-01T00:00:00"
            },
        }
        api = self.get_api()
        apis = None
        if api:
            if filter:
                apis = [api + "|" + filter]
            else:
                apis = [api]
        if cloudtrail_event:
            apis = []
        instance.update({"apis_to_run": apis})

        self.check = AwsTopologyCheck(self.CHECK_NAME, InitConfig(init_config),
                                      [instance])
        state_descriptor = self.check._get_state_descriptor()
        # clear the state
        self.check.state_manager.clear(state_descriptor)
        self.mock_object.side_effect = wrapper(
            api,
            not_authorized,
            subdirectory,
            event_name=cloudtrail_event,
            eventbridge_event_name=eventbridge_event)
        self.components_checked = 0
        self.relations_checked = 0
    def setUp(self):
        """
        Initialize and patch the check, i.e.
        """
        self.recorder = []
        method = getattr(self, self._testMethodName)
        not_authorized = []
        if hasattr(method, "not_authorized"):
            not_authorized = method.not_authorized
        events_file = None
        if hasattr(method, "events_file"):
            events_file = method.events_file
        subdirectory = ""
        if hasattr(method, "subdirectory"):
            subdirectory = method.subdirectory
        log_bucket_name = ""
        if hasattr(method, "log_bucket_name"):
            log_bucket_name = method.log_bucket_name
        use_gz = False
        if hasattr(method, "gz"):
            use_gz = method.gz
        self.patcher = patch("botocore.client.BaseClient._make_api_call",
                             autospec=True)
        self.extrapatch = patch(
            "stackstate_checks.aws_topology.AwsTopologyCheck.must_run_full",
            return_value=False)
        self.regpatch = patch(
            "stackstate_checks.aws_topology.resources.ResourceRegistry.CLOUDTRAIL",
            {
                "test": {
                    "test": CollectorMock
                },
                "lambda.amazonaws.com": {
                    "PublishVersion20150331": CollectorMock,
                    "UpdateFunctionConfiguration20150331v2": CollectorMock
                }
            })
        self.mock_object = self.patcher.start()
        self.extrapatch.start()
        self.regpatch.start()

        top.reset()
        aggregator.reset()
        init_config = InitConfig({
            "aws_access_key_id":
            "some_key",
            "aws_secret_access_key":
            "some_secret",
            "external_id":
            "disable_external_id_this_is_unsafe",
        })
        regions = self.get_region()
        if not isinstance(regions, list):
            regions = [regions]
        instance = {
            "role_arn":
            "arn:aws:iam::{}:role/RoleName".format(self.get_account_id()),
            "regions":
            regions,
            "state": {
                "last_full_topology": "2021-05-01T00:00:00"
            },
        }
        if log_bucket_name:
            instance.update({"log_bucket_name": log_bucket_name})
        apis = []
        instance.update({"apis_to_run": apis})

        self.check = AwsTopologyCheck(self.CHECK_NAME, InitConfig(init_config),
                                      [instance])
        state_descriptor = self.check._get_state_descriptor()
        # clear the state
        self.check.state_manager.clear(state_descriptor)
        self.mock_object.side_effect = wrapper(self,
                                               not_authorized,
                                               subdirectory,
                                               use_gz,
                                               events_file=events_file)
def test_collect_databases(aggregator, instance):
    # TODO this is needed because the topology retains data across tests
    topology.reset()

    host_control_url = "http://localhost:1128/SAPHostControl"
    with requests_mock.mock() as m:
        m.get(host_control_url + "/?wsdl",
              text=_read_test_file("wsdl/HostControl.wsdl"))
        m.post(host_control_url + ".cgi",
               text=_read_test_file("samples/ListDatabases.xml"))

        sap_check = SapCheck(CHECK_NAME, {}, instances=[instance])
        sap_check._get_config(instance)
        sap_check._collect_databases()

        topology.assert_snapshot(
            check_id=sap_check.check_id,
            start_snapshot=False,
            stop_snapshot=False,
            instance_key=TopologyInstance("sap", "LAB-SAP-001"),
            components=[{
                'type': 'sap-database',
                'id': 'urn:db:/LAB-SAP-001:DON',
                'data': {
                    'vendor': 'ora',
                    'name': 'DON',
                    'type': 'ora',
                    'host': 'lab-sap-001',
                    'version': '18.0.0.0.0',
                    'labels': []
                }
            }, {
                'type': 'sap-database-component',
                'id': 'urn:sap:/db_component:LAB-SAP-001:DON:Instance',
                'data': {
                    'host': 'LAB-SAP-001',
                    'description': 'Instance',
                    'database_name': 'DON',
                    'name': 'Instance',
                    'labels': []
                }
            }, {
                'type': 'sap-database-component',
                'id': 'urn:sap:/db_component:LAB-SAP-001:DON:Database',
                'data': {
                    'host': 'LAB-SAP-001',
                    'description': 'Database',
                    'database_name': 'DON',
                    'name': 'Database',
                    'labels': []
                }
            }, {
                'type': 'sap-database-component',
                'id': 'urn:sap:/db_component:LAB-SAP-001:DON:Archiver',
                'data': {
                    'host': 'LAB-SAP-001',
                    'description': 'Archiver',
                    'database_name': 'DON',
                    'name': 'Archiver',
                    'labels': []
                }
            }, {
                'type': 'sap-database-component',
                'id': 'urn:sap:/db_component:LAB-SAP-001:DON:Listener',
                'data': {
                    'host': 'LAB-SAP-001',
                    'description': 'Listener',
                    'database_name': 'DON',
                    'name': 'Listener',
                    'labels': []
                }
            }],
            relations=[{
                'type': 'is hosted on',
                'data': {},
                'source_id': 'urn:db:/LAB-SAP-001:DON',
                'target_id': 'urn:host:/LAB-SAP-001'
            }, {
                'type': 'runs on',
                'data': {},
                'source_id': 'urn:sap:/db_component:LAB-SAP-001:DON:Instance',
                'target_id': 'urn:db:/LAB-SAP-001:DON'
            }, {
                'type': 'runs on',
                'data': {},
                'source_id': 'urn:sap:/db_component:LAB-SAP-001:DON:Database',
                'target_id': 'urn:db:/LAB-SAP-001:DON'
            }, {
                'type': 'runs on',
                'data': {},
                'source_id': 'urn:sap:/db_component:LAB-SAP-001:DON:Archiver',
                'target_id': 'urn:db:/LAB-SAP-001:DON'
            }, {
                'type': 'runs on',
                'data': {},
                'source_id': 'urn:sap:/db_component:LAB-SAP-001:DON:Listener',
                'target_id': 'urn:db:/LAB-SAP-001:DON'
            }])

        aggregator.assert_event(msg_text="",
                                tags=[
                                    "status:SAPHostControl-DB-RUNNING",
                                    "database_name:DON",
                                ])

        aggregator.assert_event(msg_text="",
                                tags=[
                                    "status:SAPHostControl-DB-RUNNING",
                                    "database_component_name:Instance",
                                ])
        aggregator.assert_event(msg_text="",
                                tags=[
                                    "status:SAPHostControl-DB-RUNNING",
                                    "database_component_name:Database",
                                ])
        aggregator.assert_event(msg_text="",
                                tags=[
                                    "status:SAPHostControl-DB-RUNNING",
                                    "database_component_name:Archiver",
                                ])
        aggregator.assert_event(msg_text="",
                                tags=[
                                    "status:SAPHostControl-DB-RUNNING",
                                    "database_component_name:Listener",
                                ])

        aggregator.all_metrics_asserted()