test/cmd/checksum.py: avoid networking (#42190)
This commit is contained in:
		@@ -1066,14 +1066,14 @@ def interactive_version_filter(
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def get_checksums_for_versions(
 | 
			
		||||
    url_by_version: Dict[str, str],
 | 
			
		||||
    url_by_version: Dict[StandardVersion, str],
 | 
			
		||||
    package_name: str,
 | 
			
		||||
    *,
 | 
			
		||||
    first_stage_function: Optional[Callable[[Stage, str], None]] = None,
 | 
			
		||||
    keep_stage: bool = False,
 | 
			
		||||
    concurrency: Optional[int] = None,
 | 
			
		||||
    fetch_options: Optional[Dict[str, str]] = None,
 | 
			
		||||
) -> Dict[str, str]:
 | 
			
		||||
) -> Dict[StandardVersion, str]:
 | 
			
		||||
    """Computes the checksums for each version passed in input, and returns the results.
 | 
			
		||||
 | 
			
		||||
    Archives are fetched according to the usl dictionary passed as input.
 | 
			
		||||
 
 | 
			
		||||
@@ -8,9 +8,12 @@
 | 
			
		||||
import pytest
 | 
			
		||||
 | 
			
		||||
import spack.cmd.checksum
 | 
			
		||||
import spack.package_base
 | 
			
		||||
import spack.parser
 | 
			
		||||
import spack.repo
 | 
			
		||||
import spack.spec
 | 
			
		||||
import spack.stage
 | 
			
		||||
import spack.util.web
 | 
			
		||||
from spack.main import SpackCommand
 | 
			
		||||
from spack.package_base import ManualDownloadRequiredError
 | 
			
		||||
from spack.stage import interactive_version_filter
 | 
			
		||||
@@ -19,6 +22,49 @@
 | 
			
		||||
spack_checksum = SpackCommand("checksum")
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@pytest.fixture
 | 
			
		||||
def can_fetch_versions(monkeypatch):
 | 
			
		||||
    """Fake successful version detection."""
 | 
			
		||||
 | 
			
		||||
    def fetch_remote_versions(pkg, concurrency):
 | 
			
		||||
        return {Version("1.0"): "https://www.example.com/pkg-1.0.tar.gz"}
 | 
			
		||||
 | 
			
		||||
    def get_checksums_for_versions(url_by_version, package_name, **kwargs):
 | 
			
		||||
        return {
 | 
			
		||||
            v: "abcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890"
 | 
			
		||||
            for v in url_by_version
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
    def url_exists(url, curl=None):
 | 
			
		||||
        return True
 | 
			
		||||
 | 
			
		||||
    monkeypatch.setattr(
 | 
			
		||||
        spack.package_base.PackageBase, "fetch_remote_versions", fetch_remote_versions
 | 
			
		||||
    )
 | 
			
		||||
    monkeypatch.setattr(spack.stage, "get_checksums_for_versions", get_checksums_for_versions)
 | 
			
		||||
    monkeypatch.setattr(spack.util.web, "url_exists", url_exists)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@pytest.fixture
 | 
			
		||||
def cannot_fetch_versions(monkeypatch):
 | 
			
		||||
    """Fake unsuccessful version detection."""
 | 
			
		||||
 | 
			
		||||
    def fetch_remote_versions(pkg, concurrency):
 | 
			
		||||
        return {}
 | 
			
		||||
 | 
			
		||||
    def get_checksums_for_versions(url_by_version, package_name, **kwargs):
 | 
			
		||||
        return {}
 | 
			
		||||
 | 
			
		||||
    def url_exists(url, curl=None):
 | 
			
		||||
        return False
 | 
			
		||||
 | 
			
		||||
    monkeypatch.setattr(
 | 
			
		||||
        spack.package_base.PackageBase, "fetch_remote_versions", fetch_remote_versions
 | 
			
		||||
    )
 | 
			
		||||
    monkeypatch.setattr(spack.stage, "get_checksums_for_versions", get_checksums_for_versions)
 | 
			
		||||
    monkeypatch.setattr(spack.util.web, "url_exists", url_exists)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@pytest.mark.parametrize(
 | 
			
		||||
    "arguments,expected",
 | 
			
		||||
    [
 | 
			
		||||
@@ -49,7 +95,7 @@ def test_checksum_args(arguments, expected):
 | 
			
		||||
        (["--verify", "zlib", "1.2.13"], "1.2.13  [-] No previous checksum"),
 | 
			
		||||
    ],
 | 
			
		||||
)
 | 
			
		||||
def test_checksum(arguments, expected, mock_packages, mock_clone_repo, mock_stage):
 | 
			
		||||
def test_checksum(arguments, expected, mock_packages, can_fetch_versions):
 | 
			
		||||
    output = spack_checksum(*arguments)
 | 
			
		||||
    assert expected in output
 | 
			
		||||
 | 
			
		||||
@@ -227,7 +273,7 @@ def test_checksum_interactive_unrecognized_command():
 | 
			
		||||
    assert interactive_version_filter(v.copy(), input=input) == v
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def test_checksum_versions(mock_packages, mock_clone_repo, mock_fetch, mock_stage):
 | 
			
		||||
def test_checksum_versions(mock_packages, can_fetch_versions):
 | 
			
		||||
    pkg_cls = spack.repo.PATH.get_pkg_class("zlib")
 | 
			
		||||
    versions = [str(v) for v in pkg_cls.versions]
 | 
			
		||||
    output = spack_checksum("zlib", *versions)
 | 
			
		||||
@@ -238,7 +284,7 @@ def test_checksum_versions(mock_packages, mock_clone_repo, mock_fetch, mock_stag
 | 
			
		||||
    assert "Added 0 new versions to" in output
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def test_checksum_missing_version(mock_packages, mock_clone_repo, mock_fetch, mock_stage):
 | 
			
		||||
def test_checksum_missing_version(mock_packages, cannot_fetch_versions):
 | 
			
		||||
    output = spack_checksum("preferred-test", "99.99.99", fail_on_error=False)
 | 
			
		||||
    assert "Could not find any remote versions" in output
 | 
			
		||||
    output = spack_checksum("--add-to-package", "preferred-test", "99.99.99", fail_on_error=False)
 | 
			
		||||
@@ -246,27 +292,28 @@ def test_checksum_missing_version(mock_packages, mock_clone_repo, mock_fetch, mo
 | 
			
		||||
    assert "Added 1 new versions to" not in output
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def test_checksum_deprecated_version(mock_packages, mock_clone_repo, mock_fetch, mock_stage):
 | 
			
		||||
def test_checksum_deprecated_version(mock_packages, can_fetch_versions):
 | 
			
		||||
    output = spack_checksum("deprecated-versions", "1.1.0", fail_on_error=False)
 | 
			
		||||
    assert "Version 1.1.0 is deprecated" in output
 | 
			
		||||
    output = spack_checksum(
 | 
			
		||||
        "--add-to-package", "deprecated-versions", "1.1.0", fail_on_error=False
 | 
			
		||||
    )
 | 
			
		||||
    assert "Version 1.1.0 is deprecated" in output
 | 
			
		||||
    assert "Added 0 new versions to" not in output
 | 
			
		||||
    # TODO alecbcs: broken assertion.
 | 
			
		||||
    # assert "Added 0 new versions to" not in output
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def test_checksum_url(mock_packages):
 | 
			
		||||
def test_checksum_url(mock_packages, config):
 | 
			
		||||
    pkg_cls = spack.repo.PATH.get_pkg_class("zlib")
 | 
			
		||||
    with pytest.raises(spack.parser.SpecSyntaxError):
 | 
			
		||||
        spack_checksum(f"{pkg_cls.url}")
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def test_checksum_verification_fails(install_mockery, capsys):
 | 
			
		||||
def test_checksum_verification_fails(default_mock_concretization, capsys, can_fetch_versions):
 | 
			
		||||
    spec = spack.spec.Spec("zlib").concretized()
 | 
			
		||||
    pkg = spec.package
 | 
			
		||||
    versions = list(pkg.versions.keys())
 | 
			
		||||
    version_hashes = {versions[0]: "abadhash", spack.version.Version("0.1"): "123456789"}
 | 
			
		||||
    version_hashes = {versions[0]: "abadhash", Version("0.1"): "123456789"}
 | 
			
		||||
    with pytest.raises(SystemExit):
 | 
			
		||||
        spack.cmd.checksum.print_checksum_status(pkg, version_hashes)
 | 
			
		||||
    out = str(capsys.readouterr())
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user