|
| 1 | +# |
| 2 | +# Copyright Skodjob authors. |
| 3 | +# License: Apache License 2.0 (see the file LICENSE or http://apache.org/licenses/LICENSE-2.0.html). |
| 4 | +# |
| 5 | +from __future__ import annotations |
| 6 | + |
| 7 | +import io |
| 8 | +import pathlib |
| 9 | + |
| 10 | +import kubernetes.client |
| 11 | +from kubernetes.dynamic import DynamicClient |
| 12 | + |
| 13 | +from ocp_resources.route import Route |
| 14 | + |
| 15 | +import pytest |
| 16 | +from pytest_testconfig import config as py_config |
| 17 | + |
| 18 | +from tests.workbenches.docs import TestDoc, SuiteDoc, Contact, Desc, Step |
| 19 | +from tests.workbenches.resources import Notebook |
| 20 | +from tests.workbenches.utils import step, PodUtils, OcpResourceManager |
| 21 | +from utilities import constants |
| 22 | + |
| 23 | + |
| 24 | +@SuiteDoc( |
| 25 | + description=Desc("Verifies deployments of Notebooks via GitOps approach"), |
| 26 | + before_test_steps={ |
| 27 | + Step(value="Deploy Pipelines Operator", expected="Pipelines operator is available on the cluster"), |
| 28 | + Step(value="Deploy ServiceMesh Operator", expected="ServiceMesh operator is available on the cluster"), |
| 29 | + Step(value="Deploy Serverless Operator", expected="Serverless operator is available on the cluster"), |
| 30 | + Step(value="Install ODH operator", expected="Operator is up and running and is able to serve it's operands"), |
| 31 | + Step(value="Deploy DSCI", expected="DSCI is created and ready"), |
| 32 | + Step(value="Deploy DSC", expected="DSC is created and ready"), |
| 33 | + }, |
| 34 | + after_test_steps={ |
| 35 | + Step( |
| 36 | + value="Delete all created resources", |
| 37 | + expected="All created resources are removed", |
| 38 | + ) |
| 39 | + }, |
| 40 | +) |
| 41 | +class TestNotebookST: |
| 42 | + NTB_NAME: str = "test-odh-notebook" |
| 43 | + NTB_NAMESPACE: str = "test-odh-notebook" |
| 44 | + |
| 45 | + @pytest.mark.parametrize( |
| 46 | + "users_namespace", |
| 47 | + [ |
| 48 | + pytest.param( |
| 49 | + {"name": NTB_NAMESPACE}, |
| 50 | + ) |
| 51 | + ], |
| 52 | + indirect=True, |
| 53 | + ) |
| 54 | + @pytest.mark.parametrize( |
| 55 | + "users_persistent_volume_claim", |
| 56 | + [ |
| 57 | + pytest.param( |
| 58 | + {"name": NTB_NAME, "namespace": NTB_NAMESPACE}, |
| 59 | + ) |
| 60 | + ], |
| 61 | + indirect=True, |
| 62 | + ) |
| 63 | + @TestDoc( |
| 64 | + description=Desc("Create simple Notebook with all needed resources and see if Operator creates it properly"), |
| 65 | + contact=Contact(name="Jakub Stejskal", email="jstejska@redhat.com"), |
| 66 | + steps={ |
| 67 | + Step( |
| 68 | + value="Create namespace for Notebook resources with proper name, labels and annotations", |
| 69 | + expected="Namespace is created", |
| 70 | + ), |
| 71 | + Step(value="Create PVC with proper labels and data for Notebook", expected="PVC is created"), |
| 72 | + Step( |
| 73 | + value="Create Notebook resource with Jupyter Minimal image in pre-defined namespace", |
| 74 | + expected="Notebook resource is created", |
| 75 | + ), |
| 76 | + Step( |
| 77 | + value="Wait for Notebook pods readiness", |
| 78 | + expected="Notebook pods are up and running, Notebook is in ready state", |
| 79 | + ), |
| 80 | + }, |
| 81 | + ) |
| 82 | + def test_create_simple_notebook( |
| 83 | + self, admin_client, unprivileged_client, users_namespace, users_persistent_volume_claim |
| 84 | + ): |
| 85 | + with OcpResourceManager() as test_frame: |
| 86 | + with step("Create Notebook CR"): |
| 87 | + notebook_image: str = _get_notebook_image("jupyter-minimal-notebook", "2024.2") |
| 88 | + notebook = _load_default_notebook( |
| 89 | + client=unprivileged_client, namespace=self.NTB_NAMESPACE, name=self.NTB_NAME, image=notebook_image |
| 90 | + ) |
| 91 | + test_frame.enter(resource=notebook) |
| 92 | + |
| 93 | + with step("Wait for Notebook pod readiness"): |
| 94 | + PodUtils.wait_for_pods_ready( |
| 95 | + client=admin_client, |
| 96 | + namespace_name=self.NTB_NAMESPACE, |
| 97 | + label_selector=f"app={self.NTB_NAME}", |
| 98 | + expect_pods_count=1, |
| 99 | + ) |
| 100 | + |
| 101 | + |
| 102 | +def _get_notebook_image(image_name: str, image_tag: str) -> str: |
| 103 | + registry_path = "image-registry.openshift-image-registry.svc:5000" |
| 104 | + controllers_namespace = py_config["applications_namespace"] |
| 105 | + if py_config.get("distribution") == "upstream": |
| 106 | + image_dict = {"jupyter-minimal-notebook": "jupyter-minimal-notebook"} |
| 107 | + else: |
| 108 | + image_dict = {"jupyter-minimal-notebook": "s2i-minimal-notebook"} |
| 109 | + return registry_path + "/" + controllers_namespace + "/" + image_dict[image_name] + ":" + image_tag |
| 110 | + |
| 111 | + |
| 112 | +def _load_default_notebook(client: DynamicClient, namespace: str, name: str, image: str) -> Notebook: |
| 113 | + notebook_string = (pathlib.Path(__file__).parent / "test_data/notebook.yaml").read_text() |
| 114 | + notebook_string = notebook_string.replace("my-project", namespace).replace("my-workbench", name) |
| 115 | + # Set new Route url |
| 116 | + route_host: str = list( |
| 117 | + Route.get(client=client, name=constants.Dashboard.route_name, namespace=py_config["applications_namespace"]) |
| 118 | + )[0].host |
| 119 | + notebook_string = notebook_string.replace("odh_dashboard_route", "https://" + route_host) |
| 120 | + # Set the correct username |
| 121 | + username = _get_username(client=client) |
| 122 | + notebook_string = notebook_string.replace("odh_user", username) |
| 123 | + # Replace image |
| 124 | + notebook_string = notebook_string.replace("notebook_image_placeholder", image) |
| 125 | + |
| 126 | + nb = Notebook(yaml_file=io.StringIO(notebook_string)) |
| 127 | + |
| 128 | + return nb |
| 129 | + |
| 130 | + |
| 131 | +def _get_username(client: DynamicClient) -> str: |
| 132 | + """Gets the username for the client (see kubectl -v8 auth whoami)""" |
| 133 | + self_subject_review_resource: kubernetes.dynamic.Resource = client.resources.get( |
| 134 | + api_version="authentication.k8s.io/v1", kind="SelfSubjectReview" |
| 135 | + ) |
| 136 | + self_subject_review: kubernetes.dynamic.ResourceInstance = client.create(self_subject_review_resource) |
| 137 | + username: str = self_subject_review.status.userInfo.username |
| 138 | + return username |
0 commit comments