Example #1
0
    def setup(self):
        repository = mock.Mock()
        repository.root_dir = 'root-dir'
        repository.signing_keys = ['key-file.asc']
        repository.unauthenticated = 'false'
        repository.components = ['main', 'restricted']

        repository.runtime_config = mock.Mock(
            return_value={
                'apt_get_args': ['-c', 'apt.conf', '-y'],
                'command_env': ['env'],
                'distribution': 'xenial',
                'distribution_path': 'xenial_path'
            })
        self.manager = PackageManagerApt(repository)
    def setup(self):
        repository = mock.Mock()
        repository.root_dir = 'root-dir'

        root_bind = mock.Mock()
        root_bind.move_to_root = mock.Mock(
            return_value=['root-moved-arguments'])
        repository.root_bind = root_bind

        repository.runtime_config = mock.Mock(
            return_value={
                'apt_get_args': ['-c', 'apt.conf', '-y'],
                'command_env': ['env'],
                'distribution': 'xenial',
                'distribution_path': 'xenial_path'
            })
        self.manager = PackageManagerApt(repository)
Example #3
0
    def __new__(self, repository, package_manager, custom_args=None):
        if package_manager == 'zypper':
            manager = PackageManagerZypper(repository, custom_args)
        elif package_manager == 'dnf' or package_manager == 'yum':
            manager = PackageManagerDnf(repository, custom_args)
        elif package_manager == 'apt-get':
            manager = PackageManagerApt(repository, custom_args)
        else:
            raise KiwiPackageManagerSetupError(
                'Support for package manager %s not implemented' %
                package_manager)

        log.info('Using package manager backend: %s', package_manager)
        return manager
Example #4
0
    def setup(self):
        repository = mock.Mock()
        repository.root_dir = 'root-dir'

        root_bind = mock.Mock()
        root_bind.move_to_root = mock.Mock(
            return_value=['root-moved-arguments']
        )
        repository.root_bind = root_bind

        repository.runtime_config = mock.Mock(
            return_value={
                'apt_get_args': ['-c', 'apt.conf', '-y'],
                'command_env': ['env'],
                'distribution': 'xenial',
                'distribution_path': 'xenial_path'
            }
        )
        self.manager = PackageManagerApt(repository)
