-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Expand file tree
/
Copy pathconftest.py
More file actions
151 lines (123 loc) · 4.49 KB
/
Copy pathconftest.py
File metadata and controls
151 lines (123 loc) · 4.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# (C) Datadog, Inc. 2024-present
# All rights reserved
# Licensed under a 3-clause BSD style license (see LICENSE)
import json
import os
from unittest import mock
import pytest
from datadog_checks.dev import get_here
from datadog_checks.dev._env import get_state, save_state
from datadog_checks.dev.kind import kind_run
from datadog_checks.dev.subprocess import run_command
from datadog_checks.fluxcd import FluxcdCheck
HERE = get_here()
opj = os.path.join
# The Services in flux-system (source-controller, notification-controller) only expose the
# controllers' API port (9090) as port 80, not the Prometheus metrics port (8080), so Service DNS
# cannot reach /metrics for any controller. All four controllers are single-replica Deployments, so
# their pod IP is fetched directly and used to reach the metrics port. The `allow-scraping`
# NetworkPolicy shipped in install.yaml explicitly permits cross-namespace ingress on port 8080,
# confirming this is the intended scrape path.
CONTROLLERS = ('source-controller', 'helm-controller', 'kustomize-controller', 'notification-controller')
METRICS_PORT = 8080
POD_IP_STATE_PREFIX = 'fluxcd_pod_ip_'
def setup_fluxcd():
run_command(["kubectl", "apply", "--filename", opj(HERE, 'kind', "install.yaml")])
run_command(
[
"kubectl",
"wait",
"deployments",
"--all",
"--for=condition=Available",
"--namespace",
"flux-system",
"--timeout=300s",
]
)
# Save each controller's pod IP now, while the cluster is guaranteed to be up. `dd_environment`
# runs again (without `conditions`, so without this function) on every `ddev env` invocation,
# including `stop`, when the cluster may already be gone.
for controller in CONTROLLERS:
save_state(POD_IP_STATE_PREFIX + controller, get_controller_pod_ip(controller))
def get_controller_pod_ip(controller: str) -> str:
result = run_command(
[
"kubectl",
"get",
"pods",
"--namespace",
"flux-system",
"--selector",
f"app={controller}",
"--output",
"json",
],
capture='out',
check=True,
)
pods = json.loads(result.stdout)['items']
if len(pods) != 1 or not pods[0].get('status', {}).get('podIP'):
raise RuntimeError(f'Expected exactly one ready {controller} pod, found {len(pods)}')
return pods[0]['status']['podIP']
@pytest.fixture(scope='session')
def dd_environment():
with kind_run(conditions=[setup_fluxcd]) as kubeconfig:
instances = [
{
'openmetrics_endpoint': f'http://{get_state(POD_IP_STATE_PREFIX + controller)}:{METRICS_PORT}/metrics',
}
for controller in CONTROLLERS
]
metadata = {'agent_type': 'kubernetes', 'kubernetes': {'kubeconfig': kubeconfig}}
yield {'instances': instances}, metadata
@pytest.fixture
def instance():
return {
"openmetrics_endpoint": "http://localhost:3000/metrics",
}
@pytest.fixture
def check(instance):
return FluxcdCheck("fluxcd", {}, [instance])
@pytest.fixture()
def mock_metrics_v1():
fixture_file = os.path.join(os.path.dirname(__file__), "fixtures", "metrics-v1.txt")
with open(fixture_file, "r") as f:
content = f.read()
with mock.patch(
"requests.Session.get",
return_value=mock.MagicMock(
status_code=200,
iter_lines=lambda **kwargs: content.split("\n"),
headers={"Content-Type": "text/plain"},
),
):
yield
@pytest.fixture()
def mock_metrics_v2():
fixture_file = os.path.join(os.path.dirname(__file__), "fixtures", "metrics-v2.txt")
with open(fixture_file, "r") as f:
content = f.read()
with mock.patch(
"requests.Session.get",
return_value=mock.MagicMock(
status_code=200,
iter_lines=lambda **kwargs: content.split("\n"),
headers={"Content-Type": "text/plain"},
),
):
yield
@pytest.fixture()
def mock_metrics_ksm():
fixture_file = os.path.join(os.path.dirname(__file__), "fixtures", "metrics-ksm.txt")
with open(fixture_file, "r") as f:
content = f.read()
with mock.patch(
"requests.Session.get",
return_value=mock.MagicMock(
status_code=200,
iter_lines=lambda **kwargs: content.split("\n"),
headers={"Content-Type": "text/plain"},
),
):
yield