|
| 1 | +"""Unit tests for oras_utils.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +from unittest.mock import MagicMock, call, patch |
| 6 | + |
| 7 | +import pytest |
| 8 | + |
| 9 | +from oras_utils import oras_resolve |
| 10 | + |
| 11 | + |
| 12 | +def test_oras_resolve_calls_select_oci_auth_with_reference() -> None: |
| 13 | + """select-oci-auth is called with the image reference.""" |
| 14 | + with patch("oras_utils.run_cmd") as mock_run: |
| 15 | + mock_run.side_effect = [ |
| 16 | + MagicMock(stdout='{"auths": {}}'), |
| 17 | + MagicMock(returncode=0, stdout="sha256:abc\n"), |
| 18 | + ] |
| 19 | + oras_resolve("registry.io/repo:tag") |
| 20 | + |
| 21 | + first_call = mock_run.call_args_list[0] |
| 22 | + assert first_call == call(["select-oci-auth", "registry.io/repo:tag"]) |
| 23 | + |
| 24 | + |
| 25 | +def test_oras_resolve_passes_auth_file_to_oras() -> None: |
| 26 | + """Oras resolve is called with --registry-config pointing to the auth temp file.""" |
| 27 | + with patch("oras_utils.run_cmd") as mock_run: |
| 28 | + mock_run.side_effect = [ |
| 29 | + MagicMock(stdout='{"auths": {}}'), |
| 30 | + MagicMock(returncode=0, stdout="sha256:abc\n"), |
| 31 | + ] |
| 32 | + oras_resolve("registry.io/repo:tag") |
| 33 | + |
| 34 | + second_call = mock_run.call_args_list[1] |
| 35 | + cmd = second_call.args[0] |
| 36 | + assert cmd[0] == "oras" |
| 37 | + assert cmd[1] == "resolve" |
| 38 | + assert "--registry-config" in cmd |
| 39 | + assert "registry.io/repo:tag" in cmd |
| 40 | + |
| 41 | + |
| 42 | +def test_oras_resolve_returns_stripped_digest() -> None: |
| 43 | + """Returns the digest from oras resolve output, stripped of whitespace.""" |
| 44 | + with patch("oras_utils.run_cmd") as mock_run: |
| 45 | + mock_run.side_effect = [ |
| 46 | + MagicMock(stdout="{}"), |
| 47 | + MagicMock(returncode=0, stdout="sha256:deadbeef\n"), |
| 48 | + ] |
| 49 | + result = oras_resolve("registry.io/repo:tag") |
| 50 | + |
| 51 | + assert result == "sha256:deadbeef" |
| 52 | + |
| 53 | + |
| 54 | +def test_oras_resolve_raises_on_nonzero_returncode() -> None: |
| 55 | + """Raises RuntimeError when oras resolve exits non-zero.""" |
| 56 | + with patch("oras_utils.run_cmd") as mock_run: |
| 57 | + mock_run.side_effect = [ |
| 58 | + MagicMock(stdout="{}"), |
| 59 | + MagicMock(returncode=1, stdout="", stderr="unauthorized"), |
| 60 | + ] |
| 61 | + with pytest.raises(RuntimeError): |
| 62 | + oras_resolve("registry.io/repo:tag") |
0 commit comments