From 6d0b571d687eb889f6d1f5a1500a0ce928422c28 Mon Sep 17 00:00:00 2001 From: Paul Spooren Date: Fri, 26 Apr 2024 07:31:29 +0200 Subject: [PATCH] shelldriver: allow dashes and dots in device names 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 --- labgrid/driver/shelldriver.py | 2 +- tests/test_shelldriver.py | 17 +++++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/labgrid/driver/shelldriver.py b/labgrid/driver/shelldriver.py index c8b748d5e..c9766387e 100644 --- a/labgrid/driver/shelldriver.py +++ b/labgrid/driver/shelldriver.py @@ -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""" diff --git a/tests/test_shelldriver.py b/tests/test_shelldriver.py index 0f24928ad..f0c4eecc3 100644 --- a/tests/test_shelldriver.py +++ b/tests/test_shelldriver.py @@ -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): @@ -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")