def test_get_raw_component_asset_url_sanity_check(mock_get_component_info, component, error): mock_get_component_info.return_value = component with pytest.raises(RuntimeError, match=error): nexus.get_raw_component_asset_url("cachito-pip-raw", "foo/bar/foobar-1.0.tar.gz")
def test_get_raw_component_asset_url(mock_get_component_info, hoster): mock_get_component_info.return_value = { # "id": "1234", # "repository": "cachito-pip-raw", # "format": "raw", # "name": "foo/bar/foobar-1.0.tar.gz", # "version": None, "assets": [{ "downloadUrl": "http://nexus/repository/cachito-pip-raw/foo/bar/foobar-1.0.tar.gz", # "path": "foo/bar/foobar-1.0.tar.gz", # "id": "5678", # "repository": "cachito-pip-raw", # "format": "raw", # "checksum": { # "sha1": "abcdef", # "sha512": "123456", # "sha256": "fedcba", # "md5": "654321", # } }] } url = nexus.get_raw_component_asset_url("cachito-pip-raw", "foo/bar/foobar-1.0.tar.gz", from_nexus_hoster=hoster) assert url == "http://nexus/repository/cachito-pip-raw/foo/bar/foobar-1.0.tar.gz" mock_get_component_info.assert_called_once_with( "cachito-pip-raw", "raw", "foo/bar/foobar-1.0.tar.gz", max_attempts=1, from_nexus_hoster=hoster, )
def _get_custom_requirement_config_file(requirement_file_path, source_dir, raw_repo_name, username, password): """ Get custom pip requirement file. Generates and returns a configuration file representing a custom pip requirement file where the original URL and VCS entries are replaced with entries pointing to those resources in Nexus. :param str requirement_file_path: path to the requirement file :param Path source_dir: path to the application source code :param str raw_repo_name: name of the raw hosted Nexus repository containing the requirements :param str username: the username of the Nexus user that has access to the request's Python repositories :param str password: the password of the Nexus user that has access to the request's Python repositories :return: Cachito configuration file representation containing the custom requirement file :rtype: dict :raises CachitoError: If a valid component URL cannot be retrieved from the raw Nexus repository """ original_requirement_file = PipRequirementsFile(requirement_file_path) cachito_requirements = [] differs_from_original = False for requirement in original_requirement_file.requirements: raw_component_name = get_raw_component_name(requirement) if raw_component_name: # increase max_attempts to make sure the package upload/setup is complete worker_config = get_worker_config() max_attempts = worker_config.cachito_nexus_max_search_attempts new_url = nexus.get_raw_component_asset_url( raw_repo_name, raw_component_name, max_attempts=max_attempts, from_nexus_hoster=False, ) if not new_url: raise CachitoError( f"Could not retrieve URL for {raw_component_name} in {raw_repo_name}. Was the " "asset uploaded?") # Inject credentials if "://" not in new_url: raise CachitoError( f"Nexus raw resource URL: {new_url} is not a valid URL") new_url = new_url.replace("://", f"://{username}:{password}@", 1) requirement = requirement.copy(url=new_url) differs_from_original = True cachito_requirements.append(requirement) if not differs_from_original: # No vcs or url dependencies. No need for a custom requirements file return cachito_requirement_file = PipRequirementsFile.from_requirements_and_options( cachito_requirements, original_requirement_file.options) final_contents = [] if cachito_requirement_file.options: final_contents.append(" ".join(cachito_requirement_file.options)) final_contents.extend([ str(requirement) for requirement in cachito_requirement_file.requirements ]) req_str = "\n".join(final_contents) final_path = Path("app") / Path(requirement_file_path).relative_to( source_dir) return make_base64_config_file(req_str, final_path)
def test_get_raw_component_asset_url_not_found(mock_get_component_info): mock_get_component_info.return_value = None url = nexus.get_raw_component_asset_url("cachito-pip-raw", "foo/bar/foobar-1.0.tar.gz") assert url is None