def testInitializeFail(self): """Ensure we fail after too many _Clone() retries.""" self.mox.StubOutWithMock(self.mv, '_Clone') self.mox.StubOutWithMock(time, 'sleep') for i in range(0, self.mv._CLONE_MAX_RETRIES): self.mv._Clone().AndRaise( error.CmdError('retried git clone failure', utils.CmdResult())) time.sleep(self.mv._CLONE_RETRY_SECONDS) self.mv._Clone().AndRaise( error.CmdError('final git clone failure', utils.CmdResult())) self.mox.ReplayAll() self.assertRaises(manifest_versions.CloneException, self.mv.Initialize)
def start(self, test): """ Start ftrace profiler @param test: Autotest test in which the profiler will operate on. """ # Make sure debugfs is mounted and tracing disabled. utils.system('%s reset' % self.trace_cmd) output_dir = os.path.join(test.profdir, 'ftrace') if not os.path.isdir(output_dir): os.makedirs(output_dir) self.output = os.path.join(output_dir, 'trace.dat') cmd = [self.trace_cmd, 'record', '-o', self.output] cmd += self.trace_cmd_args self.record_job = utils.BgJob(self.join_command(cmd), stderr_tee=utils.TEE_TO_LOGS) # Wait for tracing to be enabled. If trace-cmd dies before enabling # tracing, then there was a problem. tracing_on = os.path.join(self.tracing_dir, 'tracing_on') while (self.record_job.sp.poll() is None and utils.read_file(tracing_on).strip() != '1'): time.sleep(0.1) if self.record_job.sp.poll() is not None: utils.join_bg_jobs([self.record_job]) raise error.CmdError(self.record_job.command, self.record_job.sp.returncode, 'trace-cmd exited early.')
def fetch_pkg_file(self, filename, dest_path): logging.info('Fetching %s from %s to %s', filename, self.url, dest_path) # do a quick test to verify the repo is reachable self._quick_http_test() # try to retrieve the package via http package_url = os.path.join(self.url, filename) try: cmd = self.curl_cmd_pattern % (package_url, dest_path) result = self.run_command(cmd, _run_command_dargs={'timeout': 1200}) if not self.exists(dest_path): logging.error('curl failed: %s', result) raise error.CmdError(cmd, result) logging.info('Successfully fetched %s from %s', filename, package_url) except error.CmdError as e: # remove whatever junk was retrieved when the get failed self.run_command('rm -f %s' % dest_path) raise error.PackageFetchError( '%s not found in %s\n%s' 'curl error code: %d' % (filename, package_url, e.result_obj.stderr, e.result_obj.exit_status))
def fetch_pkg_file(self, filename, dest_path): logging.info('Fetching %s from %s to %s', filename, self.url, dest_path) # do a quick test to verify the repo is reachable self._quick_http_test() # try to retrieve the package via http package_url = os.path.join(self.url, filename) try: cmd = self.wget_cmd_pattern % (package_url, dest_path) result = self.run_command(cmd) file_exists = self.run_command('ls %s' % dest_path, _run_command_dargs={ 'ignore_status': True }).exit_status == 0 if not file_exists: logging.error('wget failed: %s', result) raise error.CmdError(cmd, result) logging.debug('Successfully fetched %s from %s', filename, package_url) except error.CmdError: # remove whatever junk was retrieved when the get failed self.run_command('rm -f %s' % dest_path) raise error.PackageFetchError('%s not found in %s' % (filename, package_url))
def run(self, ignore_failures=False, retry_count=3): """Run netperf and take a performance measurement. @param ignore_failures bool True iff netperf runs that fail should be ignored. If this happens, run will return a None value rather than a NetperfResult. @param retry_count int number of times to retry the netperf command if it fails due to an internal timeout within netperf. @return NetperfResult summarizing a netperf run. """ netperf = '%s -H %s -p %s -t %s -l %d -- -P 0,%d' % ( self._command_netperf, self._target_ip, self.NETPERF_PORT, self._config.netperf_test_type, self._config.test_time, self.NETPERF_DATA_PORT) logging.debug('Running netperf client.') logging.info('Running netperf for %d seconds.', self._config.test_time) timeout = self._config.test_time + self.NETPERF_COMMAND_TIMEOUT_MARGIN for _ in range(retry_count): start_time = time.time() result = self._client_host.run(netperf, ignore_status=True, ignore_timeout=ignore_failures, timeout=timeout) if not result: logging.info('Retrying netperf after empty result.') continue # Exit retry loop on success. if not result.exit_status: break # Only retry for known retryable conditions. if 'Interrupted system call' in result.stderr: logging.info('Retrying netperf after internal timeout error.') continue if 'establish the control connection' in result.stdout: logging.info('Restarting netserv after client failed connect.') self._restart_netserv() continue # We are in an unhandled error case. logging.info('Retrying netperf after an unknown error.') if ignore_failures and (result is None or result.exit_status): return None if result is None: raise error.TestFail("No results; cmd: %s", netperf) if result.exit_status: raise error.CmdError(netperf, result, "Command returned non-zero exit status") duration = time.time() - start_time return NetperfResult.from_netperf_results(self._config.test_type, result.stdout, duration)
def test_no_readlink(self): """Test when readlink does not exist.""" run_func = self.god.create_mock_function('run_func') self.god.stub_with(base_utils, 'run', run_func) run_func.expect_call('readlink -f /sys/class/net/eth0').and_raises( error.CmdError('readlink', 'no such command')) eth = utils.get_built_in_ethernet_nic_name() self.assertEqual('eth0', eth) self.god.check_playback()
def _mock_cmd_runner(cmd, **kwargs): """ Mock running a DUT command, returning a CmdResult. We handle a few mock functions here: * cros_config $path $property: $path determines error or success. $property is not used here. * echo $text: Returns $text with a trailing newline. Additionally, if the kwarg `ignore_status` is passed in as True, then when cros_config would raise an error, it instead returns a CmdResult with an exit_status of 1. @param cmd: A command, as would be run on the DUT @param **kwargs: Kwargs that might be passed into, say, utils.run() @return: A mock response from the DUT @type cmd: string @rtype: client.common_lib.utils.CmdResult @raise error.CmdError if cros_config should raise an exception. @raise NotImplementedError if cros_config has an unexpected path """ result = utils.CmdResult(cmd) if cmd.startswith('cros_config '): _, path, _ = cmd.split() if path == CC_PATHS[SUCCEEDS]: result.stdout = CROS_CONFIG_SUCCESS_RESPONSE elif path == CC_PATHS[FAILS]: result.exit_status = 1 if not kwargs.get('ignore_status'): raise error.CmdError(cmd, result) else: raise NotImplementedError('Bad cros_config path: %s' % path) elif cmd.startswith('echo '): result.stdout = cmd.lstrip('echo ') + '\n' else: result.exit_status = 2 if not kwargs.get('ignore_status'): raise error.CmdError(cmd, result) return result
def clone(self, *args, **kwargs): """Mocks the Container.clone class method. """ # Record the particulars of this call. self.clone_count += 1 self.clone_kwargs.append(kwargs) # Simulate failure if a snapshot is requested, otherwise create # and return the clone. if kwargs['snapshot']: raise error.CmdError('fake error', None) else: return lxc.Container.clone(*args, **kwargs)
def testInitializeRetry(self): """Ensure we retry _Clone() the correct number of times.""" self.mox.StubOutWithMock(self.mv, '_Clone') self.mox.StubOutWithMock(time, 'sleep') for i in range(0, self.mv._CLONE_MAX_RETRIES): self.mv._Clone().AndRaise( error.CmdError('retried git clone failure', utils.CmdResult())) time.sleep(self.mv._CLONE_RETRY_SECONDS) self.mv._Clone() self.mox.ReplayAll() self.mv.Initialize()
def test_extract_via_ssh_has_error(self, *args): """Test extracting via ssh when has errors.""" gs_cache_client._USE_SSH_CONNECTION = True self.api._is_in_restricted_subnet = True with mock.patch('autotest_lib.client.common_lib.utils.run') as m: m.side_effect = error.CmdError('cmd', mock.MagicMock(exit_status=1)) with self.assertRaises(gs_cache_client.CommunicationError): self.api.extract(gs_cache_client._CROS_IMAGE_ARCHIVE_BUCKET, 'archive', 'file') m.side_effect = error.CmdError('cmd', mock.MagicMock( exit_status=gs_cache_client._CURL_RC_CANNOT_CONNECT_TO_HOST) ) with self.assertRaises(gs_cache_client.NoGsCacheServerError): self.api.extract(gs_cache_client._CROS_IMAGE_ARCHIVE_BUCKET, 'archive', 'file') m.side_effect = None m.return_value.stdout = '...' with self.assertRaises(gs_cache_client.ResponseContentError): self.api.extract(gs_cache_client._CROS_IMAGE_ARCHIVE_BUCKET, 'archive', 'file')
def testEnableNotificationUsingCredentialsInBucketFail(self): config_mock = self.mox.CreateMockAnything() moblab_rpc_interface._CONFIG = config_mock config_mock.get_config_value( 'CROS', 'image_storage_server').AndReturn('gs://bucket1/') self.mox.StubOutWithMock(moblab_rpc_interface.GsUtil, 'get_gsutil_cmd') moblab_rpc_interface.GsUtil.get_gsutil_cmd().AndReturn( '/path/to/gsutil') self.mox.StubOutWithMock(common_lib_utils, 'run') common_lib_utils.run('/path/to/gsutil', args=('cp', 'gs://bucket1/pubsub-key-do-not-delete.json', '/tmp')).AndRaise( error.CmdError("fakecommand", common_lib_utils.CmdResult(), "")) self.mox.ReplayAll() moblab_rpc_interface._enable_notification_using_credentials_in_bucket()
def test_run_cmd_failure_raised(self, mock_run): mock_result = mock.MagicMock() mock_run.side_effect = error.CmdError('yes', mock_result) host = local_host.LocalHost() with self.assertRaises(error.AutotestHostRunCmdError) as exc_cm: host.run('yes', timeout=123) self.assertEqual(exc_cm.exception.result_obj, mock_result) mock_run.assert_called_once_with( 'yes', timeout=123, ignore_status=False, stdout_tee=local_host.utils.TEE_TO_LOGS, stderr_tee=local_host.utils.TEE_TO_LOGS, stdin=None, ignore_timeout=False, args=(), )
def testRunBucketPerformanceTestFail(self): self.mox.StubOutWithMock(moblab_rpc_interface.GsUtil, 'get_gsutil_cmd') moblab_rpc_interface.GsUtil.get_gsutil_cmd().AndReturn( '/path/to/gsutil') self.mox.StubOutWithMock(common_lib_utils, 'run') common_lib_utils.run( '/path/to/gsutil', args=('-o', 'Credentials:gs_access_key_id=key', '-o', 'Credentials:gs_secret_access_key=secret', 'perfdiag', '-s', '1K', '-o', 'testoutput', '-n', '10', 'gs://bucket1')).AndRaise( error.CmdError("fakecommand", common_lib_utils.CmdResult(), "xxxxxx<Error>yyyyyyyyyy</Error>")) self.mox.ReplayAll() self.assertRaisesRegexp( moblab_rpc_interface.BucketPerformanceTestException, '<Error>yyyyyyyyyy', moblab_rpc_interface._run_bucket_performance_test, 'key', 'secret', 'gs://bucket1', '1K', '10', 'testoutput') self.mox.VerifyAll()