Esempio n. 1
0
class TestRepositoryApt:
    @patch('kiwi.repository.apt.Temporary.new_file')
    @patch('kiwi.repository.apt.PackageManagerTemplateAptGet')
    @patch('kiwi.repository.apt.Path.create')
    def setup(self, mock_path, mock_template, mock_temp):
        self.apt_conf = mock.Mock()
        mock_template.return_value = self.apt_conf

        template = mock.Mock()
        self.apt_conf.get_host_template.return_value = template

        tmpfile = mock.Mock()
        tmpfile.name = 'tmpfile'
        mock_temp.return_value = tmpfile
        root_bind = mock.Mock()
        root_bind.root_dir = '../data'
        root_bind.shared_location = '/shared-dir'

        with patch('builtins.open', create=True):
            self.repo = RepositoryApt(
                root_bind, custom_args=['exclude_docs']
            )

            self.exclude_docs = True
            self.apt_conf.get_host_template.assert_called_once_with(
                self.exclude_docs
            )
            template.substitute.assert_called_once_with(
                {
                    'apt_shared_base': '/shared-dir/apt-get',
                    'unauthenticated': 'true'
                }
            )
            repo = RepositoryApt(
                root_bind, custom_args=['check_signatures']
            )
            assert repo.custom_args == []
            assert repo.unauthenticated == 'false'

            repo = RepositoryApt(root_bind)
            assert repo.custom_args == []
            assert repo.unauthenticated == 'true'

    @patch('kiwi.repository.apt.Temporary.new_file')
    @patch('kiwi.repository.apt.PackageManagerTemplateAptGet')
    @patch('kiwi.repository.apt.Path.create')
    def setup_method(self, cls, mock_path, mock_template, mock_temp):
        self.setup()

    def test_use_default_location(self):
        template = mock.Mock()
        template.substitute.return_value = 'template-data'
        self.apt_conf.get_image_template.return_value = template
        with patch('builtins.open', create=True):
            self.repo.use_default_location()
        assert self.repo.shared_apt_get_dir['sources-dir'] == \
            '../data/etc/apt/sources.list.d'
        assert self.repo.shared_apt_get_dir['preferences-dir'] == \
            '../data/etc/apt/preferences.d'
        self.apt_conf.get_image_template.assert_called_once_with(
            self.exclude_docs
        )
        template.substitute.assert_called_once_with(
            {'apt_shared_base': '../data/etc/apt', 'unauthenticated': 'true'}
        )

    def test_runtime_config(self):
        assert self.repo.runtime_config()['apt_get_args'] == \
            self.repo.apt_get_args
        assert self.repo.runtime_config()['command_env'] == \
            self.repo.command_env

    def test_setup_package_database_configuration(self):
        # just pass
        self.repo.setup_package_database_configuration()

    @patch('os.path.exists')
    @patch('kiwi.command.Command.run')
    def test_add_repo_with_priority(self, mock_Command_run, mock_exists):
        mock_exists.return_value = True
        with patch('builtins.open', create=True) as mock_open:
            mock_open.return_value = MagicMock(spec=io.IOBase)
            file_handle = mock_open.return_value.__enter__.return_value
            self.repo.add_repo(
                'foo', '/srv/my-repo', 'deb', '42', 'xenial', 'a b',
                customization_script='custom_script'
            )
            assert mock_open.call_args_list == [
                call('/shared-dir/apt-get/sources.list.d/foo.sources', 'w'),
                call('/shared-dir/apt-get/preferences.d/foo.pref', 'w')
            ]
            assert file_handle.write.call_args_list == [
                call(
                    'Types: deb\n'
                    'URIs: file:/srv/my-repo\n'
                    'Suites: xenial\n'
                    'Components: a b\n'
                ),
                call('Package: *\n'),
                call('Pin: origin ""\n'),
                call('Pin-Priority: 42\n')
            ]
            assert mock_Command_run.call_args_list == [
                call(
                    [
                        'bash', '--norc', 'custom_script',
                        '/shared-dir/apt-get/sources.list.d/foo.sources'
                    ]
                ),
                call(
                    [
                        'bash', '--norc', 'custom_script',
                        '/shared-dir/apt-get/preferences.d/foo.pref'
                    ]
                )
            ]
        mock_exists.return_value = False
        with patch('builtins.open', create=True) as mock_open:
            mock_open.return_value = MagicMock(spec=io.IOBase)
            file_handle = mock_open.return_value.__enter__.return_value
            self.repo.add_repo(
                'foo',
                'http://download.opensuse.org/repositories/V:/A:/C/Debian_9.0/',
                'deb', '99', 'xenial', 'a b'
            )
            assert file_handle.write.call_args_list == [
                call(
                    'Types: deb\n'
                    'URIs: http://download.opensuse.org/repositories/V:/A:/C/Debian_9.0/\n'
                    'Suites: xenial\n'
                    'Components: a b\n'
                ),
                call('Package: *\n'),
                call('Pin: origin "download.opensuse.org"\n'),
                call('Pin-Priority: 99\n')
            ]

    @patch('os.path.exists')
    def test_add_repo_distribution(self, mock_exists):
        mock_exists.return_value = True
        with patch('builtins.open', create=True) as mock_open:
            mock_open.return_value = MagicMock(spec=io.IOBase)
            file_handle = mock_open.return_value.__enter__.return_value
            self.repo.add_repo(
                'foo', 'kiwi_iso_mount/uri', 'deb', None, 'xenial', 'a b'
            )
            file_handle.write.assert_called_once_with(
                'Types: deb\n'
                'URIs: file:/kiwi_iso_mount/uri\n'
                'Suites: xenial\n'
                'Components: a b\n'
            )
            mock_open.assert_called_once_with(
                '/shared-dir/apt-get/sources.list.d/foo.sources', 'w'
            )

    @patch('os.path.exists')
    def test_add_repo_distribution_without_gpgchecks(self, mock_exists):
        mock_exists.return_value = True
        with patch('builtins.open', create=True) as mock_open:
            mock_open.return_value = MagicMock(spec=io.IOBase)
            file_handle = mock_open.return_value.__enter__.return_value
            self.repo.add_repo(
                'foo', 'kiwi_iso_mount/uri', 'deb', None, 'xenial', 'a b',
                repo_gpgcheck=False, pkg_gpgcheck=False
            )
            file_handle.write.assert_called_once_with(
                'Types: deb\n'
                'URIs: file:/kiwi_iso_mount/uri\n'
                'Suites: xenial\n'
                'Components: a b\n'
                'trusted: yes\n'
                'check-valid-until: no\n'
            )
            mock_open.assert_called_once_with(
                '/shared-dir/apt-get/sources.list.d/foo.sources', 'w'
            )

    @patch('os.path.exists')
    def test_add_repo_distribution_default_component(self, mock_exists):
        mock_exists.return_value = True
        with patch('builtins.open', create=True) as mock_open:
            mock_open.return_value = MagicMock(spec=io.IOBase)
            file_handle = mock_open.return_value.__enter__.return_value
            self.repo.add_repo(
                'foo', '/kiwi_iso_mount/uri', 'deb', None, 'xenial'
            )
            file_handle.write.assert_called_once_with(
                'Types: deb\n'
                'URIs: file:/kiwi_iso_mount/uri\n'
                'Suites: xenial\n'
                'Components: main\n'
            )
            mock_open.assert_called_once_with(
                '/shared-dir/apt-get/sources.list.d/foo.sources', 'w'
            )

    @patch('os.path.exists')
    def test_add_repo_flat(self, mock_exists):
        mock_exists.return_value = False
        with patch('builtins.open', create=True) as mock_open:
            mock_open.return_value = MagicMock(spec=io.IOBase)
            file_handle = mock_open.return_value.__enter__.return_value
            self.repo.add_repo(
                'foo', 'http://repo.com', 'deb'
            )
            file_handle.write.assert_called_once_with(
                'Types: deb\n'
                'URIs: http://repo.com\n'
                'Suites: ./\n'
            )
            mock_open.assert_called_once_with(
                '/shared-dir/apt-get/sources.list.d/foo.sources', 'w'
            )

    @patch('kiwi.repository.apt.os.unlink')
    @patch('kiwi.repository.apt.os.path.exists')
    @patch('kiwi.repository.apt.Command.run')
    def test_import_trusted_keys(self, mock_run, mock_exists, mock_unlink):
        mock_exists.return_value = True
        self.repo.import_trusted_keys(['key-file-a.asc', 'key-file-b.asc'])
        assert mock_run.call_args_list == [
            call([
                'gpg', '--no-options', '--no-default-keyring',
                '--no-auto-check-trustdb', '--trust-model', 'always',
                '--keyring', '/shared-dir/apt-get/trusted-keybox.gpg',
                '--import', '--ignore-time-conflict', 'key-file-a.asc'
            ]), call([
                'gpg', '--no-options', '--no-default-keyring',
                '--no-auto-check-trustdb', '--trust-model', 'always',
                '--keyring', '/shared-dir/apt-get/trusted-keybox.gpg',
                '--import', '--ignore-time-conflict', 'key-file-b.asc'
            ]), call([
                'gpg', '--no-options', '--no-default-keyring',
                '--no-auto-check-trustdb', '--trust-model', 'always',
                '--keyring', '/shared-dir/apt-get/trusted-keybox.gpg',
                '--export', '--yes', '--output',
                '/shared-dir/apt-get/trusted.gpg'
            ])
        ]

    @patch('kiwi.path.Path.wipe')
    def test_delete_repo(self, mock_wipe):
        self.repo.delete_repo('foo')
        mock_wipe.assert_called_once_with(
            '/shared-dir/apt-get/sources.list.d/foo.sources'
        )

    @patch('kiwi.path.Path.wipe')
    @patch('os.walk')
    def test_cleanup_unused_repos(self, mock_walk, mock_path):
        mock_walk.return_value = [
            ('/foo', ('bar', 'baz'), ('spam', 'eggs'))
        ]
        self.repo.repo_names = ['eggs']
        self.repo.cleanup_unused_repos()
        mock_path.assert_called_once_with(
            '/shared-dir/apt-get/sources.list.d/spam'
        )

    @patch('kiwi.path.Path.wipe')
    @patch('kiwi.path.Path.create')
    def test_delete_all_repos(self, mock_create, mock_wipe):
        self.repo.delete_all_repos()
        mock_wipe.assert_called_once_with(
            '/shared-dir/apt-get/sources.list.d'
        )
        mock_create.assert_called_once_with(
            '/shared-dir/apt-get/sources.list.d'
        )

    @patch('kiwi.path.Path.wipe')
    def test_delete_repo_cache(self, mock_wipe):
        self.repo.delete_repo_cache('foo')
        assert mock_wipe.call_args_list == [
            call('/shared-dir/apt-get/archives'),
            call('/shared-dir/apt-get/pkgcache.bin'),
            call('/shared-dir/apt-get/srcpkgcache.bin')
        ]
