def test_load_schema_description_from_file_invalid(self, mock_command, mock_parse, mock_relax, mock_schematron): mock_rng_validate = mock.Mock() mock_rng_validate.validate = mock.Mock(return_value=False) mock_sch_validate = mock.Mock() mock_sch_validate.validate = mock.Mock(return_value=False) validation_report = namedtuple('report', ['text']) name_spaces = namedtuple('nspaces', ['nsmap']) mock_validation_report = mock.Mock() mock_validation_report.getroot = mock.Mock(return_value=name_spaces( nsmap="")) mock_validation_report.xpath = mock.Mock(return_value=[ validation_report(text='wrong attribute 1'), validation_report(text='wrong attribute 2') ]) mock_sch_validate.validation_report = mock_validation_report mock_relax.return_value = mock_rng_validate mock_schematron.return_value = mock_sch_validate mock_command.side_effect = KiwiCommandError('jing output') with raises(KiwiDescriptionInvalid): self.description_from_file.load()
def test_add_repo_second_attempt_on_failure( self, mock_open, mock_exists, mock_uri, mock_path, mock_command, mock_restore_package_cache, mock_backup_package_cache ): mock_command.side_effect = KiwiCommandError('error') with raises(KiwiCommandError): self.repo.add_repo('foo', 'http://foo/uri', 'rpm-md', 42) assert mock_command.call_count == 2
def poll(self): """ Iterate over process, raise on error and log output """ for line in self.command: if line: log.debug('%s: %s', self.log_topic, line) if self.command.get_error_code() != 0: raise KiwiCommandError(self.command.get_error_output())
def create_hybrid(offset, mbrid, isofile, efi_mode=False): """ Create hybrid ISO A hybrid ISO embeds both, an isolinux signature as well as a disk signature. kiwi always adds an msdos and a GPT table for the disk signatures :param str offset: hex offset :param str mbrid: boot record id :param str isofile: path to the ISO file :param bool efi_mode: sets the iso to support efi firmware or not """ ignore_errors = [ # we ignore this error message, for details see: # http://www.syslinux.org/archives/2015-March/023306.html 'Warning: more than 1024 cylinders', 'Not all BIOSes will be able to boot this device' ] isohybrid_parameters = [ '--offset', format(offset), '--id', mbrid, '--type', '0x83', ] if efi_mode: isohybrid_parameters.append('--uefi') isohybrid_call = Command.run(['isohybrid'] + isohybrid_parameters + [isofile]) # isohybrid warning messages on stderr should be treated # as fatal errors except for the ones we want to ignore # because unexpected after effects might happen if e.g a # gpt partition should be embedded but only a warning # appears if isohybrid can't find an efi loader. Thus we # are more strict and fail if isohybrid_call.error: error_list = isohybrid_call.error.split(os.linesep) error_fatal_list = [] for error in error_list: ignore = False for ignore_error in ignore_errors: if ignore_error in error: ignore = True break if not ignore and error: error_fatal_list.append(error) if error_fatal_list: raise KiwiCommandError( 'isohybrid issue not ignored by kiwi: {0}'.format( error_fatal_list))
def poll_show_progress(self, items_to_complete, match_method): """ Iterate over process and show progress in percent raise on error and log output :param list items_to_complete: all items :param function match_method: method matching item """ self._init_progress() for line in self.command: if line: log.debug('%s: %s', self.log_topic, line) self._update_progress(match_method, items_to_complete, line) self._stop_progress() if self.command.get_error_code() != 0: raise KiwiCommandError(self.command.get_error_output())
def run(command, custom_env=None, raise_on_error=True, stderr_to_stdout=False): """ Execute a program and block the caller. The return value is a hash containing the stdout, stderr and return code information. Unless raise_on_error is set to false an exception is thrown if the command exits with an error code not equal to zero Example: .. code:: python result = Command.run(['ls', '-l']) :param list command: command and arguments :param list custom_env: custom os.environ :param bool raise_on_error: control error behaviour :param bool stderr_to_stdout: redirects stderr to stdout :return: Contains call results in command type .. code:: python command(output='string', error='string', returncode=int) :rtype: namedtuple """ from .path import Path log.debug('EXEC: [%s]', ' '.join(command)) environment = os.environ if custom_env: environment = custom_env if not Path.which( command[0], custom_env=environment, access_mode=os.X_OK): message = 'Command "%s" not found in the environment' % command[0] if not raise_on_error: log.debug('EXEC: %s', message) return command_type(output=None, error=None, returncode=-1) else: raise KiwiCommandNotFound(message) stderr = subprocess.STDOUT if stderr_to_stdout else subprocess.PIPE try: process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=stderr, env=environment) except Exception as e: raise KiwiCommandError('%s: %s: %s' % (command[0], type(e).__name__, format(e))) output, error = process.communicate() if process.returncode != 0 and not error: error = bytes(b'(no output on stderr)') if process.returncode != 0 and not output: output = bytes(b'(no output on stdout)') if process.returncode != 0 and raise_on_error: log.debug('EXEC: Failed with stderr: {0}, stdout: {1}'.format( Codec.decode(error), Codec.decode(output))) raise KiwiCommandError('{0}: stderr: {1}, stdout: {2}'.format( command[0], Codec.decode(error), Codec.decode(output))) return command_type(output=Codec.decode(output), error=Codec.decode(error), returncode=process.returncode)
def call(command, custom_env=None): """ Execute a program and return an io file handle pair back. stdout and stderr are both on different channels. The caller must read from the output file handles in order to actually run the command. This can be done using the CommandIterator from command_process Example: .. code:: python process = Command.call(['ls', '-l']) :param list command: command and arguments :param list custom_env: custom os.environ :return: Contains process results in command type .. code:: python command( output='string', output_available=bool, error='string', error_available=bool, process=subprocess ) :rtype: namedtuple """ from .path import Path log.debug('EXEC: [%s]', ' '.join(command)) environment = os.environ if custom_env: environment = custom_env if not Path.which( command[0], custom_env=environment, access_mode=os.X_OK): raise KiwiCommandNotFound( 'Command "%s" not found in the environment' % command[0]) try: process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, env=environment) except Exception as e: raise KiwiCommandError('%s: %s' % (type(e).__name__, format(e))) def output_available(): def _select(): descriptor_lists = select.select([process.stdout], [], [process.stdout], 1e-4) readable = descriptor_lists[0] exceptional = descriptor_lists[2] if readable and not exceptional: return True return _select def error_available(): def _select(): descriptor_lists = select.select([process.stderr], [], [process.stderr], 1e-4) readable = descriptor_lists[0] exceptional = descriptor_lists[2] if readable and not exceptional: return True return _select command = namedtuple('command', [ 'output', 'output_available', 'error', 'error_available', 'process' ]) return command(output=process.stdout, output_available=output_available(), error=process.stderr, error_available=error_available(), process=process)
def dbpath_check_fails(command, raise_on_error): if '%_dbpath' in command: raise KiwiCommandError() else: return cmd
def test_call_failure(self, mock_popen, mock_which): mock_which.return_value = 'command' mock_popen.side_effect = KiwiCommandError('Call failure') with raises(KiwiCommandError): Command.call(['command', 'args'])
def test_run_failure(self, mock_popen, mock_which): mock_which.return_value = 'command' mock_popen.side_effect = KiwiCommandError('Run failure') Command.run(['command', 'args'])