Skip to content

Commit

Permalink
Add primitives for loading and saving step results (#2978)
Browse files Browse the repository at this point in the history
  • Loading branch information
happz committed Aug 12, 2024
1 parent 718c2e3 commit 9d51a63
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 8 deletions.
40 changes: 39 additions & 1 deletion tmt/steps/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import re
import shutil
import textwrap
from collections.abc import Iterable, Iterator
from collections.abc import Iterable, Iterator, Sequence
from re import Pattern
from typing import (
TYPE_CHECKING,
Expand Down Expand Up @@ -60,6 +60,7 @@
import tmt.steps.discover
import tmt.steps.execute
from tmt.base import Plan
from tmt.result import BaseResult
from tmt.steps.provision import Guest


Expand Down Expand Up @@ -252,6 +253,9 @@ class _RawStepData(TypedDict, total=False):
#: A type variable representing a return value of plugin's ``go()`` method.
PluginReturnValueT = TypeVar('PluginReturnValueT')

#: A type variable representing a result type.
ResultT = TypeVar('ResultT', bound='BaseResult')


@dataclasses.dataclass
class StepData(
Expand Down Expand Up @@ -687,6 +691,40 @@ def save(self) -> None:
}
self.write(Path('step.yaml'), tmt.utils.dict_to_yaml(content))

def _load_results(
self,
result_class: type[ResultT],
allow_missing: bool = False) -> list[ResultT]:
""" Load results of this step from the workdir """

try:
raw_results: list[Any] = tmt.utils.yaml_to_list(self.read(Path('results.yaml')))

return [
result_class.from_serialized(raw_result) for raw_result in raw_results
]

except tmt.utils.FileError as exc:
if allow_missing:
self.debug(f'{self.__class__.__name__} results not found.', level=2)
return []

raise GeneralError('Cannot load step results.') from exc

except Exception as exc:
raise GeneralError('Cannot load step results.') from exc

def _save_results(self, results: Sequence['BaseResult']) -> None:
""" Save results of this step to the workdir """

try:
raw_results = [result.to_serialized() for result in results]

self.write(Path('results.yaml'), tmt.utils.dict_to_yaml(raw_results))

except Exception as exc:
raise GeneralError('Cannot save step results.') from exc

def wake(self) -> None:
""" Wake up the step (process workdir and command line) """
# Cleanup possible old workdir if called with --force, but not
Expand Down
11 changes: 4 additions & 7 deletions tmt/steps/execute/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -986,17 +986,14 @@ def __init__(
def load(self) -> None:
""" Load test results """
super().load()
try:
results = tmt.utils.yaml_to_list(self.read(Path('results.yaml')))
self._results = [Result.from_serialized(data) for data in results]
except tmt.utils.FileError:
self.debug('Test results not found.', level=2)

self._results = self._load_results(Result, allow_missing=True)

def save(self) -> None:
""" Save test results to the workdir """
super().save()
results = [result.to_serialized() for result in self.results()]
self.write(Path('results.yaml'), tmt.utils.dict_to_yaml(results))

self._save_results(self.results())

def wake(self) -> None:
""" Wake up the step (process workdir and command line) """
Expand Down

0 comments on commit 9d51a63

Please sign in to comment.