File: test_parse_load_args.py

package info (click to toggle)
mkosi 14-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,268 kB
  • sloc: python: 8,878; sh: 95; makefile: 6
file content (140 lines) | stat: -rw-r--r-- 4,538 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# SPDX-License-Identifier: LGPL-2.1+

import argparse
import tempfile
import textwrap
import uuid
from contextlib import contextmanager
from os import chdir, getcwd
from pathlib import Path
from typing import Iterator, List, Optional

import pytest

import mkosi
from mkosi.backend import Distribution, MkosiConfig, MkosiException, Verb


def parse(argv: Optional[List[str]] = None) -> MkosiConfig:
    return mkosi.load_args(mkosi.parse_args(argv)["default"])

@contextmanager
def cd_temp_dir() -> Iterator[None]:
    old_dir = getcwd()

    with tempfile.TemporaryDirectory() as tmp_dir:
        chdir(tmp_dir)
        try:
            yield
        finally:
            chdir(old_dir)

def test_parse_load_verb() -> None:
    assert parse(["build"]).verb == Verb.build
    assert parse(["clean"]).verb == Verb.clean
    with pytest.raises(SystemExit):
        parse(["help"])
    assert parse(["genkey"]).verb == Verb.genkey
    assert parse(["bump"]).verb == Verb.bump
    assert parse(["serve"]).verb == Verb.serve
    assert parse(["build"]).verb == Verb.build
    assert parse(["shell"]).verb == Verb.shell
    assert parse(["boot"]).verb == Verb.boot
    assert parse(["--bootable", "qemu"]).verb == Verb.qemu
    with pytest.raises(SystemExit):
        parse(["invalid"])

def test_os_distribution() -> None:
    for dist in Distribution:
        assert parse(["-d", dist.name]).distribution == dist

    with pytest.raises(tuple((argparse.ArgumentError, SystemExit))):
        parse(["-d", "invalidDistro"])
    with pytest.raises(tuple((argparse.ArgumentError, SystemExit))):
        parse(["-d"])

    for dist in Distribution:
        with cd_temp_dir():
            config = Path("mkosi.conf")
            config.write_text(f"[Distribution]\nDistribution={dist}")
            assert parse([]).distribution == dist

def test_machine_id() -> None:
    id = uuid.uuid4().hex
    load_args = parse(["--machine-id", id])

    assert load_args.machine_id == id

    with pytest.raises(MkosiException):
        parse(["--machine-id", "notValidKey"])
    with pytest.raises(tuple((argparse.ArgumentError, SystemExit))):
        parse(["--machine-id"])

    with cd_temp_dir():
        config = Path("mkosi.conf")
        config.write_text(f"[Output]\nMachineID={id}")
        load_args = parse([])
        assert load_args.machine_id == id

    with cd_temp_dir():
        config = Path("mkosi.conf")
        config.write_text("[Output]\nMachineID=")
        with pytest.raises(MkosiException):
            parse([])

def test_hostname() -> None:
    assert parse(["--hostname", "name"]).hostname == "name"
    with pytest.raises(SystemExit):
        parse(["--hostname", "name", "additional_name"])
    with pytest.raises(SystemExit):
        parse(["--hostname"])

    with cd_temp_dir():
        config = Path("mkosi.conf")
        config.write_text("[Output]\nHostname=name")
        assert parse([]).hostname == "name"

    with cd_temp_dir():
        config = Path("mkosi.conf")
        config.write_text("[Output]\nHostname=")
        config = Path("hostname.txt")
        assert parse([]).hostname == ""

def test_centos_brtfs() -> None:
    with cd_temp_dir():
        config = Path("mkosi.conf")
        for dist in (Distribution.centos, Distribution.centos_epel):
            for release in range(2, 9):
                config.write_text(
                    textwrap.dedent(
                        f"""
                        [Distribution]
                        Distribution={dist}
                        Release={release}
                        [Output]
                        Format=gpt_btrfs
                        """
                    )
                )
                with pytest.raises(MkosiException, match=".CentOS.*btrfs"):
                    parse([])

def test_shell_boot() -> None:
    with pytest.raises(MkosiException, match=".boot.*tar"):
        parse(["--format", "tar", "boot"])

    with pytest.raises(MkosiException, match=".boot.*cpio"):
        parse(["--format", "cpio", "boot"])

    with pytest.raises(MkosiException, match=".boot.*compressed" ):
        parse(["--format", "gpt_squashfs", "--compress-output", "True", "boot"])

    with pytest.raises(MkosiException, match=".boot.*qcow2"):
        parse(["--format", "gpt_xfs", "--qcow2", "boot"])

def test_compression() -> None:
    assert parse(["--format", "gpt_squashfs"]).compress

    with pytest.raises(MkosiException, match=".*compression.*squashfs"):
        parse(["--format", "gpt_squashfs", "--compress", "False"])