Example #5
0
class TestPackageManagerApt:
    def setup(self):
        repository = mock.Mock()
        repository.root_dir = 'root-dir'
        repository.signing_keys = ['key-file.asc']
        repository.unauthenticated = 'false'
        repository.components = ['main', 'restricted']

        root_bind = mock.Mock()
        root_bind.move_to_root = mock.Mock(
            return_value=['root-moved-arguments'])
        repository.root_bind = root_bind

        repository.runtime_config = mock.Mock(
            return_value={
                'apt_get_args': ['-c', 'apt.conf', '-y'],
                'command_env': ['env'],
                'distribution': 'xenial',
                'distribution_path': 'xenial_path'
            })
        self.manager = PackageManagerApt(repository)

    def test_request_package(self):
        self.manager.request_package('name')
        assert self.manager.package_requests == ['name']

    @patch('kiwi.logger.log.warning')
    def test_request_collection(self, mock_log_warn):
        self.manager.request_collection('name')
        assert self.manager.collection_requests == []
        assert mock_log_warn.called

    @patch('kiwi.logger.log.warning')
    def test_request_product(self, mock_log_warn):
        self.manager.request_product('name')
        assert self.manager.product_requests == []
        assert mock_log_warn.called

    @patch('kiwi.logger.log.warning')
    def test_request_package_exclusion(self, mock_log_warn):
        self.manager.request_package_exclusion('name')
        assert self.manager.exclude_requests == []
        assert mock_log_warn.called

    def test_process_install_requests_bootstrap_no_dist(self):
        self.manager.distribution = None
        with raises(KiwiDebootstrapError):
            self.manager.process_install_requests_bootstrap()

    @patch('os.path.exists')
    def test_process_install_requests_bootstrap_no_debootstrap_script(
            self, mock_exists):
        mock_exists.return_value = False
        with raises(KiwiDebootstrapError):
            self.manager.process_install_requests_bootstrap()

    @patch('kiwi.command.Command.run')
    @patch('os.path.exists')
    @patch('kiwi.package_manager.apt.Path.wipe')
    def test_process_install_requests_bootstrap_failed_debootstrap(
            self, mock_wipe, mock_exists, mock_run):
        self.manager.request_package('apt-get')
        mock_run.side_effect = Exception
        mock_exists.return_value = True
        with raises(KiwiDebootstrapError):
            self.manager.process_install_requests_bootstrap()

    @patch('kiwi.logger.log.warning')
    @patch('kiwi.command.Command.call')
    @patch('kiwi.command.Command.run')
    @patch('os.path.exists')
    @patch('kiwi.package_manager.apt.DataSync')
    def test_process_install_requests_bootstrap(self, mock_sync, mock_exists,
                                                mock_run, mock_call,
                                                mock_warn):
        self.manager.request_package('apt-get')
        self.manager.request_package('vim')
        data = mock.Mock()
        mock_sync.return_value = data
        mock_exists.return_value = True
        self.manager.process_install_requests_bootstrap()
        mock_sync.assert_called_once_with('root-dir.debootstrap/', 'root-dir')
        data.sync_data.assert_called_once_with(
            options=['-a', '-H', '-X', '-A'])
        assert mock_run.call_args_list == [
            call(command=['mountpoint', '-q', 'root-dir/dev'],
                 raise_on_error=False),
            call([
                'debootstrap', '--no-check-gpg', '--variant=minbase',
                '--components=main,restricted', 'xenial',
                'root-dir.debootstrap', 'xenial_path'
            ], ['env']),
            call(['chroot', 'root-dir', 'apt-key', 'add', 'key-file.asc'],
                 ['env']),
            call(['rm', '-r', '-f', 'root-dir.debootstrap']),
            call([
                'chroot', 'root-dir', 'apt-get', 'root-moved-arguments',
                'update'
            ], ['env'])
        ]
        mock_call.assert_called_once_with([
            'chroot', 'root-dir', 'apt-get', 'root-moved-arguments', 'install',
            'vim'
        ], ['env'])
        assert mock_warn.called

    @patch('kiwi.command.Command.call')
    @patch('kiwi.command.Command.run')
    def test_process_install_requests(self, mock_run, mock_call):
        self.manager.request_package('vim')
        self.manager.process_install_requests()
        self.manager.root_bind.move_to_root(self.manager.apt_get_args)
        mock_call.assert_called_once_with([
            'chroot', 'root-dir', 'apt-get', 'root-moved-arguments', 'install',
            'vim'
        ], ['env'])

    @patch('kiwi.command.Command.call')
    @patch('kiwi.command.Command.run')
    def test_process_delete_requests(self, mock_run, mock_call):
        self.manager.request_package('vim')
        self.manager.process_delete_requests()
        mock_call.assert_called_once_with([
            'chroot', 'root-dir', 'apt-get', 'root-moved-arguments',
            '--auto-remove', 'remove', 'vim'
        ], ['env'])

    @patch('kiwi.command.Command.call')
    @patch('kiwi.command.Command.run')
    def test_process_delete_requests_force(self, mock_run, mock_call):
        self.manager.request_package('vim')
        self.manager.process_delete_requests(True)
        mock_call.assert_called_once_with(
            ['chroot', 'root-dir', 'dpkg', '--force-all', '-r', 'vim'],
            ['env'])

    @patch('kiwi.command.Command.run')
    def test_process_delete_requests_package_missing(self, mock_run):
        mock_run.side_effect = Exception
        self.manager.request_package('vim')
        with raises(KiwiRequestError):
            self.manager.process_delete_requests()

    @patch('kiwi.command.Command.call')
    def test_update(self, mock_call):
        self.manager.update()
        self.manager.root_bind.move_to_root(self.manager.apt_get_args)
        mock_call.assert_called_once_with([
            'chroot', 'root-dir', 'apt-get', 'root-moved-arguments', 'upgrade'
        ], ['env'])

    def test_process_only_required(self):
        self.manager.process_only_required()
        assert self.manager.custom_args == ['--no-install-recommends']

    def test_process_plus_recommended(self):
        self.manager.process_only_required()
        assert self.manager.custom_args == ['--no-install-recommends']
        self.manager.process_plus_recommended()
        assert '--no-install-recommends' not in self.manager.custom_args

    def test_match_package_installed(self):
        assert self.manager.match_package_installed('foo', 'Unpacking foo')

    def test_match_package_deleted(self):
        assert self.manager.match_package_deleted('foo', 'Removing foo')
