Пример #1
0
class TestRpm:
    def setup(self):
        self.rpm_host = Rpm()
        self.rpm_image = Rpm('root_dir')
        self.macro_file = os.sep.join([
            Defaults.get_custom_rpm_macros_path(),
            Defaults.get_custom_rpm_bootstrap_macro_name()
        ])

    def setup_method(self, cls):
        self.setup()

    @patch('kiwi.utils.rpm.Command.run')
    def test_expand_query(self, mock_Command_run):
        self.rpm_host.expand_query('foo')
        mock_Command_run.assert_called_once_with(['rpm', '-E', 'foo'])
        mock_Command_run.reset_mock()
        self.rpm_image.expand_query('foo')
        mock_Command_run.assert_called_once_with(
            ['chroot', 'root_dir', 'rpm', '-E', 'foo'])

    @patch('kiwi.utils.rpm.Command.run')
    def test_get_query(self, mock_Command_run):
        rpmdb_call = Mock()
        rpmdb_call.output = '%_dbpath  %{_usr}/lib/sysimage/rpm'
        mock_Command_run.return_value = rpmdb_call
        assert self.rpm_host.get_query('_dbpath') == \
            '%{_usr}/lib/sysimage/rpm'
        mock_Command_run.assert_called_once_with(['rpmdb', '--showrc'])
        mock_Command_run.reset_mock()
        assert self.rpm_image.get_query('_dbpath') == \
            '%{_usr}/lib/sysimage/rpm'
        mock_Command_run.assert_called_once_with(
            ['chroot', 'root_dir', 'rpmdb', '--showrc'])

    def test_set_config_value(self):
        self.rpm_host.set_config_value('key', 'value')
        assert self.rpm_host.custom_config[0] == '%key\tvalue'

    @patch('kiwi.utils.rpm.Path.wipe')
    def test_wipe_config(self, mock_Path_wipe):
        self.rpm_host.wipe_config()
        mock_Path_wipe.assert_called_once_with(os.sep + self.macro_file)
        mock_Path_wipe.reset_mock()
        self.rpm_image.wipe_config()
        mock_Path_wipe.assert_called_once_with(
            os.sep.join(['root_dir', self.macro_file]))

    @patch('kiwi.utils.rpm.Path.create')
    def test_write_config(self, mock_Path_create):
        self.rpm_host.set_config_value('key', 'value')

        m_open = mock_open()
        with patch('builtins.open', m_open, create=True):
            self.rpm_host.write_config()

        m_open.assert_called_once_with(os.sep + self.macro_file, 'w')
        assert m_open.return_value.write.call_args_list == [
            call('%key\tvalue\n')
        ]

        self.rpm_image.set_config_value('foo', 'bar')

        m_open.reset_mock()
        with patch('builtins.open', m_open, create=True):
            self.rpm_image.write_config()

        m_open.assert_called_once_with(
            os.sep.join(['root_dir', self.macro_file]), 'w')
        assert m_open.return_value.write.call_args_list == [
            call('%foo\tbar\n')
        ]
