Skip to content

Commit

Permalink
Simplify test code given the changes from pytest_container#216
Browse files Browse the repository at this point in the history
We now no longer have to perform the manual container & mark unbundling as
Container and DerivedContainer are now instances of ParameterSet as well
  • Loading branch information
dcermak committed Jan 17, 2025
1 parent 2efdc95 commit d7cc0af
Show file tree
Hide file tree
Showing 19 changed files with 162 additions and 253 deletions.
37 changes: 15 additions & 22 deletions bci_tester/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
import os
from datetime import timedelta
from typing import Iterable
from typing import List
from typing import Optional
from typing import Sequence
from typing import Tuple

from pytest_container import DerivedContainer
from pytest_container.container import ContainerVolume
from pytest_container.container import PortForwarding
from pytest_container.container import container_and_marks_from_pytest_param
from pytest_container.runtime import LOCALHOST

try:
Expand Down Expand Up @@ -291,7 +291,7 @@ def create_BCI(
bci_type: ImageType = ImageType.LANGUAGE_STACK,
container_user: Optional[str] = None,
**kwargs,
) -> ParameterSet:
) -> DerivedContainer:
"""Creates a DerivedContainer wrapped in a pytest.param for the BCI with the
given ``build_tag``.
Expand Down Expand Up @@ -394,14 +394,11 @@ def create_BCI(
else:
containerfile = _BCI_REPLACE_REPO_CONTAINERFILE

return pytest.param(
DerivedContainer(
base=baseurl,
containerfile=containerfile,
**kwargs,
),
return DerivedContainer(
base=baseurl,
containerfile=containerfile,
marks=marks,
id=f"{build_tag} from {baseurl}",
**kwargs,
)


Expand Down Expand Up @@ -1029,23 +1026,19 @@ def create_BCI(
)

#: all containers with zypper and with the flag to launch them as root
CONTAINERS_WITH_ZYPPER_AS_ROOT = []
for param in CONTAINERS_WITH_ZYPPER:
CONTAINERS_WITH_ZYPPER_AS_ROOT: List[DerivedContainer] = []
for ctr in CONTAINERS_WITH_ZYPPER:
# only modify the user for containers where `USER` is explicitly set,
# atm this is no container
if param not in []:
CONTAINERS_WITH_ZYPPER_AS_ROOT.append(param)
if ctr not in []:
CONTAINERS_WITH_ZYPPER_AS_ROOT.append(ctr)
else:
ctr, marks = container_and_marks_from_pytest_param(param)
CONTAINERS_WITH_ZYPPER_AS_ROOT.append(
pytest.param(
DerivedContainer(
base=ctr,
extra_launch_args=(
(ctr.extra_launch_args or []) + ["--user", "root"]
),
DerivedContainer(
base=ctr,
extra_launch_args=(
(ctr.extra_launch_args or []) + ["--user", "root"]
),
marks=marks,
)
)

Expand Down Expand Up @@ -1134,7 +1127,7 @@ def has_xfail(param: ParameterSet) -> bool:
print(
json.dumps(
[
container_and_marks_from_pytest_param(cont)[0].get_base().url
cont.get_base().url
for cont in ALL_CONTAINERS
if (not has_true_skipif(cont) and not has_xfail(cont))
]
Expand Down
11 changes: 3 additions & 8 deletions tests/test_all.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
from pytest_container import Container
from pytest_container import DerivedContainer
from pytest_container import MultiStageBuild
from pytest_container import container_and_marks_from_pytest_param
from pytest_container import get_extra_build_args
from pytest_container import get_extra_run_args
from pytest_container.container import BindMount
Expand Down Expand Up @@ -240,16 +239,12 @@ def test_glibc_present(auto_container):
# the host instead of passing it on via stdout which pollutes the logs making
# them unreadable
_CONTAINERS_WITH_VOLUME_MOUNT = []
for param in CONTAINERS_WITH_ZYPPER_AS_ROOT:
ctr, marks = container_and_marks_from_pytest_param(param)
for ctr in CONTAINERS_WITH_ZYPPER_AS_ROOT:
new_vol_mounts = (ctr.volume_mounts or []) + [BindMount("/solv/")]
kwargs = {**ctr.__dict__}
kwargs = {**ctr.dict()}
kwargs.pop("volume_mounts")
_CONTAINERS_WITH_VOLUME_MOUNT.append(
pytest.param(
DerivedContainer(volume_mounts=new_vol_mounts, **kwargs),
marks=marks,
)
DerivedContainer(volume_mounts=new_vol_mounts, **kwargs),
)


Expand Down
27 changes: 12 additions & 15 deletions tests/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import pytest
from pytest_container import DerivedContainer
from pytest_container.container import ContainerData
from pytest_container.container import container_and_marks_from_pytest_param
from pytest_container.runtime import LOCALHOST

from bci_tester.data import BASE_CONTAINER
Expand Down Expand Up @@ -232,20 +231,18 @@ def test_all_openssl_hashes_known(auto_container):

#: This is the base container with additional launch arguments applied to it so
#: that docker can be launched inside the container
DIND_CONTAINER = pytest.param(
DerivedContainer(
base=container_and_marks_from_pytest_param(BASE_CONTAINER)[0],
**{
x: getattr(BASE_CONTAINER.values[0], x)
for x in BASE_CONTAINER.values[0].__dict__
if x not in ("extra_launch_args", "base")
},
extra_launch_args=[
"--privileged=true",
"-v",
"/var/run/docker.sock:/var/run/docker.sock",
],
),
DIND_CONTAINER = DerivedContainer(
base=BASE_CONTAINER,
**{
x: getattr(BASE_CONTAINER, x)
for x in BASE_CONTAINER.dict()
if x not in ("extra_launch_args", "base")
},
extra_launch_args=[
"--privileged=true",
"-v",
"/var/run/docker.sock:/var/run/docker.sock",
],
)


Expand Down
9 changes: 5 additions & 4 deletions tests/test_dotnet.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@

import pytest
from _pytest.mark import ParameterSet
from pytest_container import DerivedContainer
from pytest_container import GitRepositoryBuild
from pytest_container import container_and_marks_from_pytest_param

from bci_tester.data import DOTNET_ASPNET_8_0_CONTAINER
from bci_tester.data import DOTNET_ASPNET_9_0_CONTAINER
Expand All @@ -36,13 +36,14 @@
)


def _generate_ctr_ver_param(ctr_ver: Tuple[ParameterSet, str]) -> ParameterSet:
def _generate_ctr_ver_param(
ctr_ver: Tuple[DerivedContainer, str],
) -> ParameterSet:
"""Converts a `(Container, Version)` tuple into a `pytest.param` that
contains the exact same values but has the marks of the container.
"""
ctr, marks = container_and_marks_from_pytest_param(ctr_ver[0])
return pytest.param(ctr, ctr_ver[1], marks=marks or ())
return pytest.param(ctr_ver[0], ctr_ver[1], marks=ctr_ver[0].marks)


@pytest.mark.parametrize(
Expand Down
39 changes: 17 additions & 22 deletions tests/test_fips.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
from pytest_container import DerivedContainer
from pytest_container.container import BindMount
from pytest_container.container import ContainerData
from pytest_container.container import container_and_marks_from_pytest_param
from pytest_container.runtime import LOCALHOST

from bci_tester.data import BASE_FIPS_CONTAINERS
Expand Down Expand Up @@ -49,8 +48,7 @@
FIPS_TESTER_IMAGES = []
FIPS_GNUTLS_TESTER_IMAGES = []
FIPS_GCRYPT_TESTER_IMAGES = []
for param in CONTAINERS_WITH_ZYPPER:
ctr, marks = container_and_marks_from_pytest_param(param)
for ctr in CONTAINERS_WITH_ZYPPER:
kwargs = {
"base": ctr,
"extra_environment_variables": ctr.extra_environment_variables,
Expand All @@ -68,35 +66,32 @@
else []
),
}
tester_ctr = DerivedContainer(containerfile=DOCKERFILE, **kwargs)
gnutls_tester_ctr = DerivedContainer(
containerfile=DOCKERFILE_GNUTLS, **kwargs
)
gcrypt_tester_ctr = DerivedContainer(
containerfile=DOCKERFILE_GCRYPT, **kwargs
)

if param not in LTSS_BASE_FIPS_CONTAINERS + BASE_FIPS_CONTAINERS:
if ctr not in LTSS_BASE_FIPS_CONTAINERS + BASE_FIPS_CONTAINERS:
# create a shallow copy here to avoid mutating the original params
marks = marks[:] + [
marks = list(ctr.marks) + [
pytest.mark.skipif(
not host_fips_enabled(),
reason="The target must run in FIPS mode for the FIPS test suite",
)
]
CONTAINER_IMAGES_WITH_ZYPPER.append(
pytest.param(ctr, marks=marks, id=param.id)
)
FIPS_TESTER_IMAGES.append(
pytest.param(tester_ctr, marks=marks, id=param.id)
else:
marks = ctr.marks

tester_ctr = DerivedContainer(
containerfile=DOCKERFILE, marks=marks, **kwargs
)
FIPS_GNUTLS_TESTER_IMAGES.append(
pytest.param(gnutls_tester_ctr, marks=marks, id=param.id)
gnutls_tester_ctr = DerivedContainer(
containerfile=DOCKERFILE_GNUTLS, marks=marks, **kwargs
)
FIPS_GCRYPT_TESTER_IMAGES.append(
pytest.param(gcrypt_tester_ctr, marks=marks, id=param.id)
gcrypt_tester_ctr = DerivedContainer(
containerfile=DOCKERFILE_GCRYPT, marks=marks, **kwargs
)

CONTAINER_IMAGES_WITH_ZYPPER.append(ctr)
FIPS_TESTER_IMAGES.append(tester_ctr)
FIPS_GNUTLS_TESTER_IMAGES.append(gnutls_tester_ctr)
FIPS_GCRYPT_TESTER_IMAGES.append(gcrypt_tester_ctr)


@pytest.mark.parametrize(
"container_per_test", FIPS_TESTER_IMAGES, indirect=True
Expand Down
11 changes: 3 additions & 8 deletions tests/test_gcc.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import pytest
from pytest_container import DerivedContainer
from pytest_container import container_and_marks_from_pytest_param
from pytest_container.container import ContainerData

from bci_tester.data import GCC_CONTAINERS
Expand All @@ -22,13 +21,9 @@
"""