Example #6
0
class TestPackageManagerApt(object):
    def setup(self):
        repository = mock.Mock()
        repository.root_dir = 'root-dir'

        root_bind = mock.Mock()
        root_bind.move_to_root = mock.Mock(
            return_value=['root-moved-arguments']
        )
        repository.root_bind = root_bind

        repository.runtime_config = mock.Mock(
            return_value={
                'apt_get_args': ['-c', 'apt.conf', '-y'],
                'command_env': ['env'],
                'distribution': 'xenial',
                'distribution_path': 'xenial_path'
            }
        )
        self.manager = PackageManagerApt(repository)

    def test_request_package(self):
        self.manager.request_package('name')
        assert self.manager.package_requests == ['name']

    @patch('kiwi.logger.log.warning')
    def test_request_collection(self, mock_log_warn):
        self.manager.request_collection('name')
        assert self.manager.collection_requests == []
        assert mock_log_warn.called

    @patch('kiwi.logger.log.warning')
    def test_request_product(self, mock_log_warn):
        self.manager.request_product('name')
        assert self.manager.product_requests == []
        assert mock_log_warn.called

    @raises(KiwiDebootstrapError)
    def test_process_install_requests_bootstrap_no_dist(self):
        self.manager.distribution = None
        self.manager.process_install_requests_bootstrap()

    @patch('os.path.exists')
    @raises(KiwiDebootstrapError)
    def test_process_install_requests_bootstrap_no_debootstrap_script(
        self, mock_exists
    ):
        mock_exists.return_value = False
        self.manager.process_install_requests_bootstrap()

    @patch('kiwi.command.Command.run')
    @patch('os.path.exists')
    @patch('kiwi.package_manager.apt.Path.wipe')
    @raises(KiwiDebootstrapError)
    def test_process_install_requests_bootstrap_failed_debootstrap(
        self, mock_wipe, mock_exists, mock_run
    ):
        self.manager.request_package('apt-get')
        mock_run.side_effect = Exception
        mock_exists.return_value = True
        self.manager.process_install_requests_bootstrap()

    @patch('kiwi.command.Command.call')
    @patch('kiwi.command.Command.run')
    @patch('os.path.exists')
    @patch('kiwi.package_manager.apt.DataSync')
    def test_process_install_requests_bootstrap(
        self, mock_sync, mock_exists, mock_run, mock_call
    ):
        self.manager.request_package('apt-get')
        self.manager.request_package('vim')
        data = mock.Mock()
        mock_sync.return_value = data
        mock_exists.return_value = True
        self.manager.process_install_requests_bootstrap()
        mock_sync.assert_called_once_with(
            'root-dir.debootstrap/', 'root-dir'
        )
        data.sync_data.assert_called_once_with(
            options=['-a', '-H', '-X', '-A']
        )
        assert mock_run.call_args_list == [
            call(command=['mountpoint', 'root-dir/dev'], raise_on_error=False),
            call([
                'debootstrap', '--no-check-gpg', 'xenial',
                'root-dir.debootstrap', 'xenial_path'],
                ['env']),
            call([
                'bash', '-c',
                'rm -r -f root-dir.debootstrap &&' + \
                ' chroot root-dir apt-get root-moved-arguments update'],
                ['env'])
        ]
        mock_call.assert_called_once_with([
            'chroot', 'root-dir', 'apt-get',
            'root-moved-arguments', 'install', 'vim'],
            ['env']
        )

    @patch('kiwi.command.Command.call')
    @patch('kiwi.command.Command.run')
    def test_process_install_requests(self, mock_run, mock_call):
        self.manager.request_package('vim')
        self.manager.process_install_requests()
        chroot_apt_get_args = self.manager.root_bind.move_to_root(
            self.manager.apt_get_args
        )
        mock_call.assert_called_once_with([
            'chroot', 'root-dir', 'apt-get',
            'root-moved-arguments', 'install', 'vim'],
            ['env']
        )

    @patch('kiwi.command.Command.call')
    @patch('kiwi.command.Command.run')
    def test_process_delete_requests(self, mock_run, mock_call):
        self.manager.request_package('vim')
        self.manager.process_delete_requests()
        mock_call.assert_called_once_with([
            'chroot', 'root-dir', 'apt-get',
            'root-moved-arguments', 'remove', 'vim'],
            ['env']
        )

    @patch('kiwi.command.Command.call')
    def test_update(self, mock_call):
        self.manager.update()
        chroot_apt_get_args = self.manager.root_bind.move_to_root(
            self.manager.apt_get_args
        )
        mock_call.assert_called_once_with([
            'chroot', 'root-dir', 'apt-get',
            'root-moved-arguments', 'upgrade'],
            ['env']
        )

    def test_process_only_required(self):
        self.manager.process_only_required()

    def test_match_package_installed(self):
        assert self.manager.match_package_installed('foo', 'Unpacking foo')

    def test_match_package_deleted(self):
        assert self.manager.match_package_deleted('foo', 'Removing foo')

    def test_database_consistent(self):
        self.manager.database_consistent()

    def test_dump_reload_package_database(self):
        self.manager.dump_reload_package_database()
