Skip to content

Commit b201e54

Browse files
jiridanekcursoragentcoderabbitai[bot]
authored
ISSUE #2949: fix(tests/containers/): extract the dict assertion to a common utility library (#2960)
Co-authored-by: Cursor <cursoragent@cursor.com> Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
1 parent d187554 commit b201e54

File tree

5 files changed

+21
-27
lines changed

5 files changed

+21
-27
lines changed

ci/cached-builds/makefile_helper.py

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,12 @@ class TestMakefile:
6161
MINIMAL_IMAGE = "jupyter-minimal-ubi9-python-3.12"
6262

6363
def test_makefile__build_image__konflux(self):
64+
import ntb # noqa: PLC0415 `import` should be at the top-level of a file
65+
6466
konflux_default = dry_run_makefile(target=self.MINIMAL_IMAGE, makefile_dir=PROJECT_ROOT)
6567
konflux_yes = dry_run_makefile(target=self.MINIMAL_IMAGE, makefile_dir=PROJECT_ROOT, env={"KONFLUX": "yes"})
6668

67-
_assert_subdict(
69+
ntb.assert_subdict(
6870
{
6971
"VARIANT": "cpu",
7072
"DOCKERFILE_NAME": "Dockerfile.cpu",
@@ -73,7 +75,7 @@ def test_makefile__build_image__konflux(self):
7375
_extract_assignments(konflux_default),
7476
)
7577

76-
_assert_subdict(
78+
ntb.assert_subdict(
7779
{
7880
"VARIANT": "cpu",
7981
"DOCKERFILE_NAME": "Dockerfile.konflux.cpu",
@@ -99,10 +101,3 @@ def _extract_assignments(makefile_output: str) -> dict[str, str]:
99101
key, value = line.split(":=", 1)
100102
assignments[key.strip()] = value.strip()
101103
return assignments
102-
103-
104-
# see also https://pypi.org/project/pytest-assert-utils/
105-
def _assert_subdict(subdict: dict[str, str], superdict: dict[str, str]):
106-
"""Filter subdict to only keys in superdict, then compare the remaining items."""
107-
__tracebackhide__ = True
108-
assert subdict == {k: superdict[k] for k in subdict if k in superdict}

ntb/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
from __future__ import annotations
44

5+
from .asserts import assert_subdict
56
from .constants import ROOT_DIR
67
from .strings import blockinfile, process_template_with_indents
78

8-
__all__ = ["ROOT_DIR", "blockinfile", "process_template_with_indents"]
9+
__all__ = ["ROOT_DIR", "assert_subdict", "blockinfile", "process_template_with_indents"]

ntb/asserts.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
"""Shared test assertion helpers."""
2+
3+
from __future__ import annotations
4+
5+
6+
# see also https://pypi.org/project/pytest-assert-utils/
7+
def assert_subdict(subdict: dict[str, str], superdict: dict[str, str]):
8+
"""Assert that subdict is a subset of superdict (matching keys have equal values)."""
9+
__tracebackhide__ = True
10+
assert subdict == {k: superdict[k] for k in subdict if k in superdict}

tests/containers/base_image_test.py

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import pytest
1717
import testcontainers.core.container
1818

19+
import ntb
1920
from tests.containers import conftest, docker_utils, skopeo_utils, utils
2021

2122
logging.basicConfig(level=logging.DEBUG)
@@ -27,13 +28,6 @@
2728
import pytest_subtests
2829

2930

30-
# see also https://pypi.org/project/pytest-assert-utils/
31-
def _assert_subdict(subdict: dict[str, str], superdict: dict[str, str]):
32-
"""Filter subdict to only keys in superdict, then compare the remaining items."""
33-
__tracebackhide__ = True
34-
assert subdict == {k: superdict[k] for k in subdict if k in superdict}
35-
36-
3731
class TestBaseImage:
3832
"""Tests that are applicable for all images we have in this repository."""
3933

@@ -330,7 +324,7 @@ def _check_aipcc_env_vars(actual: dict[str, str], subtests: pytest_subtests.SubT
330324
pypi_index_vars = ("PIP_INDEX_URL", "UV_INDEX_URL", "UV_DEFAULT_INDEX")
331325

332326
with subtests.test("AIPCC images have config file env vars"):
333-
_assert_subdict(aipcc_config_vars, actual)
327+
ntb.assert_subdict(aipcc_config_vars, actual)
334328
with subtests.test("AIPCC images do not have index URL env vars"):
335329
for key in pypi_index_vars:
336330
assert key not in actual, f"Expected {key} to NOT be present (image uses uv.lock.d)"
@@ -346,7 +340,7 @@ def _check_pypi_env_vars(actual: dict[str, str], subtests: pytest_subtests.SubTe
346340
"UV_DEFAULT_INDEX": "https://pypi.org/simple",
347341
}
348342
with subtests.test("Non-AIPCC images have index URL env vars"):
349-
_assert_subdict(pypi_env_vars, actual)
343+
ntb.assert_subdict(pypi_env_vars, actual)
350344

351345
@allure.issue("RHAIENG-2189")
352346
def test_python_package_index(self, image: str, subtests: pytest_subtests.SubTests):

tests/containers/workbenches/jupyterlab/jupyterlab_datascience_test.py

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,7 @@ def test_sklearn_smoke(self, jupyterlab_datascience_image: conftest.Image) -> No
8484
def test_mysql_connection(self, tf: TestFrame, datascience_image: Image, subtests):
8585
name_label = datascience_image.labels.get("name", "")
8686
if "-rstudio-" in name_label:
87-
pytest.skip(
88-
f"Image {datascience_image.name} does have -rstudio- in {datascience_image.labels['name']=}'"
89-
)
87+
pytest.skip(f"Image {datascience_image.name} has '-rstudio-' in {datascience_image.labels['name']=}")
9088

9189
MYSQL_CONNECTOR_PYTHON_VERSION = "9.5.0"
9290

@@ -170,11 +168,7 @@ def test_mysql_connection(self, tf: TestFrame, datascience_image: Image, subtest
170168
# Detection: label "name" contains "-code-server-" (set in Dockerfile.cpu). Fallback: image ref
171169
# contains "codeserver" when daemon is Podman and labels came from ContainerConfig (conftest handles that).
172170
is_codeserver = "-code-server-" in name_label or "codeserver" in datascience_image.name.lower()
173-
python_exe = (
174-
"/opt/app-root/bin/python3"
175-
if is_codeserver
176-
else "python"
177-
)
171+
python_exe = "/opt/app-root/bin/python3" if is_codeserver else "python"
178172
print(f"Using python executable: {python_exe}")
179173

180174
# RHOAIENG-140: code-server image users are expected to install their own db clients

0 commit comments

Comments
 (0)