def test_full_topology(dynatrace_check, requests_mock, topology):
    """
    Test e2e to collect full topology for all component types from Dynatrace
    """
    set_http_responses(requests_mock,
                       hosts=read_file("host_response.json", "samples"),
                       applications=read_file("application_response.json",
                                              "samples"),
                       services=read_file("service_response.json", "samples"),
                       processes=read_file("process_response.json", "samples"),
                       process_groups=read_file("process-group_response.json",
                                                "samples"))

    dynatrace_check.run()

    expected_topology = load_json_from_file(
        "expected_smartscape_full_topology.json", "samples")
    actual_topology = topology.get_snapshot(dynatrace_check.check_id)

    components, relations = sort_topology_data(actual_topology)
    expected_components, expected_relations = sort_topology_data(
        expected_topology)

    assert len(components) == len(expected_components)
    for component in components:
        assert component in expected_components
    assert len(relations) == len(expected_relations)
    for relation in relations:
        assert relation in expected_relations
示例#2
0
def test_get_npm_topology_data(requests_mock, test_instance, solarwinds_check):
    orion_server = test_instance.get("url")
    QUERY_URL = "https://{orion_server}:17778/SolarWinds/InformationService/v3/Json/Query".format(
        orion_server=orion_server)
    requests_mock.post(QUERY_URL,
                       text=read_file("npm_topology_data.json", "samples"))
    solarwinds_check.run()
    npm_topology_data = solarwinds_check.get_npm_topology_data("", "")
    assert npm_topology_data == load_json_from_file("npm_topology_data.json",
                                                    "samples")["results"]
def test_collect_services(requests_mock, dynatrace_check, topology):
    """
    Testing Dynatrace check should collect services and tags coming from Kubernetes
    """
    set_http_responses(requests_mock,
                       services=read_file("service_response.json", "samples"))
    dynatrace_check.run()
    test_topology = topology.get_snapshot(dynatrace_check.check_id)
    expected_topology = load_json_from_file("expected_service_topology.json",
                                            "samples")
    assert_topology(expected_topology, test_topology)
def test_collect_hosts(requests_mock, dynatrace_check, topology):
    """
    Testing Dynatrace check should collect hosts
    """
    set_http_responses(requests_mock,
                       hosts=read_file("host_response.json", "samples"))
    dynatrace_check.run()
    test_topology = topology.get_snapshot(dynatrace_check.check_id)
    expected_topology = load_json_from_file("expected_host_topology.json",
                                            "samples")
    assert_topology(expected_topology, test_topology)
示例#5
0
 def test_compact_unicode_response(self):
     http = HTTPHelper()
     mock_text = read_file('unicode_sample.json', 'samples')
     unicode_json_response = http.get({
         "endpoint": "mock://test.com",
         "mock_enable": True,
         "mock_status": 200,
         "mock_text": mock_text
     })
     expected_result = load_json_from_file('unicode_sample.json', 'samples')
     self.assertEqual(unicode_json_response.get_json(), expected_result)
def test_collect_process_groups(dynatrace_check, requests_mock, topology):
    """
    Testing Dynatrace check should collect process-groups
    """
    set_http_responses(requests_mock,
                       process_groups=read_file("process-group_response.json",
                                                "samples"))
    dynatrace_check.run()
    topology_instances = topology.get_snapshot(dynatrace_check.check_id)
    expected_topology = load_json_from_file(
        "expected_process-group_topology.json", "samples")
    assert_topology(expected_topology, topology_instances)
def test_collect_applications(dynatrace_check, requests_mock, topology):
    """
    Testing Dynatrace check should collect applications and also the tags properly coming from dynatrace
    """
    set_http_responses(requests_mock,
                       applications=read_file("application_response.json",
                                              "samples"))
    dynatrace_check.run()
    topology_instances = topology.get_snapshot(dynatrace_check.check_id)
    expected_topology = load_json_from_file(
        "expected_application_topology.json", "samples")
    assert_topology(expected_topology, topology_instances)
def test_collect_custom_devices(dynatrace_check, requests_mock, topology):
    """
    Test Dynatrace check should produce custom devices
    """
    set_http_responses(requests_mock,
                       entities=read_file("custom_device_response.json",
                                          "samples"))
    dynatrace_check.run()
    topology_instances = topology.get_snapshot(dynatrace_check.check_id)
    expected_topology = load_json_from_file(
        "expected_custom_device_topology.json", "samples")
    assert_topology(expected_topology, topology_instances)
