def test_translate_obs_project(self, mock_request_get): uri = Uri('obs://openSUSE:Leap:42.2/standard', 'yast2') uri.runtime_config = self.runtime_config uri.translate() mock_request_get.assert_called_once_with( 'obs_server/openSUSE:/Leap:/42.2/standard' )
def test_translate_obs_uri_inside_buildservice( self, mock_buildservice, mock_warn ): mock_buildservice.return_value = True uri = Uri('obs://openSUSE:Leap:42.2/standard', 'rpm-md') uri.runtime_config = self.runtime_config assert uri.translate(False) == \ 'obs_server/openSUSE:/Leap:/42.2/standard' assert mock_warn.called
def test_translate_buildservice_obsrepositories_container_path( self, mock_buildservice ): mock_buildservice.return_value = True uri = Uri('obsrepositories:/container#latest', 'container') assert uri.translate() == ''.join( [ '/usr/src/packages/SOURCES/containers/', '_obsrepositories/container#latest' ] )
def test_destructor(self, mock_wipe, mock_mkdtemp, mock_manager): mock_mkdtemp.return_value = 'tmpdir' manager = mock.Mock() manager.mountpoint = mock_mkdtemp.return_value manager.is_mounted = mock.Mock( return_value=True ) mock_manager.return_value = manager uri = Uri('iso:///image/CDs/openSUSE-13.2-DVD-x86_64.iso', 'yast2') result = uri.translate() uri.__del__() manager.umount.assert_called_once_with() mock_wipe.assert_called_once_with(manager.mountpoint)
def test_translate_iso_path(self, mock_mkdtemp, mock_manager): mock_mkdtemp.return_value = 'tmpdir' manager = mock.Mock() manager.mountpoint = mock_mkdtemp.return_value mock_manager.return_value = manager uri = Uri('iso:///image/CDs/openSUSE-13.2-DVD-x86_64.iso', 'yast2') result = uri.translate() mock_manager.assert_called_once_with( device='/image/CDs/openSUSE-13.2-DVD-x86_64.iso', mountpoint='tmpdir' ) manager.mount.assert_called_once_with() assert result == 'tmpdir' uri.mount_stack = []
def import_repositories_marked_as_imageinclude(self): """ Those <repository> sections which are marked with the imageinclude attribute should be permanently added to the image repository configuration """ repository_sections = \ self.xml_state.get_repository_sections_used_in_image() root = RootInit( root_dir=self.root_dir, allow_existing=True ) repo = Repository( RootBind(root), self.xml_state.get_package_manager() ) repo.use_default_location() for xml_repo in repository_sections: repo_type = xml_repo.get_type() repo_source = xml_repo.get_source().get_path() repo_user = xml_repo.get_username() repo_secret = xml_repo.get_password() repo_alias = xml_repo.get_alias() repo_priority = xml_repo.get_priority() repo_dist = xml_repo.get_distribution() repo_components = xml_repo.get_components() repo_repository_gpgcheck = xml_repo.get_repository_gpgcheck() repo_package_gpgcheck = xml_repo.get_package_gpgcheck() uri = Uri(repo_source, repo_type) repo_source_translated = uri.translate( check_build_environment=False ) if not repo_alias: repo_alias = uri.alias() log.info('Setting up image repository %s', repo_source) log.info('--> Type: %s', repo_type) log.info('--> Translated: %s', repo_source_translated) log.info('--> Alias: %s', repo_alias) repo.add_repo( repo_alias, repo_source_translated, repo_type, repo_priority, repo_dist, repo_components, repo_user, repo_secret, uri.credentials_file_name(), repo_repository_gpgcheck, repo_package_gpgcheck )
def test_is_public(self, mock_request): uri = Uri('xxx', 'rpm-md') assert uri.is_public() is False uri = Uri('https://example.com', 'rpm-md') assert uri.is_public() is True uri = Uri('obs://openSUSE:Leap:42.2/standard', 'yast2') self.runtime_config.is_obs_public = mock.Mock( return_value=False ) uri.runtime_config = self.runtime_config assert uri.is_public() is False
def add_repo( self, name: str, uri: str, repo_type: str = 'rpm-md', prio: int = None, dist: str = None, components: str = None, user: str = None, secret: str = None, credentials_file: str = None, repo_gpgcheck: bool = False, pkg_gpgcheck: bool = False, sourcetype: str = None, use_for_bootstrap: bool = False ) -> None: """ Add zypper repository :param str name: repository name :param str uri: repository URI :param str repo_type: repostory type name :param int prio: zypper repostory priority :param str dist: unused :param str components: unused :param str user: credentials username :param str secret: credentials password :param str credentials_file: zypper credentials file :param bool repo_gpgcheck: enable repository signature validation :param bool pkg_gpgcheck: enable package signature validation :param str sourcetype: unused :param boot use_for_bootstrap: unused """ if credentials_file: repo_secret = os.sep.join( [self.shared_zypper_dir['credentials-dir'], credentials_file] ) if os.path.exists(repo_secret): Path.wipe(repo_secret) if user and secret: uri = ''.join([uri, '?credentials=', credentials_file]) with open(repo_secret, 'w') as credentials: credentials.write('username={0}{1}'.format( user, os.linesep) ) credentials.write('password={0}{1}'.format( secret, os.linesep) ) repo_file = ''.join( [self.shared_zypper_dir['reposd-dir'], '/', name, '.repo'] ) self.repo_names.append(''.join([name, '.repo'])) if os.path.exists(repo_file): Path.wipe(repo_file) self._backup_package_cache() zypper_addrepo_command = ['zypper'] + self.zypper_args + [ '--root', self.root_dir, 'addrepo', '--refresh', '--keep-packages' if Uri(uri).is_remote() else '--no-keep-packages', '--no-check', uri, name ] try: Command.run( zypper_addrepo_command, self.command_env ) except Exception: # for whatever reason zypper sometimes failes with # a 'failed to cache rpm database' error. I could not # find any reason why and a simple recall of the exact # same command in the exact same environment works. # Thus the stupid but simple workaround to this problem # is try one recall before really failing Command.run( zypper_addrepo_command, self.command_env ) repo_config = ConfigParser() repo_config.read(repo_file) repo_config.set( name, 'repo_gpgcheck', '1' if repo_gpgcheck else '0' ) repo_config.set( name, 'pkg_gpgcheck', '1' if pkg_gpgcheck else '0' ) if prio: repo_config.set( name, 'priority', format(prio) ) with open(repo_file, 'w') as repo: repo_config.write(repo) self._restore_package_cache()
def test_is_remote_in_buildservice(self, mock_buildservice): mock_buildservice.return_value = True uri = Uri('obs://openSUSE:Leap:42.2/standard', 'yast2') assert uri.is_remote() is False
def test_translate_buildservice_container_path(self, mock_buildservice): mock_buildservice.return_value = True uri = Uri('obs://project/repo/container#latest', 'container') assert uri.translate() == \ '/usr/src/packages/SOURCES/containers/project/repo/container#latest'
def test_is_remote_raises_style_error(self): uri = Uri('xxx', 'rpm-md') with raises(KiwiUriStyleUnknown): uri.is_remote()
def test_translate_ftp_path(self): uri = Uri('ftp://example.com/foo', 'rpm-md') assert uri.translate() == 'ftp://example.com/foo'
def test_translate_http_path_with_credentials(self): uri = Uri('http://example.com/foo?credentials=kiwiRepoCredentials', 'rpm-md') assert uri.translate() == 'http://example.com/foo'
def add_obs_repositories( self, xml_state: XMLState, profile: Optional[str] = None, arch: str = 'x86_64', repo: str = 'images') -> Dict[str, obs_repo_status_type]: """ Add repositories from the obs project to the provided XMLState :param XMLState xml_state: XMLState object reference :param str arch: OBS architecture, defaults to: 'x86_64' :param str repo: OBS image package build repository name, defaults to: 'images' """ repository_status_report: Dict[str, obs_repo_status_type] = {} if not OBS._delete_obsrepositories_placeholder_repo(xml_state): # The repo list does not contain the obsrepositories flag # Therefore it's not needed to look for repos in the OBS # project configuration return repository_status_report package_name = self.package if not profile \ else f'{self.package}:{profile}' log.info(f'Using OBS repositories from {self.project}/{package_name}') buildinfo_link = os.sep.join([ self.api_server, 'build', self.project, repo, arch, package_name, '_buildinfo' ]) request = self._create_request(buildinfo_link) buildinfo_xml_tree = OBS._import_xml_request(request) repo_paths = buildinfo_xml_tree.getroot().xpath('/buildinfo/path') if not repo_paths: raise KiwiOBSPluginBuildInfoError( f'OBS buildinfo for {package_name} has no repo paths') repo_prio_ascending = 0 repo_prio_descending = 501 repo_alias = None for repo_path in repo_paths: repo_url = repo_path.get('url') if not repo_url: repo_url = 'obs://{0}/{1}'.format(repo_path.get('project'), repo_path.get('repository')) if repo_url: try: repo_uri = Uri(repo_url) repo_url = repo_uri.translate( check_build_environment=False) request = requests.get(repo_url) request.raise_for_status() except Exception as issue: repository_status_report[repo_url] = obs_repo_status_type( flag='unreachable', message=f'ignored:{issue}') continue repo_check = SolverRepositoryBase(repo_uri) repo_type = repo_check.get_repo_type() if not repo_type: repository_status_report[repo_url] = obs_repo_status_type( flag='repo_type_unknown', message='ignored:Unknown repository type') continue if repo_type == 'rpm-md': repo_prio_ascending += 1 repo_prio = repo_prio_ascending else: repo_prio_descending -= 1 repo_prio = repo_prio_descending repository_status_report[repo_url] = obs_repo_status_type( flag='ok', message='imported') xml_state.add_repository(repo_url, repo_type, repo_alias, f'{repo_prio}') return repository_status_report
def test_is_remote_in_buildservice( self, mock_buildservice ): mock_buildservice.return_value = True uri = Uri('obs://openSUSE:Leap:42.2/standard', 'yast2') assert uri.is_remote() is False
def test_translate_obs_uri_not_found(mock_request_get, self): mock_request_get.side_effect = Exception uri = Uri('obs://openSUSE:Leap:42.2/standard', 'yast2') uri.runtime_config = self.runtime_config assert uri.translate()
def test_translate_http_path_with_credentials(self): uri = Uri( 'http://example.com/foo?credentials=kiwiRepoCredentials', 'rpm-md' ) assert uri.translate() == 'http://example.com/foo'
def test_translate_obsrepositories_outside_buildservice( self, mock_buildservice ): mock_buildservice.return_value = False uri = Uri('obsrepositories:/') uri.translate()
def test_translate_buildservice_obsrepositories(self, mock_buildservice): mock_buildservice.return_value = True uri = Uri('obsrepositories:/') assert uri.translate() == '/usr/src/packages/SOURCES/repos'
def test_translate_buildservice_path(self, mock_buildservice): mock_buildservice.return_value = True uri = Uri('obs://openSUSE:13.2/standard', 'yast2') assert uri.translate() == \ '/usr/src/packages/SOURCES/repos/openSUSE:13.2/standard'
def test_init_remote_uri(self): RootImportBase('root_dir', Uri('http://example.com/image.tar.xz'))
def setup_repositories(self, clear_cache=False, signing_keys=None): """ Set up repositories for software installation and return a package manager for performing software installation tasks :param bool clear_cache: flag the clear cache before configure anything :param list signing_keys: keys imported to the package manager :return: instance of :class:`PackageManager` :rtype: PackageManager """ repository_options = [] repository_sections = \ self.xml_state.get_repository_sections_used_for_build() package_manager = self.xml_state.get_package_manager() if self.xml_state.get_rpm_check_signatures(): repository_options.append('check_signatures') if self.xml_state.get_rpm_excludedocs(): repository_options.append('exclude_docs') repo = Repository( self.root_bind, package_manager, repository_options ) if signing_keys: repo.import_trusted_keys(signing_keys) for xml_repo in repository_sections: repo_type = xml_repo.get_type() repo_source = xml_repo.get_source().get_path() repo_user = xml_repo.get_username() repo_secret = xml_repo.get_password() repo_alias = xml_repo.get_alias() repo_priority = xml_repo.get_priority() repo_dist = xml_repo.get_distribution() repo_components = xml_repo.get_components() repo_repository_gpgcheck = xml_repo.get_repository_gpgcheck() repo_package_gpgcheck = xml_repo.get_package_gpgcheck() log.info('Setting up repository %s', repo_source) log.info('--> Type: %s', repo_type) if repo_priority: log.info('--> Priority: %s', repo_priority) uri = Uri(repo_source, repo_type) repo_source_translated = uri.translate() log.info('--> Translated: %s', repo_source_translated) if not repo_alias: repo_alias = uri.alias() log.info('--> Alias: %s', repo_alias) if not uri.is_remote() and not os.path.exists( repo_source_translated ): log.warning( 'repository %s does not exist and will be skipped', repo_source ) continue if not uri.is_remote(): self.root_bind.mount_shared_directory(repo_source_translated) repo.add_repo( repo_alias, repo_source_translated, repo_type, repo_priority, repo_dist, repo_components, repo_user, repo_secret, uri.credentials_file_name(), repo_repository_gpgcheck, repo_package_gpgcheck ) if clear_cache: repo.delete_repo_cache(repo_alias) self.uri_list.append(uri) repo.cleanup_unused_repos() return PackageManager( repo, package_manager )
def test_init_non_existing(self, mock_path): mock_path.return_value = False with patch.dict('os.environ', {'HOME': '../data'}): RootImportBase('root_dir', Uri('file:///image.tar.xz')) mock_path.assert_called_once_with('/image.tar.xz')
def test_is_remote_raises_style_error(self): uri = Uri('xxx', 'rpm-md') uri.is_remote()
def test_credentials_default_file_name(self): uri = Uri('http://example.com/foo', 'rpm-md') assert uri.credentials_file_name() == 'kiwiRepoCredentials'
def test_is_remote_raises_type_error(self): uri = Uri('xtp://download.example.com', 'rpm-md') uri.is_remote()
def test_translate_dir_path(self): uri = Uri('dir:///some/path', 'rpm-md') assert uri.translate() == '/some/path'
def test_translate_unknown_style(self): uri = Uri('xxx', 'rpm-md') uri.translate()
def test_translate_buildservice_project(self, mock_buildservice): mock_buildservice.return_value = True uri = Uri('obs://Virtualization:/Appliances/CentOS_7', 'rpm-md') assert uri.translate() == \ '/usr/src/packages/SOURCES/repos/Virtualization:Appliances/CentOS_7'
def test_translate_unsupported_style(self): uri = Uri('ms://foo', 'rpm-md') uri.translate()
def test_is_remote_raises_type_error(self): uri = Uri('xtp://download.example.com', 'rpm-md') with raises(KiwiUriTypeUnknown): uri.is_remote()
def test_is_remote(self): uri = Uri('https://example.com', 'rpm-md') assert uri.is_remote() is True uri = Uri('dir:///path/to/repo', 'rpm-md') assert uri.is_remote() is False
def test_get_fragment(self): uri = Uri('file:///myimage.tar#tag') assert uri.get_fragment() == 'tag' uri = Uri('file:///myimage.tar') assert uri.get_fragment() == ''
def test_alias(self): uri = Uri('https://example.com', 'rpm-md') assert uri.alias() == hashlib.md5( 'https://example.com'.encode()).hexdigest( )
def test_init(self, mock_buildservice, mock_path): mock_buildservice.return_value = False mock_path.return_value = True with patch.dict('os.environ', {'HOME': '../data'}): RootImportBase('root_dir', Uri('file:///image.tar.xz')) assert call('/image.tar.xz') in mock_path.call_args_list
def test_translate_ibs_project(self): uri = Uri('ibs://Devel:PubCloud/SLE_12_GA', 'rpm-md') assert uri.translate() == \ 'http://download.suse.de/ibs/Devel:/PubCloud/SLE_12_GA'
def setup_repositories(self, clear_cache=False, signing_keys=None): """ Set up repositories for software installation and return a package manager for performing software installation tasks :param bool clear_cache: flag the clear cache before configure anything :param list signing_keys: keys imported to the package manager :return: instance of :class:`PackageManager` :rtype: PackageManager """ repository_options = [] repository_sections = \ self.xml_state.get_repository_sections_used_for_build() package_manager = self.xml_state.get_package_manager() rpm_locale_list = self.xml_state.get_rpm_locale() if self.xml_state.get_rpm_check_signatures(): repository_options.append('check_signatures') if self.xml_state.get_rpm_excludedocs(): repository_options.append('exclude_docs') if rpm_locale_list: repository_options.append('_install_langs%{0}'.format( ':'.join(rpm_locale_list))) repo = Repository(self.root_bind, package_manager, repository_options) repo.setup_package_database_configuration() if signing_keys: repo.import_trusted_keys(signing_keys) for xml_repo in repository_sections: repo_type = xml_repo.get_type() repo_source = xml_repo.get_source().get_path() repo_user = xml_repo.get_username() repo_secret = xml_repo.get_password() repo_alias = xml_repo.get_alias() repo_priority = xml_repo.get_priority() repo_dist = xml_repo.get_distribution() repo_components = xml_repo.get_components() repo_repository_gpgcheck = xml_repo.get_repository_gpgcheck() repo_package_gpgcheck = xml_repo.get_package_gpgcheck() repo_sourcetype = xml_repo.get_sourcetype() log.info('Setting up repository %s', repo_source) log.info('--> Type: {0}'.format(repo_type)) if repo_sourcetype: log.info('--> SourceType: {0}'.format(repo_sourcetype)) if repo_priority: log.info('--> Priority: {0}'.format(repo_priority)) uri = Uri(repo_source, repo_type) repo_source_translated = uri.translate() log.info('--> Translated: {0}'.format(repo_source_translated)) if not repo_alias: repo_alias = uri.alias() log.info('--> Alias: {0}'.format(repo_alias)) if not uri.is_remote() and not os.path.exists( repo_source_translated): log.warning('repository %s does not exist and will be skipped', repo_source) continue if not uri.is_remote(): self.root_bind.mount_shared_directory(repo_source_translated) repo.add_repo(repo_alias, repo_source_translated, repo_type, repo_priority, repo_dist, repo_components, repo_user, repo_secret, uri.credentials_file_name(), repo_repository_gpgcheck, repo_package_gpgcheck, repo_sourcetype) if clear_cache: repo.delete_repo_cache(repo_alias) self.uri_list.append(uri) repo.cleanup_unused_repos() return PackageManager(repo, package_manager)
def test_translate_obs_distro(self): uri = Uri('obs://13.2/repo/oss', 'yast2') assert uri.translate() == \ 'http://download.opensuse.org/distribution/13.2/repo/oss'
def test_init_unknown_uri(self, mock_log_warn): root = RootImportBase('root_dir', Uri('docker://opensuse:leap')) assert root.unknown_uri == 'docker://opensuse:leap' assert mock_log_warn.called
def test_translate_obs_project(self): uri = Uri('obs://Virt:Appliances/SLE_12', 'rpm-md') assert uri.translate() == \ 'http://download.opensuse.org/repositories/Virt:Appliances/SLE_12'
def test_data_sync(self, mock_path): mock_path.return_value = True with patch.dict('os.environ', {'HOME': '../data'}): root_import = RootImportBase('root_dir', Uri('file:///image.tar.xz')) root_import.sync_data()
def test_credentials_file_name(self): uri = Uri('http://example.com/foo?credentials=my_credentials&x=2', 'rpm-md') assert uri.credentials_file_name() == 'my_credentials'
def test_translate_http_path(self): uri = Uri('http://example.com/foo', 'rpm-md') assert uri.translate() == 'http://example.com/foo'
def test_translate_http_path_with_token(self): uri = Uri('http://foo.bar/baz?asdf', 'rpm-md') assert uri.translate() == 'http://foo.bar/baz?asdf'
def test_credentials_default_file_name(self): uri = Uri( 'http://example.com/foo', 'rpm-md' ) assert uri.credentials_file_name() == 'kiwiRepoCredentials'
def test_translate_obs_project(self, mock_request_get): uri = Uri('obs://openSUSE:Leap:42.2/standard', 'yast2') uri.runtime_config = self.runtime_config uri.translate() mock_request_get.assert_called_once_with( 'obs_server/openSUSE:/Leap:/42.2/standard')
def test_translate_suse_buildservice_path(self): uri = Uri('suse://openSUSE:13.2/standard', 'yast2') assert uri.translate() == \ '/usr/src/packages/SOURCES/repos/openSUSE:13.2/standard'
def test_translate_https_path(self): uri = Uri('https://example.com/foo', 'rpm-md') assert uri.translate() == 'https://example.com/foo'
def test_translate_unsupported_style(self): uri = Uri('ms://foo', 'rpm-md') with raises(KiwiUriStyleUnknown): uri.translate()
def test_credentials_file_name(self): uri = Uri( 'http://example.com/foo?credentials=my_credentials&x=2', 'rpm-md' ) assert uri.credentials_file_name() == 'my_credentials'
def test_alias(self): uri = Uri('https://example.com', 'rpm-md') assert uri.alias() == hashlib.md5( 'https://example.com'.encode()).hexdigest()
def test_translate_dir_relative_path(self, mock_abspath): mock_abspath.side_effect = lambda path: os.sep.join( ['/current/dir', path]) uri = Uri('dir:some/path', 'rpm-md') assert uri.translate() == '/current/dir/some/path'
def test_translate_unknown_style(self): uri = Uri('xxx', 'rpm-md') with raises(KiwiUriStyleUnknown): uri.translate()
def test_translate_obsrepositories_outside_buildservice( self, mock_buildservice): mock_buildservice.return_value = False uri = Uri('obsrepositories:/') with raises(KiwiUriStyleUnknown): uri.translate()
def test_is_public(self, mock_request): # unknown uri schema is considered not public uri = Uri('xxx', 'rpm-md') assert uri.is_public() is False # https is public uri = Uri('https://example.com', 'rpm-md') assert uri.is_public() is True # obs is private with obs_public set to false in config uri = Uri('obs://openSUSE:Leap:42.2/standard', 'yast2') self.runtime_config.is_obs_public = mock.Mock(return_value=False) uri.runtime_config = self.runtime_config assert uri.is_public() is False # unknown uri type considered not public uri = Uri('httpx://example.com', 'rpm-md') assert uri.is_public() is False