Skip to content

Commit 21bdea2

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 855176c commit 21bdea2

21 files changed

Lines changed: 231 additions & 349 deletions

bci_tester/data.py

Lines changed: 21 additions & 29 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
13+
from pytest_container import PortForwarding
1214
from pytest_container.container import ContainerVolume
13-
from pytest_container.container import PortForwarding
14-
from pytest_container.container import container_and_marks_from_pytest_param
1515
from pytest_container.inspect import NetworkProtocol
1616
from pytest_container.runtime import LOCALHOST
1717

@@ -306,7 +306,7 @@ def create_BCI(
306306
bci_type: ImageType = ImageType.LANGUAGE_STACK,
307307
container_user: Optional[str] = None,
308308
**kwargs,
309-
) -> ParameterSet:
309+
) -> DerivedContainer:
310310
"""Creates a DerivedContainer wrapped in a pytest.param for the BCI with the
311311
given ``build_tag``.
312312
@@ -421,14 +421,11 @@ def create_BCI(
421421
os.environ["SCC_CREDENTIAL_PASSWORD"]
422422
)
423423

424-
return pytest.param(
425-
DerivedContainer(
426-
base=baseurl,
427-
containerfile=containerfile,
428-
**kwargs,
429-
),
424+
return DerivedContainer(
425+
base=baseurl,
426+
containerfile=containerfile,
430427
marks=marks,
431-
id=f"{build_tag} from {baseurl}",
428+
**kwargs,
432429
)
433430

434431

@@ -442,9 +439,9 @@ def create_BCI(
442439
)
443440
]
444441

445-
BASE_FIPS_CONTAINERS = []
446-
LTSS_BASE_CONTAINERS = []
447-
LTSS_BASE_FIPS_CONTAINERS = []
442+
BASE_FIPS_CONTAINERS: List[DerivedContainer] = []
443+
LTSS_BASE_CONTAINERS: List[DerivedContainer] = []
444+
LTSS_BASE_FIPS_CONTAINERS: List[DerivedContainer] = []
448445

449446
if OS_VERSION == "tumbleweed":
450447
BASE_CONTAINER = create_BCI(
@@ -1156,23 +1153,19 @@ def create_BCI(
11561153
)
11571154

11581155
#: all containers with zypper and with the flag to launch them as root
1159-
CONTAINERS_WITH_ZYPPER_AS_ROOT = []
1160-
for param in CONTAINERS_WITH_ZYPPER:
1156+
CONTAINERS_WITH_ZYPPER_AS_ROOT: List[DerivedContainer] = []
1157+
for ctr in CONTAINERS_WITH_ZYPPER:
11611158
# only modify the user for containers where `USER` is explicitly set,
11621159
# atm this is no container
1163-
if param not in []:
1164-
CONTAINERS_WITH_ZYPPER_AS_ROOT.append(param)
1160+
if ctr not in []:
1161+
CONTAINERS_WITH_ZYPPER_AS_ROOT.append(ctr)
11651162
else:
1166-
ctr, marks = container_and_marks_from_pytest_param(param)
11671163
CONTAINERS_WITH_ZYPPER_AS_ROOT.append(
1168-
pytest.param(
1169-
DerivedContainer(
1170-
base=ctr,
1171-
extra_launch_args=(
1172-
(ctr.extra_launch_args or []) + ["--user", "root"]
1173-
),
1164+
DerivedContainer(
1165+
base=ctr,
1166+
extra_launch_args=(
1167+
(ctr.extra_launch_args or []) + ["--user", "root"]
11741168
),
1175-
marks=marks,
11761169
)
11771170
)
11781171

@@ -1305,13 +1298,12 @@ def has_xfail(param: ParameterSet) -> bool:
13051298
return True
13061299
return False
13071300

1308-
for param in ALL_CONTAINERS:
1301+
for ctr in ALL_CONTAINERS:
13091302
# don't check containers which are known broken or excluded
1310-
if has_true_skipif(param) or has_xfail(param):
1303+
if has_true_skipif(ctr) or has_xfail(ctr):
13111304
continue
13121305

1313-
ctr, marks = container_and_marks_from_pytest_param(param)
1314-
for mark in marks or []:
1306+
for mark in ctr.marks or []:
13151307
assert mark.name in custom_markers.union(
13161308
{"xfail", "skipif", "skip"}
13171309
), (

tests/test_all.py

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@
88
import pathlib
99
import xml.etree.ElementTree as ET
1010
from pathlib import Path
11+
from typing import List
1112

1213
import packaging.version
1314
import pytest
1415
from _pytest.config import Config
1516
from pytest_container import Container
1617
from pytest_container import DerivedContainer
1718
from pytest_container import MultiStageBuild
18-
from pytest_container import container_and_marks_from_pytest_param
1919
from pytest_container import get_extra_build_args
2020
from pytest_container import get_extra_run_args
2121
from pytest_container.container import BindMount
@@ -249,16 +249,12 @@ def test_glibc_present(auto_container):
249249
# the host instead of passing it on via stdout which pollutes the logs making
250250
# them unreadable
251251
_CONTAINERS_WITH_VOLUME_MOUNT = []
252-
for param in CONTAINERS_WITH_ZYPPER_AS_ROOT:
253-
ctr, marks = container_and_marks_from_pytest_param(param)
252+
for ctr in CONTAINERS_WITH_ZYPPER_AS_ROOT:
254253
new_vol_mounts = (ctr.volume_mounts or []) + [BindMount("/solv/")]
255-
kwargs = {**ctr.__dict__}
254+
kwargs = {**ctr.dict()}
256255
kwargs.pop("volume_mounts")
257256
_CONTAINERS_WITH_VOLUME_MOUNT.append(
258-
pytest.param(
259-
DerivedContainer(volume_mounts=new_vol_mounts, **kwargs),
260-
marks=marks,
261-
)
257+
DerivedContainer(volume_mounts=new_vol_mounts, **kwargs),
262258
)
263259

264260

@@ -593,7 +589,7 @@ def test_certificates_are_present(
593589
],
594590
indirect=True,
595591
)
596-
def test_container_build_and_repo(container_per_test, host):
592+
def test_container_build_and_repo(container_per_test: ContainerData):
597593
"""Test all containers with zypper in them whether at least the ``SLE_BCI``
598594
repository is present (if the host is unregistered). If a custom value for
599595
the repository url has been supplied, then check that it is correct.
@@ -695,13 +691,12 @@ def test_container_build_and_repo(container_per_test, host):
695691
container_per_test.connection.run_expect([0], "zypper -n ref")
696692

