1# Copyright 2018 The Chromium OS Authors. All rights reserved. 2# Use of this source code is governed by a BSD-style license that can be 3# found in the LICENSE file. 4 5import logging 6import shutil 7 8from autotest_lib.client.bin import test, utils 9from autotest_lib.client.common_lib import error 10from autotest_lib.client.cros.update_engine import update_engine_util 11 12class UpdateEngineTest(test.test, update_engine_util.UpdateEngineUtil): 13 """Base class for update engine client tests.""" 14 15 _NETWORK_INTERFACES = ['eth0', 'eth1', 'eth2'] 16 17 18 def initialize(self): 19 """Initialize for this test.""" 20 self._create_update_engine_variables() 21 self._internet_was_disabled = False 22 23 24 def cleanup(self): 25 """Cleanup for this test.""" 26 # Make sure to grab the update engine log for every test run. 27 shutil.copy(self._UPDATE_ENGINE_LOG, self.resultsdir) 28 29 # Ensure ethernet adapters are back on 30 self._enable_internet() 31 32 33 def _enable_internet(self, ping_server='google.com'): 34 """ 35 Re-enables the internet connection. 36 37 @param ping_server: The server to ping to check we are online. 38 39 """ 40 if not self._internet_was_disabled: 41 return 42 43 self._internet_was_disabled = False 44 logging.debug('Before reconnect: %s', utils.run('ifconfig')) 45 for eth in self._NETWORK_INTERFACES: 46 utils.run('ifconfig %s up' % eth, ignore_status=True) 47 utils.start_service('recover_duts', ignore_status=True) 48 49 # Print ifconfig to help debug DUTs that stay offline. 50 logging.debug('After reconnect: %s', utils.run('ifconfig')) 51 52 # We can't return right after reconnecting the network or the server 53 # test may not receive the message. So we wait a bit longer for the 54 # DUT to be reconnected. 55 utils.poll_for_condition(lambda: utils.ping(ping_server, 56 tries=3, timeout=10) == 0, 57 timeout=120, 58 sleep_interval=1, 59 exception=error.TestFail( 60 'Ping failed after reconnecting network')) 61 62 63 def _disable_internet(self, ping_server='google.com'): 64 """Disable the internet connection""" 65 self._internet_was_disabled = True 66 try: 67 logging.debug('Before disconnect: %s', utils.run('ifconfig')) 68 # DUTs in the lab have a service called recover_duts that is used to 69 # check that the DUT is online and if it is not it will bring it 70 # back online. We will need to stop this service for the length 71 # of this test. 72 utils.stop_service('recover_duts', ignore_status=True) 73 for eth in self._NETWORK_INTERFACES: 74 result = utils.run('ifconfig %s down' % eth, ignore_status=True) 75 logging.debug(result) 76 77 # Print ifconfig to help debug DUTs that stay online. 78 logging.debug('After disconnect: %s', utils.run('ifconfig')) 79 80 # Make sure we are offline 81 utils.poll_for_condition(lambda: utils.ping(ping_server, 82 deadline=5, 83 timeout=5) != 0, 84 timeout=60, 85 sleep_interval=1, 86 desc='Ping failure while offline.') 87 except (error.CmdError, utils.TimeoutError): 88 logging.exception('Failed to disconnect one or more interfaces.') 89 logging.debug(utils.run('ifconfig', ignore_status=True)) 90 raise error.TestFail('Disabling the internet connection failed.') 91 92