Skip to content

Commit

Permalink
passed state template to ctx
Browse files Browse the repository at this point in the history
  • Loading branch information
PietroPasotti committed Sep 14, 2023
1 parent c09587d commit 909b3ea
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 28 deletions.
7 changes: 4 additions & 3 deletions interface_tester/interface_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,14 @@ class _InterfaceTestContext:
"""Charm actions.yaml"""
test_fn: Callable
"""Test function."""
state_template: Optional[State]
"""Initial state that this test should be run with, according to the charm."""

"""The role (provider|requirer) that this test is about."""
schema: Optional["DataBagSchema"] = None
"""Databag schema to validate the output relation with."""
input_state: Optional[State] = None
"""Initial state that this test should be run with."""
"""Initial state that this test should be run with, according to the test."""


def check_test_case_validator_signature(fn: Callable):
Expand Down Expand Up @@ -206,7 +208,6 @@ def __init__(self, state_in: Optional[State] = None, name: Optional[str] = None)
if not self.ctx:
raise RuntimeError("Tester can only be initialized inside an interface test context.")

self._state_template = None
self._state_in = state_in or State()
self._test_name = name or self.ctx.test_fn.__name__

Expand Down Expand Up @@ -351,7 +352,7 @@ def _run(self, event: Union[str, Event]):
# some required config, a "happy" status, network information, OTHER relations.
# Typically, should NOT touch the relation that this interface test is about
# -> so we overwrite and warn on conflict: state_template is the baseline,
state = (self._state_template or State()).copy()
state = (self.ctx.state_template or State()).copy()

relations = self._generate_relations_state(
state, input_state, self.ctx.supported_endpoints, self.ctx.role
Expand Down
56 changes: 32 additions & 24 deletions interface_tester/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ class InterfaceTester:
_RAISE_IMMEDIATELY = False

def __init__(
self,
repo: str = "https://github.com/canonical/charm-relation-interfaces",
branch: str = "main",
base_path: str = "interfaces",
self,
repo: str = "https://github.com/canonical/charm-relation-interfaces",
branch: str = "main",
base_path: str = "interfaces",
):
self._repo = repo
self._branch = branch
Expand All @@ -53,18 +53,18 @@ def __init__(
self._charm_spec_cache = None

def configure(
self,
*,
charm_type: Optional[Type[CharmType]] = None,
repo: Optional[str] = None,
branch: Optional[str] = None,
base_path: Optional[str] = None,
interface_name: Optional[str] = None,
interface_version: Optional[int] = None,
state_template: Optional[State] = None,
meta: Optional[Dict[str, Any]] = None,
actions: Optional[Dict[str, Any]] = None,
config: Optional[Dict[str, Any]] = None,
self,
*,
charm_type: Optional[Type[CharmType]] = None,
repo: Optional[str] = None,
branch: Optional[str] = None,
base_path: Optional[str] = None,
interface_name: Optional[str] = None,
interface_version: Optional[int] = None,
state_template: Optional[State] = None,
meta: Optional[Dict[str, Any]] = None,
actions: Optional[Dict[str, Any]] = None,
config: Optional[Dict[str, Any]] = None,
):
"""
Expand Down Expand Up @@ -189,11 +189,11 @@ def _collect_interface_test_specs(self) -> InterfaceTestSpec:

repo_name = self._repo.split("/")[-1]
intf_spec_path = (
Path(tempdir)
/ repo_name
/ self._base_path
/ self._interface_name.replace("-", "_")
/ f"v{self._interface_version}"
Path(tempdir)
/ repo_name
/ self._base_path
/ self._interface_name.replace("-", "_")
/ f"v{self._interface_version}"
)
if not intf_spec_path.exists():
raise RuntimeError(
Expand Down Expand Up @@ -239,7 +239,7 @@ def _gather_supported_endpoints(self) -> Dict[RoleLiteral, List[str]]:
return supported_endpoints

def _yield_tests(
self,
self,
) -> Generator[Tuple[Callable, RoleLiteral, DataBagSchema], None, None]:
"""Yield all test cases applicable to this charm and interface.
Expand Down Expand Up @@ -306,6 +306,7 @@ def run(self) -> bool:
interface_name=self._interface_name,
version=self._interface_version,
charm_type=self._charm_type,
state_template=self._state_template,
meta=self.meta,
config=self.config,
actions=self.actions,
Expand All @@ -318,12 +319,19 @@ def run(self) -> bool:
except Exception as e:
if self._RAISE_IMMEDIATELY:
raise e
errors.append(e)
errors.append((ctx, e))
ran_some = True

# todo: consider raising custom exceptions here.
if errors:
raise InterfaceTestsFailed(f"interface tests completed with errors. {errors}")

msgs = []
for ctx, e in errors:
msgs.append(f" - {ctx.interface_name}[v{ctx.version}]@{ctx.role}:{ctx.test_fn} raised {e}")
long_msg = "\n".join(msgs)

raise InterfaceTestsFailed(
f"interface tests completed with {len(errors)} errors. \n" + long_msg)

if not ran_some:
msg = f"no tests gathered for {self._interface_name}/v{self._interface_version}"
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ build-backend = "setuptools.build_meta"
[project]
name = "pytest-interface-tester"

version = "1.0.1"
version = "1.0.2"
authors = [
{ name = "Pietro Pasotti", email = "[email protected]" },
]
Expand Down

0 comments on commit 909b3ea

Please sign in to comment.