Skip to content

Commit

Permalink
Network: Add get_interface_status function
Browse files Browse the repository at this point in the history
  • Loading branch information
myakove committed Jun 18, 2018
1 parent 302e398 commit dd95420
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 dd95420

Please sign in to comment.