示例#9
0
def test_get_udt_topology_data(requests_mock, test_instance, solarwinds_check):
    orion_server = test_instance.get("url")
    QUERY_URL = "https://{orion_server}:17778/SolarWinds/InformationService/v3/Json/Query".format(
        orion_server=orion_server)
    requests_mock.post(QUERY_URL,
                       text=read_file("udt_topology_data.json", "samples"))
    solarwinds_check.run()
    # Make sure we can re-use the topology data later
    global udt_topology_data
    udt_topology_data = solarwinds_check.get_udt_topology_data(
        where_clause='dummy where clause')
    assert udt_topology_data == load_json_from_file("udt_topology_data.json",
                                                    "samples")["results"]
示例#10
0
def test_create_npm_topology(solarwinds_check):
    solarwinds_domain = "StackState_Domain"
    npm_topology_data = load_json_from_file("npm_topology_data.json",
                                            "samples")["results"]
    # Make sure we can re-use the topology
    global npm_topology
    solarwinds_check.base_url = 'https://solarwinds.procyonnetworks.nl'
    npm_topology = solarwinds_check.create_npm_topology(
        npm_topology_data, solarwinds_domain)
    # Compare serialized npm topology with saved version
    serialized_npm_topology = json.loads(
        json.dumps(npm_topology, indent=4, default=lambda o: o.__dict__))
    expected_npm_topology = json.loads(
        read_file("npm_topology_dumps.json", "samples"))
    assert serialized_npm_topology == expected_npm_topology
    def test_topology_items_from_config_check(self):
        instance_config = {
            "stackstate-layer": "layer-conf-a",
            "stackstate-environment": "environment-conf-a",
            "stackstate-domain": "domain-conf-a",
            "collection_interval": 5
        }
        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 == load_json_from_file(
            'expected_topology_instance_topology_config.json', 'expected')
def test_collect_custom_devices_with_pagination(dynatrace_check, requests_mock,
                                                test_instance, topology):
    """
    Test Dynatrace check should produce custom devices with pagination
    """
    set_http_responses(requests_mock)
    url = test_instance.get('url')
    first_url = url + "/api/v2/entities?entitySelector=type%28%22CUSTOM_DEVICE%22%29&from=now-1h&fields=%2B" \
                      "fromRelationships%2C%2BtoRelationships%2C%2Btags%2C%2BmanagementZones%2C%2B" \
                      "properties.dnsNames%2C%2Bproperties.ipAddress"
    second_url = url + "/api/v2/entities?nextPageKey=nextpageresultkey"
    requests_mock.get(first_url,
                      status_code=200,
                      text=read_file("custom_device_response_next_page.json",
                                     "samples"))
    requests_mock.get(second_url,
                      status_code=200,
                      text=read_file("custom_device_response.json", "samples"))
    dynatrace_check.run()
    snapshot = topology.get_snapshot(dynatrace_check.check_id)
    expected_topology = load_json_from_file(
        "expected_custom_device_pagination_full_topology.json", "samples")
    assert_topology(expected_topology, snapshot)
def test_state_data(state, dynatrace_check, test_instance, requests_mock,
                    aggregator):
    """
    Check is the right timestamp is writen to the state
    """
    timestamp = dynatrace_check.generate_bootstrap_timestamp(
        test_instance['events_boostrap_days'])
    state_instance = StateDescriptor(
        "instance.dynatrace_health.https_instance.live.dynatrace.com",
        "dynatrace.d")
    state.assert_state(state_instance, None)
    events_file = 'no_events_response.json'
    requests_mock.get('{}/api/v1/events?from={}'.format(
        test_instance['url'], timestamp),
                      status_code=200,
                      text=read_file(events_file, 'samples'))
    dynatrace_check.run()
    aggregator.assert_service_check(dynatrace_check.SERVICE_CHECK_NAME,
                                    count=1,
                                    status=AgentCheck.OK)
    mocked_response_data = load_json_from_file(events_file, 'samples')
    new_state = State(
        {'last_processed_event_timestamp': mocked_response_data.get('to')})
    state.assert_state(state_instance, new_state)
