Skip to content

Commit d9d6244

Browse files
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
1 parent 88a4b83 commit d9d6244

File tree

2 files changed

+71
-89
lines changed

2 files changed

+71
-89
lines changed

tests/model_explainability/guardrails/conftest.py

Lines changed: 53 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -2,37 +2,28 @@
22

33
import portforward
44
import pytest
5-
import yaml
6-
from _pytest.fixtures import FixtureRequest
75
from kubernetes.dynamic import DynamicClient
86
from kubernetes.dynamic.exceptions import ResourceNotFoundError
97
from ocp_resources.cluster_service_version import ClusterServiceVersion
10-
from ocp_resources.config_map import ConfigMap
118
from ocp_resources.deployment import Deployment
12-
from ocp_resources.exceptions import MissingRequiredArgumentError
13-
from ocp_resources.guardrails_orchestrator import GuardrailsOrchestrator
149
from ocp_resources.inference_service import InferenceService
15-
from ocp_resources.jaeger import Jaeger
1610
from ocp_resources.namespace import Namespace
1711
from ocp_resources.open_telemetry_collector import OpenTelemetryCollector
1812
from ocp_resources.persistent_volume_claim import PersistentVolumeClaim
1913
from ocp_resources.pod import Pod
20-
from ocp_resources.resource import ResourceEditor, NamespacedResource
2114
from ocp_resources.route import Route
2215
from ocp_resources.secret import Secret
2316
from ocp_resources.service import Service
2417
from ocp_resources.serving_runtime import ServingRuntime
25-
from ocp_resources.subscription import Subscription
2618
from ocp_utilities.operators import install_operator, uninstall_operator
27-
from pytest_testconfig import py_config
2819
from timeout_sampler import TimeoutSampler
2920

3021
from tests.model_explainability.guardrails.constants import AUTOCONFIG_DETECTOR_LABEL
3122
from utilities.certificates_utils import create_ca_bundle_file
3223
from utilities.constants import (
3324
KServeDeploymentType,
34-
Labels,
35-
Timeout, OPENSHIFT_OPERATORS, RuntimeTemplates, Protocols,
25+
Timeout,
26+
RuntimeTemplates,
3627
)
3728
from utilities.inference_utils import create_isvc, LOGGER
3829
from utilities.operator_utils import get_cluster_service_version
@@ -157,10 +148,9 @@ def hap_detector_route(
157148
wait_for_resource=True,
158149
)
159150

151+
160152
@pytest.fixture(scope="class")
161-
def installed_tempo_operator(
162-
admin_client: DynamicClient, model_namespace: Namespace
163-
) -> Generator[None, Any, None]:
153+
def installed_tempo_operator(admin_client: DynamicClient, model_namespace: Namespace) -> Generator[None, Any, None]:
164154
"""
165155
Installs the Tempo operator and waits for its deployment.
166156
"""
@@ -196,13 +186,10 @@ def installed_tempo_operator(
196186
clean_up_namespace=False,
197187
)
198188

189+
199190
@pytest.fixture(scope="class")
200191
def tempo_stack(
201-
admin_client: DynamicClient,
202-
installed_tempo_operator: None,
203-
model_namespace: Namespace,
204-
minio_secret_otel: Secret
205-
192+
admin_client: DynamicClient, installed_tempo_operator: None, model_namespace: Namespace, minio_secret_otel: Secret
206193
) -> Generator[Any, Any, None]:
207194
"""
208195
Create a TempoStack CR in the test namespace, configured to use the MinIO backend.
@@ -225,14 +212,11 @@ def tempo_stack(
225212
tempo_stack_dict: dict[str, Any] = next(
226213
example
227214
for example in alm_examples
228-
if example["kind"] == "TempoStack"
229-
and example["apiVersion"].startswith("tempo.grafana.com/")
215+
if example["kind"] == "TempoStack" and example["apiVersion"].startswith("tempo.grafana.com/")
230216
)
231217

232218
if not tempo_stack_dict:
233-
raise ResourceNotFoundError(
234-
f"No TempoStack dict found in alm_examples for CSV {tempo_csv.name}"
235-
)
219+
raise ResourceNotFoundError(f"No TempoStack dict found in alm_examples for CSV {tempo_csv.name}")
236220

237221
# Customize metadata
238222
tempo_stack_dict["metadata"]["namespace"] = model_namespace.name
@@ -274,6 +258,7 @@ def tempo_stack(
274258
)
275259
yield created_tempo_stack
276260

261+
277262
def wait_for_tempo_pods(
278263
client: DynamicClient,
279264
tempo_name: str,
@@ -283,6 +268,7 @@ def wait_for_tempo_pods(
283268
"""
284269
Wait for pods created by a Tempo instance to be ready.
285270
"""
271+
286272
def _get_tempo_pods() -> list[Pod]:
287273
return [
288274
_pod
@@ -293,9 +279,7 @@ def _get_tempo_pods() -> list[Pod]:
293279
)
294280
]
295281

296-
sampler = TimeoutSampler(
297-
wait_timeout=timeout, sleep=1, func=lambda: bool(_get_tempo_pods())
298-
)
282+
sampler = TimeoutSampler(wait_timeout=timeout, sleep=1, func=lambda: bool(_get_tempo_pods()))
299283

300284
for sample in sampler:
301285
if sample:
@@ -308,6 +292,7 @@ def _get_tempo_pods() -> list[Pod]:
308292
status="True",
309293
)
310294

295+
311296
@pytest.fixture(scope="class")
312297
def installed_opentelemetry_operator(admin_client: DynamicClient) -> Generator[None, Any, None]:
313298
"""
@@ -346,13 +331,14 @@ def installed_opentelemetry_operator(admin_client: DynamicClient) -> Generator[N
346331
clean_up_namespace=False,
347332
)
348333

