Skip to content

Commit

Permalink
Merge pull request #105 from myakove/network-get_interface_status
Browse files Browse the repository at this point in the history
Network: Add get_interface_status function
  • Loading branch information
petr-balogh authored Jun 19, 2018
2 parents 302e398 + dd95420 commit 5cc53dc
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 9 deletions.
27 changes: 19 additions & 8 deletions rrmngmnt/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
6 changes: 5 additions & 1 deletion tests/test_network.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
}
Expand Down Expand Up @@ -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):

Expand Down

0 comments on commit 5cc53dc

Please sign in to comment.