|
| 1 | +import pytest |
| 2 | +from typing import Any, Generator |
| 3 | +import shortuuid |
| 4 | +from pytest import FixtureRequest |
| 5 | + |
| 6 | +from ocp_resources.namespace import Namespace |
| 7 | +from ocp_resources.pod import Pod |
| 8 | +from ocp_resources.route import Route |
| 9 | +from ocp_resources.service import Service |
| 10 | + |
| 11 | +from utilities.infra import create_ns |
| 12 | +from utilities.constants import OCIRegistry, MinIo, Protocols, Labels |
| 13 | + |
| 14 | +from kubernetes.dynamic import DynamicClient |
| 15 | + |
| 16 | + |
| 17 | +# OCI Registry |
| 18 | +@pytest.fixture(scope="class") |
| 19 | +def oci_namespace(admin_client: DynamicClient) -> Generator[Namespace, Any, Any]: |
| 20 | + with create_ns( |
| 21 | + name=f"{OCIRegistry.Metadata.NAME}-{shortuuid.uuid().lower()}", |
| 22 | + admin_client=admin_client, |
| 23 | + ) as ns: |
| 24 | + yield ns |
| 25 | + |
| 26 | + |
| 27 | +@pytest.fixture(scope="class") |
| 28 | +def oci_registry_pod_with_minio( |
| 29 | + request: FixtureRequest, |
| 30 | + admin_client: DynamicClient, |
| 31 | + oci_namespace: Namespace, |
| 32 | + minio_service: Service, |
| 33 | +) -> Generator[Pod, Any, Any]: |
| 34 | + pod_labels = {Labels.Openshift.APP: OCIRegistry.Metadata.NAME} |
| 35 | + |
| 36 | + if labels := request.param.get("labels"): |
| 37 | + pod_labels.update(labels) |
| 38 | + |
| 39 | + minio_fqdn = f"{minio_service.name}.{minio_service.namespace}.svc.cluster.local" |
| 40 | + minio_endpoint = f"{minio_fqdn}:{MinIo.Metadata.DEFAULT_PORT}" |
| 41 | + |
| 42 | + with Pod( |
| 43 | + client=admin_client, |
| 44 | + name=OCIRegistry.Metadata.NAME, |
| 45 | + namespace=oci_namespace.name, |
| 46 | + containers=[ |
| 47 | + { |
| 48 | + "args": request.param.get("args"), |
| 49 | + "env": [ |
| 50 | + {"name": "ZOT_STORAGE_STORAGEDRIVER_NAME", "value": OCIRegistry.Storage.STORAGE_DRIVER}, |
| 51 | + { |
| 52 | + "name": "ZOT_STORAGE_STORAGEDRIVER_ROOTDIRECTORY", |
| 53 | + "value": OCIRegistry.Storage.STORAGE_DRIVER_ROOT_DIRECTORY, |
| 54 | + }, |
| 55 | + {"name": "ZOT_STORAGE_STORAGEDRIVER_BUCKET", "value": MinIo.Buckets.MODELMESH_EXAMPLE_MODELS}, |
| 56 | + {"name": "ZOT_STORAGE_STORAGEDRIVER_REGION", "value": OCIRegistry.Storage.STORAGE_DRIVER_REGION}, |
| 57 | + {"name": "ZOT_STORAGE_STORAGEDRIVER_REGIONENDPOINT", "value": f"http://{minio_endpoint}"}, |
| 58 | + {"name": "ZOT_STORAGE_STORAGEDRIVER_ACCESSKEY", "value": MinIo.Credentials.ACCESS_KEY_VALUE}, |
| 59 | + {"name": "ZOT_STORAGE_STORAGEDRIVER_SECRETKEY", "value": MinIo.Credentials.SECRET_KEY_VALUE}, |
| 60 | + { |
| 61 | + "name": "ZOT_STORAGE_STORAGEDRIVER_SECURE", |
| 62 | + "value": OCIRegistry.Storage.STORAGE_STORAGEDRIVER_SECURE, |
| 63 | + }, |
| 64 | + { |
| 65 | + "name": "ZOT_STORAGE_STORAGEDRIVER_FORCEPATHSTYLE", |
| 66 | + "value": OCIRegistry.Storage.STORAGE_STORAGEDRIVER_FORCEPATHSTYLE, |
| 67 | + }, |
| 68 | + {"name": "ZOT_HTTP_ADDRESS", "value": OCIRegistry.Metadata.DEFAULT_HTTP_ADDRESS}, |
| 69 | + {"name": "ZOT_HTTP_PORT", "value": str(OCIRegistry.Metadata.DEFAULT_PORT)}, |
| 70 | + {"name": "ZOT_LOG_LEVEL", "value": "info"}, |
| 71 | + ], |
| 72 | + "image": request.param.get("image", OCIRegistry.PodConfig.REGISTRY_IMAGE), |
| 73 | + "name": OCIRegistry.Metadata.NAME, |
| 74 | + "securityContext": { |
| 75 | + "allowPrivilegeEscalation": False, |
| 76 | + "capabilities": {"drop": ["ALL"]}, |
| 77 | + "runAsNonRoot": True, |
| 78 | + "seccompProfile": {"type": "RuntimeDefault"}, |
| 79 | + }, |
| 80 | + "volumeMounts": [ |
| 81 | + { |
| 82 | + "name": "zot-data", |
| 83 | + "mountPath": "/var/lib/registry", |
| 84 | + } |
| 85 | + ], |
| 86 | + } |
| 87 | + ], |
| 88 | + volumes=[ |
| 89 | + { |
| 90 | + "name": "zot-data", |
| 91 | + "emptyDir": {}, |
| 92 | + } |
| 93 | + ], |
| 94 | + label=pod_labels, |
| 95 | + annotations=request.param.get("annotations"), |
| 96 | + ) as oci_pod: |
| 97 | + oci_pod.wait_for_condition(condition="Ready", status="True") |
| 98 | + yield oci_pod |
| 99 | + |
| 100 | + |
| 101 | +@pytest.fixture(scope="class") |
| 102 | +def oci_registry_service(admin_client: DynamicClient, oci_namespace: Namespace) -> Generator[Service, Any, Any]: |
| 103 | + with Service( |
| 104 | + client=admin_client, |
| 105 | + name=OCIRegistry.Metadata.NAME, |
| 106 | + namespace=oci_namespace.name, |
| 107 | + ports=[ |
| 108 | + { |
| 109 | + "name": f"{OCIRegistry.Metadata.NAME}-port", |
| 110 | + "port": OCIRegistry.Metadata.DEFAULT_PORT, |
| 111 | + "protocol": Protocols.TCP, |
| 112 | + "targetPort": OCIRegistry.Metadata.DEFAULT_PORT, |
| 113 | + } |
| 114 | + ], |
| 115 | + selector={ |
| 116 | + Labels.Openshift.APP: OCIRegistry.Metadata.NAME, |
| 117 | + }, |
| 118 | + session_affinity="ClientIP", |
| 119 | + ) as oci_service: |
| 120 | + yield oci_service |
| 121 | + |
| 122 | + |
| 123 | +@pytest.fixture(scope="class") |
| 124 | +def oci_registry_route(admin_client: DynamicClient, oci_registry_service: Service) -> Generator[Route, Any, Any]: |
| 125 | + with Route( |
| 126 | + client=admin_client, |
| 127 | + name=OCIRegistry.Metadata.NAME, |
| 128 | + namespace=oci_registry_service.namespace, |
| 129 | + service=oci_registry_service.name, |
| 130 | + ) as oci_route: |
| 131 | + yield oci_route |
0 commit comments