File: test_arch.py

package info (click to toggle)
pmbootstrap 3.9.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,864 kB
  • sloc: python: 17,395; sh: 425; makefile: 17
file content (120 lines) | stat: -rw-r--r-- 4,489 bytes parent folder | download
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
# Copyright 2024 Caleb Connolly
# SPDX-License-Identifier: GPL-3.0-or-later

import os
from pathlib import Path
from typing import Any

import pytest

from pmb.core.arch import Arch


def test_valid_arches() -> None:
    # Silly test
    assert Arch.native().is_native()

    # Test constructor interface
    assert Arch.from_str("x86") == Arch.x86
    assert Arch.from_str("x86_64") == Arch.x86_64
    assert Arch.from_str("aarch64") == Arch.aarch64
    assert Arch.from_str("armhf") == Arch.armhf

    # Test from_machine_type
    assert Arch.from_machine_type("i686") == Arch.x86
    assert Arch.from_machine_type("x86_64") == Arch.x86_64
    assert Arch.from_machine_type("aarch64") == Arch.aarch64

    # Check supported architectures
    assert Arch.x86 in Arch.supported()
    assert Arch.x86_64 in Arch.supported()
    assert Arch.aarch64 in Arch.supported()
    assert Arch.armhf in Arch.supported()
    assert Arch.armv7 in Arch.supported()
    assert Arch.riscv64 in Arch.supported()
    assert Arch.ppc64le in Arch.supported()
    assert Arch.s390x in Arch.supported()
    assert Arch.loongarch64 in Arch.supported()

    # kernel directory
    assert Arch.x86.kernel_dir() == "x86"
    assert Arch.x86_64.kernel_dir() == "x86"
    assert Arch.aarch64.kernel_dir() == "arm64"  # The fun one
    assert Arch.armhf.kernel_dir() == "arm"
    assert Arch.armv7.kernel_dir() == "arm"
    assert Arch.ppc64le.kernel_dir() == "powerpc"
    assert Arch.loongarch64.kernel_dir() == "loongarch"
    # kernel ARCH=
    assert Arch.x86.kernel_arch() == "i386"
    assert Arch.x86_64.kernel_arch() == "x86_64"
    assert Arch.aarch64.kernel_arch() == "arm64"
    assert Arch.armhf.kernel_arch() == "arm"
    assert Arch.armv7.kernel_arch() == "arm"
    assert Arch.ppc64le.kernel_arch() == "powerpc"
    assert Arch.loongarch64.kernel_arch() == "loongarch"

    # qemu arch
    assert Arch.x86.qemu_system() == "i386"
    assert Arch.x86.qemu_user() == "i386"
    assert Arch.x86_64.qemu_system() == "x86_64"
    assert Arch.x86_64.qemu_user() == "x86_64"
    assert Arch.aarch64.qemu_system() == "aarch64"
    assert Arch.aarch64.qemu_user() == "aarch64"
    assert Arch.armhf.qemu_system() == "arm"
    assert Arch.armhf.qemu_user() == "arm"
    assert Arch.armv7.qemu_system() == "arm"
    assert Arch.armv7.qemu_user() == "arm"
    assert Arch.ppc64.qemu_system() == "ppc64"
    assert Arch.ppc64.qemu_user() == "ppc64"
    assert Arch.ppc64le.qemu_system() == "ppc64"
    assert Arch.ppc64le.qemu_user() == "ppc64le"
    assert Arch.loongarch64.qemu_system() == "loongarch64"
    assert Arch.loongarch64.qemu_user() == "loongarch64"

    # Go arch
    assert Arch.armhf.go() == "arm"
    assert Arch.armv7.go() == "arm"
    assert Arch.aarch64.go() == "arm64"
    assert Arch.riscv64.go() == "riscv64"
    assert Arch.ppc64le.go() == "ppc64le"
    assert Arch.x86_64.go() == "amd64"
    assert Arch.loongarch64.go() == "loong64"
    with pytest.raises(ValueError) as excinfo:
        Arch.mips64.go()
    assert "Can not map architecture 'mips64' to Go arch" in str(excinfo.value)

    # Check that Arch.cpu_emulation_required() works
    assert Arch.native() == Arch.x86_64 or Arch.x86_64.cpu_emulation_required()
    assert Arch.native() == Arch.aarch64 or Arch.aarch64.cpu_emulation_required()

    # Check that every arch has a target triple (except "noarch")
    for arch in Arch:
        if arch == Arch.noarch:
            continue
        assert arch.alpine_triple() is not None

    # Arch-as-path magic
    assert Arch.aarch64 / Path("beep") == Path("aarch64/beep")
    assert os.fspath(Arch.aarch64 / "beep") == "aarch64/beep"
    assert isinstance(Arch.aarch64 / "beep", Path)
    assert (Arch.aarch64 / "beep").name == "beep"
    assert Path("boop") / Arch.aarch64 == Path("boop/aarch64")


def test_invalid_arches() -> None:
    excinfo: Any
    with pytest.raises(ValueError) as excinfo:
        Arch.from_str("invalid")
    assert "Invalid architecture: 'invalid'" in str(excinfo.value)

    with pytest.raises(TypeError) as excinfo:
        Arch.aarch64 / 5
    assert "unsupported operand type(s) for /: 'Arch' and 'int'" in str(excinfo.value)

    with pytest.raises(TypeError) as excinfo:
        "bap" / Arch.aarch64
    assert "unsupported operand type(s) for /: 'str' and 'Arch'" in str(excinfo.value)

    with pytest.raises(ValueError) as excinfo:
        Arch.from_machine_type("invalid")
    assert "Unsupported machine type 'invalid'" in str(excinfo.value)