From dd95420d38a65b03a5c0104ff49d9aaabeb298b5 Mon Sep 17 00:00:00 2001 From: Meni Yakove Date: Mon, 18 Jun 2018 20:44:43 +0300 Subject: [PATCH] Network: Add get_interface_status function --- rrmngmnt/network.py | 27 +++++++++++++++++++-------- tests/test_network.py | 6 +++++- 2 files changed, 24 insertions(+), 9 deletions(-) diff --git a/rrmngmnt/network.py b/rrmngmnt/network.py index e8ae147..7f42f1d 100644 --- a/rrmngmnt/network.py +++ b/rrmngmnt/network.py @@ -699,11 +699,22 @@ def get_interface_speed(self, interface): Returns: str: Interface speed, or empty string if error has occurred """ - cmd = "ethtool -i {iface}".format(iface=interface) - rc, out, err = self.host.run_command(command=shlex.split(cmd)) - if rc: - logger.error("Error fetching speed from interface: %s", err) - return "" - return self._cmd( - ["cat", "/sys/class/net/{iface}/speed".format(iface=interface)] - ) + ethtool_cmd = "ethtool -i {iface}".format(iface=interface) + self._cmd(shlex.split(ethtool_cmd)) + speed_cmd = "cat /sys/class/net/{iface}/speed".format(iface=interface) + out = self._cmd(shlex.split(speed_cmd)) + return out.strip() + + def get_interface_status(self, interface): + """ + Get interface status + + Args: + interface (str): Interface name + + Returns: + str: Interface status (up/down) + """ + cmd = "cat /sys/class/net/{iface}/operstate".format(iface=interface) + out = self._cmd(shlex.split(cmd)) + return out.strip() diff --git a/tests/test_network.py b/tests/test_network.py index 7edae24..4b94572 100644 --- a/tests/test_network.py +++ b/tests/test_network.py @@ -186,7 +186,8 @@ class TestNetwork(object): 'ip link set up interface': True, 'ip link set down interface': True, "ethtool -i eth0": (0, "driver: e1000", ""), - "cat /sys/class/net/eth0/speed": (0, "1000", "") + "cat /sys/class/net/eth0/speed": (0, "1000", ""), + "cat /sys/class/net/eth0/operstate": (0, "up", "") } files = { } @@ -261,6 +262,9 @@ def if_down(self): def test_get_interface_speed(self): assert get_host().network.get_interface_speed("eth0") == "1000" + def test_get_interface_status(self): + assert get_host().network.get_interface_status("eth0") == "up" + class TestHostNameCtl(object):