aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJames Prestwood <prestwoj@gmail.com>2020-05-06 14:32:14 -0700
committerDenis Kenzior <denkenz@gmail.com>2020-05-02 06:40:23 -0500
commitda3f66ea68a5efd3521d94580b6537671868040f (patch)
tree121388fdc31900c25c1dd229f92f5664c1d47767
parent8126899edab63e5802edd2ed95f8fb6744d32b9a (diff)
downloadiwd-da3f66ea68a5efd3521d94580b6537671868040f.tar.gz
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.
-rwxr-xr-xautotests/util/iwd.py23
1 files changed, 19 insertions, 4 deletions
diff --git a/autotests/util/iwd.py b/autotests/util/iwd.py
index 9bbb7278c..c8d11f912 100755
--- a/autotests/util/iwd.py
+++ b/autotests/util/iwd.py
@@ -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)