All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH 1/4] auto-t: return existing instance from HostapdCLI
@ 2021-08-13 22:45 James Prestwood
  2021-08-13 22:45 ` [PATCH 2/4] auto-t: iwd.py: clean up StationDebug James Prestwood
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: James Prestwood @ 2021-08-13 22:45 UTC (permalink / raw)
  To: iwd

[-- Attachment #1: Type: text/plain, Size: 3688 bytes --]

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(-)

diff --git a/autotests/util/hostapd.py b/autotests/util/hostapd.py
index a61b2d39..aabc997b 100644
--- a/autotests/util/hostapd.py
+++ b/autotests/util/hostapd.py
@@ -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)
diff --git a/tools/test-runner b/tools/test-runner
index 0ca50ddb..5a8006a5 100755
--- a/tools/test-runner
+++ b/tools/test-runner
@@ -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
-- 
2.31.1

^ permalink raw reply related	[flat|nested] 5+ messages in thread

* [PATCH 2/4] auto-t: iwd.py: clean up StationDebug
  2021-08-13 22:45 [PATCH 1/4] auto-t: return existing instance from HostapdCLI James Prestwood
@ 2021-08-13 22:45 ` James Prestwood
  2021-08-13 22:45 ` [PATCH 3/4] auto-t: turn off scan address randomization in testHotspot James Prestwood
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: James Prestwood @ 2021-08-13 22:45 UTC (permalink / raw)
  To: iwd

[-- Attachment #1: Type: text/plain, Size: 3290 bytes --]

Initialize this once and reference that for each method/property
---
 autotests/util/iwd.py | 32 +++++++++++---------------------
 1 file changed, 11 insertions(+), 21 deletions(-)

diff --git a/autotests/util/iwd.py b/autotests/util/iwd.py
index 737b2beb..54157b00 100755
--- a/autotests/util/iwd.py
+++ b/autotests/util/iwd.py
@@ -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 = ''):
-- 
2.31.1

^ permalink raw reply related	[flat|nested] 5+ messages in thread

* [PATCH 3/4] auto-t: turn off scan address randomization in testHotspot
  2021-08-13 22:45 [PATCH 1/4] auto-t: return existing instance from HostapdCLI James Prestwood
  2021-08-13 22:45 ` [PATCH 2/4] auto-t: iwd.py: clean up StationDebug James Prestwood