Esempio n. 2
0
class TestRepositoryApt(object):
    @patch('kiwi.repository.apt.NamedTemporaryFile')
    @patch_open
    @patch('kiwi.repository.apt.PackageManagerTemplateAptGet')
    @patch('kiwi.repository.apt.Path.create')
    def setup(self, mock_path, mock_template, mock_open, mock_temp):
        self.context_manager_mock = mock.Mock()
        self.file_mock = mock.Mock()
        self.enter_mock = mock.Mock()
        self.exit_mock = mock.Mock()
        self.enter_mock.return_value = self.file_mock
        setattr(self.context_manager_mock, '__enter__', self.enter_mock)
        setattr(self.context_manager_mock, '__exit__', self.exit_mock)

        self.apt_conf = mock.Mock()
        mock_template.return_value = self.apt_conf

        template = mock.Mock()
        self.apt_conf.get_host_template.return_value = template

        tmpfile = mock.Mock()
        tmpfile.name = 'tmpfile'
        mock_temp.return_value = tmpfile
        root_bind = mock.Mock()
        root_bind.move_to_root = mock.Mock(
            return_value=['root-moved-arguments']
        )
        root_bind.root_dir = '../data'
        root_bind.shared_location = '/shared-dir'

        self.repo = RepositoryApt(root_bind, ['exclude_docs'])

        self.exclude_docs = True
        self.apt_conf.get_host_template.assert_called_once_with(
            self.exclude_docs
        )
        template.substitute.assert_called_once_with(
            {
                'apt_shared_base': '/shared-dir/apt-get',
                'unauthenticated': 'true'
            }
        )

    @patch_open
    @patch('kiwi.repository.apt.NamedTemporaryFile')
    @patch('kiwi.repository.apt.Path.create')
    def test_post_init_no_custom_args(self, mock_open, mock_path, mock_temp):
        self.repo.post_init()
        assert self.repo.custom_args == []

    @patch_open
    @patch('kiwi.repository.apt.NamedTemporaryFile')
    @patch('kiwi.repository.apt.Path.create')
    def test_post_init_with_custom_args(self, mock_open, mock_path, mock_temp):
        self.repo.post_init(['check_signatures'])
        assert self.repo.custom_args == []
        assert self.repo.unauthenticated == 'false'

    @patch_open
    def test_use_default_location(self, mock_open):
        template = mock.Mock()
        self.apt_conf.get_image_template.return_value = template
        self.repo.use_default_location()
        self.apt_conf.get_image_template.assert_called_once_with(
            self.exclude_docs
        )
        template.substitute.assert_called_once_with(
            {'apt_shared_base': '../data/etc/apt', 'unauthenticated': 'true'}
        )

    def test_runtime_config(self):
        assert self.repo.runtime_config()['apt_get_args'] == \
            self.repo.apt_get_args
        assert self.repo.runtime_config()['command_env'] == \
            self.repo.command_env

    @patch('os.path.exists')
    @patch_open
    def test_add_repo_with_priority(self, mock_open, mock_exists):
        mock_open.return_value = self.context_manager_mock
        mock_exists.return_value = True
        self.repo.add_repo(
            'foo', '/srv/my-repo', 'deb', '42', 'xenial', 'a b'
        )
        assert mock_open.call_args_list == [
            call('/shared-dir/apt-get/sources.list.d/foo.list', 'w'),
            call('/shared-dir/apt-get/preferences.d/foo.pref', 'w')
        ]
        print(self.file_mock.write.call_args_list)
        assert self.file_mock.write.call_args_list == [
            call('deb file://srv/my-repo xenial a b\n'),
            call('Package: *\n'),
            call('Pin: origin ""\n'),
            call('Pin-Priority: 42\n')
        ]
        self.file_mock.reset_mock()
        mock_exists.return_value = False
        self.repo.add_repo(
            'foo',
            'http://download.opensuse.org/repositories/V:/A:/C/Debian_9.0/',
            'deb', '99', 'xenial', 'a b'
        )
        assert self.file_mock.write.call_args_list == [
            call(
                'deb http://download.opensuse.org/repositories/' +
                'V:/A:/C/Debian_9.0/ xenial a b\n'
            ),
            call('Package: *\n'),
            call('Pin: origin "download.opensuse.org"\n'),
            call('Pin-Priority: 99\n')
        ]

    @patch('os.path.exists')
    @patch_open
    def test_add_repo_distribution(self, mock_open, mock_exists):
        mock_open.return_value = self.context_manager_mock
        mock_exists.return_value = True
        self.repo.add_repo(
            'foo', 'kiwi_iso_mount/uri', 'deb', None, 'xenial', 'a b'
        )
        self.file_mock.write.assert_called_once_with(
            'deb file:/kiwi_iso_mount/uri xenial a b\n'
        )
        mock_open.assert_called_once_with(
            '/shared-dir/apt-get/sources.list.d/foo.list', 'w'
        )

    @patch('os.path.exists')
    @patch_open
    def test_add_repo_distribution_with_gpgchecks(
        self, mock_open, mock_exists
    ):
        mock_open.return_value = self.context_manager_mock
        mock_exists.return_value = True
        self.repo.add_repo(
            'foo', 'kiwi_iso_mount/uri', 'deb', None, 'xenial', 'a b',
            repo_gpgcheck=True, pkg_gpgcheck=False
        )
        self.file_mock.write.assert_called_once_with(
            'deb [trusted=no] file:/kiwi_iso_mount/uri xenial a b\n'
        )
        mock_open.assert_called_once_with(
            '/shared-dir/apt-get/sources.list.d/foo.list', 'w'
        )

    @patch('os.path.exists')
    @patch_open
    def test_add_repo_distribution_default_component(
        self, mock_open, mock_exists
    ):
        mock_open.return_value = self.context_manager_mock
        mock_exists.return_value = True
        self.repo.add_repo(
            'foo', 'kiwi_iso_mount/uri', 'deb', None, 'xenial'
        )
        self.file_mock.write.assert_called_once_with(
            'deb file:/kiwi_iso_mount/uri xenial main\n'
        )
        mock_open.assert_called_once_with(
            '/shared-dir/apt-get/sources.list.d/foo.list', 'w'
        )

    @patch('os.path.exists')
    @patch_open
    def test_add_repo_flat(self, mock_open, mock_exists):
        mock_open.return_value = self.context_manager_mock
        mock_exists.return_value = False
        self.repo.add_repo(
            'foo', 'http://repo.com', 'deb'
        )
        self.file_mock.write.assert_called_once_with(
            'deb http://repo.com ./\n'
        )
        mock_open.assert_called_once_with(
            '/shared-dir/apt-get/sources.list.d/foo.list', 'w'
        )

    def test_import_trusted_keys(self):
        self.repo.import_trusted_keys(['key-file-a.asc', 'key-file-b.asc'])
        assert self.repo.signing_keys == ['key-file-a.asc', 'key-file-b.asc']

    @patch('kiwi.path.Path.wipe')
    def test_delete_repo(self, mock_wipe):
        self.repo.delete_repo('foo')
        mock_wipe.assert_called_once_with(
            '/shared-dir/apt-get/sources.list.d/foo.list'
        )

    @patch('kiwi.path.Path.wipe')
    @patch('os.walk')
    def test_cleanup_unused_repos(self, mock_walk, mock_path):
        mock_walk.return_value = [
            ('/foo', ('bar', 'baz'), ('spam', 'eggs'))
        ]
        self.repo.repo_names = ['eggs']
        self.repo.cleanup_unused_repos()
        mock_path.assert_called_once_with(
            '/shared-dir/apt-get/sources.list.d/spam'
        )

    @patch('kiwi.path.Path.wipe')
    @patch('kiwi.path.Path.create')
    def test_delete_all_repos(self, mock_create, mock_wipe):
        self.repo.delete_all_repos()
        mock_wipe.assert_called_once_with(
            '/shared-dir/apt-get/sources.list.d'
        )
        mock_create.assert_called_once_with(
            '/shared-dir/apt-get/sources.list.d'
        )

    @patch('kiwi.path.Path.wipe')
    def test_delete_repo_cache(self, mock_wipe):
        self.repo.delete_repo_cache('foo')
        assert mock_wipe.call_args_list == [
            call('/shared-dir/apt-get/archives'),
            call('/shared-dir/apt-get/pkgcache.bin'),
            call('/shared-dir/apt-get/srcpkgcache.bin')
        ]