334+
349335
@pytest.fixture(scope="class")
350336
def otel_collector(
351337
admin_client: DynamicClient,
352338
installed_opentelemetry_operator: None,
353339
tempo_stack,
354340
model_namespace: Namespace,
355-
minio_service_otel
341+
minio_service_otel,
356342
) -> Generator[OpenTelemetryCollector, Any, Any]:
357343
"""
358344
Create an OpenTelemetryCollector CR in the test namespace.
@@ -370,14 +356,11 @@ def otel_collector(
370356
otel_cr_dict: dict[str, Any] = next(
371357
example
372358
for example in alm_examples
373-
if example["kind"] == "OpenTelemetryCollector"
374-
and example["apiVersion"] == "opentelemetry.io/v1beta1"
359+
if example["kind"] == "OpenTelemetryCollector" and example["apiVersion"] == "opentelemetry.io/v1beta1"
375360
)
376361

377362
if not otel_cr_dict:
378-
raise ResourceNotFoundError(
379-
f"No OpenTelemetryCollector example found in ALM examples for {otel_csv.name}"
380-
)
363+
raise ResourceNotFoundError(f"No OpenTelemetryCollector example found in ALM examples for {otel_csv.name}")
381364

382365
# Update the metadata and spec to match test namespace and Tempo endpoint
383366
namespace = model_namespace.name
@@ -408,17 +391,7 @@ def otel_collector(
408391
}
409392
},
410393
"telemetry": {
411-
"metrics": {
412-
"readers": [
413-
{
414-
"pull": {
415-
"exporter": {
416-
"prometheus": {"host": "0.0.0.0", "port": 8888}
417-
}
418-
}
419-
}
420-
]
421-
}
394+
"metrics": {"readers": [{"pull": {"exporter": {"prometheus": {"host": "0.0.0.0", "port": 8888}}}}]}
422395
},
423396
},
424397
}
@@ -428,6 +401,7 @@ def otel_collector(
428401
wait_for_collector_pods(admin_client, namespace=namespace)
429402
yield otel_cr
430403

404+
431405
def wait_for_collector_pods(
432406
client: DynamicClient,
433407
namespace: str,
@@ -443,14 +417,12 @@ def _get_collector_pods() -> list[Pod]:
443417
for _pod in Pod.get(
444418
dyn_client=client,
445419
namespace=namespace,
446-
label_selector=f"app.kubernetes.io/component=opentelemetry-collector",
420+
label_selector="app.kubernetes.io/component=opentelemetry-collector",
447421
)
448422
]
449423
return pods
450424

451-
sampler = TimeoutSampler(
452-
wait_timeout=timeout, sleep=1, func=lambda: bool(_get_collector_pods())
453-
)
425+
sampler = TimeoutSampler(wait_timeout=timeout, sleep=1, func=lambda: bool(_get_collector_pods()))
454426

455427
for sample in sampler:
456428
if sample:
@@ -463,6 +435,7 @@ def _get_collector_pods() -> list[Pod]:
463435
status="True",
464436
)
465437

438+
466439
@pytest.fixture(scope="class")
467440
def minio_pvc_otel(
468441
admin_client: DynamicClient,
@@ -478,34 +451,39 @@ def minio_pvc_otel(
478451
"client": admin_client,
479452
"size": "2Gi",
480453
"accessmodes": "ReadWriteOnce",
481-
"label": {"app.kubernetes.io/name": "minio"}
454+
"label": {"app.kubernetes.io/name": "minio"},
482455
}
483456

484457
with PersistentVolumeClaim(**pvc_kwargs) as pvc:
485458
pvc.wait_for_status(status=pvc.Status.PENDING, timeout=120)
486459
yield pvc
487460

461+
488462
@pytest.fixture(scope="class")
489463
def minio_deployment_otel(admin_client, model_namespace):
490464
selector = {"matchLabels": {"app.kubernetes.io/name": "minio"}}
491465
pod_template = {
492466
"metadata": {"labels": {"app.kubernetes.io/name": "minio"}},
493467
"spec": {
494-
"containers": [{
495-
"name": "minio",
496-
"image": "quay.io/minio/minio",
497-
"command": ["/bin/sh", "-c", "mkdir -p /storage/tempo && minio server /storage"],
498-
"env": [
499-
{"name": "MINIO_ACCESS_KEY", "value": "tempo"},
500-
{"name": "MINIO_SECRET_KEY", "value": "supersecret"},
501-
],
502-
"ports": [{"containerPort": 9000}],
503-
"volumeMounts": [{"mountPath": "/storage", "name": "storage"}],
504-
}],
505-
"volumes": [{
506-
"name": "storage",
507-
"persistentVolumeClaim": {"claimName": "minio"},
508-
}],
468+
"containers": [
469+
{
470+
"name": "minio",
471+
"image": "quay.io/minio/minio",
472+
"command": ["/bin/sh", "-c", "mkdir -p /storage/tempo && minio server /storage"],
473+
"env": [
474+
{"name": "MINIO_ACCESS_KEY", "value": "tempo"},
475+
{"name": "MINIO_SECRET_KEY", "value": "supersecret"},
476+
],
477+
"ports": [{"containerPort": 9000}],
478+
"volumeMounts": [{"mountPath": "/storage", "name": "storage"}],
479+
}
480+
],
481+
"volumes": [
482+
{
483+
"name": "storage",
484+
"persistentVolumeClaim": {"claimName": "minio"},
485+
}
486+
],
509487
},
510488
}
511489

@@ -551,6 +529,7 @@ def minio_service_otel(admin_client, model_namespace):
551529

552530
yield service
553531

532+
554533
@pytest.fixture(scope="class")
555534
def minio_secret_otel(admin_client, model_namespace):
556535
secret = Secret(
@@ -578,11 +557,13 @@ def otelcol_metrics_endpoint(admin_client: DynamicClient, model_namespace: Names
578557
"""
579558
namespace = model_namespace.name
580559

