From d1fced5e9986dea113d8bdce7403d95b0e1484dc Mon Sep 17 00:00:00 2001 From: David Cassany Date: Fri, 2 Feb 2018 18:16:28 +0100 Subject: [PATCH] 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 --- kiwi/system/setup.py | 22 +++++++++- test/unit/system_setup_test.py | 78 +++++++++++++++++++++++++++++++--- 2 files changed, 91 insertions(+), 9 deletions(-) diff --git a/kiwi/system/setup.py b/kiwi/system/setup.py index 0a9354b4..8a51104e 100644 --- a/kiwi/system/setup.py +++ b/kiwi/system/setup.py @@ -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 diff --git a/test/unit/system_setup_test.py b/test/unit/system_setup_test.py index 29a41a5c..a8758d5e 100644 --- a/test/unit/system_setup_test.py +++ b/test/unit/system_setup_test.py @@ -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' )