Esempio n. 3
0
class TestRepositoryApt(object):
    @patch('kiwi.repository.apt.NamedTemporaryFile')
    @patch_open
    @patch('kiwi.repository.apt.PackageManagerTemplateAptGet')
    @patch('kiwi.repository.apt.Path.create')
    def setup(self, mock_path, mock_template, mock_open, mock_temp):
        self.context_manager_mock = mock.Mock()
        self.file_mock = mock.Mock()
        self.enter_mock = mock.Mock()
        self.exit_mock = mock.Mock()
        self.enter_mock.return_value = self.file_mock
        setattr(self.context_manager_mock, '__enter__', self.enter_mock)
        setattr(self.context_manager_mock, '__exit__', self.exit_mock)

        self.apt_conf = mock.Mock()
        mock_template.return_value = self.apt_conf

        template = mock.Mock()
        self.apt_conf.get_host_template.return_value = template

        tmpfile = mock.Mock()
        tmpfile.name = 'tmpfile'
        mock_temp.return_value = tmpfile
        root_bind = mock.Mock()
        root_bind.move_to_root = mock.Mock(
            return_value=['root-moved-arguments']
        )
        root_bind.root_dir = '../data'
        root_bind.shared_location = '/shared-dir'

        self.repo = RepositoryApt(root_bind)

        self.apt_conf.get_host_template.assert_called_once_with()
        template.substitute.assert_called_once_with(
            {'apt_shared_base': '/shared-dir/apt-get'}
        )
        
    @patch_open
    def test_use_default_location(self, mock_open):
        template = mock.Mock()
        self.apt_conf.get_image_template.return_value = template
        self.repo.use_default_location()
        self.apt_conf.get_image_template.assert_called_once_with()
        template.substitute.assert_called_once_with()

    def test_runtime_config(self):
        assert self.repo.runtime_config()['apt_get_args'] == \
            self.repo.apt_get_args
        assert self.repo.runtime_config()['command_env'] == \
            self.repo.command_env

    @patch('os.path.exists')
    @patch_open
    def test_add_repo_distribution(self, mock_open, mock_exists):
        mock_open.return_value = self.context_manager_mock
        mock_exists.return_value = True
        self.repo.add_repo(
            'foo', 'kiwi_iso_mount/uri', 'deb', None, 'xenial', 'a b'
        )
        self.file_mock.write.assert_called_once_with(
            'deb file:/kiwi_iso_mount/uri xenial a b\n'
        )
        mock_open.assert_called_once_with(
            '/shared-dir/apt-get/sources.list.d/foo.list', 'w'
        )

    @patch('os.path.exists')
    @patch_open
    def test_add_repo_distribution_default_component(
        self, mock_open, mock_exists
    ):
        mock_open.return_value = self.context_manager_mock
        mock_exists.return_value = True
        self.repo.add_repo(
            'foo', 'kiwi_iso_mount/uri', 'deb', None, 'xenial'
        )
        self.file_mock.write.assert_called_once_with(
            'deb file:/kiwi_iso_mount/uri xenial main\n'
        )
        mock_open.assert_called_once_with(
            '/shared-dir/apt-get/sources.list.d/foo.list', 'w'
        )

    @patch('os.path.exists')
    @patch_open
    def test_add_repo_flat(self, mock_open, mock_exists):
        mock_open.return_value = self.context_manager_mock
        mock_exists.return_value = False
        self.repo.add_repo(
            'foo', 'http://repo.com', 'deb'
        )
        self.file_mock.write.assert_called_once_with(
            'deb http://repo.com ./\n'
        )
        mock_open.assert_called_once_with(
            '/shared-dir/apt-get/sources.list.d/foo.list', 'w'
        )

    @patch('kiwi.path.Path.wipe')
    def test_delete_repo(self, mock_wipe):
        self.repo.delete_repo('foo')
        mock_wipe.assert_called_once_with(
            '/shared-dir/apt-get/sources.list.d/foo.list'
        )

    @patch('kiwi.path.Path.wipe')
    @patch('os.walk')
    def test_cleanup_unused_repos(self, mock_walk, mock_path):
        mock_walk.return_value = [
            ('/foo', ('bar', 'baz'), ('spam', 'eggs'))
        ]
        self.repo.repo_names = ['eggs']
        self.repo.cleanup_unused_repos()
        mock_path.assert_called_once_with(
            '/shared-dir/apt-get/sources.list.d/spam'
        )

    @patch('kiwi.path.Path.wipe')
    @patch('kiwi.path.Path.create')
    def test_delete_all_repos(self, mock_create, mock_wipe):
        self.repo.delete_all_repos()
        mock_wipe.assert_called_once_with(
            '/shared-dir/apt-get/sources.list.d'
        )
        mock_create.assert_called_once_with(
            '/shared-dir/apt-get/sources.list.d'
        )
