Skip to content

Commit

Permalink
Merge pull request labgrid-project#1490 from honeytreelabs/dev-fix-de…
Browse files Browse the repository at this point in the history
…fault-interface-device-name

fix: match interface regex in ShellDriver
  • Loading branch information
Emantor authored Aug 30, 2024
2 parents 1ab0abd + 288a0d0 commit a9a74af
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
2 changes: 1 addition & 1 deletion labgrid/driver/shelldriver.py
Original file line number Diff line number Diff line change
Expand Up @@ -567,7 +567,7 @@ def get_default_interface_device_name(self, version=4):

regex = r"""default\s+via # leading strings
\s+\S+ # IP address
\s+dev\s+(\w+) # interface"""
\s+dev\s+([\w\.-]+) # interface"""

default_route = self._run_check(f"ip -{version} route list default")
matches = re.findall(regex, "\n".join(default_route), re.X)
Expand Down
35 changes: 35 additions & 0 deletions tests/test_shelldriver.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,18 @@ def test_run_with_timeout(self, target_with_fakeconsole, mocker):
res = d.run("test")
assert res == (['success'], [], 0)

def test_default_interface_device_name(self, target_with_fakeconsole, mocker):
fake_default_route_show = "default via 10.0.2.2 dev br-lan src 10.0.2.15"

t = target_with_fakeconsole
d = ShellDriver(t, "shell", prompt="dummy", login_prompt="dummy", username="dummy")
d.on_activate = mocker.MagicMock()
d = t.get_driver("ShellDriver")
d._run = mocker.MagicMock(return_value=([fake_default_route_show], [], 0))

res = d.get_default_interface_device_name()
assert res == "br-lan"

def test_get_ip_addresses(self, target_with_fakeconsole, mocker):
fake_ip_addr_show = r"""
18: br-lan.42 inet 192.168.42.1/24 brd 192.168.42.255 scope global br-lan.42\ valid_lft forever preferred_lft forever
Expand All @@ -62,3 +74,26 @@ def test_get_ip_addresses(self, target_with_fakeconsole, mocker):

res = d.get_ip_addresses("br-lan.42")
assert res[0] == IPv4Interface("192.168.42.1/24")

def test_get_ip_addresses_default(self, target_with_fakeconsole, mocker):
t = target_with_fakeconsole
d = ShellDriver(t, "shell", prompt="dummy", login_prompt="dummy", username="dummy")
d.on_activate = mocker.MagicMock()
d = t.get_driver("ShellDriver")
d._run = mocker.MagicMock()
d._run.side_effect = [
(["default via 192.168.42.255 dev br-lan.42 src 192.168.42.1"], [], 0),
(
[
r"""
18: br-lan.42 inet 192.168.42.1/24 brd 192.168.42.255 scope global br-lan.42\ valid_lft forever preferred_lft forever
18: br-lan.42 inet6 fe80::9683:c4ff:fea6:fb6b/64 scope link \ valid_lft forever preferred_lft forever
"""
],
[],
0,
),
]

res = d.get_ip_addresses()
assert res[0] == IPv4Interface("192.168.42.1/24")

0 comments on commit a9a74af

Please sign in to comment.