def test_handle_incomplete_line(self, mock):
     assert self.check.run() == ""
     health.assert_snapshot(self.check.check_id, self.check.health.stream,
                            start_snapshot={'expiry_interval_s': 0, 'repeat_interval_s': 15},
                            stop_snapshot={},
                            check_states=[{'checkStateId': '1',
                                           'health': 'CLEAR',
                                           'name': 'name1',
                                           'topologyElementIdentifier': 'id1'}
                                          ])
 def test_health_min_values(self, mock):
     assert self.check.run() == ""
     health.assert_snapshot(self.check.check_id, self.check.health.stream,
                            start_snapshot={'expiry_interval_s': 0, 'repeat_interval_s': 15},
                            stop_snapshot={},
                            check_states=[{'checkStateId': '1',
                                           'health': 'CLEAR',
                                           'name': 'name1',
                                           'topologyElementIdentifier': 'id1'},
                                          {'checkStateId': '2',
                                           'health': 'CRITICAL',
                                           'name': 'name2',
                                           'topologyElementIdentifier': 'id2'},
                                          ])
     AgentIntegrationTestUtil.assert_integration_snapshot(self.check, 'StaticHealth:health.csv')
    def test_empty_health_file(self):
        instance = {
            'type': 'csv',
            'health_file': '/dev/null',
            'delimiter': ';',
            'collection_interval': 15
        }

        config = {
            'init_config': {},
            'instances': [instance]
        }

        self.check = StaticHealthCheck(self.CHECK_NAME, config, instances=[instance])

        assert json.loads(self.check.run())[0]['message'] == 'Health CSV file is empty.'
        # Snapshot will be started but not stopped
        health.assert_snapshot(self.check.check_id, self.check.health.stream,
                               start_snapshot={'expiry_interval_s': 0, 'repeat_interval_s': 15})
 def test_wrong_health(self, mock):
     assert json.loads(self.check.run())[0]['message'] == '["Health value must be clear, deviating or critical"]'
     # Snapshot will be started but not stopped
     health.assert_snapshot(self.check.check_id, self.check.health.stream,
                            start_snapshot={'expiry_interval_s': 0, 'repeat_interval_s': 15})
 def test_missing_topology_element_identifier_field(self, mock):
     assert json.loads(self.check.run())[0]['message'] ==\
            'CSV header topology_element_identifier not found in health csv.'
     # Snapshot will be started but not stopped
     health.assert_snapshot(self.check.check_id, self.check.health.stream,
                            start_snapshot={'expiry_interval_s': 0, 'repeat_interval_s': 15})
    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="")