|
| 1 | +import tempfile |
| 2 | +from unittest.mock import patch |
| 3 | + |
| 4 | +import pytest |
| 5 | +from jumpstarter_driver_pyserial.driver import PySerial |
| 6 | + |
| 7 | +from .driver import RideSXDriver |
| 8 | +from jumpstarter.common.utils import serve |
| 9 | + |
| 10 | + |
| 11 | +@pytest.fixture(scope="session") |
| 12 | +def temp_storage_dir(): |
| 13 | + with tempfile.TemporaryDirectory() as temp_dir: |
| 14 | + yield temp_dir |
| 15 | + |
| 16 | + |
| 17 | +@pytest.fixture(scope="session") |
| 18 | +def ridesx_driver(temp_storage_dir): |
| 19 | + yield RideSXDriver( |
| 20 | + storage_dir=temp_storage_dir, |
| 21 | + children={ |
| 22 | + "serial": PySerial(url="loop://"), |
| 23 | + }, |
| 24 | + ) |
| 25 | + |
| 26 | + |
| 27 | +@pytest.fixture |
| 28 | +def ridesx_client(ridesx_driver): |
| 29 | + """Create a client instance for testing client-side methods""" |
| 30 | + with serve(ridesx_driver) as client: |
| 31 | + yield client |
| 32 | + |
| 33 | + |
| 34 | +# OCI Path Detection Tests |
| 35 | + |
| 36 | + |
| 37 | +@pytest.mark.parametrize( |
| 38 | + "path,expected", |
| 39 | + [ |
| 40 | + # OCI paths (should return True) |
| 41 | + ("oci://quay.io/myimage:latest", True), |
| 42 | + ("quay.io/myorg/myimage:latest", True), |
| 43 | + ("ghcr.io/owner/repo:tag", True), |
| 44 | + ("registry.redhat.io/rhel:9", True), |
| 45 | + # Non-OCI paths (should return False) |
| 46 | + ("docker://registry.example.com/image:v1", False), # only oci:// scheme accepted |
| 47 | + ("/path/to/file.img", False), |
| 48 | + ("relative/path/file.img", False), |
| 49 | + ("boot.img", False), |
| 50 | + ], |
| 51 | +) |
| 52 | +def test_is_oci_path(ridesx_client, path, expected): |
| 53 | + assert ridesx_client._is_oci_path(path) is expected |
| 54 | + |
| 55 | + |
| 56 | +# OCI URL from argv Tests |
| 57 | + |
| 58 | + |
| 59 | +@pytest.mark.parametrize( |
| 60 | + "argv,expected", |
| 61 | + [ |
| 62 | + (["cmd", "oci://quay.io/image:tag"], "oci://quay.io/image:tag"), |
| 63 | + (["cmd", "quay.io/org/image:v1"], "quay.io/org/image:v1"), |
| 64 | + (["cmd", "ghcr.io/owner/repo:latest"], "ghcr.io/owner/repo:latest"), |
| 65 | + (["cmd", "docker.io/library/alpine:3.18"], "docker.io/library/alpine:3.18"), |
| 66 | + (["cmd", "/path/to/file.img", "-t", "boot:file"], None), |
| 67 | + (["cmd", "-t", "boot:file", "oci://first:tag", "oci://second:tag"], "oci://first:tag"), |
| 68 | + ], |
| 69 | +) |
| 70 | +def test_oci_url_from_argv(ridesx_client, argv, expected): |
| 71 | + with patch("sys.argv", argv): |
| 72 | + result = ridesx_client._oci_url_from_argv() |
| 73 | + assert result == expected |
| 74 | + |
| 75 | + |
| 76 | +# Target Mappings from argv Tests |
| 77 | + |
| 78 | + |
| 79 | +@pytest.mark.parametrize( |
| 80 | + "argv,expected", |
| 81 | + [ |
| 82 | + (["cmd", "-t", "boot_a:boot.img"], {"boot_a": "boot.img"}), |
| 83 | + (["cmd", "--target", "system_a:rootfs.simg"], {"system_a": "rootfs.simg"}), |
| 84 | + (["cmd", "--target=boot_a:boot.img"], {"boot_a": "boot.img"}), |
| 85 | + ( |
| 86 | + ["cmd", "-t", "boot_a:boot.img", "-t", "system_a:rootfs.simg"], |
| 87 | + {"boot_a": "boot.img", "system_a": "rootfs.simg"}, |
| 88 | + ), |
| 89 | + (["cmd", "oci://image:tag"], None), |
| 90 | + ( |
| 91 | + ["cmd", "-t", "boot_a:boot.img", "oci://image:tag", "--other", "value"], |
| 92 | + {"boot_a": "boot.img"}, |
| 93 | + ), |
| 94 | + ], |
| 95 | +) |
| 96 | +def test_target_mappings_from_argv(ridesx_client, argv, expected): |
| 97 | + with patch("sys.argv", argv): |
| 98 | + result = ridesx_client._target_mappings_from_argv() |
| 99 | + assert result == expected |
| 100 | + |
| 101 | + |
| 102 | +# Resolve Flash Input Tests |
| 103 | + |
| 104 | + |
| 105 | +def test_resolve_flash_input_dict_path(ridesx_client): |
| 106 | + """Test dict path input returns partitions directly""" |
| 107 | + partitions, operators, oci_url = ridesx_client._resolve_flash_input( |
| 108 | + {"boot": "/path/boot.img"}, None, None, None |
| 109 | + ) |
| 110 | + assert partitions == {"boot": "/path/boot.img"} |
| 111 | + assert operators is None |
| 112 | + assert oci_url is None |
| 113 | + |
| 114 | + # With OCI from argv |
| 115 | + partitions, operators, oci_url = ridesx_client._resolve_flash_input( |
| 116 | + {"boot": "boot.img"}, None, None, "oci://image:tag" |
| 117 | + ) |
| 118 | + assert partitions == {"boot": "boot.img"} |
| 119 | + assert oci_url == "oci://image:tag" |
| 120 | + |
| 121 | + |
| 122 | +def test_resolve_flash_input_oci_path(ridesx_client): |
| 123 | + """Test OCI path input with and without target""" |
| 124 | + # Without target - auto-detect mode |
| 125 | + partitions, operators, oci_url = ridesx_client._resolve_flash_input( |
| 126 | + "oci://quay.io/image:tag", None, None, None |
| 127 | + ) |
| 128 | + assert partitions is None |
| 129 | + assert operators is None |
| 130 | + assert oci_url == "oci://quay.io/image:tag" |
| 131 | + |
| 132 | + # With target - explicit mapping |
| 133 | + partitions, operators, oci_url = ridesx_client._resolve_flash_input( |
| 134 | + "oci://quay.io/image:tag", "boot_a:boot.img", None, None |
| 135 | + ) |
| 136 | + assert partitions == {"boot_a": "boot.img"} |
| 137 | + assert oci_url == "oci://quay.io/image:tag" |
| 138 | + |
| 139 | + |
| 140 | +def test_resolve_flash_input_oci_path_invalid_target(ridesx_client): |
| 141 | + """Test OCI path with invalid target format raises error""" |
| 142 | + with pytest.raises(ValueError, match="Target must be in format"): |
| 143 | + ridesx_client._resolve_flash_input("oci://quay.io/image:tag", "boot_a", None, None) |
| 144 | + |
| 145 | + |
| 146 | +def test_resolve_flash_input_local_path(ridesx_client): |
| 147 | + """Test local path input requires target""" |
| 148 | + # Without target - should raise |
| 149 | + with pytest.raises(ValueError, match="This driver requires a target partition"): |
| 150 | + ridesx_client._resolve_flash_input("/path/to/boot.img", None, None, None) |
| 151 | + |
| 152 | + # With target |
| 153 | + partitions, operators, oci_url = ridesx_client._resolve_flash_input( |
| 154 | + "/path/to/boot.img", "boot_a", None, None |
| 155 | + ) |
| 156 | + assert partitions == {"boot_a": "/path/to/boot.img"} |
| 157 | + assert oci_url is None |
| 158 | + |
| 159 | + # With target and OCI from argv |
| 160 | + partitions, operators, oci_url = ridesx_client._resolve_flash_input( |
| 161 | + "/path/to/boot.img", "boot_a", None, "oci://image:tag" |
| 162 | + ) |
| 163 | + assert partitions == {"boot_a": "/path/to/boot.img"} |
| 164 | + assert oci_url == "oci://image:tag" |
| 165 | + |
| 166 | + |
| 167 | +# Validate Partition Mappings Tests |
| 168 | + |
| 169 | + |
| 170 | +def test_validate_partition_mappings(ridesx_client): |
| 171 | + """Test partition mapping validation""" |
| 172 | + # None is valid (auto-detect mode) |
| 173 | + ridesx_client._validate_partition_mappings(None) |
| 174 | + |
| 175 | + # Valid mapping |
| 176 | + ridesx_client._validate_partition_mappings({"boot": "/path/to/boot.img"}) |
| 177 | + |
| 178 | + # Empty path raises |
| 179 | + with pytest.raises(ValueError, match="has an empty file path"): |
| 180 | + ridesx_client._validate_partition_mappings({"boot": ""}) |
| 181 | + |
| 182 | + # Whitespace-only path raises |
| 183 | + with pytest.raises(ValueError, match="has an empty file path"): |
| 184 | + ridesx_client._validate_partition_mappings({"boot": " "}) |
| 185 | + |
| 186 | + |
| 187 | +# Flash OCI Auto Tests |
| 188 | + |
| 189 | + |
| 190 | +def test_flash_oci_auto_normalizes_bare_registry(ridesx_client): |
| 191 | + """Test that bare registry URLs are normalized with oci:// prefix""" |
| 192 | + with patch.object(ridesx_client, "call") as mock_call: |
| 193 | + mock_call.side_effect = [ |
| 194 | + {"status": "device_found", "device_id": "ABC123"}, |
| 195 | + {"status": "success"}, |
| 196 | + ] |
| 197 | + |
| 198 | + result = ridesx_client.flash_oci_auto("quay.io/org/image:tag") |
| 199 | + |
| 200 | + assert result == {"status": "success"} |
| 201 | + flash_call = mock_call.call_args_list[1] |
| 202 | + assert flash_call[0][1].startswith("oci://") |
| 203 | + |
| 204 | + |
| 205 | +def test_flash_oci_auto_error_cases(ridesx_client): |
| 206 | + """Test flash_oci_auto error handling""" |
| 207 | + # Invalid URL |
| 208 | + with pytest.raises(ValueError, match="Invalid OCI URL format"): |
| 209 | + ridesx_client.flash_oci_auto("invalid-url") |
| 210 | + |
| 211 | + # No device found |
| 212 | + with patch.object(ridesx_client, "call") as mock_call: |
| 213 | + mock_call.return_value = {"status": "no_device_found", "device_id": None} |
| 214 | + |
| 215 | + with pytest.raises(RuntimeError, match="No fastboot devices found"): |
| 216 | + ridesx_client.flash_oci_auto("oci://image:tag") |
0 commit comments