Esempio n. 4
0
class TestRepositoryApt:
    @patch('kiwi.repository.apt.NamedTemporaryFile')
    @patch('kiwi.repository.apt.PackageManagerTemplateAptGet')
    @patch('kiwi.repository.apt.Path.create')
    def setup(self, mock_path, mock_template, mock_temp):
        self.apt_conf = mock.Mock()
        mock_template.return_value = self.apt_conf

        template = mock.Mock()
        self.apt_conf.get_host_template.return_value = template

        tmpfile = mock.Mock()
        tmpfile.name = 'tmpfile'
        mock_temp.return_value = tmpfile
        root_bind = mock.Mock()
        root_bind.move_to_root = mock.Mock(
            return_value=['root-moved-arguments'])
        root_bind.root_dir = '../data'
        root_bind.shared_location = '/shared-dir'

        with patch('builtins.open', create=True):
            self.repo = RepositoryApt(root_bind, ['exclude_docs'])

        self.exclude_docs = True
        self.apt_conf.get_host_template.assert_called_once_with(
            self.exclude_docs)
        template.substitute.assert_called_once_with({
            'apt_shared_base': '/shared-dir/apt-get',
            'unauthenticated': 'true'
        })

    @patch('kiwi.repository.apt.NamedTemporaryFile')
    @patch('kiwi.repository.apt.Path.create')
    def test_post_init_no_custom_args(self, mock_path, mock_temp):
        self.repo.post_init()
        assert self.repo.custom_args == []

    @patch('kiwi.repository.apt.NamedTemporaryFile')
    @patch('kiwi.repository.apt.Path.create')
    def test_post_init_with_custom_args(self, mock_path, mock_temp):
        self.repo.post_init(['check_signatures'])
        assert self.repo.custom_args == []
        assert self.repo.unauthenticated == 'false'

    def test_use_default_location(self):
        template = mock.Mock()
        template.substitute.return_value = 'template-data'
        self.apt_conf.get_image_template.return_value = template
        self.repo.use_default_location()
        self.apt_conf.get_image_template.assert_called_once_with(
            self.exclude_docs)
        template.substitute.assert_called_once_with({
            'apt_shared_base': '../data/etc/apt',
            'unauthenticated': 'true'
        })

    def test_runtime_config(self):
        assert self.repo.runtime_config()['apt_get_args'] == \
            self.repo.apt_get_args
        assert self.repo.runtime_config()['command_env'] == \
            self.repo.command_env

    def test_setup_package_database_configuration(self):
        # just pass
        self.repo.setup_package_database_configuration()

    @patch('os.path.exists')
    def test_add_repo_with_priority(self, mock_exists):
        mock_exists.return_value = True
        with patch('builtins.open', create=True) as mock_open:
            mock_open.return_value = MagicMock(spec=io.IOBase)
            file_handle = mock_open.return_value.__enter__.return_value
            self.repo.add_repo('foo', '/srv/my-repo', 'deb', '42', 'xenial',
                               'a b')
            assert mock_open.call_args_list == [
                call('/shared-dir/apt-get/sources.list.d/foo.list', 'w'),
                call('/shared-dir/apt-get/preferences.d/foo.pref', 'w')
            ]
            assert file_handle.write.call_args_list == [
                call('deb file:/srv/my-repo xenial a b\n'),
                call('Package: *\n'),
                call('Pin: origin ""\n'),
                call('Pin-Priority: 42\n')
            ]
        mock_exists.return_value = False
        with patch('builtins.open', create=True) as mock_open:
            mock_open.return_value = MagicMock(spec=io.IOBase)
            file_handle = mock_open.return_value.__enter__.return_value
            self.repo.add_repo(
                'foo',
                'http://download.opensuse.org/repositories/V:/A:/C/Debian_9.0/',
                'deb', '99', 'xenial', 'a b')
            assert file_handle.write.call_args_list == [
                call('deb http://download.opensuse.org/repositories/'
                     'V:/A:/C/Debian_9.0/ xenial a b\n'),
                call('Package: *\n'),
                call('Pin: origin "download.opensuse.org"\n'),
                call('Pin-Priority: 99\n')
            ]

    @patch('os.path.exists')
    def test_add_repo_distribution(self, mock_exists):
        mock_exists.return_value = True
        with patch('builtins.open', create=True) as mock_open:
            mock_open.return_value = MagicMock(spec=io.IOBase)
            file_handle = mock_open.return_value.__enter__.return_value
            self.repo.add_repo('foo', 'kiwi_iso_mount/uri', 'deb', None,
                               'xenial', 'a b')
            file_handle.write.assert_called_once_with(
                'deb file:/kiwi_iso_mount/uri xenial a b\n')
            mock_open.assert_called_once_with(
                '/shared-dir/apt-get/sources.list.d/foo.list', 'w')

    @patch('os.path.exists')
    def test_add_repo_distribution_without_gpgchecks(self, mock_exists):
        mock_exists.return_value = True
        with patch('builtins.open', create=True) as mock_open:
            mock_open.return_value = MagicMock(spec=io.IOBase)
            file_handle = mock_open.return_value.__enter__.return_value
            self.repo.add_repo('foo',
                               'kiwi_iso_mount/uri',
                               'deb',
                               None,
                               'xenial',
                               'a b',
                               repo_gpgcheck=False,
                               pkg_gpgcheck=False)
            file_handle.write.assert_called_once_with(
                'deb [trusted=yes check-valid-until=no] file:/kiwi_iso_mount/uri xenial a b\n'
            )
            mock_open.assert_called_once_with(
                '/shared-dir/apt-get/sources.list.d/foo.list', 'w')

    @patch('os.path.exists')
    def test_add_repo_distribution_default_component(self, mock_exists):
        mock_exists.return_value = True
        with patch('builtins.open', create=True) as mock_open:
            mock_open.return_value = MagicMock(spec=io.IOBase)
            file_handle = mock_open.return_value.__enter__.return_value
            self.repo.add_repo('foo', '/kiwi_iso_mount/uri', 'deb', None,
                               'xenial')
            file_handle.write.assert_called_once_with(
                'deb file:/kiwi_iso_mount/uri xenial main\n')
            mock_open.assert_called_once_with(
                '/shared-dir/apt-get/sources.list.d/foo.list', 'w')

    @patch('os.path.exists')
    def test_add_repo_flat(self, mock_exists):
        mock_exists.return_value = False
        with patch('builtins.open', create=True) as mock_open:
            mock_open.return_value = MagicMock(spec=io.IOBase)
            file_handle = mock_open.return_value.__enter__.return_value
            self.repo.add_repo('foo', 'http://repo.com', 'deb')
            file_handle.write.assert_called_once_with(
                'deb http://repo.com ./\n')
            mock_open.assert_called_once_with(
                '/shared-dir/apt-get/sources.list.d/foo.list', 'w')

    def test_import_trusted_keys(self):
        self.repo.import_trusted_keys(['key-file-a.asc', 'key-file-b.asc'])
        assert self.repo.signing_keys == ['key-file-a.asc', 'key-file-b.asc']

    @patch('kiwi.path.Path.wipe')
    def test_delete_repo(self, mock_wipe):
        self.repo.delete_repo('foo')
        mock_wipe.assert_called_once_with(
            '/shared-dir/apt-get/sources.list.d/foo.list')

    @patch('kiwi.path.Path.wipe')
    @patch('os.walk')
    def test_cleanup_unused_repos(self, mock_walk, mock_path):
        mock_walk.return_value = [('/foo', ('bar', 'baz'), ('spam', 'eggs'))]
        self.repo.repo_names = ['eggs']
        self.repo.cleanup_unused_repos()
        mock_path.assert_called_once_with(
            '/shared-dir/apt-get/sources.list.d/spam')

    @patch('kiwi.path.Path.wipe')
    @patch('kiwi.path.Path.create')
    def test_delete_all_repos(self, mock_create, mock_wipe):
        self.repo.delete_all_repos()
        mock_wipe.assert_called_once_with('/shared-dir/apt-get/sources.list.d')
        mock_create.assert_called_once_with(
            '/shared-dir/apt-get/sources.list.d')

    @patch('kiwi.path.Path.wipe')
    def test_delete_repo_cache(self, mock_wipe):
        self.repo.delete_repo_cache('foo')
        assert mock_wipe.call_args_list == [
            call('/shared-dir/apt-get/archives'),
            call('/shared-dir/apt-get/pkgcache.bin'),
            call('/shared-dir/apt-get/srcpkgcache.bin')
        ]
