Skip to content

Commit

Permalink
shelldriver: allow dashes and dots in device names
Browse files Browse the repository at this point in the history
The `get_ip_addresses` may be called with a specific device and returns
alls IP addresses on that interface. Due to a limitation in the regex,
devices including either dashes or dots aren't detected.

On the OpenWrt distributions the default device is called `br-lan`,
usually connecting ethernet ports and WiFi devices. Likewise dots are
attached to the device name to specify VLANs.

This commit extends the regex to support both dashes and dots.

Signed-off-by: Paul Spooren <[email protected]>
  • Loading branch information
aparcar committed Apr 29, 2024
1 parent fe37a35 commit 6d0b571
Show file tree
Hide file tree
Showing 2 changed files with 18 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 @@ -596,7 +596,7 @@ def get_ip_addresses(self, device=None):
device = self.get_default_interface_device_name()

regex = r"""\d+: # leading number
\s+\w+ # interface name
\s+[\w\.-]+ # interface name
\s+inet6?\s+(\S+) # IP address, prefix
.*global # global scope, not host scope"""

Expand Down
17 changes: 17 additions & 0 deletions tests/test_shelldriver.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
from labgrid.driver import ShellDriver, ExecutionError
from labgrid.exceptions import NoDriverFoundError

from ipaddress import IPv4Interface


class TestShellDriver:
def test_instance(self, target, serial_driver):
Expand Down Expand Up @@ -45,3 +47,18 @@ def test_run_with_timeout(self, target_with_fakeconsole, mocker):
assert res == ['success']
res = d.run("test")
assert res == (['success'], [], 0)

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
18: br-lan.42 inet6 fe80::9683:c4ff:fea6:fb6b/64 scope link \ valid_lft forever preferred_lft forever
"""

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_ip_addr_show], [], 0))

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

0 comments on commit 6d0b571

Please sign in to comment.