Skip to content

Commit

Permalink
Test metrics exclusion for metrics exporter
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-zaitsev committed Nov 4, 2024
1 parent ee3ac17 commit e00d90c
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 24 deletions.
7 changes: 5 additions & 2 deletions tests/e2e/manifests/chi/test-050-labels.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@ kind: "ClickHouseInstallation"
metadata:
name: test-050
labels:
exclude_this_label: test-050
include_this_label: test-050
exclude_this_label: test-050-label
include_this_label: test-050-label
annotations:
exclude_this_annotation: test-050-annotation
include_this_annotation: test-050-annotation
spec:
useTemplates:
- name: clickhouse-version
Expand Down
10 changes: 9 additions & 1 deletion tests/e2e/manifests/chopconf/test-050-chopconf.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,14 @@ kind: "ClickHouseOperatorConfiguration"
metadata:
name: "test-050-chopconf"
spec:
metrics:
labels:
exclude:
- exclude_this_label
- exclude_this_annotation
label:
exclude:
- exclude_this_label
- exclude_this_label
annotation:
exclude:
- exclude_this_annotation
35 changes: 23 additions & 12 deletions tests/e2e/steps.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,31 +152,42 @@ def check_metrics_monitoring(
self,
operator_namespace,
operator_pod,
expect_pattern,
expect_pattern = "",
expect_metric = "",
expect_labels = "",
container="metrics-exporter",
port="8888",
max_retries=7
):
with Then(f"metrics-exporter /metrics endpoint result should contain {expect_pattern}"):
with Then(f"metrics-exporter /metrics endpoint result should contain {expect_pattern} {expect_metric} {expect_labels}"):
for i in range(1, max_retries):
url_cmd = util.make_http_get_request("127.0.0.1", port, "/metrics")
out = kubectl.launch(
f"exec {operator_pod} -c {container} -- {url_cmd}",
ns=operator_namespace,
)
# print(out)
if expect_metric != "":
lines = [m for m in out.splitlines() if m.startswith(expect_metric)]
if len(lines) > 0:
metric = lines[0]
print(metric)
expected_pattern_found = expect_labels in metric
else:
expected_pattern_found = False
break

rx = re.compile(expect_pattern, re.MULTILINE)
matches = rx.findall(out)
expected_pattern_found = False
if expect_pattern != "":
rx = re.compile(expect_pattern, re.MULTILINE)
matches = rx.findall(out)
expected_pattern_found = False

if matches:
expected_pattern_found = True
if matches:
expected_pattern_found = True

if expected_pattern_found:
break
if expected_pattern_found:
break

with Then("Not ready. Wait for " + str(i * 5) + " seconds"):
time.sleep(i * 5)
with Then("Not ready. Wait for " + str(i * 5) + " seconds"):
time.sleep(i * 5)

assert expected_pattern_found, error()
37 changes: 28 additions & 9 deletions tests/e2e/test_operator.py
Original file line number Diff line number Diff line change
Expand Up @@ -4758,20 +4758,39 @@ def test_050(self):
},
)

def test_labels(chi, label, value):
def test_labels(chi, type, key, value):

with Then(f"Pod label {label}={value} should populated from CHI"):
assert kubectl.get_field("pod", f"-l clickhouse.altinity.com/chi={chi}", f".metadata.labels.{label}") == value
with Then(f"Pod {type} {key}={value} should populated from CHI"):
assert kubectl.get_field("pod", f"-l clickhouse.altinity.com/chi={chi}", f".metadata.{type}s.{key}") == value

with And(f"Service label {label}={value} should populated from CHI"):
assert kubectl.get_field("service", f"-l clickhouse.altinity.com/chi={chi}", f".metadata.labels.{label}") == value
with And(f"Service {type} {key}={value} should populated from CHI"):
assert kubectl.get_field("service", f"-l clickhouse.altinity.com/chi={chi}", f".metadata.{type}s.{key}") == value

with And(f"PVC label {label}={value} should populated from CHI"):
assert kubectl.get_field("pvc", f"-l clickhouse.altinity.com/chi={chi}", f".metadata.labels.{label}") == value
with And(f"PVC {type} {key}={value} should populated from CHI"):
assert kubectl.get_field("pvc", f"-l clickhouse.altinity.com/chi={chi}", f".metadata.{type}s.{key}") == value

test_labels(chi, "include_this_label", "test-050")
test_labels(chi, "label", "include_this_label", "test-050-label")

test_labels(chi, "exclude_this_label", "<none>")
test_labels(chi, "label", "exclude_this_label", "<none>")

test_labels(chi, "annotation", "include_this_annotation", "test-050-annotation")

test_labels(chi, "annotation", "exclude_this_annotation", "<none>")


with Then("Check that exposed metrics do not have labels and annotations that are excluded"):
operator_namespace=current().context.operator_namespace
out = kubectl.launch("get pods -l app=clickhouse-operator", ns=operator_namespace).splitlines()[1]
operator_pod = re.split(r"[\t\r\n\s]+", out)[0]

# chi_clickhouse_metric_VersionInteger{chi="test-050",exclude_this_annotation="test-050-annotation",hostname="chi-test-050-default-0-0.test-050-e1884706-9a94-11ef-a786-367ddacfe5fd.svc.cluster.local",include_this_annotation="test-050-annotation",include_this_label="test-050-label",namespace="test-050-e1884706-9a94-11ef-a786-367ddacfe5fd"}
expect_labels = f"chi=\"test-050\",hostname=\"chi-test-050-default-0-0.{operator_namespace}.svc.cluster.local\",include_this_annotation=\"test-050-annotation\",include_this_label=\"test-050-label\""
check_metrics_monitoring(
operator_namespace = operator_namespace,
operator_pod=operator_pod,
expect_metric="chi_clickhouse_metric_VersionInteger",
expect_labels=expect_labels
)

with Finally("I clean up"):
delete_test_namespace()
Expand Down

0 comments on commit e00d90c

Please sign in to comment.