HELLO_CONTAINERS = [
pytest.param(
DerivedContainer(
base=container_and_marks_from_pytest_param(ctr)[0],
containerfile=CONTAINERFILE_HELLO,
),
marks=ctr.marks,
id=ctr.id,
DerivedContainer(
base=ctr,
containerfile=CONTAINERFILE_HELLO,
)
for ctr in CONTAINER_IMAGES
]
Expand Down
12 changes: 2 additions & 10 deletions tests/test_git.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import pytest
from pytest_container.container import DerivedContainer
from pytest_container.container import ImageFormat
from pytest_container.container import container_and_marks_from_pytest_param
from pytest_container.pod import Pod
from pytest_container.pod import PodData
from pytest_container.runtime import LOCALHOST
Expand Down Expand Up @@ -96,16 +95,9 @@ def test_git_clone_https(auto_container_per_test):
extra_launch_args=["--cap-add", "AUDIT_WRITE"],
)

_git_container, _marks = container_and_marks_from_pytest_param(GIT_CONTAINER)

GIT_POD = pytest.param(
Pod(
containers=[
GIT_SERVER_CONTAINER,
_git_container,
],
),
marks=_marks,
GIT_POD = Pod(
containers=[GIT_SERVER_CONTAINER, GIT_CONTAINER],
)


Expand Down
16 changes: 4 additions & 12 deletions tests/test_kernel_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@
import re

import pytest
from _pytest.mark import ParameterSet
from pytest_container import DerivedContainer
from pytest_container import GitRepositoryBuild
from pytest_container import container_and_marks_from_pytest_param
from pytest_container.container import ContainerData
from pytest_container.runtime import LOCALHOST

Expand All @@ -23,16 +21,10 @@
_DRBD_VERSION = "9.2.11"


def create_kernel_test(containerfile: str) -> ParameterSet:
return pytest.param(
DerivedContainer(
base=container_and_marks_from_pytest_param(
KERNEL_MODULE_CONTAINER
)[0],
containerfile=containerfile,
),
marks=KERNEL_MODULE_CONTAINER.marks,
id=KERNEL_MODULE_CONTAINER.id,
def create_kernel_test(containerfile: str) -> DerivedContainer:
return DerivedContainer(
base=KERNEL_MODULE_CONTAINER,
containerfile=containerfile,
)


Expand Down
17 changes: 6 additions & 11 deletions tests/test_kiwi.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

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

for kiwi_ctr in KIWI_CONTAINERS:
ctr, marks = container_and_marks_from_pytest_param(kiwi_ctr)
for ctr in KIWI_CONTAINERS:
KIWI_CONTAINER_EXTENDED.append(
pytest.param(
DerivedContainer(
base=ctr,
containerfile=CONTAINERFILE_KIWI_EXTENDED,
image_format=ImageFormat.DOCKER,
extra_launch_args=["--privileged", "-v", "/dev:/dev"],
),
marks=marks,
DerivedContainer(
base=ctr,
containerfile=CONTAINERFILE_KIWI_EXTENDED,
image_format=ImageFormat.DOCKER,
extra_launch_args=["--privileged", "-v", "/dev:/dev"],
)
)

Expand Down
Loading

0 comments on commit d7cc0af

Please sign in to comment.