Skip to content

Commit 3be5bbe

Browse files
committed
Simplify test code given the changes from pytest_container#216
We now no longer have to perform the manual container & mark unbundling as Container and DerivedContainer are now instances of ParameterSet as well
1 parent 255fc90 commit 3be5bbe

21 files changed

Lines changed: 192 additions & 289 deletions

bci_tester/data.py

Lines changed: 17 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@
44
from datetime import timedelta
55
from pathlib import Path
66
from typing import Iterable
7+
from typing import List
78
from typing import Optional
89
from typing import Sequence
910
from typing import Tuple
1011

1112
from pytest_container import DerivedContainer
1213
from pytest_container.container import ContainerVolume
1314
from pytest_container.container import PortForwarding
14-
from pytest_container.container import container_and_marks_from_pytest_param
1515
from pytest_container.runtime import LOCALHOST
1616

1717
try:
@@ -292,7 +292,7 @@ def create_BCI(
292292
bci_type: ImageType = ImageType.LANGUAGE_STACK,
293293
container_user: Optional[str] = None,
294294
**kwargs,
295-
) -> ParameterSet:
295+
) -> DerivedContainer:
296296
"""Creates a DerivedContainer wrapped in a pytest.param for the BCI with the
297297
given ``build_tag``.
298298
@@ -395,14 +395,11 @@ def create_BCI(
395395
else:
396396
containerfile = _BCI_REPLACE_REPO_CONTAINERFILE
397397

398-
return pytest.param(
399-
DerivedContainer(
400-
base=baseurl,
401-
containerfile=containerfile,
402-
**kwargs,
403-
),
398+
return DerivedContainer(
399+
base=baseurl,
400+
containerfile=containerfile,
404401
marks=marks,
405-
id=f"{build_tag} from {baseurl}",
402+
**kwargs,
406403
)
407404

408405

@@ -1086,23 +1083,19 @@ def create_BCI(
10861083
)
10871084

10881085
#: all containers with zypper and with the flag to launch them as root
1089-
CONTAINERS_WITH_ZYPPER_AS_ROOT = []
1090-
for param in CONTAINERS_WITH_ZYPPER:
1086+
CONTAINERS_WITH_ZYPPER_AS_ROOT: List[DerivedContainer] = []
1087+
for ctr in CONTAINERS_WITH_ZYPPER:
10911088
# only modify the user for containers where `USER` is explicitly set,
10921089
# atm this is no container
1093-
if param not in []:
1094-
CONTAINERS_WITH_ZYPPER_AS_ROOT.append(param)
1090+
if ctr not in []:
1091+
CONTAINERS_WITH_ZYPPER_AS_ROOT.append(ctr)
10951092
else:
1096-
ctr, marks = container_and_marks_from_pytest_param(param)
10971093
CONTAINERS_WITH_ZYPPER_AS_ROOT.append(
1098-
pytest.param(
1099-
DerivedContainer(
1100-
base=ctr,
1101-
extra_launch_args=(
1102-
(ctr.extra_launch_args or []) + ["--user", "root"]
1103-
),
1094+
DerivedContainer(
1095+
base=ctr,
1096+
extra_launch_args=(
1097+
(ctr.extra_launch_args or []) + ["--user", "root"]
11041098
),
1105-
marks=marks,
11061099
)
11071100
)
11081101

@@ -1214,13 +1207,12 @@ def has_xfail(param: ParameterSet) -> bool:
12141207
return True
12151208
return False
12161209

1217-
for param in ALL_CONTAINERS:
1210+
for ctr in ALL_CONTAINERS:
12181211
# don't check containers which are known broken or excluded
1219-
if has_true_skipif(param) or has_xfail(param):
1212+
if has_true_skipif(ctr) or has_xfail(ctr):
12201213
continue
12211214

1222-
ctr, marks = container_and_marks_from_pytest_param(param)
1223-
for mark in marks or []:
1215+
for mark in ctr.marks or []:
12241216
assert mark.name in custom_markers.union(
12251217
{"xfail", "skipif", "skip"}
12261218
), (

tests/test_all.py

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
from pytest_container import Container
1515
from pytest_container import DerivedContainer
1616
from pytest_container import MultiStageBuild
17-
from pytest_container import container_and_marks_from_pytest_param
1817
from pytest_container import get_extra_build_args
1918
from pytest_container import get_extra_run_args
2019
from pytest_container.container import BindMount
@@ -240,16 +239,12 @@ def test_glibc_present(auto_container):
240239
# the host instead of passing it on via stdout which pollutes the logs making
241240
# them unreadable
242241
_CONTAINERS_WITH_VOLUME_MOUNT = []
243-
for param in CONTAINERS_WITH_ZYPPER_AS_ROOT:
244-
ctr, marks = container_and_marks_from_pytest_param(param)
242+
for ctr in CONTAINERS_WITH_ZYPPER_AS_ROOT:
245243
new_vol_mounts = (ctr.volume_mounts or []) + [BindMount("/solv/")]
246-
kwargs = {**ctr.__dict__}
244+
kwargs = {**ctr.dict()}
247245
kwargs.pop("volume_mounts")
248246
_CONTAINERS_WITH_VOLUME_MOUNT.append(
249-
pytest.param(
250-
DerivedContainer(volume_mounts=new_vol_mounts, **kwargs),
251-
marks=marks,
252-
)
247+
DerivedContainer(volume_mounts=new_vol_mounts, **kwargs),
253248
)
254249

255250

tests/test_base.py

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
import pytest
99
from pytest_container import DerivedContainer
1010
from pytest_container.container import ContainerData
11-
from pytest_container.container import container_and_marks_from_pytest_param
1211
from pytest_container.runtime import LOCALHOST
1312

1413
from bci_tester.data import BASE_CONTAINER
@@ -241,20 +240,18 @@ def test_all_openssl_hashes_known(auto_container):
241240

242241
#: This is the base container with additional launch arguments applied to it so
243242
#: that docker can be launched inside the container
244-
DIND_CONTAINER = pytest.param(
245-
DerivedContainer(
246-
base=container_and_marks_from_pytest_param(BASE_CONTAINER)[0],
247-
**{
248-
x: getattr(BASE_CONTAINER.values[0], x)
249-
for x in BASE_CONTAINER.values[0].__dict__
250-
if x not in ("extra_launch_args", "base")
251-
},
252-
extra_launch_args=[
253-
"--privileged=true",
254-
"-v",
255-
"/var/run/docker.sock:/var/run/docker.sock",
256-
],
257-
),
243+
DIND_CONTAINER = DerivedContainer(
244+
base=BASE_CONTAINER,
245+
**{
246+
x: getattr(BASE_CONTAINER, x)
247+
for x in BASE_CONTAINER.dict()
248+
if x not in ("extra_launch_args", "base")
249+
},
250+
extra_launch_args=[
251+
"--privileged=true",
252+
"-v",
253+
"/var/run/docker.sock:/var/run/docker.sock",
254+
],
258255
)
259256

260257

tests/test_dotnet.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010

1111
import pytest
1212
from _pytest.mark import ParameterSet
13+
from pytest_container import DerivedContainer
1314
from pytest_container import GitRepositoryBuild
14-
from pytest_container import container_and_marks_from_pytest_param
1515

1616
from bci_tester.data import DOTNET_ASPNET_8_0_CONTAINER
1717
from bci_tester.data import DOTNET_ASPNET_9_0_CONTAINER
@@ -36,13 +36,14 @@
3636
)
3737

3838

39-
def _generate_ctr_ver_param(ctr_ver: Tuple[ParameterSet, str]) -> ParameterSet:
39+
def _generate_ctr_ver_param(
40+
ctr_ver: Tuple[DerivedContainer, str],
41+
) -> ParameterSet:
4042
"""Converts a `(Container, Version)` tuple into a `pytest.param` that
4143
contains the exact same values but has the marks of the container.
4244
4345
"""
44-
ctr, marks = container_and_marks_from_pytest_param(ctr_ver[0])
45-
return pytest.param(ctr, ctr_ver[1], marks=marks or ())
46+
return pytest.param(ctr_ver[0], ctr_ver[1], marks=ctr_ver[0].marks)
4647

4748

4849
@pytest.mark.parametrize(

tests/test_fips.py

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
from pytest_container import DerivedContainer
1111
from pytest_container.container import BindMount
1212
from pytest_container.container import ContainerData
13-
from pytest_container.container import container_and_marks_from_pytest_param
1413
from pytest_container.runtime import LOCALHOST
1514

1615
from bci_tester.data import BASE_FIPS_CONTAINERS
@@ -49,8 +48,7 @@
4948
FIPS_TESTER_IMAGES = []
5049
FIPS_GNUTLS_TESTER_IMAGES = []
5150
FIPS_GCRYPT_TESTER_IMAGES = []
52-
for param in CONTAINERS_WITH_ZYPPER:
53-
ctr, marks = container_and_marks_from_pytest_param(param)
51+
for ctr in CONTAINERS_WITH_ZYPPER:
5452
kwargs = {
5553
"base": ctr,
5654
"extra_environment_variables": ctr.extra_environment_variables,
@@ -68,34 +66,33 @@
6866
else []
6967
),
7068
}
71-
tester_ctr = DerivedContainer(containerfile=DOCKERFILE, **kwargs)
72-
gnutls_tester_ctr = DerivedContainer(
73-
containerfile=DOCKERFILE_GNUTLS, **kwargs
74-
)
75-
gcrypt_tester_ctr = DerivedContainer(
76-
containerfile=DOCKERFILE_GCRYPT, **kwargs
77-
)
78-
79-
if param not in LTSS_BASE_FIPS_CONTAINERS + BASE_FIPS_CONTAINERS:
69+
if ctr not in LTSS_BASE_FIPS_CONTAINERS + BASE_FIPS_CONTAINERS:
8070
# create a shallow copy here to avoid mutating the original params
81-
marks = marks[:] + [
71+
marks = [
8272
pytest.mark.skipif(
8373
not host_fips_enabled(),
8474
reason="The target must run in FIPS mode for the FIPS test suite",
8575
)
8676
]
87-
CONTAINER_IMAGES_WITH_ZYPPER.append(
88-
pytest.param(ctr, marks=marks, id=param.id)
77+
else:
78+
marks = []
79+
80+
tester_ctr = DerivedContainer(
81+
containerfile=DOCKERFILE, marks=marks, **kwargs
8982
)
90-
FIPS_TESTER_IMAGES.append(
91-
pytest.param(tester_ctr, marks=marks, id=param.id)
83+
gnutls_tester_ctr = DerivedContainer(
84+
containerfile=DOCKERFILE_GNUTLS, marks=marks, **kwargs
9285
)
93-
FIPS_GNUTLS_TESTER_IMAGES.append(
94-
pytest.param(gnutls_tester_ctr, marks=marks, id=param.id)
86+
gcrypt_tester_ctr = DerivedContainer(
87+
containerfile=DOCKERFILE_GCRYPT, marks=marks, **kwargs
9588
)
96-
FIPS_GCRYPT_TESTER_IMAGES.append(
97-
pytest.param(gcrypt_tester_ctr, marks=marks, id=param.id)
89+
90+
CONTAINER_IMAGES_WITH_ZYPPER.append(
91+
DerivedContainer(base=ctr, marks=marks)
9892
)
93+
FIPS_TESTER_IMAGES.append(tester_ctr)
94+
FIPS_GNUTLS_TESTER_IMAGES.append(gnutls_tester_ctr)
95+
FIPS_GCRYPT_TESTER_IMAGES.append(gcrypt_tester_ctr)
9996

10097

10198
@pytest.mark.parametrize(

tests/test_gcc.py

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import pytest
44
from pytest_container import DerivedContainer
5-
from pytest_container import container_and_marks_from_pytest_param
65
from pytest_container.container import ContainerData
76

87
from bci_tester.data import GCC_CONTAINERS
@@ -22,13 +21,9 @@
2221
"""
2322

