File: test_verify_system_image.py

package info (click to toggle)
python-briefcase 0.3.22-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 7,300 kB
  • sloc: python: 59,405; makefile: 57
file content (143 lines) | stat: -rw-r--r-- 4,682 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import platform
from subprocess import CalledProcessError

import pytest

from briefcase.exceptions import BriefcaseCommandError


@pytest.mark.parametrize(
    "host_os, host_arch",
    [
        ("Windows", "arm64"),
        ("Linux", "armv7l"),
    ],
)
def test_unsupported_abi(mock_tools, android_sdk, host_os, host_arch):
    """If the system has an unsupported ABI, raise an error."""
    # Mock the hardware and OS
    mock_tools.host_os = host_os
    mock_tools.host_arch = host_arch

    with pytest.raises(
        BriefcaseCommandError,
        match=f"The Android emulator does not currently support {host_os} {host_arch} hardware",
    ):
        android_sdk.verify_system_image("system-images;android-31;default;x86_64")


@pytest.mark.parametrize(
    "bad_image_name",
    [
        # Patently invalid
        "cheesecake",
        # Only the prefix
        "system-images",
        # Not enough parts
        "system-images;android-31;default",
        # Broadly good structure, but bad initial prefix
        "system-image;android-31;default;anything",
    ],
)
def test_invalid_system_image(mock_tools, android_sdk, bad_image_name):
    """If the system image name doesn't make sense, raise an error."""
    # Mock the host arch
    mock_tools.host_arch = "x86_64"

    # Verify a system image that doesn't match the host architecture
    with pytest.raises(
        BriefcaseCommandError,
        match=rf"{bad_image_name!r} is not a valid system image name.",
    ):
        android_sdk.verify_system_image(bad_image_name)


def test_incompatible_abi(mock_tools, android_sdk, capsys):
    """If the system image doesn't match the emulator ABI, warn the user, but
    continue."""
    # Mock the host arch
    mock_tools.host_arch = "AMD64" if platform.system() == "Windows" else "x86_64"

    # Verify a system image that doesn't match the host architecture
    android_sdk.verify_system_image("system-images;android-31;default;anything")

    # A warning message was output
    assert "WARNING: Unexpected emulator ABI" in capsys.readouterr().out

    # The system image was installed.
    mock_tools.subprocess.run.assert_called_once_with(
        [
            android_sdk.sdkmanager_path,
            "system-images;android-31;default;anything",
        ],
        env=android_sdk.env,
        check=True,
        stream_output=False,
    )


def test_existing_system_image(mock_tools, android_sdk):
    """If the system image already exists, don't attempt to download it again."""
    # Mock the host arch
    mock_tools.host_arch = "AMD64" if platform.system() == "Windows" else "x86_64"

    # Mock the existence of a system image
    (android_sdk.root_path / "system-images/android-31/default/x86_64").mkdir(
        parents=True
    )

    # Verify the system image that we already have
    android_sdk.verify_system_image("system-images;android-31;default;x86_64")

    # sdkmanager was *not* called.
    mock_tools.subprocess.run.assert_not_called()


def test_new_system_image(mock_tools, android_sdk):
    """If the system image doesn't exist locally, it will be installed."""
    # Mock the host arch
    mock_tools.host_arch = "AMD64" if platform.system() == "Windows" else "x86_64"

    # Verify the system image, triggering a download
    android_sdk.verify_system_image("system-images;android-31;default;x86_64")

    # The system image was installed.
    mock_tools.subprocess.run.assert_called_once_with(
        [
            android_sdk.sdkmanager_path,
            "system-images;android-31;default;x86_64",
        ],
        env=android_sdk.env,
        check=True,
        stream_output=False,
    )


def test_problem_downloading_system_image(mock_tools, android_sdk):
    """If there is a failure downloading the system image, an error is raised."""
    # Mock the host arch
    mock_tools.host_arch = "AMD64" if platform.system() == "Windows" else "x86_64"

    # Mock a failure condition on subprocess.run
    mock_tools.subprocess.run.side_effect = CalledProcessError(
        returncode=1,
        cmd="sdkmanager",
    )

    # Attempt to verify the system image
    with pytest.raises(
        BriefcaseCommandError,
        match="Error while installing the 'system-images;android-31;default;x86_64' Android system image.",
    ):
        android_sdk.verify_system_image("system-images;android-31;default;x86_64")

    # An attempt to install the system image was made
    mock_tools.subprocess.run.assert_called_once_with(
        [
            android_sdk.sdkmanager_path,
            "system-images;android-31;default;x86_64",
        ],
        env=android_sdk.env,
        check=True,
        stream_output=False,
    )