581-
service = next(Service.get(
582-
dyn_client=admin_client,
583-
namespace=namespace,
584-
label_selector="app.kubernetes.io/component=opentelemetry-collector",
585-
))
560+
service = next(
561+
Service.get(
562+
dyn_client=admin_client,
563+
namespace=namespace,
564+
label_selector="app.kubernetes.io/component=opentelemetry-collector",
565+
)
566+
)
586567

587568
service_name = service.name
588569

@@ -600,6 +581,7 @@ def tempo_traces_endpoint(tempo_stack, model_namespace: Namespace):
600581
port = 4317
601582
return f"http://{service_name}.{namespace}.svc.cluster.local:{port}"
602583

584+
603585
@pytest.fixture(scope="class")
604586
def otel_exporter_config(otelcol_metrics_endpoint, tempo_traces_endpoint):
605587
return {
@@ -610,6 +592,7 @@ def otel_exporter_config(otelcol_metrics_endpoint, tempo_traces_endpoint):
610592
"tracesProtocol": "grpc",
611593
}
612594

595+
613596
@pytest.fixture(scope="class")
614597
def tempo_traces_service_portforward(
615598
admin_client: DynamicClient,
@@ -638,4 +621,4 @@ def tempo_traces_service_portforward(
638621
yield local_url
639622
except Exception as e:
640623
LOGGER.error(f"Failed to set up port forwarding for {service_name}: {e}")
641-
raise
624+
raise

0 commit comments

Comments
 (0)