2423
HELLO_CONTAINERS = [
25-
pytest.param(
26-
DerivedContainer(
27-
base=container_and_marks_from_pytest_param(ctr)[0],
28-
containerfile=CONTAINERFILE_HELLO,
29-
),
30-
marks=ctr.marks,
31-
id=ctr.id,
24+
DerivedContainer(
25+
base=ctr,
26+
containerfile=CONTAINERFILE_HELLO,
3227
)
3328
for ctr in CONTAINER_IMAGES
3429
]

tests/test_git.py

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import pytest
44
from pytest_container.container import DerivedContainer
55
from pytest_container.container import ImageFormat
6-
from pytest_container.container import container_and_marks_from_pytest_param
76
from pytest_container.pod import Pod
87
from pytest_container.pod import PodData
98
from pytest_container.runtime import LOCALHOST
@@ -96,16 +95,9 @@ def test_git_clone_https(auto_container_per_test):
9695
extra_launch_args=["--cap-add", "AUDIT_WRITE"],
9796
)
9897

99-
_git_container, _marks = container_and_marks_from_pytest_param(GIT_CONTAINER)
10098

101-
GIT_POD = pytest.param(
102-
Pod(
103-
containers=[
104-
GIT_SERVER_CONTAINER,
105-
_git_container,
106-
],
107-
),
108-
marks=_marks,
99+
GIT_POD = Pod(
100+
containers=[GIT_SERVER_CONTAINER, GIT_CONTAINER],
109101
)
110102

111103

tests/test_kea.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import pytest
66
from pytest_container import DerivedContainer
77
from pytest_container.container import ContainerLauncher
8-
from pytest_container.container import container_and_marks_from_pytest_param
98
from pytest_container.runtime import OciRuntimeBase
109

1110
from bci_tester.data import BASE_CONTAINER
@@ -45,7 +44,7 @@ def test_kea_dhcp4(
4544
)
4645

4746
dhcp_client_ctr = DerivedContainer(
48-
base=container_and_marks_from_pytest_param(BASE_CONTAINER)[0],
47+
base=BASE_CONTAINER,
4948
containerfile="RUN zypper refresh && zypper -n install dhcp-client iproute2 && zypper clean --all",
5049
custom_entry_point="/bin/sh",
5150
extra_launch_args=[f"--network={network_name}", "--privileged"],

tests/test_kernel_module.py

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,8 @@
33
import re
44

55
import pytest
6-
from _pytest.mark import ParameterSet
76
from pytest_container import DerivedContainer
87
from pytest_container import GitRepositoryBuild
9-
from pytest_container import container_and_marks_from_pytest_param
108
from pytest_container.container import ContainerData
119
from pytest_container.runtime import LOCALHOST
1210

@@ -23,16 +21,10 @@
2321
_DRBD_VERSION = "9.2.11"
2422

2523

26-
def create_kernel_test(containerfile: str) -> ParameterSet:
27-
return pytest.param(
28-
DerivedContainer(
29-
base=container_and_marks_from_pytest_param(
30-
KERNEL_MODULE_CONTAINER
31-
)[0],
32-
containerfile=containerfile,
33-
),
34-
marks=KERNEL_MODULE_CONTAINER.marks,
35-
id=KERNEL_MODULE_CONTAINER.id,
24+
def create_kernel_test(containerfile: str) -> DerivedContainer:
25+
return DerivedContainer(
26+
base=KERNEL_MODULE_CONTAINER,
27+
containerfile=containerfile,
3628
)
3729

3830

tests/test_kiwi.py

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
import pytest
66
from pytest_container import DerivedContainer
7-
from pytest_container import container_and_marks_from_pytest_param
87
from pytest_container.container import ContainerData
98
from pytest_container.container import ImageFormat
109
from pytest_container.runtime import LOCALHOST
@@ -22,17 +21,13 @@
2221
RUN curl -Lsf -o - https://github.com/OSInside/kiwi/archive/refs/heads/master.tar.gz | tar --no-same-permissions --no-same-owner -xzf - | true
2322
"""
2423

25-
for kiwi_ctr in KIWI_CONTAINERS:
26-
ctr, marks = container_and_marks_from_pytest_param(kiwi_ctr)
24+
for ctr in KIWI_CONTAINERS:
2725
KIWI_CONTAINER_EXTENDED.append(
28-
pytest.param(
29-
DerivedContainer(
30-
base=ctr,
31-
containerfile=CONTAINERFILE_KIWI_EXTENDED,
32-
image_format=ImageFormat.DOCKER,
33-
extra_launch_args=["--privileged", "-v", "/dev:/dev"],
34-
),
35-
marks=marks,
26+
DerivedContainer(
27+
base=ctr,
28+
containerfile=CONTAINERFILE_KIWI_EXTENDED,
29+
image_format=ImageFormat.DOCKER,
30+
extra_launch_args=["--privileged", "-v", "/dev:/dev"],
3631
)
3732
)
3833

0 commit comments

Comments
 (0)