|
2 | 2 | from typing import Self |
3 | 3 |
|
4 | 4 | from simple_logger.logger import get_logger |
5 | | -from _pytest.fixtures import FixtureRequest |
6 | 5 |
|
7 | | -from ocp_resources.namespace import Namespace |
8 | 6 | from ocp_resources.pod import Pod |
9 | 7 | from ocp_resources.deployment import Deployment |
10 | 8 | from tests.model_registry.scc.utils import ( |
11 | | - get_uid_from_namespace, |
12 | | - validate_pod_security_context, |
13 | | - KEYS_TO_VALIDATE, |
14 | | - validate_containers_pod_security_context, |
| 9 | + validate_deployment_scc, |
| 10 | + validate_pod_scc, |
15 | 11 | ) |
16 | | -from tests.model_registry.constants import MODEL_DICT, MR_INSTANCE_NAME, MODEL_REGISTRY_POD_FILTER |
| 12 | +from tests.model_registry.constants import MR_INSTANCE_NAME |
17 | 13 |
|
18 | | -from kubernetes.dynamic import DynamicClient |
19 | 14 |
|
20 | 15 | LOGGER = get_logger(name=__name__) |
21 | 16 |
|
22 | | - |
23 | | -@pytest.fixture(scope="class") |
24 | | -def model_registry_scc_namespace(model_registry_namespace: str): |
25 | | - mr_annotations = Namespace(name=model_registry_namespace).instance.metadata.annotations |
26 | | - return { |
27 | | - "seLinuxOptions": mr_annotations.get("openshift.io/sa.scc.mcs"), |
28 | | - "uid-range": mr_annotations.get("openshift.io/sa.scc.uid-range"), |
29 | | - } |
30 | | - |
31 | | - |
32 | | -@pytest.fixture(scope="class") |
33 | | -def model_registry_resource( |
34 | | - request: FixtureRequest, admin_client: DynamicClient, model_registry_namespace: str |
35 | | -) -> Deployment | Pod: |
36 | | - if request.param["kind"] == Deployment: |
37 | | - return Deployment(name=MR_INSTANCE_NAME, namespace=model_registry_namespace, ensure_exists=True) |
38 | | - elif request.param["kind"] == Pod: |
39 | | - pods = list( |
40 | | - Pod.get( |
41 | | - dyn_client=admin_client, |
42 | | - namespace=model_registry_namespace, |
43 | | - label_selector=MODEL_REGISTRY_POD_FILTER, |
44 | | - ) |
45 | | - ) |
46 | | - if len(pods) != 1: |
47 | | - pytest.fail( |
48 | | - "Expected one model registry pod. Found: {[{pod.name: pod.status} for pod in pods] if pods else None}" |
49 | | - ) |
50 | | - return pods[0] |
51 | | - else: |
52 | | - raise AssertionError(f"Invalid resource: {request.param['kind']}. Valid options: Deployment and Pod") |
| 17 | +MODEL_CATALOG_STR = "model-catalog" |
| 18 | +MR_POSTGRES_DEPLOYMENT_NAME_STR = f"{MR_INSTANCE_NAME}-postgres" |
53 | 19 |
|
54 | 20 |
|
55 | 21 | @pytest.mark.parametrize( |
56 | | - "registered_model", |
| 22 | + "model_registry_metadata_db_resources, model_registry_instance", |
57 | 23 | [ |
58 | | - pytest.param( |
59 | | - MODEL_DICT, |
60 | | - ), |
| 24 | + pytest.param({}, {}), |
| 25 | + pytest.param({"db_name": "default"}, {"db_name": "default"}), |
61 | 26 | ], |
62 | 27 | indirect=True, |
63 | 28 | ) |
64 | 29 | @pytest.mark.usefixtures( |
65 | 30 | "updated_dsc_component_state_scope_session", |
66 | 31 | "model_registry_metadata_db_resources", |
67 | 32 | "model_registry_instance", |
68 | | - "registered_model", |
69 | 33 | ) |
70 | 34 | @pytest.mark.custom_namespace |
71 | 35 | class TestModelRegistrySecurityContextValidation: |
72 | 36 | @pytest.mark.parametrize( |
73 | | - "model_registry_resource", |
| 37 | + "deployment_model_registry_ns", |
74 | 38 | [ |
75 | | - pytest.param({"kind": Deployment}), |
| 39 | + pytest.param({"deployment_name": MR_INSTANCE_NAME}), |
| 40 | + pytest.param({"deployment_name": MR_POSTGRES_DEPLOYMENT_NAME_STR}), |
76 | 41 | ], |
77 | | - indirect=["model_registry_resource"], |
| 42 | + indirect=["deployment_model_registry_ns"], |
78 | 43 | ) |
79 | 44 | @pytest.mark.sanity |
80 | 45 | def test_model_registry_deployment_security_context_validation( |
81 | 46 | self: Self, |
82 | | - model_registry_resource: Deployment, |
| 47 | + skip_if_not_valid_check: None, |
| 48 | + deployment_model_registry_ns: Deployment, |
83 | 49 | ): |
84 | 50 | """ |
85 | 51 | Validate that model registry deployment does not set runAsUser/runAsGroup |
86 | 52 | """ |
87 | | - error = [] |
88 | | - for container in model_registry_resource.instance.spec.template.spec.containers: |
89 | | - if not all([True for key in KEYS_TO_VALIDATE if not container.get(key)]): |
90 | | - error.append({container.name: container.securityContext}) |
91 | | - |
92 | | - if error: |
93 | | - pytest.fail( |
94 | | - f"{model_registry_resource.name} {model_registry_resource.kind} containers expected to not " |
95 | | - f"set {KEYS_TO_VALIDATE}, actual: {error}" |
96 | | - ) |
| 53 | + validate_deployment_scc(deployment=deployment_model_registry_ns) |
97 | 54 |
|
98 | 55 | @pytest.mark.parametrize( |
99 | | - "model_registry_resource", |
| 56 | + "pod_model_registry_ns", |
100 | 57 | [ |
101 | | - pytest.param({"kind": Pod}), |
| 58 | + pytest.param({"deployment_name": MR_INSTANCE_NAME}), |
| 59 | + pytest.param({"deployment_name": MR_POSTGRES_DEPLOYMENT_NAME_STR}), |
102 | 60 | ], |
103 | | - indirect=["model_registry_resource"], |
| 61 | + indirect=["pod_model_registry_ns"], |
104 | 62 | ) |
105 | 63 | @pytest.mark.sanity |
106 | 64 | def test_model_registry_pod_security_context_validation( |
107 | 65 | self: Self, |
108 | | - model_registry_resource: Pod, |
| 66 | + skip_if_not_valid_check: None, |
| 67 | + pod_model_registry_ns: Pod, |
109 | 68 | model_registry_scc_namespace: dict[str, str], |
110 | 69 | ): |
111 | 70 | """ |
112 | 71 | Validate that model registry pod gets runAsUser/runAsGroup from openshift and the values matches namespace |
113 | 72 | annotations |
114 | 73 | """ |
115 | | - ns_uid = get_uid_from_namespace(namespace_scc=model_registry_scc_namespace) |
116 | | - pod_spec = model_registry_resource.instance.spec |
117 | | - errors = validate_pod_security_context( |
118 | | - pod_security_context=pod_spec.securityContext, |
119 | | - namespace_scc=model_registry_scc_namespace, |
120 | | - model_registry_pod=model_registry_resource, |
121 | | - ns_uid=ns_uid, |
122 | | - ) |
123 | | - errors.extend( |
124 | | - validate_containers_pod_security_context(model_registry_pod=model_registry_resource, namespace_uid=ns_uid) |
125 | | - ) |
126 | | - if errors: |
127 | | - pytest.fail( |
128 | | - f"{model_registry_resource.name} {model_registry_resource.kind} pod security context validation failed" |
129 | | - f" with error: {errors}" |
130 | | - ) |
| 74 | + validate_pod_scc(pod=pod_model_registry_ns, model_registry_scc_namespace=model_registry_scc_namespace) |
0 commit comments