def test_generated_events(dynatrace_check, test_instance, requests_mock,
                          aggregator, telemetry, health):
    """
    Testing Dynatrace check should produce full events
    """
    url = test_instance['url']
    timestamp = dynatrace_check.generate_bootstrap_timestamp(
        test_instance['events_boostrap_days'])
    requests_mock.get('{}/api/v1/events?from={}'.format(url, timestamp),
                      status_code=200,
                      text=read_file('9_events_response.json', 'samples'))
    dynatrace_check.run()
    aggregator.assert_service_check(dynatrace_check.SERVICE_CHECK_NAME,
                                    count=1,
                                    status=AgentCheck.OK)
    health.assert_snapshot(
        dynatrace_check.check_id,
        dynatrace_check.health.stream,
        check_states=[{
            'checkStateId':
            'HOST-1E81568BDBFFE7FC',
            'health':
            'DEVIATING',
            'message':
            'Event: CPU_SATURATED Severity: '
            'RESOURCE_CONTENTION Impact: INFRASTRUCTURE Open '
            'Since: 1613466660000 Source: builtin',
            'name':
            'Dynatrace event',
            'topologyElementIdentifier':
            'urn:dynatrace:/HOST-1E81568BDBFFE7FC'
        }, {
            'checkStateId':
            'HOST-AA7D1491C9FE6BC4',
            'health':
            'CRITICAL',
            'message':
            'Event: CONNECTION_LOST Severity: AVAILABILITY '
            'Impact: INFRASTRUCTURE Open Since: '
            '1613430060468 Source: builtin',
            'name':
            'Dynatrace event',
            'topologyElementIdentifier':
            'urn:dynatrace:/HOST-AA7D1491C9FE6BC4'
        }, {
            'checkStateId':
            'HOST-8F3EDA24EEF51137',
            'health':
            'CRITICAL',
            'message':
            'Event: CONNECTION_LOST Severity: AVAILABILITY '
            'Impact: INFRASTRUCTURE Open Since: '
            '1613430060467 Source: builtin',
            'name':
            'Dynatrace event',
            'topologyElementIdentifier':
            'urn:dynatrace:/HOST-8F3EDA24EEF51137'
        }],
        start_snapshot={
            'expiry_interval_s': 0,
            'repeat_interval_s': 15
        },
        stop_snapshot={})
    assert len(telemetry._topology_events) == 1
    expected_topology_events = load_json_from_file(
        'expected_topology_events.json', 'samples')
    for expected_event in expected_topology_events:
        assert expected_event == telemetry._topology_events[0]
示例#15
0
 def test_load_json_from_same_directory(self):
     dict_from_json = load_json_from_file('test_data_sample.json')
     assert dict_from_json == json.loads(self.SAMPLE_FILE_CONTET)
    def test_check(self):
        result = self.check.run()
        assert result == ''
        topo_instances = topology.get_snapshot(self.check.check_id)
        self.assertEqual(len(topo_instances['components']), 6)
        self.assertEqual(len(topo_instances['relations']), 3)

        assert topo_instances == load_json_from_file(
            'expected_topology_instance.json', 'expected')

        aggregator.assert_metric(
            'system.cpu.usage',
            count=3,
            tags=["hostname:this-host", "region:eu-west-1"])
        aggregator.assert_metric(
            'location.availability',
            count=3,
            tags=["hostname:this-host", "region:eu-west-1"])
        aggregator.assert_metric(
            '2xx.responses',
            count=4,
            tags=["application:some_application", "region:eu-west-1"])
        aggregator.assert_metric(
            '5xx.responses',
            count=4,
            tags=["application:some_application", "region:eu-west-1"])
        aggregator.assert_metric('check_runs',
                                 count=1,
                                 tags=["integration:agent_integration_sample"])
        aggregator.assert_event(
            'Http request to {} timed out after {} seconds.'.format(
                'http://localhost', 5.0),
            count=1)
        telemetry.assert_topology_event(
            {
                "timestamp": int(1),
                "event_type": "HTTP_TIMEOUT",
                "source_type_name": "HTTP_TIMEOUT",
                "msg_title": "URL timeout",
                "msg_text":
                "Http request to http://localhost timed out after 5.0 seconds.",
                "aggregation_key": "instance-request-http://localhost",
                "context": {
                    "source_identifier":
                    "source_identifier_value",
                    "element_identifiers": ["urn:host:/123"],
                    "source":
                    "source_value",
                    "category":
                    "my_category",
                    "data": {
                        "big_black_hole": "here",
                        "another_thing": 1,
                        "test": {
                            "1": "test"
                        }
                    },
                    "source_links": [{
                        "title": "my_event_external_link",
                        "url": "http://localhost"
                    }]
                }
            },
            count=1)
        aggregator.assert_service_check('example.can_connect', self.check.OK)
        health.assert_snapshot(self.check.check_id,
                               self.check.health.stream,
                               start_snapshot={
                                   'expiry_interval_s': 0,
                                   'repeat_interval_s': 30
                               },
                               stop_snapshot={},
                               check_states=[{
                                   'checkStateId': 'id',
                                   'health': 'CRITICAL',
                                   'name': 'name',
                                   'topologyElementIdentifier': 'identifier',
                                   'message': 'msg'
                               }])

        telemetry.assert_metric(
            "raw.metrics",
            count=2,
            value=20,
            tags=["application:some_application", "region:eu-west-1"],
            hostname="hostname")
        telemetry.assert_metric("raw.metrics",
                                count=1,
                                value=30,
                                tags=["no:hostname", "region:eu-west-1"],
                                hostname="")