Fix packages file generation

This commit checks the %_dbpath value of the image rpm tool. This is
needed since recent rpm versions switched the default database path,
thus running rpm queries to different roots might lead to errors.

Fixes #605
This commit is contained in:
David Cassany 2018-02-02 18:16:28 +01:00
parent df22ab6c44
commit d1fced5e99
2 changed files with 91 additions and 9 deletions

View File

@ -853,6 +853,11 @@ class SystemSetup(object):
def _export_rpm_package_list(self, filename):
log.info('Export rpm packages metadata')
dbpath = self._get_rpm_database_location()
if dbpath:
dbpath_option = ['--dbpath', dbpath]
else:
dbpath_option = []
query_call = Command.run(
[
'rpm', '--root', self.root_dir, '-qa', '--qf',
@ -862,7 +867,7 @@ class SystemSetup(object):
'%{RELEASE}', '%{ARCH}', '%{DISTURL}'
]
) + '\\n'
]
] + dbpath_option
)
with open(filename, 'w') as packages:
packages.write(query_call.output)
@ -886,8 +891,13 @@ class SystemSetup(object):
def _export_rpm_package_verification(self, filename):
log.info('Export rpm verification metadata')
dbpath = self._get_rpm_database_location()
if dbpath:
dbpath_option = ['--dbpath', dbpath]
else:
dbpath_option = []
query_call = Command.run(
command=['rpm', '--root', self.root_dir, '-Va'],
command=['rpm', '--root', self.root_dir, '-Va'] + dbpath_option,
raise_on_error=False
)
with open(filename, 'w') as verified:
@ -904,3 +914,11 @@ class SystemSetup(object):
)
with open(filename, 'w') as verified:
verified.write(query_call.output)
def _get_rpm_database_location(self):
try:
return Command.run(
['chroot', self.root_dir, 'rpm', '-E', '%_dbpath']
).output.rstrip('\r\n')
except Exception:
return None

View File

@ -12,7 +12,8 @@ from kiwi.xml_description import XMLDescription
from kiwi.xml_state import XMLState
from kiwi.exceptions import (
KiwiScriptFailed,
KiwiImportDescriptionError
KiwiImportDescriptionError,
KiwiCommandError
)
from kiwi.defaults import Defaults
@ -752,9 +753,41 @@ class TestSystemSetup(object):
mock_command.return_value = command
result = self.setup.export_package_list('target_dir')
assert result == 'target_dir/some-image.x86_64-1.2.3.packages'
mock_command.assert_called_once_with([
'rpm', '--root', 'root_dir', '-qa', '--qf',
'%{NAME}|%{EPOCH}|%{VERSION}|%{RELEASE}|%{ARCH}|%{DISTURL}\\n'
mock_command.assert_has_calls([
call(['chroot', 'root_dir', 'rpm', '-E', '%_dbpath']),
call([
'rpm', '--root', 'root_dir', '-qa', '--qf',
'%{NAME}|%{EPOCH}|%{VERSION}|%{RELEASE}|%{ARCH}|%{DISTURL}\\n',
'--dbpath', 'packages_data'
])
])
mock_open.assert_called_once_with(
'target_dir/some-image.x86_64-1.2.3.packages', 'w'
)
@patch('kiwi.system.setup.Command.run')
@patch_open
def test_export_package_list_rpm_no_dbpath(
self, mock_open, mock_command
):
cmd = mock.Mock()
cmd.output = 'packages_data'
def dbpath_check_fails(command):
if '%_dbpath' in command:
raise KiwiCommandError()
else:
return cmd
mock_command.side_effect = dbpath_check_fails
result = self.setup.export_package_list('target_dir')
assert result == 'target_dir/some-image.x86_64-1.2.3.packages'
mock_command.assert_has_calls([
call(['chroot', 'root_dir', 'rpm', '-E', '%_dbpath']),
call([
'rpm', '--root', 'root_dir', '-qa', '--qf',
'%{NAME}|%{EPOCH}|%{VERSION}|%{RELEASE}|%{ARCH}|%{DISTURL}\\n'
])
])
mock_open.assert_called_once_with(
'target_dir/some-image.x86_64-1.2.3.packages', 'w'
@ -791,10 +824,41 @@ class TestSystemSetup(object):
mock_command.return_value = command
result = self.setup.export_package_verification('target_dir')
assert result == 'target_dir/some-image.x86_64-1.2.3.verified'
mock_command.assert_called_once_with(
command=['rpm', '--root', 'root_dir', '-Va'],
raise_on_error=False
mock_command.assert_has_calls([
call(['chroot', 'root_dir', 'rpm', '-E', '%_dbpath']),
call(command=[
'rpm', '--root', 'root_dir', '-Va',
'--dbpath', 'verification_data'
], raise_on_error=False)
])
mock_open.assert_called_once_with(
'target_dir/some-image.x86_64-1.2.3.verified', 'w'
)
@patch('kiwi.system.setup.Command.run')
@patch_open
def test_export_package_verification_no_dbpath(
self, mock_open, mock_command
):
cmd = mock.Mock()
cmd.output = 'verification_data'
def dbpath_check_fails(command, raise_on_error):
if '%_dbpath' in command:
raise KiwiCommandError()
else:
return cmd
mock_command.side_effect = dbpath_check_fails
result = self.setup.export_package_verification('target_dir')
assert result == 'target_dir/some-image.x86_64-1.2.3.verified'
mock_command.assert_has_calls([
call(['chroot', 'root_dir', 'rpm', '-E', '%_dbpath']),
call(
command=['rpm', '--root', 'root_dir', '-Va'],
raise_on_error=False
)
])
mock_open.assert_called_once_with(
'target_dir/some-image.x86_64-1.2.3.verified', 'w'
)