|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import pathlib |
| 4 | +import tempfile |
| 5 | + |
| 6 | +import allure |
| 7 | + |
| 8 | +from tests.containers import conftest, docker_utils |
| 9 | +from tests.containers.workbenches.workbench_image_test import WorkbenchContainer |
| 10 | + |
| 11 | + |
| 12 | +class TestJupyterLabDatascienceImage: |
| 13 | + """Tests for JupyterLab Workbench images in this repository that are not -minimal-.""" |
| 14 | + |
| 15 | + APP_ROOT_HOME = "/opt/app-root/src" |
| 16 | + |
| 17 | + @allure.issue("RHOAIENG-26843") |
| 18 | + @allure.description("Check that basic scikit-learn functionality is working.") |
| 19 | + def test_sklearn_smoke(self, jupyterlab_datascience_image: conftest.Image) -> None: |
| 20 | + container = WorkbenchContainer(image=jupyterlab_datascience_image.name, user=4321, group_add=[0]) |
| 21 | + # language=Python |
| 22 | + test_script_content = """ |
| 23 | +import sklearn |
| 24 | +from sklearn.linear_model import LogisticRegression |
| 25 | +import numpy as np |
| 26 | +
|
| 27 | +# Set random seed for reproducibility |
| 28 | +np.random.seed(42) |
| 29 | +
|
| 30 | +# Simple dataset |
| 31 | +X = np.array([[1], [2], [3], [4], [5]]) |
| 32 | +y = np.array([0, 0, 1, 1, 1]) |
| 33 | +
|
| 34 | +# Train a model |
| 35 | +model = LogisticRegression(solver='liblinear', random_state=42) |
| 36 | +model.fit(X, y) |
| 37 | +
|
| 38 | +# Make a prediction |
| 39 | +pred = model.predict([[3.5]]) |
| 40 | +print(f"NumPy version: {np.__version__}") |
| 41 | +print(f"Scikit-learn version: {sklearn.__version__}") |
| 42 | +print(f"Prediction: {pred}") |
| 43 | +# We expect class 1 for input 3.5 |
| 44 | +assert pred[0] == 1, "Prediction is not as expected" |
| 45 | +
|
| 46 | +print("Scikit-learn smoke test completed successfully.") |
| 47 | +""" |
| 48 | + test_script_name = "test_sklearn.py" |
| 49 | + try: |
| 50 | + container.start(wait_for_readiness=True) |
| 51 | + with tempfile.TemporaryDirectory() as tmpdir: |
| 52 | + tmpdir_path = pathlib.Path(tmpdir) |
| 53 | + script_path = tmpdir_path / test_script_name |
| 54 | + script_path.write_text(test_script_content) |
| 55 | + docker_utils.container_cp( |
| 56 | + container.get_wrapped_container(), |
| 57 | + src=str(script_path), |
| 58 | + dst=self.APP_ROOT_HOME, |
| 59 | + ) |
| 60 | + |
| 61 | + script_container_path = f"{self.APP_ROOT_HOME}/{test_script_name}" |
| 62 | + exit_code, output = container.exec(["python", script_container_path]) |
| 63 | + output_str = output.decode() |
| 64 | + |
| 65 | + print(f"Script output:\n{output_str}") |
| 66 | + |
| 67 | + assert exit_code == 0, f"Script execution failed with exit code {exit_code}. Output:\n{output_str}" |
| 68 | + assert "Scikit-learn smoke test completed successfully." in output_str |
| 69 | + assert "Prediction: [1]" in output_str |
| 70 | + |
| 71 | + finally: |
| 72 | + docker_utils.NotebookContainer(container).stop(timeout=0) |
0 commit comments