Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog-entries/907.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Fixed `Systemtest.py` so each Docker service receives its own build arguments from `components.yaml` (e.g. per-component `PLATFORM` and `PRECICE_REF`). [#907](https://github.com/precice/tutorials/pull/907)
154 changes: 108 additions & 46 deletions tests/systemtests/Systemtest.py
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,7 @@ class Systemtest:
run_before: str | None = None
run_after: str | None = None
params_to_use: Dict[str, str] = field(init=False)
build_arguments_by_component: Dict[str, Dict[str, str]] = field(init=False)
env: Dict[str, str] = field(init=False)

def __eq__(self, other) -> bool:
Expand Down Expand Up @@ -279,38 +280,87 @@ def _resolve_build_timeout(self) -> int:

def __init_args_to_use(self):
"""
Forwards the command-line arguments to the params_to_use dictionary, substituting any missing arguments with their defaults.
Resolve build arguments per component from components.yaml defaults and
global CLI overrides, then merge them for shared stages (prepare).

Previously, this function was also checking if all required parameters were provided, and was raising exceptions for parameters not provided and not having a default value. This check made adding optional parameters with empty defaults (e.g., the TUTORIALS_PR) complicated, and it was removed.
Comment thread
MakisH marked this conversation as resolved.
Resolved
"""

# Forward all provided arguments to params_to_use
provided_arguments = self.arguments.arguments
self.params_to_use = provided_arguments
self.build_arguments_by_component = {}

# Find out which parameters are needed
needed_parameters = set()
for case in self.case_combination.cases:
needed_parameters.update(case.component.parameters)

# Substitute defaults for non-provided, needed arguments
for needed_param in needed_parameters:
if not needed_param.key in provided_arguments:
logging.info(
f"No argument provided for needed parameter {needed_param.key}. Substituting with {needed_param.default}.")
self.params_to_use[needed_param.key] = needed_param.default
if needed_param.key.endswith("_REF") and needed_param.key in provided_arguments:
logging.debug(
f"The parameter {needed_param.key} points to the repository {needed_param.repository}.")
# If a commit has already been resolved and added to the params_to_use, it will be propagated to the next test in the test suite.
# To avoid resolving the same commit again, simply check if the key has the same length as the output of _resolve_branch_ref_to_commit.
# The whole process assumes that all components use the same refs.
Comment thread
PranjalManhgaye marked this conversation as resolved.
Resolved
if len(self.params_to_use[needed_param.key]) == 40:
logging.debug(
f"Git ref {self.params_to_use[needed_param.key]} is 40 characters long and probably already a commit.")
component = case.component
if component.name in self.build_arguments_by_component:
continue

component_args: Dict[str, str] = {}
for param in component.parameters:
if param.key not in provided_arguments:
logging.info(
f"No argument provided for needed parameter {param.key} "
f"on component {component.name}. "
f"Substituting with {param.default}.")
value = param.default
else:
self.params_to_use[needed_param.key] = self._resolve_branch_ref_to_commit(
needed_param.repository, self.params_to_use[needed_param.key])
value = provided_arguments[param.key]

if param.key.endswith("_REF") and param.key in provided_arguments:
logging.debug(
f"The parameter {param.key} on component {component.name} "
f"points to the repository {param.repository}.")
# If a commit has already been resolved and added to the build
# arguments, it will be propagated to the next test in the test suite.
# To avoid resolving the same commit again, simply check if the key
# has the same length as the output of _resolve_branch_ref_to_commit.
# The whole process assumes that all components use the same refs.
Comment on lines +311 to +315
if value and len(value) == 40:
logging.debug(
f"Git ref {value} is 40 characters long and probably already a commit.")
elif value and len(value) != 40:
value = self._resolve_branch_ref_to_commit(
param.repository, value)
provided_arguments[param.key] = value

component_args[param.key] = value

self.build_arguments_by_component[component.name] = component_args

self.params_to_use = self._merged_build_arguments()

def _primary_case(self) -> Case:
return self.case_combination.cases[0]

def _primary_platform(self) -> str | None:
return self.build_arguments_by_component[
self._primary_case().component.name
].get("PLATFORM")

def _dockerfile_context_relative(self, platform: str) -> Path:
return Path("..") / "tests" / "dockerfiles" / Path(platform)

def _merged_build_arguments(self) -> Dict[str, str]:
merged: Dict[str, str] = {}
for case in self.case_combination.cases:
component_args = self.build_arguments_by_component[case.component.name]
for key, value in component_args.items():
if key in merged and merged[key] != value:
logging.warning(
f"Build argument '{key}' differs between components "
f"({merged[key]!r} vs {value!r}); using {merged[key]!r} "
f"for shared build stages.")
elif key not in merged:
merged[key] = value
return merged

def _validate_dockerfile_platform(self, platform: str, context: str = "") -> None:
# Use an absolute path here only for validation that the requested
# dockerfile context exists on the machine running the system tests.
dockerfile_context = PRECICE_TESTS_DIR / "dockerfiles" / Path(platform)
if not dockerfile_context.exists():
suffix = f" for {context}" if context else ""
raise ValueError(
f"The path {dockerfile_context.resolve()} resulting from argument "
f"PLATFORM={platform}{suffix} could not be found in the system")

def __get_docker_services(self) -> Dict[str, str]:
"""
Expand All @@ -319,26 +369,38 @@ def __get_docker_services(self) -> Dict[str, str]:
Returns:
A dictionary of rendered services per case name.
"""
platform_requested = self.params_to_use.get("PLATFORM")
seen_platforms: set[str] = set()
for case in self.case_combination.cases:
platform = self.build_arguments_by_component[case.component.name].get(
"PLATFORM")
if platform and platform not in seen_platforms:
self._validate_dockerfile_platform(
platform, f"component {case.component.name}")
seen_platforms.add(platform)

primary_platform = self._primary_platform()
if not primary_platform:
raise KeyError("Please specify a PLATFORM argument")
self.dockerfile_context = (
PRECICE_TESTS_DIR / "dockerfiles" / Path(primary_platform)
)

# Use an absolute path here only for validation that the requested
# dockerfile context exists on the machine running the system tests.
Comment thread
PranjalManhgaye marked this conversation as resolved.
Resolved
self.dockerfile_context = PRECICE_TESTS_DIR / "dockerfiles" / Path(platform_requested)
if not self.dockerfile_context.exists():
raise ValueError(
f"The path {self.dockerfile_context.resolve()} resulting from argument PLATFORM={platform_requested} could not be found in the system")
def render_service_template_per_case(case: Case) -> str:
build_arguments = self.build_arguments_by_component[case.component.name]
platform = build_arguments.get("PLATFORM")
if not platform:
raise KeyError(
f"Please specify a PLATFORM argument for component "
f"{case.component.name}")

def render_service_template_per_case(case: Case, params_to_use: Dict[str, str]) -> str:
# Inside the individual system test directory (`self.system_test_dir`)
# we copy a full `tests/` tree into the parent run directory
# (see __copy_tools_and_tests). From the point of view of the system test
# directory we therefore need to go one level up to reach the
# shared `tests/` folder:
# <run_directory>/tests/dockerfiles/<PLATFORM>
# ^-------------^ parent of self.system_test_dir
dockerfile_context_relative = (
Comment thread
PranjalManhgaye marked this conversation as resolved.
Resolved
Path("..") / "tests" / "dockerfiles" / Path(platform_requested)
)
dockerfile_context_relative = self._dockerfile_context_relative(platform)

render_dict = {
# Use a relative path to the *parent* run directory so that
Expand All @@ -347,8 +409,8 @@ def render_service_template_per_case(case: Case, params_to_use: Dict[str, str])
# runner's absolute paths.
'run_directory': "..",
'tutorial_folder': self.tutorial_folder,
'build_arguments': params_to_use,
'params': params_to_use,
'build_arguments': build_arguments,
'params': build_arguments,
'case_folder': case.path,
'run': case.run_cmd,
'dockerfile_context': dockerfile_context_relative,
Expand All @@ -359,8 +421,7 @@ def render_service_template_per_case(case: Case, params_to_use: Dict[str, str])

rendered_services = {}
for case in self.case_combination.cases:
rendered_services[case.name] = render_service_template_per_case(
case, self.params_to_use)
rendered_services[case.name] = render_service_template_per_case(case)
return rendered_services

def __get_docker_compose_file(self):
Expand All @@ -377,9 +438,8 @@ def __get_docker_compose_file(self):
# used as a build context path and does not need to be
# absolute – it will be resolved relative to the system test
# directory.
'dockerfile_context': (
Path("..") / "tests" / "dockerfiles" / Path(self.params_to_use.get("PLATFORM"))
),
'dockerfile_context': self._dockerfile_context_relative(
self._primary_platform()),
'precice_output_folder': PRECICE_REL_OUTPUT_DIR,
}
jinja_env = Environment(loader=FileSystemLoader(PRECICE_TESTS_DIR))
Expand All @@ -400,9 +460,8 @@ def __get_field_compare_compose_file(self):
# used as a build context path and does not need to be
# absolute – it will be resolved relative to the system test
# directory.
'dockerfile_context': (
Path("..") / "tests" / "dockerfiles" / Path(self.params_to_use.get("PLATFORM"))
),
'dockerfile_context': self._dockerfile_context_relative(
self._primary_platform()),
}
jinja_env = Environment(loader=FileSystemLoader(PRECICE_TESTS_DIR))
template = jinja_env.get_template(
Expand Down Expand Up @@ -1075,6 +1134,9 @@ def __prepare_for_run(self, run_directory: Path):
host_uid, host_gid = self.__get_uid_gid()
self.params_to_use['PRECICE_UID'] = host_uid
self.params_to_use['PRECICE_GID'] = host_gid
for component_args in self.build_arguments_by_component.values():
component_args['PRECICE_UID'] = host_uid
component_args['PRECICE_GID'] = host_gid

def run(self, run_directory: Path):
"""
Expand Down