@ 2021-08-13 22:45 ` James Prestwood
  2021-08-13 22:45 ` [PATCH 4/4] test-runner: only remove GLib timeout if it exists James Prestwood
  2021-08-14  1:39 ` [PATCH 1/4] auto-t: return existing instance from HostapdCLI Denis Kenzior
  3 siblings, 0 replies; 5+ messages in thread
From: James Prestwood @ 2021-08-13 22:45 UTC (permalink / raw)
  To: iwd

[-- Attachment #1: Type: text/plain, Size: 5630 bytes --]

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

diff --git a/autotests/testHotspot/anqp_delay_test.py b/autotests/testHotspot/anqp_delay_test.py
index 323e1fd3..04e5a1d7 100644
--- a/autotests/testHotspot/anqp_delay_test.py
+++ b/autotests/testHotspot/anqp_delay_test.py
@@ -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):
diff --git a/autotests/testHotspot/anqp_disabled.conf b/autotests/testHotspot/anqp_disabled.conf
new file mode 100644
index 00000000..b18f66d7
--- /dev/null
+++ b/autotests/testHotspot/anqp_disabled.conf
@@ -0,0 +1,5 @@
+[General]
+DisableANQP=1
+
+[Scan]
+DisableMacAddressRandomization=true
diff --git a/autotests/testHotspot/anqp_enabled.conf b/autotests/testHotspot/anqp_enabled.conf
new file mode 100644
index 00000000..c66a63e0
--- /dev/null
+++ b/autotests/testHotspot/anqp_enabled.conf
@@ -0,0 +1,5 @@
+[General]
+DisableANQP=0
+
+[Scan]
+DisableMacAddressRandomization=true
diff --git a/autotests/testHotspot/autoconnect_test.py b/autotests/testHotspot/autoconnect_test.py
index 354bf61b..777a0670 100644
--- a/autotests/testHotspot/autoconnect_test.py
+++ b/autotests/testHotspot/autoconnect_test.py
@@ -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):
diff --git a/autotests/testHotspot/hessid_test.py b/autotests/testHotspot/hessid_test.py
index 5d3954b9..88e750de 100644
--- a/autotests/testHotspot/hessid_test.py
+++ b/autotests/testHotspot/hessid_test.py
@@ -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):
diff --git a/autotests/testHotspot/hotspot_test.py b/autotests/testHotspot/hotspot_test.py
index 460ff89f..f49477dc 100644
--- a/autotests/testHotspot/hotspot_test.py
+++ b/autotests/testHotspot/hotspot_test.py
@@ -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):
diff --git a/autotests/testHotspot/roaming_test.py b/autotests/testHotspot/roaming_test.py
index d18864e0..6cdd6922 100644
--- a/autotests/testHotspot/roaming_test.py
+++ b/autotests/testHotspot/roaming_test.py
@@ -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):
-- 
2.31.1

^ permalink raw reply related	[flat|nested] 5+ messages in thread

* [PATCH 4/4] test-runner: only remove GLib timeout if it exists
  2021-08-13 22:45 [PATCH 1/4] auto-t: return existing instance from HostapdCLI James Prestwood
  2021-08-13 22:45 ` [PATCH 2/4] auto-t: iwd.py: clean up StationDebug James Prestwood
  2021-08-13 22:45 ` [PATCH 3/4] auto-t: turn off scan address randomization in testHotspot James Prestwood
@ 2021-08-13 22:45 ` James Prestwood
  2021-08-14  1:39 ` [PATCH 1/4] auto-t: return existing instance from HostapdCLI Denis Kenzior
  3 siblings, 0 replies; 5+ messages in thread
From: James Prestwood @ 2021-08-13 22:45 UTC (permalink / raw)
  To: iwd

[-- Attachment #1: Type: text/plain, Size: 900 bytes --]

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(-)

diff --git a/tools/test-runner b/tools/test-runner
index 5a8006a5..dcb1ba5e 100755
--- a/tools/test-runner
+++ b/tools/test-runner
@@ -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)
-- 
2.31.1

^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH 1/4] auto-t: return existing instance from HostapdCLI
  2021-08-13 22:45 [PATCH 1/4] auto-t: return existing instance from HostapdCLI James Prestwood
                   ` (2 preceding siblings ...)
  2021-08-13 22:45 ` [PATCH 4/4] test-runner: only remove GLib timeout if it exists James Prestwood
@ 2021-08-14  1:39 ` Denis Kenzior
  3 siblings, 0 replies; 5+ messages in thread
From: Denis Kenzior @ 2021-08-14  1:39 UTC (permalink / raw)
  To: iwd

[-- Attachment #1: Type: text/plain, Size: 772 bytes --]

Hi James,

On 8/13/21 5:45 PM, James Prestwood wrote:
> 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(-)
> 

All applied, thanks.

Regards,
-Denis

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2021-08-14  1:39 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-08-13 22:45 [PATCH 1/4] auto-t: return existing instance from HostapdCLI James Prestwood
2021-08-13 22:45 ` [PATCH 2/4] auto-t: iwd.py: clean up StationDebug James Prestwood
2021-08-13 22:45 ` [PATCH 3/4] auto-t: turn off scan address randomization in testHotspot James Prestwood
2021-08-13 22:45 ` [PATCH 4/4] test-runner: only remove GLib timeout if it exists James Prestwood
2021-08-14  1:39 ` [PATCH 1/4] auto-t: return existing instance from HostapdCLI Denis Kenzior

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.