Skip to content

Commit eab617a

Browse files
RHOAIENG-26843, RHOAIENG-26066: tests: add scikit-learn smoke test (#1171)
* feat: Add scikit-learn smoke test Adds a smoke test to verify basic scikit-learn functionality in JupyterLab images. This test trains a simple Logistic Regression model and checks predictions to ensure the scikit-learn installation is working correctly after package updates. * RHOAIENG-27131: fix PEP 8 spacing issues in JupyterLab test * RHOAIENG-26843: update sklearn smoke test with refined allure issue link and minor script improvements * RHOAIENG-26843: add pytest fixture for JupyterLab datascience images and move sklearn smoke test to dedicated test file * RHOAIENG-27131: fix PEP 8 formatting in datascience image fixture * RHOAIENG-27141: add NumPy version logging to JupyterLab datascience test * RHOAIENG-27142: add random seed to sklearn test for reproducibility --------- Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com>
1 parent a2225df commit eab617a

File tree

2 files changed

+82
-0
lines changed

2 files changed

+82
-0
lines changed

tests/containers/conftest.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,16 @@ def jupyterlab_image(image: str) -> Image:
152152
return image_metadata
153153

154154

155+
@pytest.fixture(scope="function")
156+
def jupyterlab_datascience_image(jupyterlab_image: Image) -> Image:
157+
if "-minimal-" in jupyterlab_image.labels["name"]:
158+
pytest.skip(
159+
f"Image {jupyterlab_image.name} is not datascience image because it has '-minimal-' in {jupyterlab_image.labels['name']=}'"
160+
)
161+
162+
return jupyterlab_image
163+
164+
155165
@pytest.fixture(scope="function")
156166
def rstudio_image(image: str) -> Image:
157167
image_metadata = skip_if_not_workbench_image(image)
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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

Comments
 (0)