Skip to content

Commit

Permalink
WIP: add as_pytest_param property to ContainerBaseABC & Pod
Browse files Browse the repository at this point in the history
  • Loading branch information
dcermak committed Jul 19, 2024
1 parent a872ff9 commit 799bceb
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 0 deletions.
9 changes: 9 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,12 @@ strict = true
[[tool.mypy.overrides]]
module = "testinfra,deprecation"
ignore_missing_imports = true

[tool.pytest.ini_options]
xfail_strict = true
addopts = "--strict-markers"
markers = [
'secretleapmark',
'othersecretmark',
'secretpodmark',
]
36 changes: 36 additions & 0 deletions pytest_container/container.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,12 @@

import _pytest.mark
import deprecation
import pytest
import testinfra
from filelock import BaseFileLock
from filelock import FileLock
from pytest import Mark
from pytest import MarkDecorator
from pytest import param
from pytest_container.helpers import get_always_pull_option
from pytest_container.inspect import ContainerHealth
Expand Down Expand Up @@ -493,6 +496,9 @@ class ContainerBase:
default_factory=list
)

#: optional list of marks applied to this container image under test
marks: Collection[Union[MarkDecorator, Mark]] = field(default_factory=list)

_is_local: bool = False

def __post_init__(self) -> None:
Expand Down Expand Up @@ -631,6 +637,11 @@ class ContainerBaseABC(ABC):
"""

@property
@abstractmethod
def as_pytest_param(self) -> _pytest.mark.ParameterSet:
""""""

@abstractmethod
def prepare_container(
self,
Expand Down Expand Up @@ -694,6 +705,10 @@ def baseurl(self) -> Optional[str]:
return None
return self.url

@property
def as_pytest_param(self) -> _pytest.mark.ParameterSet:
return pytest.param(self, marks=self.marks, id=str(self))


@dataclass(unsafe_hash=True)
class DerivedContainer(ContainerBase, ContainerBaseABC):
Expand Down Expand Up @@ -723,11 +738,32 @@ class DerivedContainer(ContainerBase, ContainerBaseABC):
#: has been built
add_build_tags: List[str] = field(default_factory=list)

@staticmethod
def _get_recursive_marks(
ctr: Union[Container, "DerivedContainer", str]
) -> Collection[Union[MarkDecorator, Mark]]:
if isinstance(ctr, str):
return []

Check warning on line 746 in pytest_container/container.py

View check run for this annotation

Codecov / codecov/patch

pytest_container/container.py#L746

Added line #L746 was not covered by tests
if isinstance(ctr, Container):
return ctr.marks

return tuple(ctr.marks) + tuple(
DerivedContainer._get_recursive_marks(ctr.base)
)

def __post_init__(self) -> None:
super().__post_init__()
if not self.base:
raise ValueError("A base container must be provided")

@property
def as_pytest_param(self) -> _pytest.mark.ParameterSet:
return pytest.param(
self,
marks=DerivedContainer._get_recursive_marks(self),
id=str(self),
)

@property
def baseurl(self) -> Optional[str]:
return self.url or self.get_base().baseurl
Expand Down
18 changes: 18 additions & 0 deletions pytest_container/pod.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,16 @@
from pathlib import Path
from subprocess import check_output
from types import TracebackType
from typing import Collection
from typing import List
from typing import Optional
from typing import Type
from typing import Union

import pytest
from _pytest.mark import ParameterSet
from pytest import Mark
from pytest import MarkDecorator
from pytest_container.container import Container
from pytest_container.container import ContainerData
from pytest_container.container import ContainerLauncher
Expand Down Expand Up @@ -40,6 +44,20 @@ class Pod:
#: ports exposed by the pod
forwarded_ports: List[PortForwarding] = field(default_factory=list)

marks: Collection[Union[MarkDecorator, Mark]] = field(default_factory=list)

@property
def as_pytest_param(self) -> ParameterSet:
marks = tuple(self.marks)
for ctr in self.containers:
marks += tuple(ctr.as_pytest_param.marks)
return pytest.param(
self,
marks=marks,
id="Pod with containers: "
+ ",".join(str(c) for c in self.containers),
)


@dataclass(frozen=True)
class PodData:
Expand Down
53 changes: 53 additions & 0 deletions tests/test_marks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import pytest
from pytest_container.container import Container
from pytest_container.container import DerivedContainer
from pytest_container.pod import Pod

from tests.images import LEAP_URL

LEAP_WITH_MARK = Container(url=LEAP_URL, marks=[pytest.mark.secretleapmark])

DERIVED_ON_LEAP_WITH_MARK = DerivedContainer(base=LEAP_WITH_MARK)

SECOND_DERIVED_ON_LEAP = DerivedContainer(
base=DERIVED_ON_LEAP_WITH_MARK, marks=[pytest.mark.othersecretmark]
)

INDEPENDENT_OTHER_LEAP = Container(
url=LEAP_URL, marks=[pytest.mark.othersecretmark]
)

UNMARKED_POD = Pod(containers=[LEAP_WITH_MARK, INDEPENDENT_OTHER_LEAP])

MARKED_POD = Pod(
containers=[LEAP_WITH_MARK, INDEPENDENT_OTHER_LEAP],
marks=[pytest.mark.secretpodmark],
)


def test_marks() -> None:
assert list(LEAP_WITH_MARK.as_pytest_param.marks) == [
pytest.mark.secretleapmark
]
assert list(DERIVED_ON_LEAP_WITH_MARK.as_pytest_param.marks) == [
pytest.mark.secretleapmark
]
assert list(SECOND_DERIVED_ON_LEAP.as_pytest_param.marks) == [
pytest.mark.othersecretmark,
pytest.mark.secretleapmark,
]

pod_marks = UNMARKED_POD.as_pytest_param.marks
assert (
len(pod_marks) == 2
and pytest.mark.othersecretmark in pod_marks
and pytest.mark.secretleapmark in pod_marks
)

pod_marks = MARKED_POD.as_pytest_param.marks
assert (
len(pod_marks) == 3
and pytest.mark.othersecretmark in pod_marks
and pytest.mark.secretleapmark in pod_marks
and pytest.mark.secretpodmark in pod_marks
)

0 comments on commit 799bceb

Please sign in to comment.