Example #7
0
class TestPackageManagerApt:
    @fixture(autouse=True)
    def inject_fixtures(self, caplog):
        self._caplog = caplog

    def setup(self):
        repository = mock.Mock()
        repository.root_dir = 'root-dir'
        repository.signing_keys = ['key-file.asc']
        repository.keyring = 'trusted.gpg'
        repository.unauthenticated = 'false'
        repository.components = ['main', 'restricted']

        repository.runtime_config = mock.Mock(
            return_value={
                'apt_get_args': ['-c', 'apt.conf', '-y'],
                'command_env': ['env'],
                'distribution': 'xenial',
                'distribution_path': 'xenial_path'
            })
        self.manager = PackageManagerApt(repository)

    def test_request_package(self):
        self.manager.request_package('name')
        assert self.manager.package_requests == ['name']

    def test_request_collection(self):
        self.manager.request_collection('name')
        with self._caplog.at_level(logging.WARNING):
            assert self.manager.collection_requests == []

    def test_request_product(self):
        self.manager.request_product('name')
        with self._caplog.at_level(logging.WARNING):
            assert self.manager.product_requests == []

    def test_request_package_exclusion(self):
        self.manager.request_package_exclusion('name')
        with self._caplog.at_level(logging.WARNING):
            assert self.manager.exclude_requests == []

    def test_process_install_requests_bootstrap_no_dist(self):
        self.manager.distribution = None
        with raises(KiwiDebootstrapError):
            self.manager.process_install_requests_bootstrap()

    @patch('os.path.exists')
    def test_process_install_requests_bootstrap_no_debootstrap_script(
            self, mock_exists):
        mock_exists.return_value = False
        with raises(KiwiDebootstrapError):
            self.manager.process_install_requests_bootstrap()

    @patch('kiwi.command.Command.run')
    @patch('os.path.exists')
    @patch('kiwi.package_manager.apt.Path.wipe')
    def test_process_install_requests_bootstrap_failed_debootstrap(
            self, mock_wipe, mock_exists, mock_run):
        self.manager.request_package('apt-get')
        mock_run.side_effect = Exception
        mock_exists.return_value = True
        with raises(KiwiDebootstrapError):
            self.manager.process_install_requests_bootstrap()

    @patch('kiwi.command.Command.call')
    @patch('kiwi.command.Command.run')
    @patch('os.path.exists')
    @patch('kiwi.package_manager.apt.DataSync')
    def test_process_install_requests_bootstrap(self, mock_sync, mock_exists,
                                                mock_run, mock_call):
        self.manager.request_package('apt-get')
        self.manager.request_package('vim')
        data = mock.Mock()
        mock_sync.return_value = data
        mock_exists.return_value = True
        self.manager.process_install_requests_bootstrap()
        mock_sync.assert_called_once_with('root-dir.debootstrap/', 'root-dir')
        data.sync_data.assert_called_once_with(
            exclude=['proc', 'sys', 'dev'], options=['-a', '-H', '-X', '-A'])
        assert mock_run.call_args_list == [
            call([
                'debootstrap', '--keyring=trusted.gpg', '--variant=minbase',
                '--components=main,restricted', 'xenial',
                'root-dir.debootstrap', 'xenial_path'
            ], ['env']),
            call(['rm', '-r', '-f', 'root-dir.debootstrap']),
            call([
                'chroot', 'root-dir', 'apt-get', '-c', 'apt.conf', '-y',
                'update'
            ], ['env'])
        ]
        mock_call.assert_called_once_with([
            'chroot', 'root-dir', 'apt-get', '-c', 'apt.conf', '-y', 'install',
            'vim'
        ], ['env'])

    @patch('kiwi.command.Command.call')
    @patch('kiwi.command.Command.run')
    @patch('os.path.exists')
    @patch('kiwi.package_manager.apt.DataSync')
    def test_process_install_requests_bootstrap_no_gpg_check(
            self, mock_sync, mock_exists, mock_run, mock_call):
        self.manager.request_package('apt-get')
        self.manager.request_package('vim')
        data = mock.Mock()
        mock_sync.return_value = data
        mock_exists.side_effect = lambda x: True if 'xenial' in x else False
        self.manager.process_install_requests_bootstrap()
        mock_sync.assert_called_once_with('root-dir.debootstrap/', 'root-dir')
        data.sync_data.assert_called_once_with(
            exclude=['proc', 'sys', 'dev'], options=['-a', '-H', '-X', '-A'])
        assert mock_run.call_args_list == [
            call([
                'debootstrap', '--no-check-gpg', '--variant=minbase',
                '--components=main,restricted', 'xenial',
                'root-dir.debootstrap', 'xenial_path'
            ], ['env']),
            call([
                'chroot', 'root-dir', 'apt-get', '-c', 'apt.conf', '-y',
                'update'
            ], ['env'])
        ]
        mock_call.assert_called_once_with([
            'chroot', 'root-dir', 'apt-get', '-c', 'apt.conf', '-y', 'install',
            'vim'
        ], ['env'])

    @patch('kiwi.command.Command.call')
    @patch('kiwi.command.Command.run')
    def test_process_install_requests(self, mock_run, mock_call):
        self.manager.request_package('vim')
        self.manager.process_install_requests()
        mock_call.assert_called_once_with([
            'chroot', 'root-dir', 'apt-get', '-c', 'apt.conf', '-y', 'install',
            'vim'
        ], ['env'])

    @patch('kiwi.command.Command.call')
    @patch('kiwi.command.Command.run')
    def test_process_delete_requests(self, mock_run, mock_call):
        self.manager.request_package('vim')
        self.manager.process_delete_requests()
        mock_call.assert_called_once_with([
            'chroot', 'root-dir', 'apt-get', '-c', 'apt.conf', '-y',
            '--auto-remove', 'remove', 'vim'
        ], ['env'])

    @patch('kiwi.command.Command.call')
    @patch('kiwi.command.Command.run')
    def test_process_delete_requests_force(self, mock_run, mock_call):
        self.manager.request_package('vim')
        self.manager.process_delete_requests(True)
        mock_call.assert_called_once_with(
            ['chroot', 'root-dir', 'dpkg', '--force-all', '-r', 'vim'],
            ['env'])

    @patch('kiwi.command.Command.run')
    def test_process_delete_requests_package_missing(self, mock_run):
        mock_run.side_effect = Exception
        self.manager.request_package('vim')
        with raises(KiwiRequestError):
            self.manager.process_delete_requests()

    @patch('kiwi.command.Command.call')
    def test_update(self, mock_call):
        self.manager.update()
        mock_call.assert_called_once_with([
            'chroot', 'root-dir', 'apt-get', '-c', 'apt.conf', '-y', 'upgrade'
        ], ['env'])

    def test_process_only_required(self):
        self.manager.process_only_required()
        assert self.manager.custom_args == ['--no-install-recommends']

    def test_process_plus_recommended(self):
        self.manager.process_only_required()
        assert self.manager.custom_args == ['--no-install-recommends']
        self.manager.process_plus_recommended()
        assert '--no-install-recommends' not in self.manager.custom_args

    def test_match_package_installed(self):
        assert self.manager.match_package_installed('foo', 'Unpacking foo')

    def test_match_package_deleted(self):
        assert self.manager.match_package_deleted('foo', 'Removing foo')
