On Fri, Apr 12, 2024 at 1:11 PM Luca Vizzarro <[email protected]> wrote:
>
> Add a new TestPmdPort data structure to represent the output
> returned by `show port info`, which is implemented as part of
> TestPmdShell.
>
> The TestPmdPort data structure and its derived classes are modelled
> based on the relevant testpmd source code.
>
> This implementation makes extensive use of regular expressions, which
> all parse individually. The rationale behind this is to lower the risk
> of the testpmd output changing as part of development. Therefore
> minimising breakage.
>
> Bugzilla ID: 1407
>
> Signed-off-by: Luca Vizzarro <[email protected]>
> Reviewed-by: Paul Szczepanek <[email protected]>
<snip>
> +@dataclass
> +class TestPmdPort(TextParser):
This and the classes above are missing docstrings.
<snip>
> @@ -225,6 +664,38 @@ def set_forward_mode(self, mode: TestPmdForwardingModes,
> verify: bool = True):
> f"Test pmd failed to set fwd mode to {mode.value}"
> )
>
> + def show_port_info_all(self) -> list[TestPmdPort]:
> + """Returns the information of all the ports."""
Can we add sample output so that the format of what we're trying to
parse is clear?
> + output = self.send_command("show port info all")
> +
> + ports = []
> + iter = re.finditer(r"\*+.+\*+", output)
> + if next(iter, False): # we are slicing retrospectively, skip first
> block
> + start_pos = 0
> + for block in iter:
> + end_pos = block.start()
> + ports.append(TestPmdPort.parse(output[start_pos:end_pos]))
> + start_pos = end_pos
> +
> + ports.append(TestPmdPort.parse(output[start_pos:]))
> +
> + return ports
Can this be done the same way it's done in the last commit?
iter = re.finditer(r"(^ #*.+#*$[^#]+)^ #*$", output, re.MULTILINE)
return [TestPmdPortStats.parse(block.group(1)) for block in iter]
Looks much better.
> +
> + def show_port_info(self, port_id: int) -> TestPmdPort:
> + """Returns the given port information.
> +
> + Args:
> + port_id: The port ID to gather information for.
> +
> + Raises:
> + InteractiveCommandExecutionError: If `port_id` is invalid.
> + """
> + output = self.send_command(f"show port info {port_id}",
> skip_first_line=True)
> + if output.startswith("Invalid port"):
> + raise InteractiveCommandExecutionError("invalid port given")
> +
> + return TestPmdPort.parse(output)
> +
> def close(self) -> None:
> """Overrides :meth:`~.interactive_shell.close`."""
> self.send_command("quit", "")
> --
> 2.34.1
>