|
| 1 | +import sys |
| 2 | + |
| 3 | +import pytest |
| 4 | +import requests |
| 5 | +import requests_mock |
| 6 | + |
| 7 | +import gardenlinux.github.__main__ as gh |
| 8 | +from gardenlinux.github.release import upload_to_github_release_page |
| 9 | + |
| 10 | +from ..constants import TEST_GARDENLINUX_RELEASE |
| 11 | + |
| 12 | + |
| 13 | +def test_upload_to_github_release_page_dryrun(caplog, artifact_for_upload): |
| 14 | + with requests_mock.Mocker(): |
| 15 | + assert upload_to_github_release_page( |
| 16 | + "gardenlinux", |
| 17 | + "gardenlinux", |
| 18 | + TEST_GARDENLINUX_RELEASE, |
| 19 | + artifact_for_upload, |
| 20 | + dry_run=True) is None |
| 21 | + assert any("Dry run: would upload" in record.message for record in caplog.records), "Expected a dry‑run log entry" |
| 22 | + |
| 23 | + |
| 24 | +def test_upload_to_github_release_page_needs_github_token(downloads_dir, artifact_for_upload): |
| 25 | + with requests_mock.Mocker(): |
| 26 | + with pytest.raises(ValueError) as exn: |
| 27 | + upload_to_github_release_page( |
| 28 | + "gardenlinux", |
| 29 | + "gardenlinux", |
| 30 | + TEST_GARDENLINUX_RELEASE, |
| 31 | + artifact_for_upload, |
| 32 | + dry_run=False) |
| 33 | + assert str(exn.value) == "GITHUB_TOKEN environment variable not set", \ |
| 34 | + "Expected an exception to be raised on missing GITHUB_TOKEN environment variable" |
| 35 | + |
| 36 | + |
| 37 | +def test_upload_to_github_release_page(downloads_dir, caplog, github_token, artifact_for_upload): |
| 38 | + with requests_mock.Mocker(real_http=True) as m: |
| 39 | + m.post( |
| 40 | + f"https://uploads.github.com/repos/gardenlinux/gardenlinux/releases/{TEST_GARDENLINUX_RELEASE}/assets?name=artifact.log", |
| 41 | + text="{}", |
| 42 | + status_code=201 |
| 43 | + ) |
| 44 | + |
| 45 | + upload_to_github_release_page( |
| 46 | + "gardenlinux", |
| 47 | + "gardenlinux", |
| 48 | + TEST_GARDENLINUX_RELEASE, |
| 49 | + artifact_for_upload, |
| 50 | + dry_run=False) |
| 51 | + assert any("Upload successful" in record.message for record in caplog.records), \ |
| 52 | + "Expected an upload confirmation log entry" |
| 53 | + |
| 54 | + |
| 55 | +def test_upload_to_github_release_page_unreadable_artifact(downloads_dir, caplog, github_token, artifact_for_upload): |
| 56 | + artifact_for_upload.chmod(0) |
| 57 | + |
| 58 | + upload_to_github_release_page( |
| 59 | + "gardenlinux", |
| 60 | + "gardenlinux", |
| 61 | + TEST_GARDENLINUX_RELEASE, |
| 62 | + artifact_for_upload, |
| 63 | + dry_run=False) |
| 64 | + assert any("Error reading file" in record.message for record in caplog.records), \ |
| 65 | + "Expected an error message log entry" |
| 66 | + |
| 67 | + |
| 68 | +def test_upload_to_github_release_page_failed(downloads_dir, caplog, github_token, artifact_for_upload): |
| 69 | + with requests_mock.Mocker(real_http=True) as m: |
| 70 | + m.post( |
| 71 | + f"https://uploads.github.com/repos/gardenlinux/gardenlinux/releases/{TEST_GARDENLINUX_RELEASE}/assets?name=artifact.log", |
| 72 | + text="{}", |
| 73 | + status_code=503 |
| 74 | + ) |
| 75 | + |
| 76 | + with pytest.raises(requests.exceptions.HTTPError): |
| 77 | + upload_to_github_release_page( |
| 78 | + "gardenlinux", |
| 79 | + "gardenlinux", |
| 80 | + TEST_GARDENLINUX_RELEASE, |
| 81 | + artifact_for_upload, |
| 82 | + dry_run=False) |
| 83 | + assert any("Upload failed with status code 503:" in record.message for record in caplog.records), \ |
| 84 | + "Expected an error HTTP status code to be logged" |
| 85 | + |
| 86 | + |
| 87 | +def test_script_parse_args_wrong_command(monkeypatch, capfd): |
| 88 | + monkeypatch.setattr(sys, "argv", ["gh", "rejoice"]) |
| 89 | + |
| 90 | + with pytest.raises(SystemExit): |
| 91 | + gh.main() |
| 92 | + captured = capfd.readouterr() |
| 93 | + |
| 94 | + assert "argument command: invalid choice: 'rejoice'" in captured.err, "Expected help message printed" |
| 95 | + |
| 96 | + |
| 97 | +def test_script_parse_args_upload_command_required_args(monkeypatch, capfd): |
| 98 | + monkeypatch.setattr(sys, "argv", ["gh", "upload", "--owner", "gardenlinux", "--repo", "gardenlinux"]) |
| 99 | + |
| 100 | + with pytest.raises(SystemExit): |
| 101 | + gh.main() |
| 102 | + captured = capfd.readouterr() |
| 103 | + |
| 104 | + assert "the following arguments are required: --release_id, --file_path" in captured.err, \ |
| 105 | + "Expected help message on missing arguments for 'upload' command" |
| 106 | + |
| 107 | + |
| 108 | +def test_script_upload_dry_run(monkeypatch, capfd): |
| 109 | + monkeypatch.setattr(sys, "argv", ["gh", "upload", "--owner", "gardenlinux", "--repo", |
| 110 | + "gardenlinux", "--release_id", TEST_GARDENLINUX_RELEASE, "--file_path", "foo", "--dry-run"]) |
| 111 | + monkeypatch.setattr("gardenlinux.github.__main__.upload_to_github_release_page", |
| 112 | + lambda a1, a2, a3, a4, dry_run: print(f"dry-run: {dry_run}")) |
| 113 | + |
| 114 | + gh.main() |
| 115 | + captured = capfd.readouterr() |
| 116 | + |
| 117 | + assert captured.out == "dry-run: True\n" |
0 commit comments