Repository navigation
Download PETSc from Zenodo, with the DataverseNL mirror as the fallback #20
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
ce1f759
Download PETSc from Zenodo, with the DataverseNL mirror as the fallback
timlichtenberg ea124ce
Merge main into tl/petsc-zenodo
timlichtenberg a1cfe36
Point the PETSc download at its DataverseNL mirror
timlichtenberg fb246ac
Stop on an empty PETSc hash and time out stalled downloads
timlichtenberg a706b9d
Stop the PETSc download when no hash tool is installed
timlichtenberg File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| """Tests for the verified PETSc download in ``tools/get_petsc.sh``. | ||
|
|
||
| The function and the pins are read from the shipped script, so a change to the | ||
| script re-runs these cases against its new text. Sources are ``file://`` URLs: | ||
| no network and no build. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import hashlib | ||
| import re | ||
| import subprocess | ||
| from pathlib import Path | ||
|
|
||
| import pytest | ||
|
|
||
| pytestmark = [pytest.mark.unit, pytest.mark.timeout(30)] | ||
|
|
||
| SCRIPT = Path(__file__).resolve().parents[1] / 'tools' / 'get_petsc.sh' | ||
|
|
||
|
|
||
| def _function() -> str: | ||
| """Return the shipped fetch_verified definition with the console helpers it calls.""" | ||
| text = SCRIPT.read_text() | ||
| helpers = re.search(r'^console\(\) \{.*?^\}\n\nannounce\(\) \{.*?^\}\n', text, re.S | re.M) | ||
| body = re.search(r'^fetch_verified\(\) \{.*?^\}\n', text, re.S | re.M) | ||
| return 'set -euo pipefail\nexec 3>&1\n' + helpers.group(0) + body.group(0) | ||
|
|
||
|
|
||
| def _run(body: str) -> subprocess.CompletedProcess: | ||
| """Run ``body`` after the shipped function definitions.""" | ||
| return subprocess.run(['bash', '-c', _function() + body], capture_output=True, text=True) | ||
|
|
||
|
|
||
| def _sha256(data: bytes) -> str: | ||
| """Return the SHA-256 hex digest of ``data``.""" | ||
| return hashlib.sha256(data).hexdigest() | ||
|
|
||
|
|
||
| def test_the_first_source_with_the_pinned_hash_wins(tmp_path): | ||
| """A failed download, an empty mirror and a wrong file each give way to the next source, | ||
| so only an archive with the pinned SHA-256 reaches unzip and the PETSc build.""" | ||
| good, bad = tmp_path / 'good.zip', tmp_path / 'bad.zip' | ||
| good.write_bytes(b'petsc archive') | ||
| bad.write_bytes(b'an error page') | ||
| dest = tmp_path / 'petsc.zip' | ||
| result = _run( | ||
| f'fetch_verified {_sha256(b"petsc archive")} "{dest}" ' | ||
| f'"file://{tmp_path}/missing.zip" "" "file://{bad}" "file://{good}"\necho REACHED_UNZIP\n' | ||
| ) | ||
| assert result.returncode == 0, result.stderr | ||
| assert dest.read_bytes() == b'petsc archive' | ||
| assert 'REACHED_UNZIP' in result.stdout | ||
| assert f'download from file://{tmp_path}/missing.zip failed' in result.stdout | ||
| assert f'served a file with SHA-256 {_sha256(b"an error page")}' in result.stdout | ||
|
|
||
|
|
||
| def test_no_matching_source_stops_before_unzip(tmp_path): | ||
| """With no source serving the pinned file the script stops and leaves no archive.""" | ||
| bad = tmp_path / 'bad.zip' | ||
| bad.write_bytes(b'an error page') | ||
| dest = tmp_path / 'petsc.zip' | ||
| result = _run(f'fetch_verified {"0" * 64} "{dest}" "file://{bad}" ""\necho REACHED_UNZIP\n') | ||
| assert result.returncode == 1 | ||
| assert 'REACHED_UNZIP' not in result.stdout | ||
| assert not dest.exists() | ||
| assert f'no source served petsc.zip with SHA-256 {"0" * 64}' in result.stdout | ||
|
|
||
|
|
||
| def test_an_empty_hash_or_no_hash_tool_stops_before_any_download(tmp_path): | ||
| """A blank pin, or no shasum and no sha256sum, stops at once instead of accepting whatever a | ||
| source serves; the shipped script pins the Zenodo archive, its mirror and a 64-digit hash.""" | ||
| good = tmp_path / 'good.zip' | ||
| good.write_bytes(b'petsc archive') | ||
| call = f'fetch_verified "{{}}" "{tmp_path}/p.zip" "file://{good}"\necho REACHED_UNZIP\n' | ||
| blank = _run(call.format('')) | ||
| no_tool = subprocess.run( | ||
| ['/bin/bash', '-c', _function() + call.format(_sha256(b'petsc archive'))], | ||
| capture_output=True, | ||
| text=True, | ||
| env={'PATH': str(tmp_path / 'empty')}, | ||
| ) | ||
| assert blank.returncode == 1 and 'no SHA-256 pin for p.zip' in blank.stdout | ||
| assert no_tool.returncode == 1 | ||
| assert 'neither shasum nor sha256sum is installed' in no_tool.stdout | ||
| for result in (blank, no_tool): | ||
| assert 'Downloading' not in result.stdout and 'REACHED_UNZIP' not in result.stdout | ||
| text = SCRIPT.read_text() | ||
| assert ( | ||
| 'url="${PETSC_URL:-https://zenodo.org/records/15805756/files/petsc.zip?download=1}"' | ||
| in text | ||
| ) | ||
| assert ( | ||
| 'mirror_url="${PETSC_MIRROR_URL:-https://dataverse.nl/api/access/datafile/683669}"' | ||
| in text | ||
| ) | ||
| assert re.search(r'^petsc_sha256="[0-9a-f]{64}"$', text, re.M) | ||
| assert 'fetch_verified "$petsc_sha256" "$zipfile" "$url" "$mirror_url"' in text |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.