Example #8
0
class TestPackageManagerApt:
    @fixture(autouse=True)
    def inject_fixtures(self, caplog):
        self._caplog = caplog

    def setup(self):
        repository = Mock()
        repository.root_dir = 'root-dir'
        repository.signing_keys = ['key-file.asc']
        repository.keyring = 'trusted.gpg'
        repository.unauthenticated = 'false'
        repository.components = ['main', 'restricted']

        repository.runtime_config = Mock(
            return_value={
                'apt_get_args': ['-c', 'apt.conf', '-y'],
                'command_env': ['env'],
                'distribution': 'xenial',
                'distribution_path': 'xenial_path'
            })
        self.manager = PackageManagerApt(repository)

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

    def test_request_package(self):
        self.manager.request_package('name')
        assert self.manager.package_requests == ['name']

    def test_request_collection(self):
        self.manager.request_collection('name')
        with self._caplog.at_level(logging.WARNING):
            assert self.manager.collection_requests == []

    def test_request_product(self):
        self.manager.request_product('name')
        with self._caplog.at_level(logging.WARNING):
            assert self.manager.product_requests == []

    def test_request_package_exclusion(self):
        self.manager.request_package_exclusion('name')
        with self._caplog.at_level(logging.WARNING):
            assert self.manager.exclude_requests == []

    def test_setup_repository_modules(self):
        self.manager.setup_repository_modules({})

    def test_process_install_requests_bootstrap_no_dist(self):
        self.manager.distribution = None
        with raises(KiwiDebootstrapError):
            self.manager.process_install_requests_bootstrap()

    @patch('os.path.exists')
    def test_process_install_requests_bootstrap_no_debootstrap_script(
            self, mock_exists):
        mock_exists.return_value = False
        with raises(KiwiDebootstrapError):
            self.manager.process_install_requests_bootstrap()

    @patch('kiwi.command.Command.call')
    @patch('kiwi.package_manager.apt.os.path.exists')
    @patch('kiwi.package_manager.apt.Path.wipe')
    def test_process_install_requests_bootstrap_failed_debootstrap(
            self, mock_wipe, mock_exists, mock_call):
        self.manager.request_package('apt')
        mock_call.side_effect = Exception
        mock_exists.return_value = True
        mock_root_bind = Mock()
        with raises(KiwiDebootstrapError):
            self.manager.process_install_requests_bootstrap(mock_root_bind)

    @patch('kiwi.package_manager.apt.os.path.exists')
    def test_get_error_details(self, mock_exists):
        mock_exists.return_value = True
        with patch('builtins.open', create=True) as mock_open:
            file_handle = mock_open.return_value.__enter__.return_value
            file_handle.read.return_value = 'log-data'
            assert self.manager.get_error_details() == \
                file_handle.read.return_value
        mock_open.assert_called_once_with(
            'root-dir/debootstrap/debootstrap.log')

    @patch('kiwi.package_manager.apt.os.path.exists')
    def test_get_error_details_no_log_file(self, mock_exists):
        mock_exists.return_value = False
        assert self.manager.get_error_details() == \
            "logfile 'root-dir/debootstrap/debootstrap.log' does not exist"

    @patch('kiwi.package_manager.apt.os.path.exists')
    def test_get_error_details_logfile_is_empty(self, mock_exists):
        mock_exists.return_value = True
        with patch('builtins.open', create=True) as mock_open:
            file_handle = mock_open.return_value.__enter__.return_value
            file_handle.read.return_value = ''
            assert self.manager.get_error_details() == \
                'logfile is empty'

    @patch('kiwi.command.Command.call')
    @patch('kiwi.package_manager.apt.Path.wipe')
    @patch('kiwi.package_manager.apt.os.path.exists')
    def test_process_install_requests_bootstrap(self, mock_exists, mock_wipe,
                                                mock_call):
        self.manager.request_package('apt')
        self.manager.request_package('vim')
        call_result = Mock()
        call_result.process.communicate.return_value = ('stdout', 'stderr')
        mock_call.return_value = call_result
        mock_root_bind = Mock()
        mock_exists.return_value = True
        self.manager.process_install_requests_bootstrap(mock_root_bind)
        mock_call.assert_called_once_with([
            'debootstrap', '--keyring=trusted.gpg', '--variant=minbase',
            '--include=vim', '--components=main,restricted', 'xenial',
            'root-dir', 'xenial_path'
        ], ['env'])
        assert mock_wipe.call_args_list == [
            call('root-dir/dev/fd'),
            call('root-dir/dev/pts')
        ]
        mock_root_bind.umount_kernel_file_systems.assert_called_once_with()

    def test_post_process_install_requests_bootstrap(self):
        mock_root_bind = Mock()
        self.manager.post_process_install_requests_bootstrap(mock_root_bind)
        mock_root_bind.mount_kernel_file_systems.assert_called_once_with()

    @patch('kiwi.command.Command.call')
    @patch('kiwi.package_manager.apt.Path.wipe')
    @patch('kiwi.package_manager.apt.os.path.exists')
    def test_process_install_requests_bootstrap_no_gpg_check(
            self, mock_exists, mock_wipe, mock_call):
        self.manager.request_package('apt')
        self.manager.request_package('vim')
        call_result = Mock()
        call_result.process.communicate.return_value = ('stdout', 'stderr')
        mock_root_bind = Mock()
        mock_call.return_value = call_result
        mock_exists.side_effect = lambda x: True if 'xenial' in x else False
        self.manager.process_install_requests_bootstrap(mock_root_bind)
        mock_call.assert_called_once_with([
            'debootstrap', '--no-check-gpg', '--variant=minbase',
            '--include=vim', '--components=main,restricted', 'xenial',
            'root-dir', 'xenial_path'
        ], ['env'])
        assert mock_wipe.call_args_list == [
            call('root-dir/dev/fd'),
            call('root-dir/dev/pts')
        ]
        mock_root_bind.umount_kernel_file_systems.assert_called_once_with()

    @patch('kiwi.command.Command.call')
    @patch('kiwi.command.Command.run')
    def test_process_install_requests(self, mock_run, mock_call):
        self.manager.request_package('vim')
        self.manager.process_install_requests()
        mock_call.assert_called_once_with([
            'chroot', 'root-dir', 'apt-get', '-c', 'apt.conf', '-y', 'install',
            'vim'
        ], ['env'])

    @patch('kiwi.command.Command.call')
    @patch('kiwi.command.Command.run')
    def test_process_delete_requests(self, mock_run, mock_call):
        self.manager.request_package('vim')
        self.manager.process_delete_requests()
        mock_call.assert_called_once_with([
            'chroot', 'root-dir', 'apt-get', '-c', 'apt.conf', '-y',
            '--auto-remove', 'remove', 'vim'
        ], ['env'])

    @patch('kiwi.command.Command.call')
    @patch('kiwi.command.Command.run')
    @patch('kiwi.package_manager.apt.Path.wipe')
    @patch('glob.iglob')
    def test_process_delete_requests_force(self, mock_iglob, mock_Path_wipe,
                                           mock_run, mock_call):
        mock_iglob.return_value = ['glob-result']
        self.manager.request_package('vim')
        self.manager.process_delete_requests(True)
        assert mock_run.call_args_list == [
            call(['chroot', 'root-dir', 'dpkg', '-l', 'vim']),
            call([
                'cp', 'root-dir/usr/sbin/ldconfig',
                'root-dir/usr/sbin/ldconfig.orig'
            ]),
            call(['cp', 'root-dir/usr/bin/true', 'root-dir/usr/sbin/ldconfig'])
        ]
        mock_call.assert_called_once_with([
            'chroot', 'root-dir', 'dpkg', '--remove',
            '--force-remove-reinstreq', '--force-remove-essential',
            '--force-depends', 'vim'
        ], ['env'])
        mock_iglob.call_args_list == [
            call('root-dir/var/lib/dpkg/info/vim*.pre*'),
            call('root-dir/var/lib/dpkg/info/vim*.post*')
        ]
        mock_Path_wipe.call_args_list == [
            call('glob-result'), call('glob-result')
        ]

    @patch('kiwi.command.Command.run')
    def test_post_process_delete_requests(self, mock_run):
        self.manager.post_process_delete_requests()
        assert mock_run.call_args_list == [
            call([
                'mv', 'root-dir/usr/sbin/ldconfig.orig',
                'root-dir/usr/sbin/ldconfig'
            ])
        ]

    @patch('kiwi.command.Command.run')
    def test_process_delete_requests_package_missing(self, mock_run):
        mock_run.side_effect = Exception
        self.manager.request_package('vim')
        with raises(KiwiRequestError):
            self.manager.process_delete_requests()

    @patch('kiwi.command.Command.call')
    def test_update(self, mock_call):
        self.manager.update()
        mock_call.assert_called_once_with([
            'chroot', 'root-dir', 'apt-get', '-c', 'apt.conf', '-y', 'upgrade'
        ], ['env'])

    def test_process_only_required(self):
        self.manager.process_only_required()
        assert self.manager.custom_args == ['--no-install-recommends']

    def test_process_plus_recommended(self):
        self.manager.process_only_required()
        assert self.manager.custom_args == ['--no-install-recommends']
        self.manager.process_plus_recommended()
        assert '--no-install-recommends' not in self.manager.custom_args

    def test_match_package_installed(self):
        assert self.manager.match_package_installed('foo', 'Unpacking foo')

    def test_match_package_deleted(self):
        assert self.manager.match_package_deleted('foo', 'Removing foo')
