auto-t: add optional scan argument to get_ordered_network(s)
PATCH v4 1/2 auto-t: add optional scan argument to get_ordered_network(s)
There is a very common block of code inside many autotests which goes something like:
device.scan()
condition = 'obj.scanning'wd.wait_for_object_condition(device, condition)
condition = 'not obj.scanning'wd.wait_for_object_condition(device, condition)
network = device.get_ordered_network('an-ssid')
When you see the same pattern in nearly all the tests this shows we need a helper. Basic autotests which merely check that a connection succeeded should not need to write the same code again and again. This code ends up being copy-pasted which can lead to bugs.
There is also a code pattern which attempts to get ordered networks, and if this fails it scans and tries again. This, while not optimal, does prevent unneeded scanning by first checking if any networks already exist.
This patch solves both the code reuse issue as well as the recovery if get_ordered_network(s) fails. A new optional parameter was added to get_ordered_network(s) which is False by default. If True get_ordered_network(s) will perform a scan if the initial call yields no networks. Tests will now be able to simply call get_ordered_network(s) without performing a scan before hand.
--- autotests/util/iwd.py | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-)
autotests/util/iwd.py +19 -4
@@ -360,7 +360,7 @@ class Device(IWDDBusAbstract): self._wait_for_async_op() - def get_ordered_networks(self): + def get_ordered_networks(self, scan_if_needed = False): '''Return the list of networks found in the most recent scan, sorted by their user interface importance score as calculated by iwd. If the device is
@@ -377,17 +377,32 @@ class Device(IWDDBusAbstract): ordered_network = OrderedNetwork(bus_obj) ordered_networks.append(ordered_network) - if len(ordered_networks) == 0: + if len(ordered_networks) > 0: + return ordered_networks + elif not scan_if_needed: return None + iwd = IWD.get_instance() + + self.scan() + + condition = 'obj.scanning' + iwd.wait_for_object_condition(self, condition) + condition = 'not obj.scanning' + iwd.wait_for_object_condition(self, condition) + + for bus_obj in self._station.GetOrderedNetworks(): + ordered_network = OrderedNetwork(bus_obj) + ordered_networks.append(ordered_network) + return ordered_networks - def get_ordered_network(self, network): + def get_ordered_network(self, network, scan_if_needed = False): '''Returns a single network from ordered network call, or None if the network wasn't found. If the network is not found an exception is raised, this removes the need to extra asserts in autotests. ''' - ordered_networks = self.get_ordered_networks() + ordered_networks = self.get_ordered_networks(scan_if_needed) if not ordered_networks: raise Exception('Network %s not found' % network)
Signature · 1 line
2.21.1
PATCH v4 2/2 auto-t: use scan_if_needed for testConnectAutoconnect
In certain cases the autoconnect portion of each subtest was connecting to the network so fast that the check for obj.scanning was never successful since IWD was already connected (and in turn not scanning). Since the autoconnect path will wait for the device to be connected there really isn't a reason to wait for any scanning conditions. The normal connect path does need to wait for scanning though, and for this we can now use the new scan_if_needed parameter to get_ordered_networks.
--- autotests/testConnectAutoconnect/validation.py | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-)
autotests/testConnectAutoconnect/validation.py +1 -7
@@ -12,7 +12,7 @@ from iwd import NetworkType class TestConnectAutoConnect(unittest.TestCase): def check_connect(self, wd, device, ssid, throws): - ordered_network = device.get_ordered_network(ssid) + ordered_network = device.get_ordered_network(ssid, scan_if_needed=True) condition = 'not obj.connected' wd.wait_for_object_condition(ordered_network.network_object, condition)
@@ -54,12 +54,6 @@ class TestConnectAutoConnect(unittest.TestCase): self.assertIsNotNone(devices) device = devices[0] - condition = 'obj.scanning' - wd.wait_for_object_condition(device, condition) - - condition = 'not obj.scanning' - wd.wait_for_object_condition(device, condition) - if autoconnect: self.check_autoconnect(wd, device, ssid, throws) else:
Signature · 1 line
2.21.1
Hi James,
Quoted 27 lines · James Prestwood
There is a very common block of code inside many autotests which goes something like:
device.scan()
condition = 'obj.scanning'wd.wait_for_object_condition(device, condition)
condition = 'not obj.scanning'wd.wait_for_object_condition(device, condition)
network =device.get_ordered_network('an-ssid')When you see the same pattern in nearly all the tests this shows we need a helper. Basic autotests which merely check that a connection succeeded should not need to write the same code again and again. This code ends up being copy-pasted which can lead to bugs.
There is also a code pattern which attempts to get ordered networks, and if this fails it scans and tries again. This, while not optimal, does prevent unneeded scanning by first checking if any networks already exist.
This patch solves both the code reuse issue as well as the recovery if
get_ordered_network(s)fails. A new optional parameter was added toget_ordered_network(s)which is False by default. If Trueget_ordered_network(s)will perform a scan if the initial call yields no networks. Tests will now be able to simply callget_ordered_network(s)without performing a scan before hand.
Quoted 4 lines
--- autotests/util/iwd.py | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-)
Both applied, thanks.
Regards,
-Denis