697693

698-
_CONTAINERS_WITH_ZYPP_CREDENTIALS_MOUNTED = []
699-
for param in CONTAINERS_WITH_ZYPPER_AS_ROOT:
700-
if param in LTSS_BASE_CONTAINERS:
694+
_CONTAINERS_WITH_ZYPP_CREDENTIALS_MOUNTED: List[DerivedContainer] = []
695+
for ctr in CONTAINERS_WITH_ZYPPER_AS_ROOT:
696+
if ctr in LTSS_BASE_CONTAINERS:
701697
# LTSS containers are broken with the SLE BCI repo
702698
continue
703699

704-
ctr, marks = container_and_marks_from_pytest_param(param)
705700
new_vol_mounts = (ctr.volume_mounts or []) + [
706701
BindMount(
707702
container_path=ZYPP_CREDENTIALS_DIR,
@@ -713,11 +708,7 @@ def test_container_build_and_repo(container_per_test, host):
713708
kwargs.pop("volume_mounts")
714709

715710
_CONTAINERS_WITH_ZYPP_CREDENTIALS_MOUNTED.append(
716-
pytest.param(
717-
DerivedContainer(**kwargs, volume_mounts=new_vol_mounts),
718-
marks=marks,
719-
id=param.id,
720-
)
711+
DerivedContainer(**kwargs, volume_mounts=new_vol_mounts)
721712
)
722713

723714

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
@@ -246,20 +245,18 @@ def test_all_openssl_hashes_known(auto_container):
246245

247246
#: This is the base container with additional launch arguments applied to it so
248247
#: that docker can be launched inside the container
249-
DIND_CONTAINER = pytest.param(
250-
DerivedContainer(
251-
base=container_and_marks_from_pytest_param(BASE_CONTAINER)[0],
252-
**{
253-
x: getattr(BASE_CONTAINER.values[0], x)
254-
for x in BASE_CONTAINER.values[0].__dict__
255-
if x not in ("extra_launch_args", "base")
256-
},
257-
extra_launch_args=[
258-
"--privileged=true",
259-
"-v",
260-
"/var/run/docker.sock:/var/run/docker.sock",
261-
],
262-
),
248+
DIND_CONTAINER = DerivedContainer(
249+
base=BASE_CONTAINER,
250+
**{
251+
x: getattr(BASE_CONTAINER, x)
252+
for x in BASE_CONTAINER.dict()
253+
if x not in ("extra_launch_args", "base")
254+
},
255+
extra_launch_args=[
256+
"--privileged=true",
257+
"-v",
258+
"/var/run/docker.sock:/var/run/docker.sock",
259+
],
263260
)
264261

265262

tests/test_bind.py

Lines changed: 34 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
from dns.rrset import RRset
1616
from pytest_container import BindMount
1717
from pytest_container import DerivedContainer
18-
from pytest_container import container_and_marks_from_pytest_param
1918
from pytest_container.container import ContainerData
2019
from pytest_container.container import EntrypointSelection
2120

@@ -117,32 +116,28 @@ def env_to_dict(env_stdout: str) -> Dict[str, str]:
117116
assert env_to_dict(env) == env_to_dict(env_with_source)
118117

119118

120-
_BIND_WITH_CUSTOM_CONF = []
121-
for param in BIND_CONTAINERS:
122-
ctr, marks = container_and_marks_from_pytest_param(param)
119+
_BIND_WITH_CUSTOM_CONF: List[DerivedContainer] = []
120+
for ctr in BIND_CONTAINERS:
123121
_NAMED_CONF = "/etc/bind/named.conf"
124122
_BIND_WITH_CUSTOM_CONF.append(
125-
pytest.param(
126-
DerivedContainer(
127-
base=ctr,
128-
extra_environment_variables={"NAMED_CONF": _NAMED_CONF},
129-
volume_mounts=[
130-
BindMount(
131-
container_path=_NAMED_CONF,
132-
host_path=str(
133-
Path(__file__).parent / "files" / "named.conf"
134-
),
123+
DerivedContainer(
124+
base=ctr,
125+
extra_environment_variables={"NAMED_CONF": _NAMED_CONF},
126+
volume_mounts=[
127+
BindMount(
128+
container_path=_NAMED_CONF,
129+
host_path=str(
130+
Path(__file__).parent / "files" / "named.conf"
135131
),
136-
BindMount(
137-
container_path="/etc/bind/db.blocked",
138-
host_path=str(
139-
Path(__file__).parent / "files" / "db.blocked"
140-
),
132+
),
133+
BindMount(
134+
container_path="/etc/bind/db.blocked",
135+
host_path=str(
136+
Path(__file__).parent / "files" / "db.blocked"
141137
),
142-
],
143-
forwarded_ports=ctr.forwarded_ports,
144-
),
145-
marks=marks,
138+
),
139+
],
140+
forwarded_ports=ctr.forwarded_ports,
146141
)
147142
)
148143

@@ -167,21 +162,16 @@ def test_custom_named_config(container: ContainerData) -> None:
167162
assert not empty_resp.answer
168163

169164

170-
_BIND_WITH_CUSTOM_CHECKER = []
165+
_BIND_WITH_CUSTOM_CHECKER: List[DerivedContainer] = []
171166
_CHECKER_TOUCHED_FILE = "/tmp/check-conf-worked"
172-
for param in BIND_CONTAINERS:
173-
ctr, marks = container_and_marks_from_pytest_param(param)
174-
167+
for ctr in BIND_CONTAINERS:
175168
_BIND_WITH_CUSTOM_CHECKER.append(
176-
pytest.param(
177-
DerivedContainer(
178-
base=ctr,
179-
extra_environment_variables={
180-
"NAMED_CHECKCONF_BIN": "/usr/bin/touch",
181-
"NAMED_CHECKCONF_ARGS": _CHECKER_TOUCHED_FILE,
182-
},
183-
),
184-
marks=marks,
169+
DerivedContainer(
170+
base=ctr,
171+
extra_environment_variables={
172+
"NAMED_CHECKCONF_BIN": "/usr/bin/touch",
173+
"NAMED_CHECKCONF_ARGS": _CHECKER_TOUCHED_FILE,
174+
},
185175
)
186176
)
187177

@@ -198,20 +188,15 @@ def test_custom_checker(container: ContainerData) -> None:
198188
assert container.connection.file(_CHECKER_TOUCHED_FILE).exists
199189

200190

201-
_BIND_WITH_BASH = []
202-
for param in BIND_CONTAINERS:
203-
ctr, marks = container_and_marks_from_pytest_param(param)
204-
191+
_BIND_WITH_BASH: List[DerivedContainer] = []
192+
for ctr in BIND_CONTAINERS:
205193
_BIND_WITH_BASH.append(
206-
pytest.param(
207-
DerivedContainer(
208-
base=ctr,
209-
# don't launch bind as it chown's /var/lib/named
210-
entry_point=EntrypointSelection.BASH,
211-
# ignore healthcheck, bind is not running in this container
212-
healthcheck_timeout=timedelta(seconds=-1),
213-
),
214-
marks=marks,
194+
DerivedContainer(
195+
base=ctr,
196+
# don't launch bind as it chown's /var/lib/named
197+
entry_point=EntrypointSelection.BASH,
198+
# ignore healthcheck, bind is not running in this container
199+
healthcheck_timeout=timedelta(seconds=-1),
215200
)
216201
)
217202

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(

0 commit comments

Comments
 (0)