diff --git a/lnst/Tests/Iperf.py b/lnst/Tests/Iperf.py index 7fffc3258..857063d52 100644 --- a/lnst/Tests/Iperf.py +++ b/lnst/Tests/Iperf.py @@ -32,59 +32,47 @@ def run(self): server = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, close_fds=True) - try: - stdout, stderr = server.communicate() - stdout = stdout.decode() - stderr = stderr.decode() - except KeyboardInterrupt: - pass + stdout, stderr = server.communicate() + stdout = stdout.decode().strip() + stderr = stderr.decode().strip() + + if server.returncode > 0: + msg = f"iperf {self._role} returncode = {server.returncode}" + logging.error(msg) + logging.error(f"iperf stderr: {stderr}") + self._res_data["msg"] = msg + self._res_data["stderr"] = stderr + return False 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 + 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 - try: - self._check_json_sanity() - except: - self._res_data["msg"] = "Iperf provided incomplete json data" + if not self._is_json_sane(self._res_data["data"]): + msg = "Iperf provided incomplete json data" + logging.error(msg) + logging.error(f"iperf stderr: {stderr}") + self._res_data["msg"] = msg self._res_data["data"] = stdout 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"]) 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 + def _is_json_sane(self, data: dict) -> bool: + return ( + "start" in data + and "end" in data + and len(data["intervals"]) > 0 + and "streams" in data["end"] + ) class IperfServer(IperfBase):