Esempio n. 5
0
class TestRepositoryApt(object):
    @patch('kiwi.repository.apt.NamedTemporaryFile')
    @patch_open
    @patch('kiwi.repository.apt.PackageManagerTemplateAptGet')
    @patch('kiwi.repository.apt.Path.create')
    def setup(self, mock_path, mock_template, mock_open, mock_temp):
        self.context_manager_mock = mock.Mock()
        self.file_mock = mock.Mock()
        self.enter_mock = mock.Mock()
        self.exit_mock = mock.Mock()
        self.enter_mock.return_value = self.file_mock
        setattr(self.context_manager_mock, '__enter__', self.enter_mock)
        setattr(self.context_manager_mock, '__exit__', self.exit_mock)

        self.apt_conf = mock.Mock()
        mock_template.return_value = self.apt_conf

        template = mock.Mock()
        self.apt_conf.get_host_template.return_value = template

        tmpfile = mock.Mock()
        tmpfile.name = 'tmpfile'
        mock_temp.return_value = tmpfile
        root_bind = mock.Mock()
        root_bind.move_to_root = mock.Mock(
            return_value=['root-moved-arguments']
        )
        root_bind.root_dir = '../data'
        root_bind.shared_location = '/shared-dir'

        self.repo = RepositoryApt(root_bind, ['exclude_docs'])

        self.exclude_docs = True
        self.apt_conf.get_host_template.assert_called_once_with(
            self.exclude_docs
        )
        template.substitute.assert_called_once_with(
            {
                'apt_shared_base': '/shared-dir/apt-get',
                'unauthenticated': 'true'
            }
        )

    @patch_open
    @patch('kiwi.repository.apt.NamedTemporaryFile')
    @patch('kiwi.repository.apt.Path.create')
    def test_post_init_no_custom_args(self, mock_open, mock_path, mock_temp):
        self.repo.post_init()
        assert self.repo.custom_args == []

    @patch_open
    @patch('kiwi.repository.apt.NamedTemporaryFile')
    @patch('kiwi.repository.apt.Path.create')
    def test_post_init_with_custom_args(self, mock_open, mock_path, mock_temp):
        self.repo.post_init(['check_signatures'])
        assert self.repo.custom_args == []
        assert self.repo.unauthenticated == 'false'

    @patch_open
    def test_use_default_location(self, mock_open):
        template = mock.Mock()
        self.apt_conf.get_image_template.return_value = template
        self.repo.use_default_location()
        self.apt_conf.get_image_template.assert_called_once_with(
            self.exclude_docs
        )
        template.substitute.assert_called_once_with(
            {'apt_shared_base': '../data/etc/apt', 'unauthenticated': 'true'}
        )

    def test_runtime_config(self):
        assert self.repo.runtime_config()['apt_get_args'] == \
            self.repo.apt_get_args
        assert self.repo.runtime_config()['command_env'] == \
            self.repo.command_env

    @patch('os.path.exists')
    @patch_open
    def test_add_repo_distribution(self, mock_open, mock_exists):
        mock_open.return_value = self.context_manager_mock
        mock_exists.return_value = True
        self.repo.add_repo(
            'foo', 'kiwi_iso_mount/uri', 'deb', None, 'xenial', 'a b'
        )
        self.file_mock.write.assert_called_once_with(
            'deb file:/kiwi_iso_mount/uri xenial a b\n'
        )
        mock_open.assert_called_once_with(
            '/shared-dir/apt-get/sources.list.d/foo.list', 'w'
        )

    @patch('os.path.exists')
    @patch_open
    def test_add_repo_distribution_with_gpgchecks(
        self, mock_open, mock_exists
    ):
        mock_open.return_value = self.context_manager_mock
        mock_exists.return_value = True
        self.repo.add_repo(
            'foo', 'kiwi_iso_mount/uri', 'deb', None, 'xenial', 'a b',
            repo_gpgcheck=True, pkg_gpgcheck=False
        )
        self.file_mock.write.assert_called_once_with(
            'deb [trusted=no] file:/kiwi_iso_mount/uri xenial a b\n'
        )
        mock_open.assert_called_once_with(
            '/shared-dir/apt-get/sources.list.d/foo.list', 'w'
        )

    @patch('os.path.exists')
    @patch_open
    def test_add_repo_distribution_default_component(
        self, mock_open, mock_exists
    ):
        mock_open.return_value = self.context_manager_mock
        mock_exists.return_value = True
        self.repo.add_repo(
            'foo', 'kiwi_iso_mount/uri', 'deb', None, 'xenial'
        )
        self.file_mock.write.assert_called_once_with(
            'deb file:/kiwi_iso_mount/uri xenial main\n'
        )
        mock_open.assert_called_once_with(
            '/shared-dir/apt-get/sources.list.d/foo.list', 'w'
        )

    @patch('os.path.exists')
    @patch_open
    def test_add_repo_flat(self, mock_open, mock_exists):
        mock_open.return_value = self.context_manager_mock
        mock_exists.return_value = False
        self.repo.add_repo(
            'foo', 'http://repo.com', 'deb'
        )
        self.file_mock.write.assert_called_once_with(
            'deb http://repo.com ./\n'
        )
        mock_open.assert_called_once_with(
            '/shared-dir/apt-get/sources.list.d/foo.list', 'w'
        )

    def test_import_trusted_keys(self):
        self.repo.import_trusted_keys(['key-file-a.asc', 'key-file-b.asc'])
        assert self.repo.signing_keys == ['key-file-a.asc', 'key-file-b.asc']

    @patch('kiwi.path.Path.wipe')
    def test_delete_repo(self, mock_wipe):
        self.repo.delete_repo('foo')
        mock_wipe.assert_called_once_with(
            '/shared-dir/apt-get/sources.list.d/foo.list'
        )

    @patch('kiwi.path.Path.wipe')
    @patch('os.walk')
    def test_cleanup_unused_repos(self, mock_walk, mock_path):
        mock_walk.return_value = [
            ('/foo', ('bar', 'baz'), ('spam', 'eggs'))
        ]
        self.repo.repo_names = ['eggs']
        self.repo.cleanup_unused_repos()
        mock_path.assert_called_once_with(
            '/shared-dir/apt-get/sources.list.d/spam'
        )

    @patch('kiwi.path.Path.wipe')
    @patch('kiwi.path.Path.create')
    def test_delete_all_repos(self, mock_create, mock_wipe):
        self.repo.delete_all_repos()
        mock_wipe.assert_called_once_with(
            '/shared-dir/apt-get/sources.list.d'
        )
        mock_create.assert_called_once_with(
            '/shared-dir/apt-get/sources.list.d'
        )

    @patch('kiwi.path.Path.wipe')
    def test_delete_repo_cache(self, mock_wipe):
        self.repo.delete_repo_cache('foo')
        assert mock_wipe.call_args_list == [
            call('/shared-dir/apt-get/archives'),
            call('/shared-dir/apt-get/pkgcache.bin'),
            call('/shared-dir/apt-get/srcpkgcache.bin')
        ]
