Skip to content

Commit a69ac09

Browse files
committed
add unit tests, setup mocks, fix integration tests
1 parent 52e2c73 commit a69ac09

File tree

5 files changed

+30
-25
lines changed

5 files changed

+30
-25
lines changed
Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,26 @@
11
# (C) Datadog, Inc. 2024-present
22
# All rights reserved
33
# Licensed under a 3-clause BSD style license (see LICENSE)
4-
import copy
54

65
from datadog_checks.base import OpenMetricsBaseCheckV2
7-
from datadog_checks.base.errors import CheckException # noqa: F401
86

9-
# from datadog_checks.base.utils.db import QueryManager
10-
# from requests.exceptions import ConnectionError, HTTPError, InvalidURL, Timeout
11-
# from json import JSONDecodeError
7+
METRIC_MAP = {
8+
"process_state": "process_state",
9+
# "certificate_mismatch_total": "certificate_mismatch_total",
10+
"rx": "rx",
11+
"server_interactive_sessions_total": "server_interactive_sessions_total",
12+
# "teleport_build_info": "teleport_build_info",
13+
"teleport_cache_events": "teleport_cache_events",
14+
"teleport_cache_stale_events": "teleport_cache_stale_events",
15+
"tx": "tx",
16+
}
1217

1318

1419
class TeleportCheck(OpenMetricsBaseCheckV2):
15-
# This will be the prefix of every metric and service check the integration sends
1620
__NAMESPACE__ = 'teleport'
1721

1822
def __init__(self, name, init_config, instances):
1923
super().__init__(name, init_config, instances)
20-
self.diagnostic_url = self.instance.get("diagnostic_url")
2124
self.check_initializations.appendleft(self._parse_config)
2225

2326
def check(self, _):
@@ -32,6 +35,7 @@ def check(self, _):
3235
pass
3336

3437
def _parse_config(self):
38+
self.diagnostic_url = self.instance.get("diagnostic_url")
3539
if self.diagnostic_url:
3640
self.instance.setdefault("openmetrics_endpoint", self.diagnostic_url + "/metrics")
37-
self.instance.setdefault("metrics", ["process_state"])
41+
self.instance.setdefault("metrics", [METRIC_MAP])

teleport/tests/conftest.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@
1212
def dd_environment():
1313
compose_file = os.path.join(get_here(), 'docker', 'docker-compose.yaml')
1414

15-
with docker_run(compose_file, sleep=5):
15+
def nothing():
16+
pass
17+
18+
with docker_run(compose_file, sleep=5, down=nothing):
1619
instance = {"diagnostic_url": "http://127.0.0.1:3000"}
1720
yield instance
1821

teleport/tests/fixtures/metrics.txt

Whitespace-only changes.

teleport/tests/test_integration.py

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
import pytest
66

7-
from datadog_checks.base import AgentCheck
87
from datadog_checks.teleport import TeleportCheck
98

109
pytestmark = [pytest.mark.integration, pytest.mark.usefixtures('dd_environment')]
@@ -14,25 +13,13 @@ def test_connect_ok(aggregator, dd_run_check):
1413
instance = {"diagnostic_url": "http://127.0.0.1:3000"}
1514
check = TeleportCheck('teleport', {}, [instance])
1615
dd_run_check(check)
17-
aggregator.assert_service_check('teleport.health.up', status=AgentCheck.OK, count=1)
18-
aggregator.assert_service_check('teleport.health.up', status=AgentCheck.CRITICAL, count=0)
16+
aggregator.assert_service_check('teleport.health.up', status=TeleportCheck.OK, count=1)
17+
aggregator.assert_service_check('teleport.health.up', status=TeleportCheck.CRITICAL, count=0)
1918

2019

2120
def test_check_collects_teleport_common_metrics(aggregator, dd_run_check):
2221
instance = {"diagnostic_url": "http://127.0.0.1:3000"}
2322
check = TeleportCheck('teleport', {}, [instance])
2423
dd_run_check(check)
2524

26-
common_metrics = [
27-
"process_state",
28-
"certificate_mismatch_total",
29-
"rx",
30-
"server_interactive_sessions_total",
31-
"teleport_build_info",
32-
"teleport_cache_events",
33-
"teleport_cache_stale_events",
34-
"tx",
35-
]
36-
37-
for metric in common_metrics:
38-
aggregator.assert_metric(f"teleport.{metric}", tags=None, count=1)
25+
aggregator.assert_metric("teleport.process_state")

teleport/tests/test_unit.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@
22
# All rights reserved
33
# Licensed under a 3-clause BSD style license (see LICENSE)
44

5+
import os
6+
57
import pytest
68

9+
from datadog_checks.dev import get_here
710
from datadog_checks.teleport import TeleportCheck
811

912
pytestmark = [pytest.mark.unit]
@@ -14,3 +17,11 @@ def test_connect_exception(dd_run_check):
1417
check = TeleportCheck('teleport', {}, [instance])
1518
with pytest.raises(Exception):
1619
dd_run_check(check)
20+
21+
22+
def test_common_teleport_metrics(dd_run_check, mock_http_response):
23+
fixtures_path = os.path.join(get_here(), 'fixtures', 'metrics.txt')
24+
mock_http_response(file_path=fixtures_path)
25+
instance = {"diagnostic_url": "http://127.0.0.1:3000"}
26+
check = TeleportCheck('teleport', {}, [instance])
27+
dd_run_check(check)

0 commit comments

Comments
 (0)