diff --git a/lnst/Tests/Iperf.py b/lnst/Tests/Iperf.py index 7fffc3258..089e78c17 100644 --- a/lnst/Tests/Iperf.py +++ b/lnst/Tests/Iperf.py @@ -33,58 +33,53 @@ def run(self): stderr=subprocess.PIPE, close_fds=True) try: - stdout, stderr = server.communicate() - stdout = stdout.decode() - stderr = stderr.decode() + server.wait() except KeyboardInterrupt: pass + finally: + stdout, stderr = server.communicate() + stdout = stdout.decode().strip() + stderr = stderr.decode().strip() - try: - self._res_data["data"] = json.loads(stdout) - except JSONDecodeError: - self._res_data["msg"] = "Error while parsing the iperf json output" - self._res_data["data"] = stdout + if server.returncode > 0: + msg = f"iperf {self._role} returncode = {server.returncode}" + logging.error(msg) + logging.error(f"iperf stderr: {stderr}") + logging.debug(f"iperf stdout: {stdout}") + self._res_data["msg"] = msg self._res_data["stderr"] = stderr - logging.error(self._res_data["msg"]) return False try: - self._check_json_sanity() - except: - self._res_data["msg"] = "Iperf provided incomplete json data" - self._res_data["data"] = stdout + self._res_data["data"] = json.loads(stdout) + except JSONDecodeError: + msg = "Error while parsing the iperf json output" + logging.error(msg) + logging.error(f"iperf stderr: {stderr}") + logging.debug(f"iperf stdout: {stdout}") + self._res_data["msg"] = msg self._res_data["stderr"] = stderr - logging.error(self._res_data["msg"]) return False - self._res_data["stderr"] = stderr - - if stderr != "": - self._res_data["msg"] = "errors reported by iperf" - logging.error(self._res_data["msg"]) - logging.error(self._res_data["stderr"]) - - if server.returncode > 0: - self._res_data["msg"] = "{} returncode = {}".format( - self._role, server.returncode) - logging.error(self._res_data["msg"]) + if not self._is_json_complete(self._res_data["data"]): + msg = "Iperf provided incomplete json data" + logging.error(msg) + logging.error(f"iperf stderr: {stderr}") + logging.debug(f"iperf stdout: {stdout}") + self._res_data["msg"] = msg + self._res_data["stderr"] = stderr return False return True - def _check_json_sanity(self): - data = self._res_data["data"] - if "start" not in data: - raise Exception() - - if "end" not in data: - raise Exception() - - if len(data["intervals"]) == 0: - raise Exception() - - if "streams" not in data["end"]: - raise Exception + @staticmethod + def _is_json_complete(data: dict) -> bool: + return ( + "start" in data + and "end" in data + and len(data["intervals"]) > 0 + and "streams" in data["end"] + ) class IperfServer(IperfBase):