diff mbox series

[1/2] auto-t: require a complete hostapd default config

Message ID 20230829143955.267742-1-prestwoj@gmail.com (mailing list archive)
State Accepted, archived
Headers show
Series [1/2] auto-t: require a complete hostapd default config | expand

Commit Message

James Prestwood Aug. 29, 2023, 2:39 p.m. UTC
In many tests the hostapd configuration does not include all the
values that a test uses. Its expected that each individual test
will add the values required. In many cases its required each test
slightly alter the configuration for each change every other test
has to set the value back to either a default or its own setting.
This results in a ton of duplicated code mainly setting things
back to defaults.

To help with this problem the hostapd configuration is read in
initially and stored as the default. Tests can then simply call
.default() to set everything back. This significantly reduces or
completely removes a ton of set_value() calls.

This does require that each hostapd configuration file includes all
values any of the subtests will set, which is a small price for the
convenience.
---
 autotests/util/hostapd.py | 27 ++++++++++++++++++++++++++-
 1 file changed, 26 insertions(+), 1 deletion(-)

Comments

Denis Kenzior Aug. 31, 2023, 1:41 a.m. UTC | #1
Hi James,

On 8/29/23 09:39, James Prestwood wrote:
> In many tests the hostapd configuration does not include all the
> values that a test uses. Its expected that each individual test
> will add the values required. In many cases its required each test
> slightly alter the configuration for each change every other test
> has to set the value back to either a default or its own setting.
> This results in a ton of duplicated code mainly setting things
> back to defaults.
> 
> To help with this problem the hostapd configuration is read in
> initially and stored as the default. Tests can then simply call
> .default() to set everything back. This significantly reduces or
> completely removes a ton of set_value() calls.
> 
> This does require that each hostapd configuration file includes all
> values any of the subtests will set, which is a small price for the
> convenience.
> ---
>   autotests/util/hostapd.py | 27 ++++++++++++++++++++++++++-
>   1 file changed, 26 insertions(+), 1 deletion(-)
> 

Both applied, thanks.

Regards,
-Denis
diff mbox series

Patch

diff --git a/autotests/util/hostapd.py b/autotests/util/hostapd.py
index bb27686f..1b1bc399 100644
--- a/autotests/util/hostapd.py
+++ b/autotests/util/hostapd.py
@@ -64,7 +64,16 @@  class HostapdCLI(object):
         hapd = ctx.get_hapd_instance(config)
 
         self.interface = hapd.intf
-        self.config = hapd.config
+        self._config_path = "/tmp/" + hapd.config
+        self._default_config = self._get_config(self._config_path)
+
+        # The vendor_elements is somewhat of a special case because you can't
+        # set it to 'nothing' within the hostapd config. In most cases tests do
+        # not use this and we don't want to require they unset this value
+        # during initialization. Default this to '' so set_value won't throw
+        # an exception.
+        if self._default_config.get('vendor_elements', None) == None:
+            self._default_config['vendor_elements'] = ''
 
         if not self.interface:
             raise Exception('config %s not found' % config)
@@ -89,6 +98,12 @@  class HostapdCLI(object):
 
         ctrl_count = ctrl_count + 1
 
+    def _get_config(self, path):
+        f = open(path)
+        lines = f.readlines()
+        f.close()
+        return dict([[v.strip() for v in kv] for kv in [l.split('#', 1)[0].split('=', 1) for l in lines] if len(kv) == 2])
+
     def _handle_data_in(self, sock, *args):
         newdata = sock.recv(4096)
 
@@ -151,6 +166,10 @@  class HostapdCLI(object):
             pass
 
     def set_value(self, key, value):
+        # Don't allow new settings, defaults should always be in hostapd config
+        if self._default_config.get(key, None) == None:
+            raise Exception("Untracked setting '%s'! Please set default in hostapd config" % key)
+
         cmd = self.cmdline + ['set', key, value]
         ctx.start_process(cmd).wait()
 
@@ -179,6 +198,12 @@  class HostapdCLI(object):
         ctx.start_process(self.cmdline + ['disable']).wait()
         ctx.start_process(self.cmdline + ['enable']).wait()
 
+    def default(self):
+        for k, v in self._default_config.items():
+            self.set_value(k, v)
+
+        self.reload()
+
     def disable(self):
         ctx.start_process(self.cmdline + ['disable']).wait()