Skip to contentSkip to conversation

auto-t: return existing instance from HostapdCLI

PATCH In review James Prestwood 2 participants 5 messages Aug 13, 2021 – Aug 14, 2021

lore
Share & tools
lore
#0 lore raw

PATCH 1/4 auto-t: return existing instance from HostapdCLI

This addresses the TODO where HostapdCLI was creating separate objects each time HostapdCLI was called. This was worked around by manually setting the important members but instead the class can be re-worked to act as somewhat of a singleton, per-config at least.

If there is no HostapdCLI instance for a given config one is created and initialized. Subsequent HostapdCLI calls (for the same config) will be returned the same object rather than a new one.

---
 autotests/util/hostapd.py | 48 +++++++++++++++++++++++----------------
 tools/test-runner         |  4 ++++
 2 files changed, 33 insertions(+), 19 deletions(-)
autotests/util/hostapd.py +29 -19
index a61b2d39..aabc997b 100644
@@ -28,10 +28,28 @@ chan_freq_map = [
 ctrl_count = 0
 mainloop = GLib.MainLoop()
 
-class HostapdCLI:
-    def _init_hostapd(self, config=None):
+class HostapdCLI(object):
+    _instances = {}
+
+    def __new__(cls, config=None, *args, **kwargs):
+        hapd = ctx.hostapd[config]
+
+        if not config:
+            config = hapd.config
+
+        if not config in cls._instances.keys():
+            cls._instances[config] = object.__new__(cls, *args, **kwargs)
+            cls._instances[config]._initialized = False
+
+        return cls._instances[config]
+
+    def _init_hostapd(self, config):
         global ctrl_count
-        interface = None
+
+        if self._initialized:
+            return
+
+        self._initialized = True
         self.ctrl_sock = None
 
         if not ctx.hostapd:
@@ -45,21 +63,6 @@ class HostapdCLI:
         if not hasattr(self, '_hostapd_restarted'):
             self._hostapd_restarted = False
 
-        #
-        # TODO: In theory some type of instance singleton could be created for
-        #       HostapdCLI where test-runner initializes but any subsequent
-        #       call to HostapdCLI (with the same config) returns the same
-        #       object that test-runner has. This would avoid setting these
-        #       variables.
-        #
-        if hapd.cli:
-            self.ifname = hapd.cli.ifname
-            self.ctrl_sock = hapd.cli.ctrl_sock
-            self.cmdline = hapd.cli.cmdline
-            self.interface = hapd.intf
-            self.config = hapd.config
-            return
-
         self.interface = hapd.intf
         self.config = hapd.config
 
@@ -83,7 +86,7 @@ class HostapdCLI:
 
         ctrl_count = ctrl_count + 1
 
-    def __init__(self, config=None):
+    def __init__(self, config=None, *args, **kwargs):
         self._init_hostapd(config)
 
     def _poll_event(self, event):
@@ -137,6 +140,13 @@ class HostapdCLI:
     def __del__(self):
         self._del_hostapd()
 
+        HostapdCLI._instances[self.config] = None
+
+        # Check if this is the final instance
+        destroy = len([hapd for hapd in HostapdCLI._instances.values() if hapd is not None]) == 0
+        if destroy:
+            HostapdCLI._instances = {}
+
     def set_value(self, key, value):
         cmd = self.cmdline + ['set', key, value]
         ctx.start_process(cmd, wait=True)
tools/test-runner +4 -0
index 0ca50ddb..5a8006a5 100755
@@ -545,6 +545,10 @@ class Hostapd:
 		except:
 			print("Failed to remove %s" % self.global_ctrl_iface)
 
+		for hapd in self.instances:
+			hapd.cli._instances[hapd.config] = None
+			hapd.cli = None
+
 		self.instances = None
 
 		# Hostapd may have already been stopped
Signature · 1 line
2.31.1
#1 lore raw

PATCH 2/4 auto-t: iwd.py: clean up StationDebug

Initialize this once and reference that for each method/property

---
 autotests/util/iwd.py | 32 +++++++++++---------------------
 1 file changed, 11 insertions(+), 21 deletions(-)
autotests/util/iwd.py +11 -21
index 737b2beb..54157b00 100755
@@ -248,6 +248,12 @@ class StationDebug(IWDDBusAbstract):
     def autoconnect(self):
         return self._properties['AutoConnect']
 
+    def connect_bssid(self, address):
+        self._iface.ConnectBssid(dbus.ByteArray.fromhex(address.replace(':', '')))
+
+    def roam(self, address):
+        self._iface.Roam(dbus.ByteArray.fromhex(address.replace(':', '')))
+
     def scan(self, frequencies):
         frequencies = dbus.Array([dbus.UInt16(f) for f in frequencies])
         self._iface.Scan(frequencies)
@@ -278,9 +284,11 @@ class Device(IWDDBusAbstract):
         self._wps_manager_if = None
         self._station_if = None
         self._station_props = None
-        self._station_debug = None
+
         IWDDBusAbstract.__init__(self, *args, **kwargs)
 
+        self._station_debug = StationDebug(*args, **kwargs)
+
     @property
     def _wps_manager(self):
         if self._wps_manager_if is None:
@@ -397,16 +405,10 @@ class Device(IWDDBusAbstract):
 
     @property
     def autoconnect(self):
-        if not self._station_debug:
-            self._station_debug = StationDebug(self._object_path)
-
         return self._station_debug.autoconnect
 
     @autoconnect.setter
     def autoconnect(self, value):
-        if not self._station_debug:
-            self._station_debug = StationDebug(self._object_path)
-
         self._station_debug._prop_proxy.Set(IWD_STATION_DEBUG_INTERFACE,
                                             'AutoConnect', value)
 
@@ -602,27 +604,15 @@ class Device(IWDDBusAbstract):
         self._prop_proxy.Set(IWD_DEVICE_INTERFACE, 'Mode', 'station')
 
     def connect_bssid(self, address):
-        self._station_debug_if = dbus.Interface(self._bus.get_object(IWD_SERVICE,
-                                                self.device_path),
-                                                IWD_STATION_DEBUG_INTERFACE)
-        self._station_debug_if.ConnectBssid(dbus.ByteArray.fromhex(address.replace(':', '')))
+        self._station_debug.connect_bssid(address)
 
     def roam(self, address):
-        self._station_debug_if = dbus.Interface(self._bus.get_object(IWD_SERVICE,
-                                                self.device_path),
-                                                IWD_STATION_DEBUG_INTERFACE)
-        self._station_debug_if.Roam(dbus.ByteArray.fromhex(address.replace(':', '')))
+        self._station_debug.roam(address)
 
     def debug_scan(self, frequencies):
-        if not self._station_debug:
-            self._station_debug = StationDebug(self._object_path)
-
         self._station_debug.scan(frequencies)
 
     def wait_for_event(self, event, timeout=10):
-        if not self._station_debug:
-            self._station_debug = StationDebug(self._object_path)
-
         self._station_debug.wait_for_event(event, timeout)
 
     def __str__(self, prefix = ''):
Signature · 1 line
2.31.1
#2 lore raw

PATCH 3/4 auto-t: turn off scan address randomization in testHotspot

In reply to James Prestwood

This (hopefully) will make this test pass better on slower machines. In addition the mechanism of copying over separate main.conf files was changed (rather than echo'ing the option into /tmp/main.conf)

---
 autotests/testHotspot/anqp_delay_test.py  | 5 ++---
 autotests/testHotspot/anqp_disabled.conf  | 5 +++++
 autotests/testHotspot/anqp_enabled.conf   | 5 +++++
 autotests/testHotspot/autoconnect_test.py | 5 ++---
 autotests/testHotspot/hessid_test.py      | 5 ++---
 autotests/testHotspot/hotspot_test.py     | 5 ++---
 autotests/testHotspot/roaming_test.py     | 5 ++---
 7 files changed, 20 insertions(+), 15 deletions(-)
 create mode 100644 autotests/testHotspot/anqp_disabled.conf
 create mode 100644 autotests/testHotspot/anqp_enabled.conf
autotests/testHotspot/anqp_delay_test.py +2 -3
index 323e1fd3..04e5a1d7 100644
@@ -5,8 +5,8 @@ import sys
 import os
 
 sys.path.append('../util')
-import iwd
 from iwd import IWD
+from iwd import IWD_CONFIG_DIR
 from iwd import PSKAgent
 from iwd import NetworkType
 from hostapd import HostapdCLI
@@ -87,8 +87,7 @@ class Test(unittest.TestCase):
     @classmethod
     def setUpClass(cls):
         IWD.copy_to_hotspot('example.conf')
-        conf = '[General]\nDisableANQP=0\n'
-        os.system('echo "%s" > /tmp/main.conf' % conf)
+        IWD.copy_to_storage('anqp_enabled.conf', storage_dir=IWD_CONFIG_DIR, name='main.conf')
 
     @classmethod
     def tearDownClass(cls):
autotests/testHotspot/anqp_disabled.conf new file +5 -0
new file mode 100644
index 00000000..b18f66d7
@@ -0,0 +1,5 @@
+[General]
+DisableANQP=1
+
+[Scan]
+DisableMacAddressRandomization=true
autotests/testHotspot/anqp_enabled.conf new file +5 -0
new file mode 100644
index 00000000..c66a63e0
@@ -0,0 +1,5 @@
+[General]
+DisableANQP=0
+
+[Scan]
+DisableMacAddressRandomization=true
autotests/testHotspot/autoconnect_test.py +2 -3
index 354bf61b..777a0670 100644
@@ -5,8 +5,8 @@ import sys
 import os
 
 sys.path.append('../util')
-import iwd
 from iwd import IWD
+from iwd import IWD_CONFIG_DIR
 from iwd import NetworkType
 from hostapd import HostapdCLI
 import testutil
@@ -109,8 +109,7 @@ class Test(unittest.TestCase):
     def setUpClass(cls):
         IWD.copy_to_hotspot('autoconnect.conf')
         IWD.copy_to_storage('ssidWPA2-1.psk')
-        conf = '[General]\nDisableANQP=0\n'
-        os.system('echo "%s" > /tmp/main.conf' % conf)
+        IWD.copy_to_storage('anqp_enabled.conf', storage_dir=IWD_CONFIG_DIR, name='main.conf')
 
     @classmethod
     def tearDownClass(cls):
autotests/testHotspot/hessid_test.py +2 -3
index 5d3954b9..88e750de 100644
@@ -5,8 +5,8 @@ import sys
 import os
 
 sys.path.append('../util')
-import iwd
 from iwd import IWD
+from iwd import IWD_CONFIG_DIR
 from iwd import PSKAgent
 from iwd import NetworkType
 from hostapd import HostapdCLI
@@ -50,8 +50,7 @@ class Test(unittest.TestCase):
     @classmethod
     def setUpClass(cls):
         IWD.copy_to_hotspot('hessid.conf')
-        conf = '[General]\nDisableANQP=1\n'
-        os.system('echo "%s" > /tmp/main.conf' % conf)
+        IWD.copy_to_storage('anqp_disabled.conf', storage_dir=IWD_CONFIG_DIR, name='main.conf')
 
     @classmethod
     def tearDownClass(cls):
autotests/testHotspot/hotspot_test.py +2 -3
index 460ff89f..f49477dc 100644
@@ -5,8 +5,8 @@ import sys
 import os
 
 sys.path.append('../util')
-import iwd
 from iwd import IWD
+from iwd import IWD_CONFIG_DIR
 from iwd import PSKAgent
 from iwd import NetworkType
 from hostapd import HostapdCLI
@@ -50,8 +50,7 @@ class Test(unittest.TestCase):
     @classmethod
     def setUpClass(cls):
         IWD.copy_to_hotspot('example.conf')
-        conf = '[General]\nDisableANQP=0\n'
-        os.system('echo "%s" > /tmp/main.conf' % conf)
+        IWD.copy_to_storage('anqp_enabled.conf', storage_dir=IWD_CONFIG_DIR, name='main.conf')
 
     @classmethod
     def tearDownClass(cls):
autotests/testHotspot/roaming_test.py +2 -3
index d18864e0..6cdd6922 100644
@@ -5,8 +5,8 @@ import sys
 import os
 
 sys.path.append('../util')
-import iwd
 from iwd import IWD
+from iwd import IWD_CONFIG_DIR
 from iwd import PSKAgent
 from iwd import NetworkType
 from hostapd import HostapdCLI
@@ -50,8 +50,7 @@ class Test(unittest.TestCase):
     @classmethod
     def setUpClass(cls):
         IWD.copy_to_hotspot('roaming.conf')
-        conf = '[General]\nDisableANQP=1\n'
-        os.system('echo "%s" > /tmp/main.conf' % conf)
+        IWD.copy_to_storage('anqp_disabled.conf', storage_dir=IWD_CONFIG_DIR, name='main.conf')
 
     @classmethod
     def tearDownClass(cls):
Signature · 1 line
2.31.1
#3 lore raw

PATCH 4/4 test-runner: only remove GLib timeout if it exists

In reply to James Prestwood

There was a race condition here where the GLib timeout could have fired but the test function returned successfully prior to the end of the while loop. This would end up causing source_remove to print a warning that the source did not exist.

Instead check if the timeout fired prior to removing it.

---
 tools/test-runner | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
tools/test-runner +4 -2
index 5a8006a5..dcb1ba5e 100755
@@ -751,10 +751,12 @@ class Namespace:
 			try:
 				ret = func(*args)
 				if ret:
-					GLib.source_remove(timeout)
+					if not done.value:
+						GLib.source_remove(timeout)
 					return ret
 			except Exception as e:
-				GLib.source_remove(timeout)
+				if not done.value:
+					GLib.source_remove(timeout)
 				raise e
 
 			sleep(0.1)
Signature · 1 line
2.31.1
#4 lore raw
In reply to James Prestwood

Hi James,

Quoted 6 lines · James Prestwood

This addresses the TODO where HostapdCLI was creating separate objects each time HostapdCLI was called. This was worked around by manually setting the important members but instead the class can be re-worked to act as somewhat of a singleton, per-config at least.

Quoted 9 lines

If there is no HostapdCLI instance for a given config one is created and initialized. Subsequent HostapdCLI calls (for the same config) will be returned the same object rather than a new one.

---
  autotests/util/hostapd.py | 48 +++++++++++++++++++++++----------------
  tools/test-runner         |  4 ++++
  2 files changed, 33 insertions(+), 19 deletions(-)

All applied, thanks.

Regards,
-Denis