Skip to content

Commit

Permalink
Reorganize Iperf error reporting
Browse files Browse the repository at this point in the history
  • Loading branch information
Kuba314 committed Nov 22, 2023
1 parent be9f9a0 commit 3b9391b
Showing 1 changed file with 34 additions and 38 deletions.
72 changes: 34 additions & 38 deletions lnst/Tests/Iperf.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import logging
import signal
import subprocess
import json
from json.decoder import JSONDecodeError
Expand Down Expand Up @@ -34,57 +35,52 @@ def run(self):

try:
stdout, stderr = server.communicate()
stdout = stdout.decode()
stderr = stderr.decode()
except KeyboardInterrupt:
pass
server.send_signal(signal.SIGINT)
stdout, stderr = server.communicate()
finally:
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):
Expand Down

0 comments on commit 3b9391b

Please sign in to comment.