From 392a3f160081aa1459c77b033f50fdfd8ca9f94c Mon Sep 17 00:00:00 2001 From: muendelezaji Date: Wed, 31 Aug 2016 16:00:50 +0100 Subject: [PATCH] Add network check methods to Device and Workload classes - Device subclasses should provide their own implementation. Default behaviour is to raise a `NotImplementedError` - Workload subclasses can set `requires_network` to `True` and network connectivity check will be performed during `setup()` --- wlauto/common/android/workload.py | 3 +++ wlauto/common/linux/device.py | 27 ++++++++++++++++----------- wlauto/core/device.py | 7 +++++++ wlauto/core/workload.py | 11 +++++++++-- 4 files changed, 35 insertions(+), 13 deletions(-) diff --git a/wlauto/common/android/workload.py b/wlauto/common/android/workload.py index 01bf2562..96497172 100644 --- a/wlauto/common/android/workload.py +++ b/wlauto/common/android/workload.py @@ -86,6 +86,7 @@ class UiAutomatorWorkload(Workload): self.uiauto_package = os.path.splitext(os.path.basename(self.uiauto_file))[0] def setup(self, context): + super(UiAutomatorWorkload, self).setup(context) method_string = '{}.{}#{}'.format(self.uiauto_package, self.uiauto_class, self.uiauto_method) params_dict = self.uiauto_params params_dict['workdir'] = self.device.working_directory @@ -179,6 +180,7 @@ class ApkWorkload(Workload): self.logcat_log = None def setup(self, context): + super(ApkWorkload, self).setup(context) # Get APK for the correct version and device ABI self.apk_file = context.resolver.get(ApkFile(self, self.device.abi), version=getattr(self, 'version', None), @@ -363,6 +365,7 @@ class ReventWorkload(Workload): self.run_timeout = self.run_timeout or default_run_timeout def setup(self, context): + super(ReventWorkload, self).setup(context) self.device.killall('revent') command = '{} replay {}'.format(self.on_device_revent_binary, self.on_device_setup_revent) self.device.execute(command, timeout=self.setup_timeout) diff --git a/wlauto/common/linux/device.py b/wlauto/common/linux/device.py index b65f148b..db76f2ca 100644 --- a/wlauto/common/linux/device.py +++ b/wlauto/common/linux/device.py @@ -39,6 +39,8 @@ FstabEntry = namedtuple('FstabEntry', ['device', 'mount_point', 'fs_type', 'opti PsEntry = namedtuple('PsEntry', 'user pid ppid vsize rss wchan pc state name') LsmodEntry = namedtuple('LsmodEntry', ['name', 'size', 'use_count', 'used_by']) +GOOGLE_DNS_SERVER_ADDRESS = '8.8.8.8' + class BaseLinuxDevice(Device): # pylint: disable=abstract-method @@ -318,25 +320,28 @@ class BaseLinuxDevice(Device): # pylint: disable=abstract-method please see: https://pythonhosted.org/wlauto/writing_extensions.html""") - def is_network_connected(self, ip_address='8.8.8.8'): + def is_network_connected(self): """ - Checks for internet connectivity on the device - by pinging IP address provided. + Checks for internet connectivity on the device by pinging IP address provided. - :param ip_address: the IP address to ping - (default - google-public-dns-a.google.com) + :param ip_address: IP address to ping. Default is Google's public DNS server (8.8.8.8) + + :returns: ``True`` if internet is available, ``False`` otherwise. """ - self.logger.debug('Checking for internet connectivity.') - output = self.execute('ping -q -w 1 -c 1 {}'.format(ip_address), + self.logger.debug('Checking for internet connectivity...') + return self._ping_server(GOOGLE_DNS_SERVER_ADDRESS) + + def _ping_server(self, ip_address, timeout=1, packet_count=1): + output = self.execute('ping -q -c {} -w {} {}'.format(packet_count, timeout, ip_address), check_exit_code=False) - if 'network is unreachable' not in output.lower(): - self.logger.debug('Found IP address {}'.format(ip_address)) - return True - else: + if 'network is unreachable' in output.lower(): self.logger.debug('Cannot find IP address {}'.format(ip_address)) return False + else: + self.logger.debug('Found IP address {}'.format(ip_address)) + return True def get_binary_path(self, name, search_system_binaries=True): """ diff --git a/wlauto/core/device.py b/wlauto/core/device.py index 18ae643e..5441bad2 100644 --- a/wlauto/core/device.py +++ b/wlauto/core/device.py @@ -426,6 +426,13 @@ class Device(Extension): """ pass + def is_network_connected(self): + """ + Checks if the device is connected to the internet + + """ + raise NotImplementedError() + def __str__(self): return 'Device<{}>'.format(self.name) diff --git a/wlauto/core/workload.py b/wlauto/core/workload.py index cec0df31..329b29cd 100644 --- a/wlauto/core/workload.py +++ b/wlauto/core/workload.py @@ -37,6 +37,7 @@ class Workload(Extension): supported_devices = [] supported_platforms = [] summary_metrics = [] + requires_network = False def __init__(self, device, **kwargs): """ @@ -69,7 +70,7 @@ class Workload(Extension): """ pass - def setup(self, context): + def setup(self, context): # pylint: disable=unused-argument """ Perform the setup necessary to run the workload, such as copying the necessary files to the device, configuring the environments, etc. @@ -78,7 +79,8 @@ class Workload(Extension): the workload. """ - pass + if self.requires_network: + self.check_network_connected() def run(self, context): """Execute the workload. This is the method that performs the actual "work" of the""" @@ -99,5 +101,10 @@ class Workload(Extension): def finalize(self, context): pass + def check_network_connected(self): + if not self.device.is_network_connected(): + message = 'Workload "{}" requires internet. Device "{}" does not appear to be connected to the internet.' + raise WorkloadError(message.format(self.name, self.device.name)) + def __str__(self): return ''.format(self.name)