Пример #2
0
class RpmDataBase:
    """
    **Setup RPM database configuration**
    """
    def __init__(self, root_dir, macro_file=None):
        self.rpmdb_host = Rpm()
        self.rpmdb_image = Rpm(root_dir, macro_file)
        self.root_dir = root_dir

    def has_rpm(self):
        """
        Check if rpmdb binary was found in root_dir to indicate
        that the rpm system is present.
        """
        rpm_search_env = {
            'PATH': os.sep.join([self.root_dir, 'usr', 'bin'])
        }
        rpm_bin = Path.which(
            'rpmdb', custom_env=rpm_search_env, access_mode=os.X_OK
        )
        if not rpm_bin:
            return False
        return True

    def rebuild_database(self):
        """
        Rebuild image rpm database taking current macro setup into account
        """
        Command.run(
            ['chroot', self.root_dir, 'rpmdb', '--rebuilddb']
        )

    def init_database(self):
        Command.run(
            ['rpm', '--root', self.root_dir, '--initdb']
        )

    def set_macro_from_string(self, setting):
        """
        Setup given macro setting in image rpm configuration.
        The following format for the setting is expected:

            macro_base_key_name%macro_value.

        If this expression is not matched the macro setup will be
        skipped. Please note the macro_base_key_name includes
        the name of the macro as rpm expects it exlcuding the
        leading '%' character.

        Also note macro defintions must happen before calling
        set_database_* methods in order to become effective

        :param string setting: macro key and value as one string
        """
        match = re.match('(.*)%(.*)', setting)
        if match:
            self.rpmdb_image.set_config_value(
                match.group(1), match.group(2)
            )

    def write_config(self):
        self.rpmdb_image.write_config()

    def import_signing_key_to_image(self, key):
        """
        Import the given signing key to the image rpm database
        """
        Command.run(
            [
                'rpm', '--root', self.root_dir, '--import', key,
                '--dbpath', self.rpmdb_host.expand_query('%_dbpath')
            ]
        )

    def set_database_to_host_path(self):
        """
        Setup dbpath to point to the host rpm dbpath configuration
        and write the configuration file
        """
        self.rpmdb_image.set_config_value(
            '_dbpath', self.rpmdb_host.get_query('_dbpath')
        )
        self.rpmdb_image.write_config()

    def link_database_to_host_path(self):
        """
        Create a link from host database location to image database
        location. The link is only created if both locations differ
        and if the host database location does not exist.
        """
        rpm_host_dbpath = self.rpmdb_host.expand_query('%_dbpath')
        rpm_image_dbpath = self.rpmdb_image.expand_query('%_dbpath')
        if rpm_host_dbpath != rpm_image_dbpath:
            root_rpm_host_dbpath = os.path.normpath(
                os.sep.join([self.root_dir, rpm_host_dbpath])
            )
            if not os.path.exists(root_rpm_host_dbpath):
                Path.create(os.path.dirname(root_rpm_host_dbpath))
                host_to_root = ''.join(
                    '../' for i in os.path.dirname(
                        rpm_host_dbpath
                    ).lstrip(os.sep).split(os.sep)
                )
                Command.run(
                    [
                        'ln', '-s', os.path.normpath(
                            os.sep.join([host_to_root, rpm_image_dbpath])
                        ), root_rpm_host_dbpath
                    ]
                )

    def set_database_to_image_path(self):
        """
        Setup dbpath to point to the image rpm dbpath configuration
        Rebuild the database such that it gets moved to the standard
        path and delete KIWI's custom macro setup
        """
        self.rpmdb_image.wipe_config()
        rpm_image_dbpath = self.rpmdb_image.expand_query('%_dbpath')
        rpm_host_dbpath = self.rpmdb_host.expand_query('%_dbpath')
        if rpm_image_dbpath != rpm_host_dbpath:
            root_rpm_image_dbpath = os.path.normpath(
                os.sep.join([self.root_dir, rpm_image_dbpath])
            )
            root_rpm_host_dbpath = os.path.normpath(
                os.sep.join([self.root_dir, rpm_host_dbpath])
            )

            if os.path.islink(root_rpm_host_dbpath):
                self.rebuild_database()
                return

            if os.path.islink(root_rpm_image_dbpath):
                os.unlink(root_rpm_image_dbpath)
            else:
                Path.wipe(root_rpm_image_dbpath)

            self.rpmdb_image.set_config_value(
                '_dbpath', rpm_host_dbpath
            )
            self.rpmdb_image.set_config_value(
                '_dbpath_rebuild', self.rpmdb_image.get_query('_dbpath')
            )

            self.rpmdb_image.write_config()
            self.rebuild_database()
            self.rpmdb_image.wipe_config()

            root_rpm_alternatives = os.sep.join(
                [root_rpm_host_dbpath, 'alternatives']
            )
            if os.path.exists(root_rpm_alternatives):
                Command.run(
                    ['mv', root_rpm_alternatives, root_rpm_image_dbpath]
                )

            Path.wipe(root_rpm_host_dbpath)
Пример #3
0
class RpmDataBase(object):
    """
    **Setup RPM database configuration**
    """
    def __init__(self, root_dir, macro_file=None):
        self.rpmdb_host = Rpm()
        self.rpmdb_image = Rpm(root_dir, macro_file)
        self.root_dir = root_dir

    def has_rpm(self):
        """
        Check if rpm binary was found in root_dir
        """
        rpm_search_env = {'PATH': os.sep.join([self.root_dir, 'usr', 'bin'])}
        rpm_bin = Path.which('rpm',
                             custom_env=rpm_search_env,
                             access_mode=os.X_OK)
        if not rpm_bin:
            return False
        return True

    def rebuild_database(self):
        """
        Rebuild image rpm database taking current macro setup into account
        """
        Command.run(['chroot', self.root_dir, 'rpmdb', '--rebuilddb'])

    def set_macro_from_string(self, setting):
        """
        Setup given macro setting in image rpm configuration.
        The following format for the setting is expected:

            macro_base_key_name%macro_value.

        If this expression is not matched the macro setup will be
        skipped. Please note the macro_base_key_name includes
        the name of the macro as rpm expects it exlcuding the
        leading '%' character.

        Also note macro defintions must happen before calling
        set_database_* methods in order to become effective

        :param string setting: macro key and value as one string
        """
        match = re.match('(.*)%(.*)', setting)
        if match:
            self.rpmdb_image.set_config_value(match.group(1), match.group(2))

    def write_config(self):
        self.rpmdb_image.write_config()

    def set_database_to_host_path(self):
        """
        Setup dbpath to point to the host rpm dbpath configuration
        and write the configuration file
        """
        self.rpmdb_image.set_config_value('_dbpath',
                                          self.rpmdb_host.get_query('_dbpath'))
        self.rpmdb_image.write_config()

    def set_database_to_image_path(self):
        """
        Setup dbpath to point to the image rpm dbpath configuration
        Rebuild the database such that it gets moved to the standard
        path and delete KIWI's custom macro setup
        """
        self.rpmdb_image.wipe_config()
        rpm_image_dbpath = self.rpmdb_image.expand_query('%_dbpath')
        if rpm_image_dbpath != self.rpmdb_host.expand_query('%_dbpath'):
            self.rpmdb_image.set_config_value(
                '_dbpath_rebuild', self.rpmdb_image.get_query('_dbpath'))
            Path.wipe(os.sep.join([self.root_dir, rpm_image_dbpath]))
            self.rpmdb_image.write_config()
            self.rebuild_database()
            self.rpmdb_image.wipe_config()