-
Notifications
You must be signed in to change notification settings - Fork 0
feat: add fwts test to imgtests lib #400
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
4d45ad1
feat: add fwts test to imgtests lib
X1pster c515ad5
fix: possible errors in fwts results parsing
X1pster bd77a20
feat: add unit tests for fwts
X1pster 8c5115e
Merge branch 'main' into port_fwts_test
Artanias 0ff633a
fix: corrects unit tests.
Artanias 6c9b5c8
Revert "fix: corrects unit tests."
Artanias e1a0dfd
fix: test statuses count in fwts
X1pster 6251f09
refactor: simplify FwtsTest constructor by removing subsystems parame…
Artanias 494559f
refactor: fix fwts.py metrics_to_json function typing.
Artanias File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,14 +1,126 @@ | ||
| import logging | ||
| from typing import TYPE_CHECKING | ||
| import re | ||
| from typing import TYPE_CHECKING, Any, NamedTuple | ||
|
|
||
| from imgtests.exec.base_util import GenericUtil | ||
| from imgtests.results_adapter import AdapterResult | ||
| from imgtests.types import MetricSample | ||
|
|
||
| if TYPE_CHECKING: | ||
| from imgtests.exec.exec import SSHClient | ||
| from imgtests.exec.exec import ExecResult, SSHClient | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| class FwtsResult(NamedTuple): | ||
| tests: list[dict[str, Any]] | ||
| summary: dict[str, int] | ||
|
|
||
|
|
||
| class Fwts(GenericUtil): | ||
| def __init__(self, ssh_client: SSHClient | None = None) -> None: | ||
| super().__init__("fwts", ssh_client) | ||
|
|
||
| def run(self) -> tuple[ExecResult, FwtsResult]: | ||
| result = self() | ||
| parsed = ( | ||
| self.parse_metrics(result.stdout) | ||
| if result.stdout | ||
| else FwtsResult( | ||
| tests=[], | ||
| summary={"passed": 0, "failed": 0, "skipped": 0, "aborted": 0}, | ||
| ) | ||
| ) | ||
| return result, parsed | ||
|
|
||
| @staticmethod | ||
| def parse_metrics(raw_output: str) -> FwtsResult: # noqa: PLR0912, C901 | ||
| statuses: dict[str, int] = {"passed": 0, "failed": 0, "skipped": 0, "aborted": 0} | ||
| tests: list[dict[str, Any]] = [] | ||
|
|
||
| sections = re.split(r"^Test: ", raw_output, flags=re.MULTILINE) | ||
|
|
||
| for section in sections[1:]: | ||
| lines = section.splitlines() | ||
| test_name = lines[0].strip().rstrip(".") | ||
| subtotal: dict[str, int] = {"passed": 0, "failed": 0, "skipped": 0, "aborted": 0} | ||
|
|
||
| for line in lines[1:]: | ||
| stripped = line.strip().lower() | ||
|
|
||
| if stripped in ("test aborted", "test aborted."): | ||
| subtotal["aborted"] += 1 | ||
| elif stripped in ("test skipped", "test skipped."): | ||
| subtotal["skipped"] += 1 | ||
| else: | ||
| for match in re.finditer( | ||
| r"(\d+)?\s+(passed|failed|skipped|aborted|info only)", | ||
| stripped.lower(), | ||
| ): | ||
| if match.group(1) is not None: | ||
| count = int(match.group(1)) | ||
| status = match.group(2) | ||
| if status == "info only": | ||
| """ | ||
| Info only tests are tests that just check some system info, e.g. | ||
| Gather kernel system information. 1 info only | ||
| count as passed | ||
| """ | ||
| subtotal["passed"] += count | ||
| elif status in subtotal: | ||
| subtotal[status] += count | ||
| else: | ||
| status = match.group(2) | ||
| if status == "info only": | ||
| subtotal["passed"] += 1 | ||
| elif status in subtotal: | ||
| subtotal[status] += 1 | ||
|
|
||
| if all(v == 0 for v in subtotal.values()): | ||
| subtotal["skipped"] += 1 | ||
|
|
||
| for key, value in subtotal.items(): | ||
| statuses[key] += value | ||
| tests.append({"name": test_name, "subtests": subtotal}) | ||
|
|
||
| return FwtsResult(tests=tests, summary=statuses) | ||
|
|
||
| @staticmethod | ||
| def metrics_to_json(metrics: FwtsResult) -> AdapterResult: | ||
| raw: dict[str, Any] = {"fwts_tests": metrics.tests, "fwts_summary": metrics.summary} | ||
| return Fwts.split_result(raw_metrics=raw) | ||
|
|
||
| @staticmethod | ||
| def split_result(raw_metrics: dict[str, Any]) -> AdapterResult: | ||
| if not raw_metrics: | ||
| return AdapterResult(tool="fwts", test_type={}, time={}, metrics={}) | ||
|
|
||
| tests = raw_metrics.get("fwts_tests", []) | ||
| summary = raw_metrics.get("fwts_summary", {}) | ||
|
|
||
| test_type = {"type": "firmware"} | ||
| time = {} | ||
|
Artanias marked this conversation as resolved.
|
||
| metrics: dict[str, Any] = {str(i): t for i, t in enumerate(tests)} | ||
| metrics["summary"] = summary | ||
|
|
||
| return AdapterResult(tool="fwts", test_type=test_type, time=time, metrics=metrics) | ||
|
|
||
|
|
||
| def fwts_metrics_to_samples( | ||
| stage_name: str, | ||
| subsystem: str, | ||
| metrics: dict[str, Any], | ||
| ) -> list[MetricSample]: | ||
| samples: list[MetricSample] = [] | ||
| summary = metrics.get("summary", {}) | ||
| for key, label in ( | ||
| ("passed", "Passed"), | ||
| ("failed", "Failed"), | ||
| ("skipped", "Skipped"), | ||
| ("aborted", "Aborted"), | ||
| ): | ||
| value = summary.get(key, 0) | ||
| samples.append( | ||
| MetricSample(stage_name, subsystem, f"fwts.{key}", float(value), label=label), | ||
| ) | ||
| return samples | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| from typing import TYPE_CHECKING | ||
|
|
||
| from imgtests.exec.loaders import Fwts | ||
| from imgtests.planning import AbstractRunnableManyTimesTest | ||
| from imgtests.types import Subsystem, TestResult, TestStatus | ||
|
|
||
| if TYPE_CHECKING: | ||
| from collections.abc import Iterable | ||
| from concurrent.futures import ThreadPoolExecutor | ||
|
|
||
| from imgtests.exec.exec import SSHClient | ||
|
|
||
|
|
||
| class FwtsTest(AbstractRunnableManyTimesTest): | ||
| def __init__( | ||
| self, | ||
| iterations: int = 1, | ||
| ) -> None: | ||
| super().__init__("FWTS firmware tests.", frozenset({Subsystem.SYSTEM}), iterations) | ||
|
|
||
| def _run( | ||
| self, | ||
| executor: ThreadPoolExecutor, | ||
| client: SSHClient | None, | ||
| iterations: int, | ||
| ) -> Iterable[TestResult]: | ||
| fwts = Fwts(client) | ||
|
|
||
| for _ in range(iterations): | ||
| future = executor.submit(fwts.run) | ||
| result, parsed = future.result() | ||
|
|
||
| if result.returncode and not parsed.tests: | ||
| self.logger.error("FWTS test BROKEN (returncode=%d)", result.returncode) | ||
| yield TestResult(status=TestStatus.BROKEN) | ||
| continue | ||
|
|
||
| tests_passed = parsed.summary.get("passed", 0) | ||
| tests_failed = parsed.summary.get("failed", 0) | ||
| tests_skipped = parsed.summary.get("skipped", 0) | ||
| tests_aborted = parsed.summary.get("aborted", 0) | ||
|
|
||
| if tests_failed > 0: | ||
| self.logger.error( | ||
| "FWTS test FAILED (%d passed, %d failed, %d skipped, %d aborted)", | ||
| tests_passed, | ||
| tests_failed, | ||
| tests_skipped, | ||
| tests_aborted, | ||
| ) | ||
| yield TestResult( | ||
| status=TestStatus.FAILED, | ||
| metrics=fwts.metrics_to_json(parsed), | ||
| command="fwts", | ||
| ) | ||
| else: | ||
| self.logger.info( | ||
| "FWTS test PASSED (%d passed, %d failed, %d skipped, %d aborted)", | ||
| tests_passed, | ||
| tests_failed, | ||
| tests_skipped, | ||
| tests_aborted, | ||
| ) | ||
| yield TestResult( | ||
| status=TestStatus.PASSED, | ||
| metrics=fwts.metrics_to_json(parsed), | ||
| command="fwts", | ||
| ) |
Empty file.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.