-
Notifications
You must be signed in to change notification settings - Fork 35
/
test_prometheus_scrape_multiunit.py
430 lines (372 loc) · 16.6 KB
/
test_prometheus_scrape_multiunit.py
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
#!/usr/bin/env python3
# Copyright 2021 Canonical Ltd.
# See LICENSE file for licensing details.
"""This test module tests the prometheus_scrape interface with multiple-to-multiple units related.
This test scaling up/down both sides of the relation, and upgrading.
1. Deploy several units of prometheus and several units of a "provider" charm, and relate them.
2. Confirm all units of prometheus have the correct and same targets and rules.
3. Upgrade prometheus.
4. Scale prometheus up and down.
5. Scale the "provider" charm up and down.
"""
import asyncio
import logging
import pytest
import yaml
from deepdiff import DeepDiff
from helpers import (
check_prometheus_is_ready,
get_prometheus_active_targets,
get_prometheus_config,
get_prometheus_rules,
oci_image,
run_promql,
)
from pytest_operator.plugin import OpsTest
logger = logging.getLogger(__name__)
prometheus_app_name = "prometheus"
prometheus_resources = {"prometheus-image": oci_image("./metadata.yaml", "prometheus-image")}
scrape_tester = "tester"
scrape_tester_resources = {
"prometheus-tester-image": oci_image(
"./tests/integration/prometheus-tester/metadata.yaml",
"prometheus-tester-image",
)
}
remote_write_tester = "grafana-agent"
num_units = 2 # Using the same number of units for both prometheus and the testers
# The period of time required to be idle before `wait_for_idle` returns is set to 90 sec because
# unit upgrades were observed to take place 40-70 seconds apart.
idle_period = 90
@pytest.mark.skip(reason="xfail")
async def test_setup_env(ops_test: OpsTest):
await ops_test.model.set_config(
{"logging-config": "<root>=WARNING; unit=DEBUG", "update-status-hook-interval": "60m"}
)
@pytest.mark.skip(reason="xfail")
async def test_prometheus_scrape_relation_with_prometheus_tester(
ops_test: OpsTest, prometheus_charm, prometheus_tester_charm
):
"""Relate several units of prometheus and several units of the tester charm.
- Deploy several units of prometheus and several units of a "provider" charm, and relate them.
- Confirm all units of prometheus have the correct and same targets and rules.
"""
app_names = [prometheus_app_name, scrape_tester, remote_write_tester]
# GIVEN prometheus and the tester charm are deployed with two units each
await asyncio.gather(
ops_test.model.deploy(
prometheus_charm,
resources=prometheus_resources,
application_name=prometheus_app_name,
num_units=num_units,
trust=True, # otherwise errors on ghwf (persistentvolumeclaims ... is forbidden)
),
ops_test.model.deploy(
prometheus_tester_charm,
resources=scrape_tester_resources,
application_name=scrape_tester,
num_units=num_units,
),
ops_test.model.deploy(
"ch:grafana-agent-k8s",
application_name=remote_write_tester,
channel="edge",
num_units=num_units,
trust=True,
),
)
await ops_test.model.wait_for_idle(
apps=app_names, status="active", wait_for_units=num_units, timeout=600
)
await asyncio.gather(
*[check_prometheus_is_ready(ops_test, prometheus_app_name, u) for u in range(num_units)]
)
# WHEN prometheus is not related to anything
# THEN all prometheus units should have only one scrape target (self-scraping)
for unit_num in range(num_units):
targets = await get_prometheus_active_targets(ops_test, prometheus_app_name, unit_num)
assert len(targets) == 1
self_scrape = next(iter(targets))
assert self_scrape["labels"]["job"] == "prometheus"
assert self_scrape["labels"]["host"] == "localhost"
# WHEN prometheus is related to the testers
await asyncio.gather(
ops_test.model.add_relation(
f"{prometheus_app_name}:metrics-endpoint", f"{scrape_tester}:metrics-endpoint"
),
ops_test.model.add_relation(
f"{prometheus_app_name}:receive-remote-write",
f"{remote_write_tester}:send-remote-write",
),
)
await ops_test.model.wait_for_idle(apps=app_names, status="active")
# THEN all prometheus units should have all scrape units as targets (as well as self-scraping)
# `targets_by_unit` is a List[List[dict]]: every unit has a List[dict] targets.
targets_by_unit = await asyncio.gather(
*[
get_prometheus_active_targets(ops_test, prometheus_app_name, u)
for u in range(num_units)
]
)
assert all(len(targets) == num_units + 1 for targets in targets_by_unit)
# AND all prometheus units have the exact same targets
# Only comparing the `labels` because comparing the entire `targets` dict would be cumbersome:
# would need to pop 'lastScrape', 'lastScrapeDuration', whose values may differ across units.
labels = [[{"labels": d["labels"]} for d in unit_targets] for unit_targets in targets_by_unit]
for u in range(1, len(targets_by_unit)):
assert DeepDiff(labels[0], labels[u], ignore_order=True) == {}
# Could use `set`, but that would produce unhelpful error messages.
# assert len(set(map(lambda x: json.dumps(x, sort_keys=True), targets_by_unit))) == 1
# AND all prometheus units have the exact same config
config_by_unit = await asyncio.gather(
*[get_prometheus_config(ops_test, prometheus_app_name, u) for u in range(num_units)]
)
# Convert the yaml strings into dicts
config_by_unit = list(map(yaml.safe_load, config_by_unit))
for u in range(1, num_units):
# Exclude keys that are expected to differ (different IP address per unit)
assert (
DeepDiff(
config_by_unit[0],
config_by_unit[u],
ignore_order=True,
exclude_regex_paths=r"\['static_configs'\]\['targets'\]|\['labels'\]\['juju_unit'\]",
)
== {}
)
# AND all prometheus units have the exact same rules
rules_by_unit = await asyncio.gather(
*[get_prometheus_rules(ops_test, prometheus_app_name, u) for u in range(num_units)]
)
for u in range(1, len(rules_by_unit)):
# Some fields will most likely differ, such as "evaluationTime" and "lastEvaluation".
# Also excluding the following, which occasionally fails CI:
# - "alerts" because the 'rules' endpoint returns a dict of firing alerts,
# which may vary across prometheus units given the share nothing and the units starting
# up at different times.
# - "health", which takes time to switch from "unknown" to "ok".
# - "state", which takes time to switch from "inactive" to "firing".
assert (
DeepDiff(
rules_by_unit[0],
rules_by_unit[u],
ignore_order=True,
exclude_regex_paths=r"evaluationTime|lastEvaluation|activeAt|alerts|health|state",
)
== {}
)
@pytest.mark.skip(reason="xfail")
async def test_upgrade_prometheus(ops_test: OpsTest, prometheus_charm):
"""Upgrade prometheus and confirm all is still green (see also test_upgrade_charm.py)."""
# GIVEN an existing "up" timeseries
query = 'count_over_time(up{host="localhost",job="prometheus"}[1y])'
up_before = await asyncio.gather(
*[run_promql(ops_test, query, prometheus_app_name, u) for u in range(num_units)]
)
# Each response looks like this:
# [
# {
# "metric":{"instance":"localhost:9090","job":"prometheus"},
# "value":[1652985131.383,"711"]
# }
# ]
# Extract the count value and convert it to int
up_before = [int(next(iter(response))["value"][1]) for response in up_before]
# Sanity check: make sure it's not empty
assert len(up_before) > 0
assert all(up_before)
# WHEN prometheus is upgraded
await ops_test.model.applications[prometheus_app_name].refresh(
path=prometheus_charm, resources=prometheus_resources
)
# THEN nothing breaks
await ops_test.model.wait_for_idle(status="active", idle_period=idle_period, timeout=600)
await asyncio.gather(
*[check_prometheus_is_ready(ops_test, prometheus_app_name, u) for u in range(num_units)]
)
# AND series continuity is maintained
up_after = await asyncio.gather(
*[run_promql(ops_test, query, prometheus_app_name, u) for u in range(num_units)]
)
up_after = [int(next(iter(response))["value"][1]) for response in up_after]
# The count after an upgrade must be greater than or equal to the count before the upgrade, for
# every prometheus unit (units start at different times so the count across units may differ).
assert all(up_before[i] <= up_after[i] for i in range(num_units))
@pytest.mark.skip(reason="xfail")
async def test_rescale_prometheus(ops_test: OpsTest):
# GitHub runner doesn't have enough resources to deploy 3 unit with the default "requests", and
# the unit fails to schedule. Setting a low limit, so it is able to schedule.
await ops_test.model.applications[prometheus_app_name].set_config(
{"cpu": "0.2", "memory": "0.1Gi"}
)
await ops_test.model.wait_for_idle(apps=[prometheus_app_name], status="active", timeout=240)
# WHEN prometheus is scaled up
num_additional_units = 1
await ops_test.model.applications[prometheus_app_name].scale(scale_change=num_additional_units)
new_num_units = num_units + num_additional_units
# THEN nothing breaks
await ops_test.model.wait_for_idle(
apps=[prometheus_app_name],
status="active",
timeout=240,
wait_for_exact_units=new_num_units,
)
await ops_test.model.wait_for_idle(status="active")
await asyncio.gather(
*[
check_prometheus_is_ready(ops_test, prometheus_app_name, u)
for u in range(new_num_units)
]
)
# WHEN prometheus is scaled back down
await ops_test.model.applications[prometheus_app_name].scale(
scale_change=-num_additional_units
)
# THEN nothing breaks
await ops_test.model.wait_for_idle(
apps=[prometheus_app_name], status="active", timeout=240, wait_for_exact_units=num_units
)
await ops_test.model.wait_for_idle(status="active")
await asyncio.gather(
*[check_prometheus_is_ready(ops_test, prometheus_app_name, u) for u in range(num_units)]
)
@pytest.mark.skip(reason="xfail")
async def test_rescale_tester(ops_test: OpsTest):
# WHEN testers are scaled up
num_additional_units = 1
await asyncio.gather(
ops_test.model.applications[scrape_tester].scale(scale_change=num_additional_units),
ops_test.model.applications[remote_write_tester].scale(scale_change=num_additional_units),
)
new_num_units = num_units + num_additional_units
# THEN nothing breaks
await ops_test.model.wait_for_idle(
apps=[scrape_tester, remote_write_tester],
status="active",
timeout=240,
wait_for_exact_units=new_num_units,
)
await ops_test.model.wait_for_idle(status="active")
await asyncio.gather(
*[check_prometheus_is_ready(ops_test, prometheus_app_name, u) for u in range(num_units)]
)
# WHEN tester is scaled back down
await asyncio.gather(
ops_test.model.applications[scrape_tester].scale(scale_change=-num_additional_units),
ops_test.model.applications[remote_write_tester].scale(scale_change=-num_additional_units),
)
# THEN nothing breaks
await ops_test.model.wait_for_idle(
apps=[scrape_tester, remote_write_tester],
status="active",
timeout=240,
wait_for_exact_units=num_units,
)
await ops_test.model.wait_for_idle(status="active")
await asyncio.gather(
*[check_prometheus_is_ready(ops_test, prometheus_app_name, u) for u in range(num_units)]
)
@pytest.mark.skip(reason="xfail")
async def test_upgrade_prometheus_while_rescaling_testers(ops_test: OpsTest, prometheus_charm):
"""Upgrade prometheus and rescale testers at the same time (without waiting for idle)."""
# WHEN prometheus is upgraded at the same time that the testers are scaled up
num_additional_units = 1
logger.info("Upgrading prometheus and scaling-up testers at the same time...")
await asyncio.gather(
ops_test.model.applications[prometheus_app_name].refresh(
path=prometheus_charm, resources=prometheus_resources
),
ops_test.model.applications[scrape_tester].scale(scale_change=num_additional_units),
ops_test.model.applications[remote_write_tester].scale(scale_change=num_additional_units),
)
new_num_units = num_units + num_additional_units
# AND tester becomes active/idle after scale-up
logger.info("Waiting for testers to become active/idle...")
await ops_test.model.wait_for_idle(
apps=[scrape_tester, remote_write_tester],
status="active",
timeout=300,
wait_for_exact_units=new_num_units,
)
# AND all apps become idle after prometheus upgrade
logger.info("Waiting for all apps to become active/idle...")
await ops_test.model.wait_for_idle(status="active", idle_period=idle_period, timeout=600)
# THEN nothing breaks
await asyncio.gather(
*[check_prometheus_is_ready(ops_test, prometheus_app_name, u) for u in range(num_units)]
)
# WHEN prometheus is upgraded at the same time that the testers are scaled back down
logger.info("Upgrading prometheus and scaling-down testers at the same time...")
await asyncio.gather(
ops_test.model.applications[prometheus_app_name].refresh(
path=prometheus_charm, resources=prometheus_resources
),
ops_test.model.applications[scrape_tester].scale(scale_change=-num_additional_units),
ops_test.model.applications[remote_write_tester].scale(scale_change=-num_additional_units),
)
# AND tester becomes active/idle after scale-down
logger.info("Waiting for testers to become active/idle...")
await ops_test.model.wait_for_idle(
apps=[scrape_tester, remote_write_tester],
status="active",
timeout=300,
wait_for_exact_units=num_units,
)
# AND all apps become idle after prometheus upgrade
logger.info("Waiting for all apps to become active/idle...")
await ops_test.model.wait_for_idle(status="active", idle_period=idle_period, timeout=300)
# THEN nothing breaks
await asyncio.gather(
*[check_prometheus_is_ready(ops_test, prometheus_app_name, u) for u in range(num_units)]
)
@pytest.mark.skip(reason="xfail")
async def test_rescale_prometheus_while_upgrading_testers(
ops_test: OpsTest, prometheus_tester_charm
):
# WHEN prometheus is scaled up at the same time the testers are upgraded
num_additional_units = 1
await asyncio.gather(
ops_test.model.applications[scrape_tester].refresh(
path=prometheus_tester_charm, resources=scrape_tester_resources
),
# ops_test.model.applications[remote_write_tester].refresh(channel="edge"),
ops_test.model.applications[prometheus_app_name].scale(scale_change=num_additional_units),
)
new_num_units = num_units + num_additional_units
# AND prometheus becomes active/idle after scale-up
await ops_test.model.wait_for_idle(
apps=[prometheus_app_name],
status="active",
timeout=300,
wait_for_exact_units=new_num_units,
)
# AND all apps become idle after tester upgrade
await ops_test.model.wait_for_idle(status="active", idle_period=idle_period, timeout=300)
# THEN nothing breaks
await asyncio.gather(
*[
check_prometheus_is_ready(ops_test, prometheus_app_name, u)
for u in range(new_num_units)
]
)
# WHEN prometheus is scaled back down at the same time the tester is upgraded
await asyncio.gather(
ops_test.model.applications[scrape_tester].refresh(
path=prometheus_tester_charm, resources=scrape_tester_resources
),
# ops_test.model.applications[remote_write_tester].refresh(channel="edge"),
ops_test.model.applications[prometheus_app_name].scale(scale_change=-num_additional_units),
)
# AND prometheus becomes active/idle after scale-down
await ops_test.model.wait_for_idle(
apps=[prometheus_app_name],
status="active",
timeout=300,
wait_for_exact_units=num_units,
)
# AND all apps become idle after tester upgrade
await ops_test.model.wait_for_idle(status="active", idle_period=idle_period, timeout=300)
# THEN nothing breaks
await asyncio.gather(
*[check_prometheus_is_ready(ops_test, prometheus_app_name, u) for u in range(num_units)]
)