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 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654
|
# SPDX-License-Identifier: LGPL-2.1-or-later
import argparse
import itertools
import logging
import operator
import os
from pathlib import Path
import pytest
import mkosi.resources
from mkosi import expand_kernel_specifiers
from mkosi.config import (
Architecture,
ArtifactOutput,
Compression,
Config,
ConfigFeature,
ConfigTree,
OutputFormat,
Verb,
config_parse_bytes,
in_box,
parse_config,
parse_ini,
)
from mkosi.distribution import Distribution, detect_distribution
from mkosi.util import chdir, resource_path
def test_compression_enum_creation() -> None:
assert Compression["none"] == Compression.none
assert Compression["zstd"] == Compression.zstd
assert Compression["zst"] == Compression.zstd
assert Compression["xz"] == Compression.xz
assert Compression["bz2"] == Compression.bz2
assert Compression["gz"] == Compression.gz
assert Compression["lz4"] == Compression.lz4
assert Compression["lzma"] == Compression.lzma
def test_compression_enum_bool() -> None:
assert not bool(Compression.none)
assert bool(Compression.zstd)
assert bool(Compression.xz)
assert bool(Compression.bz2)
assert bool(Compression.gz)
assert bool(Compression.lz4)
assert bool(Compression.lzma)
def test_compression_enum_str() -> None:
assert str(Compression.none) == "none"
assert str(Compression.zstd) == "zstd"
assert str(Compression.zst) == "zstd"
assert str(Compression.xz) == "xz"
assert str(Compression.bz2) == "bz2"
assert str(Compression.gz) == "gz"
assert str(Compression.lz4) == "lz4"
assert str(Compression.lzma) == "lzma"
def test_parse_ini(tmp_path: Path) -> None:
p = tmp_path / "ini"
p.write_text(
"""\
[MySection]
Value=abc
Other=def
ALLCAPS=txt
# Comment
[EmptySection]
[AnotherSection]
EmptyValue=
Multiline=abc
def
qed
ord
"""
)
g = parse_ini(p)
assert next(g) == ("MySection", "Value", "abc")
assert next(g) == ("MySection", "Other", "def")
assert next(g) == ("MySection", "ALLCAPS", "txt")
assert next(g) == ("MySection", "", "")
assert next(g) == ("EmptySection", "", "")
assert next(g) == ("AnotherSection", "EmptyValue", "")
assert next(g) == ("AnotherSection", "Multiline", "abc\ndef\nqed\nord")
def test_parse_config(tmp_path: Path) -> None:
d = tmp_path
(d / "mkosi.conf").write_text(
"""\
[Distribution]
Distribution=ubuntu
Architecture=arm64
Repositories=epel,epel-next
[Config]
Profiles=abc
[Build]
Environment=MY_KEY=MY_VALUE
[Output]
Format=cpio
ImageId=base
[Runtime]
Credentials=my.cred=my.value
"""
)
with chdir(d):
_, _, [config] = parse_config()
assert config.distribution == Distribution.ubuntu
assert config.architecture == Architecture.arm64
assert config.profiles == ["abc"]
assert config.output_format == OutputFormat.cpio
assert config.image_id == "base"
with chdir(d):
_, _, [config] = parse_config(
[
"--distribution", "fedora",
"--environment", "MY_KEY=CLI_VALUE",
"--credential", "my.cred=cli.value",
"--repositories", "universe",
]
) # fmt: skip
# Values from the CLI should take priority.
assert config.distribution == Distribution.fedora
assert config.environment["MY_KEY"] == "CLI_VALUE"
assert config.credentials["my.cred"] == "cli.value"
assert config.repositories == ["epel", "epel-next", "universe"]
with chdir(d):
_, _, [config] = parse_config(
[
"--distribution", "",
"--environment", "",
"--credential", "",
"--repositories", "",
]
) # fmt: skip
# Empty values on the CLIs resets non-collection based settings to their defaults and collection based
# settings to empty collections.
assert config.distribution == (detect_distribution()[0] or Distribution.custom)
assert "MY_KEY" not in config.environment
assert "my.cred" not in config.credentials
assert config.repositories == []
(d / "mkosi.conf.d").mkdir()
(d / "mkosi.conf.d/d1.conf").write_text(
"""\
[Distribution]
Distribution=debian
[Config]
Profiles=qed
def
[Output]
ImageId=00-dropin
ImageVersion=0
@Output=abc
"""
)
with chdir(d):
_, _, [config] = parse_config(["--profile", "last"])
# Setting a value explicitly in a dropin should override the default from mkosi.conf.
assert config.distribution == Distribution.debian
# Lists should be merged by appending the new values to the existing values. Any values from the CLI
# should be appended to the values from the configuration files.
assert config.profiles == ["abc", "qed", "def", "last"]
assert config.output_format == OutputFormat.cpio
assert config.image_id == "00-dropin"
assert config.image_version == "0"
# '@' specifier should be automatically dropped.
assert config.output == "abc"
(d / "mkosi.version").write_text("1.2.3")
(d / "mkosi.conf.d/d2.conf").write_text(
"""\
[Content]
Packages=
[Output]
ImageId=
"""
)
with chdir(d):
_, _, [config] = parse_config()
# Test that empty assignment resets settings.
assert config.packages == []
assert config.image_id is None
# mkosi.version should only be used if no version is set explicitly.
assert config.image_version == "0"
(d / "mkosi.conf.d/d1.conf").unlink()
with chdir(d):
_, _, [config] = parse_config()
# ImageVersion= is not set explicitly anymore, so now the version from mkosi.version should be used.
assert config.image_version == "1.2.3"
(d / "abc").mkdir()
(d / "abc/mkosi.conf").write_text(
"""\
[Content]
BuildPackages=abc
[Runtime]
CXL=yes
"""
)
(d / "abc/mkosi.conf.d").mkdir()
(d / "abc/mkosi.conf.d/abc.conf").write_text(
"""\
[Output]
SplitArtifacts=yes
"""
)
with chdir(d):
_, _, [config] = parse_config()
assert not config.cxl
assert config.split_artifacts == ArtifactOutput.compat_no()
# Passing the directory should include both the main config file and the dropin.
_, _, [config] = parse_config(["--include", os.fspath(d / "abc")] * 2)
assert config.cxl
assert config.split_artifacts == ArtifactOutput.compat_yes()
# The same extra config should not be parsed more than once.
assert config.build_packages == ["abc"]
# Passing the main config file should not include the dropin.
_, _, [config] = parse_config(["--include", os.fspath(d / "abc/mkosi.conf")])
assert config.cxl
assert config.split_artifacts == ArtifactOutput.compat_no()
(d / "mkosi.images").mkdir()
(d / "mkosi.images/one.conf").write_text(
"""\
[Content]
Packages=one
"""
)
(d / "mkosi.images/two").mkdir()
(d / "mkosi.images/two/mkosi.skeleton").mkdir()
(d / "mkosi.images/two/mkosi.conf").write_text(
"""
[Content]
Packages=two
[Output]
ImageVersion=4.5.6
"""
)
with chdir(d):
_, _, [one, two, config] = parse_config(
["--package", "qed", "--build-package", "def", "--repositories", "cli"]
)
# Universal settings should always come from the main image.
assert one.distribution == config.distribution
assert two.distribution == config.distribution
assert one.release == config.release
assert two.release == config.release
# Non-universal settings should not be passed to the subimages.
assert one.packages == ["one"]
assert two.packages == ["two"]
assert one.build_packages == []
assert two.build_packages == []
# But should apply to the main image of course.
assert config.packages == ["qed"]
assert config.build_packages == ["def"]
# Inherited settings should be passed down to subimages but overridable by subimages.
assert one.image_version == "1.2.3"
assert two.image_version == "4.5.6"
# Default values from subimages for universal settings should not be picked up.
assert len(one.sandbox_trees) == 0
assert len(two.sandbox_trees) == 0
with chdir(d):
_, _, [one, two, config] = parse_config(["--image-version", "7.8.9"])
# Inherited settings specified on the CLI should not override subimages that configure the setting
# explicitly.
assert config.image_version == "7.8.9"
assert one.image_version == "7.8.9"
assert two.image_version == "4.5.6"
def test_parse_includes_once(tmp_path: Path) -> None:
d = tmp_path
(d / "mkosi.conf").write_text(
"""\
[Content]
BuildPackages=abc
"""
)
(d / "abc.conf").write_text(
"""\
[Content]
BuildPackages=def
"""
)
with chdir(d):
_, _, [config] = parse_config(["--include", "abc.conf", "--include", "abc.conf"])
assert config.build_packages == ["abc", "def"]
(d / "mkosi.images").mkdir()
for n in ("one", "two"):
(d / "mkosi.images" / f"{n}.conf").write_text(
"""\
[Config]
Include=abc.conf
"""
)
with chdir(d):
_, _, [one, two, config] = parse_config([])
assert one.build_packages == ["def"]
assert two.build_packages == ["def"]
def test_profiles(tmp_path: Path) -> None:
d = tmp_path
(d / "mkosi.profiles").mkdir()
(d / "mkosi.profiles/profile.conf").write_text(
"""\
[Distribution]
Distribution=fedora
[Runtime]
KVM=yes
"""
)
(d / "mkosi.conf").write_text(
"""\
[Config]
Profiles=profile
"""
)
(d / "mkosi.conf.d").mkdir()
(d / "mkosi.conf.d/abc.conf").write_text(
"""\
[Distribution]
Distribution=debian
"""
)
with chdir(d):
_, _, [config] = parse_config()
assert config.profiles == ["profile"]
# The profile should override mkosi.conf.d/
assert config.distribution == Distribution.fedora
assert config.kvm == ConfigFeature.enabled
(d / "mkosi.conf").unlink()
with chdir(d):
_, _, [config] = parse_config(["--profile", "profile"])
assert config.profiles == ["profile"]
# The profile should override mkosi.conf.d/
assert config.distribution == Distribution.fedora
assert config.kvm == ConfigFeature.enabled
(d / "mkosi.conf").write_text(
"""\
[Config]
Profiles=profile,abc
"""
)
(d / "mkosi.profiles/abc.conf").write_text(
"""\
[Match]
Profiles=abc
[Distribution]
Distribution=opensuse
"""
)
with chdir(d):
_, _, [config] = parse_config()
assert config.profiles == ["profile", "abc"]
assert config.distribution == Distribution.opensuse
# Check that mkosi.profiles/ is parsed in subimages as well.
(d / "mkosi.images/subimage/mkosi.profiles").mkdir(parents=True)
(d / "mkosi.images/subimage/mkosi.profiles/abc.conf").write_text(
"""
[Build]
Environment=Image=%I
"""
)
with chdir(d):
_, _, [subimage, config] = parse_config()
assert subimage.environment["Image"] == "subimage"
def test_override_default(tmp_path: Path) -> None:
d = tmp_path
(d / "mkosi.conf").write_text(
"""\
[Build]
Environment=MY_KEY=MY_VALUE
ToolsTree=yes
"""
)
with chdir(d):
_, _, [config] = parse_config(["--tools-tree", "", "--environment", ""])
assert config.tools_tree is None
assert "MY_KEY" not in config.environment
(d / "mkosi.tools.conf").touch()
(d / "mkosi.local.conf").write_text(
"""\
[Build]
ToolsTree=
"""
)
with chdir(d):
_, _, [config] = parse_config([])
assert config.tools_tree is None
def test_local_config(tmp_path: Path) -> None:
d = tmp_path
(d / "mkosi.local.conf").write_text(
"""\
[Distribution]
Distribution=debian
[Content]
WithTests=yes
Environment=FOO=override
Environment=BAZ=normal
"""
)
with chdir(d):
_, _, [config] = parse_config()
assert config.distribution == Distribution.debian
(d / "mkosi.conf").write_text(
"""\
[Distribution]
Distribution=fedora
[Content]
WithTests=no
Environment=FOO=normal
Environment=BAR=normal
"""
)
with chdir(d):
_, _, [config] = parse_config()
# Local config should take precedence over non-local config.
assert config.distribution == Distribution.debian
assert config.with_tests
with chdir(d):
_, _, [config] = parse_config(["--distribution", "fedora", "-T"])
assert config.distribution == Distribution.fedora
assert not config.with_tests
(d / "mkosi.local/mkosi.conf.d").mkdir(parents=True)
(d / "mkosi.local/mkosi.conf.d/10-test.conf").write_text(
"""\
[Content]
Environment=BAR=override
Environment=BAZ=override
"""
)
with chdir(d):
_, _, [config] = parse_config()
assert config.environment == {"FOO": "override", "BAR": "override", "BAZ": "override"}
def test_parse_load_verb(tmp_path: Path) -> None:
with chdir(tmp_path):
assert parse_config(["build"])[0].verb == Verb.build
assert parse_config(["clean"])[0].verb == Verb.clean
assert parse_config(["genkey"])[0].verb == Verb.genkey
assert parse_config(["bump"])[0].verb == Verb.bump
assert parse_config(["serve"])[0].verb == Verb.serve
assert parse_config(["build"])[0].verb == Verb.build
assert parse_config(["shell"])[0].verb == Verb.shell
assert parse_config(["boot"])[0].verb == Verb.boot
assert parse_config(["qemu"])[0].verb == Verb.qemu
assert parse_config(["vm"])[0].verb == Verb.vm
assert parse_config(["journalctl"])[0].verb == Verb.journalctl
assert parse_config(["coredumpctl"])[0].verb == Verb.coredumpctl
with pytest.raises(SystemExit):
parse_config(["invalid"])
def test_os_distribution(tmp_path: Path) -> None:
with chdir(tmp_path):
for dist in Distribution:
_, _, [config] = parse_config(["-d", dist.value])
assert config.distribution == dist
with pytest.raises(tuple((argparse.ArgumentError, SystemExit))):
parse_config(["-d", "invalidDistro"])
with pytest.raises(tuple((argparse.ArgumentError, SystemExit))):
parse_config(["-d"])
for dist in Distribution:
Path("mkosi.conf").write_text(f"[Distribution]\nDistribution={dist}")
_, _, [config] = parse_config()
assert config.distribution == dist
def test_parse_config_files_filter(tmp_path: Path) -> None:
with chdir(tmp_path):
confd = Path("mkosi.conf.d")
confd.mkdir()
(confd / "10-file.conf").write_text("[Content]\nPackages=yes")
(confd / "20-file.noconf").write_text("[Content]\nPackages=nope")
_, _, [config] = parse_config()
assert config.packages == ["yes"]
def test_compression(tmp_path: Path) -> None:
with chdir(tmp_path):
_, _, [config] = parse_config(["--format", "disk", "--compress-output", "False"])
assert config.compress_output == Compression.none
def test_match_only(tmp_path: Path) -> None:
with chdir(tmp_path):
Path("mkosi.conf").write_text(
"""\
[Match]
Format=|directory
Format=|disk
"""
)
Path("mkosi.conf.d").mkdir()
Path("mkosi.conf.d/10-abc.conf").write_text(
"""\
[Output]
ImageId=abcde
"""
)
_, _, [config] = parse_config(["--format", "tar"])
assert config.image_id != "abcde"
def test_match_multiple(tmp_path: Path) -> None:
with chdir(tmp_path):
Path("mkosi.conf").write_text(
"""\
[Match]
Format=|disk
Format=|directory
[Match]
Architecture=|x86-64
Architecture=|arm64
[Output]
ImageId=abcde
"""
)
# Both sections are not matched, so image ID should not be "abcde".
_, _, [config] = parse_config(["--format", "tar", "--architecture", "s390x"])
assert config.image_id != "abcde"
# Only a single section is matched, so image ID should not be "abcde".
_, _, [config] = parse_config(["--format", "disk", "--architecture", "s390x"])
assert config.image_id != "abcde"
# Both sections are matched, so image ID should be "abcde".
_, _, [config] = parse_config(["--format", "disk", "--architecture", "x86-64"])
assert config.image_id == "abcde"
Path("mkosi.conf").write_text(
"""\
[TriggerMatch]
Format=disk
Architecture=x86-64
[TriggerMatch]
Format=directory
Architecture=arm64
[Output]
ImageId=abcde
"""
)
# Both sections are not matched, so image ID should not be "abcde".
_, _, [config] = parse_config(["--format", "tar", "--architecture", "s390x"])
assert config.image_id != "abcde"
# The first section is matched, so image ID should be "abcde".
_, _, [config] = parse_config(["--format", "disk", "--architecture", "x86-64"])
assert config.image_id == "abcde"
# The second section is matched, so image ID should be "abcde".
_, _, [config] = parse_config(["--format", "directory", "--architecture", "arm64"])
assert config.image_id == "abcde"
# Parts of all section are matched, but none is matched fully, so image ID should not be "abcde".
_, _, [config] = parse_config(["--format", "disk", "--architecture", "arm64"])
assert config.image_id != "abcde"
Path("mkosi.conf").write_text(
"""\
[TriggerMatch]
Format=|disk
Format=|directory
[TriggerMatch]
Format=directory
Architecture=arm64
[Output]
ImageId=abcde
"""
)
# The first section is matched, so image ID should be "abcde".
_, _, [config] = parse_config(["--format", "disk"])
assert config.image_id == "abcde"
Path("mkosi.conf").write_text(
"""\
[TriggerMatch]
Format=|disk
Format=|directory
Architecture=x86-64
[TriggerMatch]
Format=directory
Architecture=arm64
[Output]
ImageId=abcde
"""
)
# No sections are matched, so image ID should be not "abcde".
_, _, [config] = parse_config(["--format", "disk", "--architecture=arm64"])
assert config.image_id != "abcde"
# Mixing both [Match] and [TriggerMatch]
Path("mkosi.conf").write_text(
"""\
[Match]
Format=disk
[TriggerMatch]
Architecture=arm64
[TriggerMatch]
Architecture=x86-64
[Output]
ImageId=abcde
"""
)
# Match and first TriggerMatch sections match
_, _, [config] = parse_config(["--format", "disk", "--architecture=arm64"])
assert config.image_id == "abcde"
# Match section matches, but no TriggerMatch section matches
_, _, [config] = parse_config(["--format", "disk", "--architecture=s390x"])
assert config.image_id != "abcde"
# Second TriggerMatch section matches, but the Match section does not
_, _, [config] = parse_config(["--format", "tar", "--architecture=x86-64"])
assert config.image_id != "abcde"
def test_match_empty(tmp_path: Path) -> None:
with chdir(tmp_path):
Path("mkosi.conf").write_text(
"""\
[Match]
Profiles=
[Build]
Environment=ABC=QED
"""
)
_, _, [config] = parse_config([])
assert config.environment.get("ABC") == "QED"
_, _, [config] = parse_config(["--profile", "profile"])
assert config.environment.get("ABC") is None
@pytest.mark.parametrize(
"dist1,dist2",
itertools.combinations_with_replacement([Distribution.debian, Distribution.opensuse], 2),
)
def test_match_distribution(tmp_path: Path, dist1: Distribution, dist2: Distribution) -> None:
with chdir(tmp_path):
parent = Path("mkosi.conf")
parent.write_text(
f"""\
[Distribution]
Distribution={dist1}
"""
)
Path("mkosi.conf.d").mkdir()
child1 = Path("mkosi.conf.d/child1.conf")
child1.write_text(
f"""\
[Match]
Distribution={dist1}
[Content]
Packages=testpkg1
"""
)
child2 = Path("mkosi.conf.d/child2.conf")
child2.write_text(
f"""\
[Match]
Distribution={dist2}
[Content]
Packages=testpkg2
"""
)
child3 = Path("mkosi.conf.d/child3.conf")
child3.write_text(
f"""\
[Match]
Distribution=|{dist1}
Distribution=|{dist2}
[Content]
Packages=testpkg3
"""
)
_, _, [conf] = parse_config()
assert "testpkg1" in conf.packages
if dist1 == dist2:
assert "testpkg2" in conf.packages
else:
assert "testpkg2" not in conf.packages
assert "testpkg3" in conf.packages
@pytest.mark.parametrize("release1,release2", itertools.combinations_with_replacement([36, 37], 2))
def test_match_release(tmp_path: Path, release1: int, release2: int) -> None:
with chdir(tmp_path):
parent = Path("mkosi.conf")
parent.write_text(
f"""\
[Distribution]
Distribution=fedora
Release={release1}
"""
)
Path("mkosi.conf.d").mkdir()
child1 = Path("mkosi.conf.d/child1.conf")
child1.write_text(
f"""\
[Match]
Release={release1}
[Content]
Packages=testpkg1
"""
)
child2 = Path("mkosi.conf.d/child2.conf")
child2.write_text(
f"""\
[Match]
Release={release2}
[Content]
Packages=testpkg2
"""
)
child3 = Path("mkosi.conf.d/child3.conf")
child3.write_text(
f"""\
[Match]
Release=|{release1}
Release=|{release2}
[Content]
Packages=testpkg3
"""
)
_, _, [conf] = parse_config()
assert "testpkg1" in conf.packages
if release1 == release2:
assert "testpkg2" in conf.packages
else:
assert "testpkg2" not in conf.packages
assert "testpkg3" in conf.packages
def test_match_build_sources(tmp_path: Path) -> None:
d = tmp_path
(d / "mkosi.conf").write_text(
"""\
[Match]
BuildSources=kernel
BuildSources=/kernel
[Output]
Output=abc
"""
)
with chdir(d):
_, _, [config] = parse_config(["--build-sources", ".:kernel"])
assert config.output == "abc"
def test_match_repositories(tmp_path: Path) -> None:
d = tmp_path
(d / "mkosi.conf").write_text(
"""\
[Match]
Repositories=epel
[Content]
Output=qed
"""
)
with chdir(d):
_, _, [config] = parse_config(["--repositories", "epel,epel-next"])
assert config.output == "qed"
def test_match_architecture(tmp_path: Path) -> None:
d = tmp_path
(d / "mkosi.conf").write_text(
"""\
[Match]
Architecture=uefi
[Content]
Output=qed
"""
)
with chdir(d):
_, _, [config] = parse_config(["--architecture", "arm64"])
assert config.output == "qed"
@pytest.mark.parametrize("image1,image2", itertools.combinations_with_replacement(["image_a", "image_b"], 2))
def test_match_imageid(tmp_path: Path, image1: str, image2: str) -> None:
with chdir(tmp_path):
parent = Path("mkosi.conf")
parent.write_text(
f"""\
[Distribution]
Distribution=fedora
[Output]
ImageId={image1}
"""
)
Path("mkosi.conf.d").mkdir()
child1 = Path("mkosi.conf.d/child1.conf")
child1.write_text(
f"""\
[Match]
ImageId={image1}
[Content]
Packages=testpkg1
"""
)
child2 = Path("mkosi.conf.d/child2.conf")
child2.write_text(
f"""\
[Match]
ImageId={image2}
[Content]
Packages=testpkg2
"""
)
child3 = Path("mkosi.conf.d/child3.conf")
child3.write_text(
f"""\
[Match]
ImageId=|{image1}
ImageId=|{image2}
[Content]
Packages=testpkg3
"""
)
child4 = Path("mkosi.conf.d/child4.conf")
child4.write_text(
"""\
[Match]
ImageId=image*
[Content]
Packages=testpkg4
"""
)
_, _, [conf] = parse_config()
assert "testpkg1" in conf.packages
if image1 == image2:
assert "testpkg2" in conf.packages
else:
assert "testpkg2" not in conf.packages
assert "testpkg3" in conf.packages
assert "testpkg4" in conf.packages
@pytest.mark.parametrize(
"op,version",
itertools.product(
["", "==", "<", ">", "<=", ">="],
[122, 123],
),
)
def test_match_imageversion(tmp_path: Path, op: str, version: str) -> None:
opfunc = {
"==": operator.eq,
"!=": operator.ne,
"<": operator.lt,
"<=": operator.le,
">": operator.gt,
">=": operator.ge,
}.get(op, operator.eq)
with chdir(tmp_path):
parent = Path("mkosi.conf")
parent.write_text(
"""\
[Output]
ImageId=testimage
ImageVersion=123
"""
)
Path("mkosi.conf.d").mkdir()
child1 = Path("mkosi.conf.d/child1.conf")
child1.write_text(
f"""\
[Match]
ImageVersion={op}{version}
[Content]
Packages=testpkg1
"""
)
child2 = Path("mkosi.conf.d/child2.conf")
child2.write_text(
f"""\
[Match]
ImageVersion=<200
ImageVersion={op}{version}
[Content]
Packages=testpkg2
"""
)
child3 = Path("mkosi.conf.d/child3.conf")
child3.write_text(
f"""\
[Match]
ImageVersion=>9000
ImageVersion={op}{version}
[Content]
Packages=testpkg3
"""
)
_, _, [conf] = parse_config()
assert ("testpkg1" in conf.packages) == opfunc(123, version)
assert ("testpkg2" in conf.packages) == opfunc(123, version)
assert "testpkg3" not in conf.packages
def test_match_environment(tmp_path: Path) -> None:
d = tmp_path
(d / "mkosi.conf").write_text(
"""\
[Match]
Environment=MYENV=abc
[Content]
ImageId=matched
"""
)
with chdir(d):
_, _, [conf] = parse_config(["--environment", "MYENV=abc"])
assert conf.image_id == "matched"
_, _, [conf] = parse_config(["--environment", "MYENV=bad"])
assert conf.image_id != "matched"
_, _, [conf] = parse_config(["--environment", "MYEN=abc"])
assert conf.image_id != "matched"
_, _, [conf] = parse_config(["--environment", "MYEN=bad"])
assert conf.image_id != "matched"
(d / "mkosi.conf").write_text(
"""\
[Match]
Environment=MYENV
[Content]
ImageId=matched
"""
)
with chdir(d):
_, _, [conf] = parse_config(["--environment", "MYENV=abc"])
assert conf.image_id == "matched"
_, _, [conf] = parse_config(["--environment", "MYENV=bad"])
assert conf.image_id == "matched"
_, _, [conf] = parse_config(["--environment", "MYEN=abc"])
assert conf.image_id != "matched"
def test_paths_with_default_factory(tmp_path: Path) -> None:
"""
If both paths= and default_factory= are defined, default_factory= should not
be used when at least one of the files/directories from paths= has been found.
"""
with chdir(tmp_path):
Path("mkosi.sandbox.tar").touch()
_, _, [config] = parse_config()
assert config.sandbox_trees == [
ConfigTree(Path.cwd() / "mkosi.sandbox.tar", None),
]
@pytest.mark.parametrize(
"sections,args,warning_count",
[
(["Output"], [], 0),
(["Content"], [], 1),
(["Content", "Output"], [], 1),
(["Output", "Content"], [], 1),
(["Output", "Content", "Distribution"], [], 2),
(["Content"], ["--image-id=testimage"], 1),
],
)
def test_wrong_section_warning(
tmp_path: Path,
caplog: pytest.LogCaptureFixture,
sections: list[str],
args: list[str],
warning_count: int,
) -> None:
with chdir(tmp_path):
# Create a config with ImageId in the wrong section,
# and sometimes in the correct section
Path("mkosi.conf").write_text(
"\n".join(
f"""\
[{section}]
ImageId=testimage
"""
for section in sections
)
)
with caplog.at_level(logging.WARNING):
# Parse the config, with --image-id sometimes given on the command line
parse_config(args)
assert len(caplog.records) == warning_count
def test_config_parse_bytes() -> None:
assert config_parse_bytes(None) is None
assert config_parse_bytes("1") == 4096
assert config_parse_bytes("8000") == 8192
assert config_parse_bytes("8K") == 8192
assert config_parse_bytes("4097") == 8192
assert config_parse_bytes("1M") == 1024**2
assert config_parse_bytes("1.9M") == 1994752
assert config_parse_bytes("1G") == 1024**3
assert config_parse_bytes("7.3G") == 7838318592
with pytest.raises(SystemExit):
config_parse_bytes("-1")
with pytest.raises(SystemExit):
config_parse_bytes("-2K")
with pytest.raises(SystemExit):
config_parse_bytes("-3M")
with pytest.raises(SystemExit):
config_parse_bytes("-4G")
def test_specifiers(tmp_path: Path) -> None:
d = tmp_path
(d / "mkosi.conf").write_text(
"""\
[Distribution]
Distribution=ubuntu
Release=lunar
Architecture=arm64
[Output]
ImageId=my-image-id
ImageVersion=1.2.3
OutputDirectory=abcde
Output=test
[Build]
Environment=Distribution=%d
Release=%r
Architecture=%a
Image=%I
ImageId=%i
ImageVersion=%v
OutputDirectory=%O
Output=%o
ConfigRootDirectory=%D
ConfigRootConfdir=%C
ConfigRootPwd=%P
Filesystem=%F
"""
)
(d / "mkosi.conf.d").mkdir()
(d / "mkosi.conf.d/abc.conf").write_text(
"""\
[Build]
Environment=ConfigAbcDirectory=%D
ConfigAbcConfdir=%C
ConfigAbcPwd=%P
"""
)
(d / "mkosi.conf.d/qed").mkdir()
(d / "mkosi.conf.d/qed/mkosi.conf").write_text(
"""
[Build]
Environment=ConfigQedDirectory=%D
ConfigQedConfdir=%C
ConfigQedPwd=%P
"""
)
(d / "mkosi.images").mkdir()
(d / "mkosi.images/subimage.conf").write_text(
"""
[Build]
Environment=Image=%I
"""
)
with chdir(d):
_, _, [subimage, config] = parse_config()
expected = {
"Distribution": "ubuntu",
"Release": "lunar",
"Architecture": "arm64",
"Image": "main",
"ImageId": "my-image-id",
"ImageVersion": "1.2.3",
"OutputDirectory": os.fspath(Path.cwd() / "abcde"),
"Output": "test",
"ConfigRootDirectory": os.fspath(d),
"ConfigRootConfdir": os.fspath(d),
"ConfigRootPwd": os.fspath(d),
"ConfigAbcDirectory": os.fspath(d),
"ConfigAbcConfdir": os.fspath(d / "mkosi.conf.d"),
"ConfigAbcPwd": os.fspath(d),
"ConfigQedDirectory": os.fspath(d),
"ConfigQedConfdir": os.fspath(d / "mkosi.conf.d/qed"),
"ConfigQedPwd": os.fspath(d / "mkosi.conf.d/qed"),
"Filesystem": "ext4",
}
assert {k: v for k, v in config.environment.items() if k in expected} == expected
assert subimage.environment["Image"] == "subimage"
def test_kernel_specifiers(tmp_path: Path) -> None:
kver = "13.0.8-5.10.0-1057-oem" # taken from reporter of #1638
token = "MySystemImage"
roothash = "67e893261799236dcf20529115ba9fae4fd7c2269e1e658d42269503e5760d38"
def test_expand_kernel_specifiers(text: str) -> str:
return expand_kernel_specifiers(
text,
kver=kver,
token=token,
roothash=roothash,
)
assert test_expand_kernel_specifiers("&&") == "&"
assert test_expand_kernel_specifiers("&k") == kver
assert test_expand_kernel_specifiers("&e") == token
assert test_expand_kernel_specifiers("&h") == roothash
assert test_expand_kernel_specifiers("Image_1.0.3") == "Image_1.0.3"
assert test_expand_kernel_specifiers("Image+&h-&k-&e") == f"Image+{roothash}-{kver}-{token}"
def test_output_id_version(tmp_path: Path) -> None:
d = tmp_path
(d / "mkosi.conf").write_text(
"""
[Output]
ImageId=output
ImageVersion=1.2.3
"""
)
with chdir(d):
_, _, [config] = parse_config()
assert config.output == "output_1.2.3"
def test_deterministic() -> None:
assert Config.default() == Config.default()
def test_environment(tmp_path: Path) -> None:
d = tmp_path
(d / "mkosi.conf").write_text(
"""\
[Config]
PassEnvironment=PassThisEnv
[Build]
Environment=TestValue2=300
TestValue3=400
PassThisEnv=abc
EnvironmentFiles=other.env
"""
)
(d / "mkosi.env").write_text(
"""\
TestValue1=90
TestValue4=99
"""
)
(d / "other.env").write_text(
"""\
TestValue1=100
TestValue2=200
"""
)
(d / "mkosi.images").mkdir()
(d / "mkosi.images/sub.conf").touch()
with chdir(d):
_, _, [sub, config] = parse_config()
expected = {
"TestValue1": "100", # from other.env
"TestValue2": "300", # from mkosi.conf
"TestValue3": "400", # from mkosi.conf
"TestValue4": "99", # from mkosi.env
}
# Only check values for keys from expected, as config.environment contains other items as well
assert {k: config.finalize_environment()[k] for k in expected.keys()} == expected
assert config.environment_files == [Path.cwd() / "mkosi.env", Path.cwd() / "other.env"]
assert sub.environment["PassThisEnv"] == "abc"
assert "TestValue2" not in sub.environment
def test_proxy(tmp_path: Path) -> None:
d = tmp_path
# Verify environment variables are set correctly when GIT_CONFIG_COUNT is not set
(d / "mkosi.conf").write_text(
"""\
[Build]
ProxyUrl=http://proxy:8080
"""
)
with chdir(d):
_, _, [config] = parse_config()
expected = {
"GIT_CONFIG_COUNT": "2",
"GIT_CONFIG_KEY_0": "http.proxy",
"GIT_CONFIG_VALUE_0": "http://proxy:8080",
"GIT_CONFIG_KEY_1": "https.proxy",
"GIT_CONFIG_VALUE_1": "http://proxy:8080",
}
# Only check values for keys from expected, as config.environment contains other items as well
assert {k: config.finalize_environment()[k] for k in expected.keys()} == expected
(d / "mkosi.conf").write_text(
"""\
[Build]
ProxyUrl=http://proxy:8080
Environment=GIT_CONFIG_COUNT=1
GIT_CONFIG_KEY_0=user.name
GIT_CONFIG_VALUE_0=bob
"""
)
with chdir(d):
_, _, [config] = parse_config()
expected = {
"GIT_CONFIG_COUNT": "3",
"GIT_CONFIG_KEY_0": "user.name",
"GIT_CONFIG_VALUE_0": "bob",
"GIT_CONFIG_KEY_1": "http.proxy",
"GIT_CONFIG_VALUE_1": "http://proxy:8080",
"GIT_CONFIG_KEY_2": "https.proxy",
"GIT_CONFIG_VALUE_2": "http://proxy:8080",
}
# Only check values for keys from expected, as config.environment contains other items as well
assert {k: config.finalize_environment()[k] for k in expected.keys()} == expected
def test_mkosi_version_executable(tmp_path: Path) -> None:
d = tmp_path
version = d / "mkosi.version"
version.write_text("#!/bin/sh\necho '1.2.3'\n")
with chdir(d):
with pytest.raises(SystemExit) as error:
_, _, [config] = parse_config()
assert error.type is SystemExit
assert error.value.code != 0
version.chmod(0o755)
with chdir(d):
_, _, [config] = parse_config()
assert config.image_version == "1.2.3"
def test_split_artifacts(tmp_path: Path) -> None:
d = tmp_path
(d / "mkosi.conf").write_text(
"""
[Output]
SplitArtifacts=uki
"""
)
with chdir(d):
_, _, [config] = parse_config()
assert config.split_artifacts == [ArtifactOutput.uki]
(d / "mkosi.conf").write_text(
"""
[Output]
SplitArtifacts=uki
SplitArtifacts=kernel
SplitArtifacts=initrd
"""
)
with chdir(d):
_, _, [config] = parse_config()
assert config.split_artifacts == [
ArtifactOutput.uki,
ArtifactOutput.kernel,
ArtifactOutput.initrd,
]
def test_split_artifacts_compat(tmp_path: Path) -> None:
d = tmp_path
with chdir(d):
_, _, [config] = parse_config()
assert config.split_artifacts == ArtifactOutput.compat_no()
(d / "mkosi.conf").write_text(
"""
[Output]
SplitArtifacts=yes
"""
)
with chdir(d):
_, _, [config] = parse_config()
assert config.split_artifacts == ArtifactOutput.compat_yes()
def test_cli_collection_reset(tmp_path: Path) -> None:
d = tmp_path
(d / "mkosi.conf").write_text(
"""
[Content]
Packages=abc
"""
)
with chdir(d):
_, _, [config] = parse_config(["--package", ""])
assert config.packages == []
_, _, [config] = parse_config(["--package", "", "--package", "foo"])
assert config.packages == ["foo"]
_, _, [config] = parse_config(["--package", "foo", "--package", "", "--package", "bar"])
assert config.packages == ["bar"]
_, _, [config] = parse_config(["--package", "foo", "--package", ""])
assert config.packages == []
def test_tools(tmp_path: Path) -> None:
d = tmp_path
argv = ["--tools-tree=default"]
if in_box():
pytest.skip("Cannot run test_tools() test within mkosi box environment")
with resource_path(mkosi.resources) as resources, chdir(d):
_, tools, _ = parse_config(argv, resources=resources)
assert tools
host = detect_distribution()[0]
if host:
assert tools.distribution == (
host.installer.default_tools_tree_distribution() or tools.distribution
)
(d / "mkosi.tools.conf").write_text(
f"""
[Content]
PackageDirectories={d}
"""
)
_, tools, _ = parse_config(argv, resources=resources)
assert tools
assert tools.package_directories == [Path(d)]
_, tools, _ = parse_config(
argv + ["--tools-tree-distribution=arch", "--tools-tree-package-directory=/tmp"],
resources=resources,
)
assert tools
assert tools.distribution == Distribution.arch
assert tools.package_directories == [Path(d), Path("/tmp")]
_, tools, _ = parse_config(argv + ["--tools-tree-package-directory="], resources=resources)
assert tools
assert tools.package_directories == []
(d / "mkosi.conf").write_text(
"""
[Build]
ToolsTreeDistribution=arch
"""
)
_, tools, _ = parse_config(argv, resources=resources)
assert tools
assert tools.distribution == Distribution.arch
def test_subdir(tmp_path: Path) -> None:
d = tmp_path
with chdir(d):
(d / "mkosi").mkdir()
(d / "mkosi/mkosi.conf").write_text(
"""
[Output]
Output=qed
"""
)
_, _, [config] = parse_config()
assert config.output == "qed"
os.chdir(d)
(d / "mkosi.conf").write_text(
"""
[Output]
Output=abc
"""
)
_, _, [config] = parse_config()
assert config.output == "abc"
def test_assert(tmp_path: Path) -> None:
d = tmp_path
with chdir(d):
(d / "mkosi.conf").write_text(
"""
[Assert]
ImageId=abcde
"""
)
with pytest.raises(SystemExit):
parse_config()
# Does not raise, i.e. parses successfully, but we don't care for the content.
parse_config(["--image-id", "abcde"])
(d / "mkosi.conf").write_text(
"""
[Assert]
ImageId=abcde
[Assert]
Environment=ABC=QED
"""
)
with pytest.raises(SystemExit):
parse_config([])
with pytest.raises(SystemExit):
parse_config(["--image-id", "abcde"])
with pytest.raises(SystemExit):
parse_config(["--environment", "ABC=QED"])
parse_config(["--image-id", "abcde", "--environment", "ABC=QED"])
(d / "mkosi.conf").write_text(
"""
[TriggerAssert]
ImageId=abcde
[TriggerAssert]
Environment=ABC=QED
"""
)
with pytest.raises(SystemExit):
parse_config()
parse_config(["--image-id", "abcde"])
parse_config(["--environment", "ABC=QED"])
(d / "mkosi.conf").write_text(
"""
[Assert]
ImageId=abcde
[TriggerAssert]
Environment=ABC=QED
[TriggerAssert]
Environment=DEF=QEE
"""
)
with pytest.raises(SystemExit):
parse_config()
parse_config(["--image-id", "abcde", "--environment", "ABC=QED"])
parse_config(["--image-id", "abcde", "--environment", "DEF=QEE"])
|