From df4d0eef5c5fa77b835a0ccbde905d6ece9f9dd5 Mon Sep 17 00:00:00 2001 From: James Prestwood Date: Tue, 29 Aug 2023 07:39:54 -0700 Subject: [PATCH] auto-t: require a complete hostapd default config 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(-) 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()