Esempio n. 6
0
class TestRepositoryApt(object):
    @patch('kiwi.repository.apt.NamedTemporaryFile')
    @patch_open
    @patch('kiwi.repository.apt.PackageManagerTemplateAptGet')
    @patch('kiwi.repository.apt.Path.create')
    def setup(self, mock_path, mock_template, mock_open, mock_temp):
        self.context_manager_mock = mock.Mock()
        self.file_mock = mock.Mock()
        self.enter_mock = mock.Mock()
        self.exit_mock = mock.Mock()
        self.enter_mock.return_value = self.file_mock
        setattr(self.context_manager_mock, '__enter__', self.enter_mock)
        setattr(self.context_manager_mock, '__exit__', self.exit_mock)

        self.apt_conf = mock.Mock()
        mock_template.return_value = self.apt_conf

        template = mock.Mock()
        self.apt_conf.get_host_template.return_value = template

        tmpfile = mock.Mock()
        tmpfile.name = 'tmpfile'
        mock_temp.return_value = tmpfile
        root_bind = mock.Mock()
        root_bind.move_to_root = mock.Mock(
            return_value=['root-moved-arguments'])
        root_bind.root_dir = '../data'
        root_bind.shared_location = '/shared-dir'

        self.repo = RepositoryApt(root_bind)

        self.apt_conf.get_host_template.assert_called_once_with()
        template.substitute.assert_called_once_with(
            {'apt_shared_base': '/shared-dir/apt-get'})

    @patch_open
    def test_use_default_location(self, mock_open):
        template = mock.Mock()
        self.apt_conf.get_image_template.return_value = template
        self.repo.use_default_location()
        self.apt_conf.get_image_template.assert_called_once_with()
        template.substitute.assert_called_once_with()

    def test_runtime_config(self):
        assert self.repo.runtime_config()['apt_get_args'] == \
            self.repo.apt_get_args
        assert self.repo.runtime_config()['command_env'] == \
            self.repo.command_env

    @patch('os.path.exists')
    @patch_open
    def test_add_repo_distribution(self, mock_open, mock_exists):
        mock_open.return_value = self.context_manager_mock
        mock_exists.return_value = True
        self.repo.add_repo('foo', 'kiwi_iso_mount/uri', 'deb', None, 'xenial',
                           'a b')
        self.file_mock.write.assert_called_once_with(
            'deb file:/kiwi_iso_mount/uri xenial a b\n')
        mock_open.assert_called_once_with(
            '/shared-dir/apt-get/sources.list.d/foo.list', 'w')

    @patch('os.path.exists')
    @patch_open
    def test_add_repo_distribution_default_component(self, mock_open,
                                                     mock_exists):
        mock_open.return_value = self.context_manager_mock
        mock_exists.return_value = True
        self.repo.add_repo('foo', 'kiwi_iso_mount/uri', 'deb', None, 'xenial')
        self.file_mock.write.assert_called_once_with(
            'deb file:/kiwi_iso_mount/uri xenial main\n')
        mock_open.assert_called_once_with(
            '/shared-dir/apt-get/sources.list.d/foo.list', 'w')

    @patch('os.path.exists')
    @patch_open
    def test_add_repo_flat(self, mock_open, mock_exists):
        mock_open.return_value = self.context_manager_mock
        mock_exists.return_value = False
        self.repo.add_repo('foo', 'http://repo.com', 'deb')
        self.file_mock.write.assert_called_once_with(
            'deb http://repo.com ./\n')
        mock_open.assert_called_once_with(
            '/shared-dir/apt-get/sources.list.d/foo.list', 'w')

    @patch('kiwi.path.Path.wipe')
    def test_delete_repo(self, mock_wipe):
        self.repo.delete_repo('foo')
        mock_wipe.assert_called_once_with(
            '/shared-dir/apt-get/sources.list.d/foo.list')

    @patch('kiwi.path.Path.wipe')
    @patch('os.walk')
    def test_cleanup_unused_repos(self, mock_walk, mock_path):
        mock_walk.return_value = [('/foo', ('bar', 'baz'), ('spam', 'eggs'))]
        self.repo.repo_names = ['eggs']
        self.repo.cleanup_unused_repos()
        mock_path.assert_called_once_with(
            '/shared-dir/apt-get/sources.list.d/spam')

    @patch('kiwi.path.Path.wipe')
    @patch('kiwi.path.Path.create')
    def test_delete_all_repos(self, mock_create, mock_wipe):
        self.repo.delete_all_repos()
        mock_wipe.assert_called_once_with('/shared-dir/apt-get/sources.list.d')
        mock_create.assert_called_once_with(
            '/shared-dir/apt-get/sources.list.d')