class TestPackageManagerApt(object):
    def setup(self):
        repository = mock.Mock()
        repository.root_dir = 'root-dir'

        root_bind = mock.Mock()
        root_bind.move_to_root = mock.Mock(
            return_value=['root-moved-arguments'])
        repository.root_bind = root_bind

        repository.runtime_config = mock.Mock(
            return_value={
                'apt_get_args': ['-c', 'apt.conf', '-y'],
                'command_env': ['env'],
                'distribution': 'xenial',
                'distribution_path': 'xenial_path'
            })
        self.manager = PackageManagerApt(repository)

    def test_request_package(self):
        self.manager.request_package('name')
        assert self.manager.package_requests == ['name']

    @patch('kiwi.logger.log.warning')
    def test_request_collection(self, mock_log_warn):
        self.manager.request_collection('name')
        assert self.manager.collection_requests == []
        assert mock_log_warn.called

    @patch('kiwi.logger.log.warning')
    def test_request_product(self, mock_log_warn):
        self.manager.request_product('name')
        assert self.manager.product_requests == []
        assert mock_log_warn.called

    @raises(KiwiDebootstrapError)
    def test_process_install_requests_bootstrap_no_dist(self):
        self.manager.distribution = None
        self.manager.process_install_requests_bootstrap()

    @patch('os.path.exists')
    @raises(KiwiDebootstrapError)
    def test_process_install_requests_bootstrap_no_debootstrap_script(
            self, mock_exists):
        mock_exists.return_value = False
        self.manager.process_install_requests_bootstrap()

    @patch('kiwi.command.Command.run')
    @patch('os.path.exists')
    @patch('kiwi.package_manager.apt.Path.wipe')
    @raises(KiwiDebootstrapError)
    def test_process_install_requests_bootstrap_failed_debootstrap(
            self, mock_wipe, mock_exists, mock_run):
        self.manager.request_package('apt-get')
        mock_run.side_effect = Exception
        mock_exists.return_value = True
        self.manager.process_install_requests_bootstrap()

    @patch('kiwi.command.Command.call')
    @patch('kiwi.command.Command.run')
    @patch('os.path.exists')
    @patch('kiwi.package_manager.apt.DataSync')
    def test_process_install_requests_bootstrap(self, mock_sync, mock_exists,
                                                mock_run, mock_call):
        self.manager.request_package('apt-get')
        self.manager.request_package('vim')
        data = mock.Mock()
        mock_sync.return_value = data
        mock_exists.return_value = True
        self.manager.process_install_requests_bootstrap()
        mock_sync.assert_called_once_with('root-dir.debootstrap/', 'root-dir')
        data.sync_data.assert_called_once_with(
            options=['-a', '-H', '-X', '-A'])
        assert mock_run.call_args_list == [
            call(command=['mountpoint', 'root-dir/dev'], raise_on_error=False),
            call([
                'debootstrap', '--no-check-gpg', 'xenial',
                'root-dir.debootstrap', 'xenial_path'],
                ['env']),
            call([
                'bash', '-c',
                'rm -r -f root-dir.debootstrap &&' + \
                ' chroot root-dir apt-get root-moved-arguments update'],
                ['env'])
        ]
        mock_call.assert_called_once_with([
            'chroot', 'root-dir', 'apt-get', 'root-moved-arguments', 'install',
            'vim'
        ], ['env'])

    @patch('kiwi.command.Command.call')
    @patch('kiwi.command.Command.run')
    def test_process_install_requests(self, mock_run, mock_call):
        self.manager.request_package('vim')
        self.manager.process_install_requests()
        chroot_apt_get_args = self.manager.root_bind.move_to_root(
            self.manager.apt_get_args)
        mock_call.assert_called_once_with([
            'chroot', 'root-dir', 'apt-get', 'root-moved-arguments', 'install',
            'vim'
        ], ['env'])

    @patch('kiwi.command.Command.call')
    @patch('kiwi.command.Command.run')
    def test_process_delete_requests(self, mock_run, mock_call):
        self.manager.request_package('vim')
        self.manager.process_delete_requests()
        mock_call.assert_called_once_with([
            'chroot', 'root-dir', 'apt-get', 'root-moved-arguments', 'remove',
            'vim'
        ], ['env'])

    @patch('kiwi.command.Command.call')
    def test_update(self, mock_call):
        self.manager.update()
        chroot_apt_get_args = self.manager.root_bind.move_to_root(
            self.manager.apt_get_args)
        mock_call.assert_called_once_with([
            'chroot', 'root-dir', 'apt-get', 'root-moved-arguments', 'upgrade'
        ], ['env'])

    def test_process_only_required(self):
        self.manager.process_only_required()

    def test_match_package_installed(self):
        assert self.manager.match_package_installed('foo', 'Unpacking foo')

    def test_match_package_deleted(self):
        assert self.manager.match_package_deleted('foo', 'Removing foo')

    def test_database_consistent(self):
        self.manager.database_consistent()

    def test_dump_reload_package_database(self):
        self.manager.dump_reload_package_database()