Repository navigation
Expand file tree
/
Copy pathci.py
More file actions
1812 lines (1660 loc) · 69.6 KB
/
Copy pathci.py
File metadata and controls
1812 lines (1660 loc) · 69.6 KB
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
"""Helpers used by nvx continuous-integration jobs."""
from __future__ import annotations
import hashlib
import json
import math
import os
import re
import shlex
import shutil
import stat
import sys
import zipfile
from collections.abc import Mapping, Sequence
from dataclasses import dataclass
from pathlib import Path, PurePosixPath
from typing import cast
from xml.etree import ElementTree
from .build_constants import (
AlpineBuildConstants,
BuildConstants,
GuestRustBuildConstants,
KernelBuildConstants,
OpenVMMBuildConstants,
ZstdBuildConstants,
)
from .common import (
ScriptError,
artifact_path,
bytes_to_mib,
download,
github_job,
is_link_or_junction,
openvmm_git_state,
path_exists,
require_file,
require_success,
require_tool,
run_capture,
run_checked,
sha256_file,
strict_json_object,
)
OPENVMM_TEST_BACKENDS = ("kvm", "mshv", "whp")
# The operating system that builds and runs each backend's VMM tests.
OPENVMM_TEST_OPERATING_SYSTEMS = {"kvm": "linux", "mshv": "linux", "whp": "windows"}
# `build-openvmm-tests` describes the test content that it builds in this file,
# which `test-openvmm --content` reads. Format 2 added the target, the build
# options, and the SHA-256 of each file.
OPENVMM_TEST_CONTENT_MANIFEST = "nvx-openvmm-tests.json"
OPENVMM_TEST_CONTENT_FORMAT = 2
# The vmm-tests-run target of each operating system's content. NVX supports
# only x86-64 hosts, and the WHP tests run only windows-x64 content, so the
# build names its target rather than taking the host's.
OPENVMM_TEST_TARGETS = {"linux": "linux-x64", "windows": "windows-x64"}
# The vmm-tests-run options that build the content: a release build without
# optional features, whose run script runs the tests with the nextest CI
# profile and without the interactive VHD prompt.
OPENVMM_TEST_BUILD_OPTIONS = (
"--release",
"--ci-profile",
"--skip-vhd-prompt",
"--build-only",
)
# CI caches the test content of each operating system across runs under a key
# of everything that determines it (doc/ci.md#openvmm-vmm-test-cache): this
# version, the target, the OpenVMM revision, and the SHA-256 of every field of
# the content's manifest that describes what built it. Increment the version
# when what a build makes, or what a reuse of the content checks, changes in a
# way that none of those fields captures, such as the environment in which the
# tests build.
OPENVMM_TEST_CACHE_VERSION = 1
# The script that `vmm-tests-run --build-only` writes to run the content.
OPENVMM_TEST_RUN_SCRIPTS = {"linux": "run.sh", "windows": "run.ps1"}
# The nextest archive of the tests, in the content's directory of its target.
OPENVMM_TEST_ARCHIVE_NAME = "vmm_tests.tar.zst"
# Petri writes the results of a content run into this directory of the
# content, which also holds the run's test duration report.
OPENVMM_TEST_RESULTS = "test_results"
# A content run takes the remapped workspace from the content, so nextest
# writes the JUnit report that OpenVMM's nextest configuration gives the ci
# profile, which the content's runs use, under the content's target directory.
OPENVMM_TEST_JUNIT = PurePosixPath("target", "nextest", "ci", "junit.xml")
# `test-openvmm --content` records each test's duration in this report in its
# results, from which `plan-openvmm-test-shards` plans the shards
# (doc/usage.md#test-duration-report).
OPENVMM_TEST_DURATIONS_NAME = "openvmm-test-durations.json"
OPENVMM_TEST_DURATIONS_SCHEMA = "nvx-openvmm-test-durations"
# Changes only when a reader of the previous version would misread a report;
# added fields keep it.
OPENVMM_TEST_DURATIONS_SCHEMA_VERSION = 1
# The images that the pinned vmm-tests-run-target downloads before it runs the
# tests, by the name that a run script passes with --artifacts, with the file
# and the size that petri_artifacts_vmm_test declares for each. They come from
# OpenVMM's public test image container, from which Petri streams every other
# image on demand.
OPENVMM_TEST_IMAGE_URL = "https://hvlitetestvhds.blob.core.windows.net/vhds"
OPENVMM_TEST_IMAGES = {"VmgsWithBootEntry": ("sample-vmgs.vhd", 4194816)}
# The vmm-tests-run-target options that the pinned run scripts pass, and
# whether each takes a value. The consumer passes its own --dir and --filter.
OPENVMM_TEST_RUN_TARGET_OPTIONS = {
"--target": True,
"--repetitions": True,
"--artifacts": True,
"--prep-steps": True,
"--skip-vhd-prompt": False,
"--ci-profile": False,
"--no-reuse-prepped-vhds": False,
"--needs-hardware-isolation": False,
"--needs-igvm-agent": False,
}
REQUIRED_CI_RESULT_ENVIRONMENTS = {
"quality": "QUALITY_RESULT",
"aci-edge-sandboxes": "ACI_EDGE_SANDBOXES_RESULT",
"nvx-rs": "NVX_RS_RESULT",
"nvx-cli-linux": "NVX_CLI_LINUX_RESULT",
"nvx-cli-windows": "NVX_CLI_WINDOWS_RESULT",
"openvmm-changes": "CHANGES_RESULT",
"artifacts": "ARTIFACTS_RESULT",
"distro-artifacts": "DISTRO_ARTIFACTS_RESULT",
"debug-kernel": "DEBUG_KERNEL_RESULT",
"guest-artifact-reuse": "GUEST_ARTIFACT_REUSE_RESULT",
"build-openvmm-linux-gnu": "BUILD_LINUX_GNU_RESULT",
"build-openvmm-linux-musl": "BUILD_LINUX_MUSL_RESULT",
"build-openvmm-windows-msvc": "BUILD_WINDOWS_MSVC_RESULT",
"build-openvmm-vmm-tests-linux": "BUILD_VMM_TESTS_LINUX_RESULT",
"build-openvmm-vmm-tests-windows": "BUILD_VMM_TESTS_WINDOWS_RESULT",
"openvmm-vmm-tests-linux": "VMM_TESTS_LINUX_RESULT",
"openvmm-vmm-tests-windows": "VMM_TESTS_WINDOWS_RESULT",
"openvmm-unit-tests": "UNIT_TESTS_RESULT",
"nvx-microvm-tests-kvm": "MICROVM_KVM_RESULT",
"nvx-microvm-tests-mshv": "MICROVM_MSHV_RESULT",
"nvx-microvm-tests-whp": "MICROVM_WHP_RESULT",
"nvx-microvm-guests-kvm": "MICROVM_GUESTS_KVM_RESULT",
"nvx-microvm-guests-mshv": "MICROVM_GUESTS_MSHV_RESULT",
"nvx-microvm-guests-whp": "MICROVM_GUESTS_WHP_RESULT",
"nvx-microvm-debug-kvm": "MICROVM_DEBUG_KVM_RESULT",
"nvx-microvm-debug-mshv": "MICROVM_DEBUG_MSHV_RESULT",
"nvx-microvm-debug-whp": "MICROVM_DEBUG_WHP_RESULT",
"platform-kvm": "PLATFORM_KVM_RESULT",
"platform-mshv": "PLATFORM_MSHV_RESULT",
"platform-whp": "PLATFORM_WHP_RESULT",
"performance-gate": "PERFORMANCE_GATE_RESULT",
}
REQUIRED_CI_BUILD_JOBS = (
"build-openvmm-linux-gnu",
"build-openvmm-linux-musl",
"build-openvmm-windows-msvc",
)
# GitHub-hosted, once per OS; the microVM and platform jobs of each OS wait for
# its result.
REQUIRED_CI_CLI_JOBS = ("nvx-cli-linux", "nvx-cli-windows")
REQUIRED_CI_OPENVMM_TEST_JOBS = ("openvmm-unit-tests",)
# OpenVMM VMM tests boot the NVX guest artifacts, so they need the job that
# builds them.
REQUIRED_CI_OPENVMM_ARTIFACT_TEST_JOBS = (
"openvmm-vmm-tests-linux",
"openvmm-vmm-tests-windows",
)
# Each builds, without the guest artifacts, the VMM tests that the test jobs of
# its OS run, so it runs whenever a change selects the VMM tests.
REQUIRED_CI_OPENVMM_TEST_BUILD_JOBS = (
"build-openvmm-vmm-tests-linux",
"build-openvmm-vmm-tests-windows",
)
# Each backend runs the default suite in one job and, beside it, the guest
# smoke and sandbox steps that follow the suite in another.
REQUIRED_CI_MICROVM_TEST_JOBS = (
"nvx-microvm-tests-kvm",
"nvx-microvm-tests-mshv",
"nvx-microvm-tests-whp",
"nvx-microvm-guests-kvm",
"nvx-microvm-guests-mshv",
"nvx-microvm-guests-whp",
)
# The CI debug kernel runs on KVM for every pull request and on every backend
# for dev pushes and full runs.
REQUIRED_CI_MICROVM_DEBUG_JOBS = ("nvx-microvm-debug-kvm",)
REQUIRED_CI_MICROVM_DEBUG_PUSH_JOBS = (
"nvx-microvm-debug-mshv",
"nvx-microvm-debug-whp",
)
REQUIRED_CI_ARTIFACT_JOB = "artifacts"
# GitHub-hosted, beside the kernel and Alpine artifacts; only the microVM guest
# and sandbox jobs and the platform jobs, which read the Ubuntu and Azure Linux
# images, wait for it.
REQUIRED_CI_DISTRO_ARTIFACT_JOB = "distro-artifacts"
# GitHub-hosted, beside the shared artifacts; only the debug jobs wait for it.
REQUIRED_CI_DEBUG_KERNEL_JOB = "debug-kernel"
# GitHub-hosted, after both guest artifact jobs of a same-repository pull
# request, whose artifacts it checks as the dev push that merges it would
# before it reuses them; only the required status check waits for it.
REQUIRED_CI_GUEST_ARTIFACT_REUSE_JOB = "guest-artifact-reuse"
REQUIRED_CI_PLATFORM_JOBS = (
"platform-kvm",
"platform-mshv",
"platform-whp",
)
OPENVMM_UNIT_TEST_EXCLUDED_PACKAGES = (
"vmm_tests",
"cca_tests",
"guest_test_uefi",
"inspect_derive",
"mesh_derive",
"save_restore_derive",
"test_with_tracing_macro",
"pal_async_test",
"vmm_test_macros",
"flowey_core",
)
# The workflow's change detector emits one output per entry, and runs the full
# matrix when it cannot classify a change.
CI_CHANGE_OUTPUTS = (
"run-guest-artifacts",
"run-openvmm-binaries",
"run-openvmm-unit-tests",
"run-openvmm-vmm-tests",
"run-microvm-tests",
"run-benchmarks",
)
CI_DOCUMENTATION_PATHS = re.compile(r"^(doc/|.*\.md$)", re.IGNORECASE)
# Inputs of the NVX microVM tests: the OpenVMM pin, the guests and every input
# of their cache keys, the nvx-rs executable that their scenarios run, the
# crates and the runtime adapter that they test, the workflows, actions, and
# CLI modules that their jobs and the jobs that build their artifacts run, the
# runners, and the attributes that check out their scripts on Windows. Every
# module of the CLI is an input but those that none of these jobs runs.
MICROVM_TEST_INPUT_PATHS = re.compile(
r"^(openvmm($|/)|rust-toolchain\.toml$|scripts/setup/|\.gitattributes$|"
r"nvx-rs/|aci_edge_sandboxes/|tests/runtime-adapter/|"
r"kernel/|guest/|ubuntu/|azurelinux/|"
r"docker/|\.dockerignore$|SOURCE-MANIFEST\.json$|"
r"\.github/workflows/(ci|build-openvmm-binary|run-nvx-microvm-tests)\.yml$|"
r"\.github/actions/(build-guest-artifacts|build-openvmm|checkout-openvmm|"
r"sccache|setup-curl|validate-runner)/|"
r"scripts/nvx\.py$|"
r"scripts/nvx_tools/(?!(adversarial|adversarial_broker|adversarial_executor|"
r"agent_bundling_tests|create_linux_source_archive|nightly_ci|performance)\.py$|"
r"adversarial_cases/))"
)
# Inputs of the platform jobs, which benchmark each backend and package its
# release, and of the regression gate that reads their results: the OpenVMM
# pin, the guests and every input of their cache keys, the nvx-rs executable
# that the packages ship, the performance history and expected hosts, the
# release version and license, the workflows, actions, and CLI modules that
# these jobs and the jobs that build their artifacts run, the runners, and the
# attributes that check out their inputs on Windows. Every module of the CLI is
# an input but those that none of these jobs runs.
BENCHMARK_INPUT_PATHS = re.compile(
r"^(openvmm($|/)|rust-toolchain\.toml$|scripts/setup/|\.gitattributes$|"
r"nvx-rs/|kernel/|guest/|ubuntu/|azurelinux/|"
r"docker/|\.dockerignore$|SOURCE-MANIFEST\.json$|"
r"data/|VERSION$|LICENSE$|"
r"\.github/workflows/(ci|build-openvmm-binary|run-platform)\.yml$|"
r"\.github/actions/(build-guest-artifacts|build-openvmm|checkout-openvmm|"
r"gate-performance|package-release|prepare-performance-results|"
r"run-benchmark|sccache|setup-curl|validate-runner)/|"
r"scripts/nvx\.py$|"
r"scripts/nvx_tools/(?!(aci_edge_sandboxes_tests|adversarial|"
r"adversarial_broker|adversarial_executor|agent_bundling_tests|"
r"create_linux_source_archive|managed_exec_tests|microvm_durations|"
r"microvm_shards|microvm_tests|nightly_ci|openvmm_process|"
r"openvmm_test_shards|sandbox_lifecycle_tests|sandbox_proxy_tests|"
r"shard_plans)\.py$|"
r"(microvm|openvmm_test)_shards\.json$|adversarial_cases/|"
r"microvm_test_scripts/))"
)
# Inputs of the OpenVMM unit and documentation tests: the pinned revision, the
# Rust release, the runner setup, and the code and workflow that run them.
OPENVMM_UNIT_TEST_INPUT_PATHS = re.compile(
r"^(openvmm($|/)|rust-toolchain\.toml$|scripts/setup/|"
r"\.github/workflows/ci\.yml$|"
r"\.github/actions/(checkout-openvmm|sccache|setup-curl|validate-runner)/|"
r"scripts/nvx\.py$|scripts/nvx_tools/(__init__|build_constants|ci|common)\.py$)"
)
# The OpenVMM VMM tests also boot the NVX kernel and Alpine initramfs, so they
# add the inputs of those artifacts' cache keys and of the job that builds them,
# and the modules and the partition that split the WHP tests into shards.
# Of nvx-rs, the Alpine key covers only the files that the guest binaries' build
# reads, through their source identity, so a change to its host side or tests
# leaves the VMM tests out.
OPENVMM_VMM_TEST_INPUT_PATHS = re.compile(
r"^(openvmm($|/)|rust-toolchain\.toml$|scripts/setup/|"
r"\.github/workflows/ci\.yml$|"
r"\.github/actions/(build-guest-artifacts|checkout-openvmm|sccache|"
r"setup-curl|validate-runner)/|"
r"scripts/nvx\.py$|"
r"scripts/nvx_tools/"
r"(__init__|build|build_config|build_constants|ci|common|elf|"
r"guest_binaries|guests|openvmm_test_shards|shard_plans)\.py$|"
r"scripts/nvx_tools/openvmm_test_shards\.json$|"
r"SOURCE-MANIFEST\.json$|docker/Dockerfile$|\.dockerignore$|kernel/|guest/(alpine|common)/|"
rf"(?:{GuestRustBuildConstants.SOURCE_PATTERN.pattern})$)"
)
@dataclass(frozen=True)
class CiChanges:
"""The suites that a change runs, as the change detector reports them."""
run_guest_artifacts: bool
run_openvmm_binaries: bool
run_openvmm_unit_tests: bool
run_openvmm_vmm_tests: bool
run_microvm_tests: bool
run_benchmarks: bool
def outputs(self) -> dict[str, bool]:
return dict(
zip(
CI_CHANGE_OUTPUTS,
(
self.run_guest_artifacts,
self.run_openvmm_binaries,
self.run_openvmm_unit_tests,
self.run_openvmm_vmm_tests,
self.run_microvm_tests,
self.run_benchmarks,
),
strict=True,
)
)
def kernel_patch_identity(patch_root: Path) -> str:
"""Return a digest of each patch's relative path and content, in path order.
A content-only hash ignores names and order, so renaming a patch reused a
kernel whose provenance still named the old path. Both are part of the key.
The file set matches the top-level ``*.patch`` files that kernel provenance
records, so unrelated files do not change the key and a missing or empty
directory fails instead of producing the empty digest.
"""
files = sorted(
(path.relative_to(patch_root).as_posix(), path)
for path in patch_root.glob("*.patch")
if path.is_file()
)
if not files:
raise ScriptError("no kernel patches were found")
digest = hashlib.sha256()
for relative, path in files:
digest.update(relative.encode("utf-8") + b"\0")
digest.update(hashlib.sha256(path.read_bytes()).digest())
return digest.hexdigest()[:16]
def _ci_inputs(changed_files: Sequence[str]) -> list[str]:
"""Return the changed files that can select a suite: every one but the
documentation, which no suite builds, boots, or runs."""
return [
path
for path in changed_files
if path and not CI_DOCUMENTATION_PATHS.match(path)
]
def classify_ci_changes(event_name: str, changed_files: Sequence[str]) -> CiChanges:
"""Select the suites that a change between two commits runs.
Documentation never selects a suite. Pull requests and pushes run each test
suite only when one of its own inputs changed. A pull request runs the
platform jobs only when one of their inputs changed, but a push runs them
for every change but documentation, so that every such `dev` commit
publishes its development release and records its performance results. The
guest artifacts and the OpenVMM binaries are built exactly when a job that
uses them runs.
"""
if event_name not in ("pull_request", "push"):
raise ValueError(f"unsupported CI event {event_name!r}")
files = [path for path in changed_files if path]
inputs = _ci_inputs(files)
def changed(pattern: re.Pattern[str]) -> bool:
return any(pattern.match(path) for path in inputs)
run_openvmm_vmm_tests = changed(OPENVMM_VMM_TEST_INPUT_PATHS)
run_microvm_tests = changed(MICROVM_TEST_INPUT_PATHS)
# An empty diff runs the platform jobs, as it ran every workload before
# the classifier selected them separately.
run_benchmarks = (
not files
or changed(BENCHMARK_INPUT_PATHS)
or (event_name == "push" and bool(inputs))
)
return CiChanges(
run_guest_artifacts=run_openvmm_vmm_tests
or run_microvm_tests
or run_benchmarks,
run_openvmm_binaries=run_microvm_tests or run_benchmarks,
run_openvmm_unit_tests=changed(OPENVMM_UNIT_TEST_INPUT_PATHS),
run_openvmm_vmm_tests=run_openvmm_vmm_tests,
run_microvm_tests=run_microvm_tests,
run_benchmarks=run_benchmarks,
)
def describe_ci_changes(
event_name: str,
changed_files: Sequence[str],
changes: CiChanges,
) -> str:
"""Return the Markdown job summary that explains each decision."""
files = [path for path in changed_files if path]
inputs = _ci_inputs(files)
def evidence(pattern: re.Pattern[str]) -> str:
matches = [path for path in inputs if pattern.match(path)]
shown = ", ".join(f"`{path}`" for path in matches[:3])
if len(matches) > 3:
shown += f", and {len(matches) - 3} more"
return shown
def section(title: str, selected: bool, reason: str) -> str:
return f"### {title}: {'run' if selected else 'skip'}\n{reason}\n"
def inputs_reason(selected: bool, pattern: re.Pattern[str]) -> str:
if selected and any(pattern.match(path) for path in inputs):
return f"Inputs changed: {evidence(pattern)}."
if not files:
return "No file changed."
if not inputs:
return "Only documentation files changed."
return "No input changed."
def users_reason(users: dict[str, bool]) -> str:
names = [name for name, runs in users.items() if runs]
if not names:
return "No job that uses them runs."
if len(names) > 2:
names = [", ".join(names[:-1]) + ",", names[-1]]
return f"Jobs that use them run: {' and '.join(names)}."
if changes.run_benchmarks and not files:
benchmarks_reason = "No file changed, so the platform jobs run."
elif (
changes.run_benchmarks
and event_name == "push"
and not any(BENCHMARK_INPUT_PATHS.match(path) for path in inputs)
):
benchmarks_reason = (
"A push runs the platform jobs for every change but documentation, "
"so that it publishes its development release and records its "
"performance results."
)
else:
benchmarks_reason = inputs_reason(changes.run_benchmarks, BENCHMARK_INPUT_PATHS)
return "".join(
(
section(
"Guest artifacts",
changes.run_guest_artifacts,
users_reason(
{
"the OpenVMM VMM tests": changes.run_openvmm_vmm_tests,
"the NVX microVM tests": changes.run_microvm_tests,
"the platform benchmarks": changes.run_benchmarks,
},
),
),
section(
"OpenVMM binaries",
changes.run_openvmm_binaries,
users_reason(
{
"the NVX microVM tests": changes.run_microvm_tests,
"the platform benchmarks": changes.run_benchmarks,
},
),
),
section(
"OpenVMM unit tests",
changes.run_openvmm_unit_tests,
inputs_reason(
changes.run_openvmm_unit_tests, OPENVMM_UNIT_TEST_INPUT_PATHS
),
),
section(
"OpenVMM VMM tests",
changes.run_openvmm_vmm_tests,
inputs_reason(
changes.run_openvmm_vmm_tests, OPENVMM_VMM_TEST_INPUT_PATHS
),
),
section(
"NVX microVM tests",
changes.run_microvm_tests,
inputs_reason(changes.run_microvm_tests, MICROVM_TEST_INPUT_PATHS),
),
section("Platform benchmarks", changes.run_benchmarks, benchmarks_reason),
)
)
def required_ci_expected_results(
event_name: str,
*,
same_repository: bool,
changes: CiChanges,
) -> dict[str, str]:
if event_name not in (
"pull_request",
"push",
"schedule",
"workflow_dispatch",
):
raise ValueError(f"unsupported CI event {event_name!r}")
# Each job runs only when its suite is selected and the jobs that build the
# artifacts that it uses are selected too, as its condition in ci.yml says.
run_guest_artifacts = changes.run_guest_artifacts
run_openvmm_binaries = changes.run_openvmm_binaries
run_microvm_tests = (
changes.run_microvm_tests and run_guest_artifacts and run_openvmm_binaries
)
run_benchmarks = (
changes.run_benchmarks and run_guest_artifacts and run_openvmm_binaries
)
repository_jobs_enabled = event_name != "pull_request" or same_repository
expected = {
"quality": "success",
"aci-edge-sandboxes": "success",
"nvx-rs": "success",
"openvmm-changes": "success",
REQUIRED_CI_ARTIFACT_JOB: "success" if run_guest_artifacts else "skipped",
REQUIRED_CI_DISTRO_ARTIFACT_JOB: (
"success" if run_guest_artifacts else "skipped"
),
REQUIRED_CI_DEBUG_KERNEL_JOB: "success" if run_guest_artifacts else "skipped",
REQUIRED_CI_GUEST_ARTIFACT_REUSE_JOB: (
"success"
if event_name == "pull_request" and same_repository and run_guest_artifacts
else "skipped"
),
"performance-gate": (
"success"
if event_name == "pull_request"
and repository_jobs_enabled
and run_benchmarks
else "skipped"
),
}
expected.update(
{
job: "success" if repository_jobs_enabled else "skipped"
for job in REQUIRED_CI_CLI_JOBS
}
)
expected.update(
{
job: (
"success"
if repository_jobs_enabled and run_openvmm_binaries
else "skipped"
)
for job in REQUIRED_CI_BUILD_JOBS
}
)
expected.update(
{
job: (
"success"
if repository_jobs_enabled and changes.run_openvmm_unit_tests
else "skipped"
)
for job in REQUIRED_CI_OPENVMM_TEST_JOBS
}
)
expected.update(
{
job: (
"success"
if repository_jobs_enabled and changes.run_openvmm_vmm_tests
else "skipped"
)
for job in REQUIRED_CI_OPENVMM_TEST_BUILD_JOBS
}
)
expected.update(
{
job: (
"success"
if repository_jobs_enabled
and changes.run_openvmm_vmm_tests
and run_guest_artifacts
else "skipped"
)
for job in REQUIRED_CI_OPENVMM_ARTIFACT_TEST_JOBS
}
)
expected.update(
{
job: (
"success"
if repository_jobs_enabled and run_microvm_tests
else "skipped"
)
for job in (
*REQUIRED_CI_MICROVM_TEST_JOBS,
*REQUIRED_CI_MICROVM_DEBUG_JOBS,
)
}
)
expected.update(
{
job: (
"success"
if event_name != "pull_request" and run_microvm_tests
else "skipped"
)
for job in REQUIRED_CI_MICROVM_DEBUG_PUSH_JOBS
}
)
expected.update(
{
job: (
"success" if repository_jobs_enabled and run_benchmarks else "skipped"
)
for job in REQUIRED_CI_PLATFORM_JOBS
}
)
return expected
def required_ci_failures(
event_name: str,
*,
same_repository: bool,
changes: CiChanges,
results: Mapping[str, str],
) -> list[str]:
expected = required_ci_expected_results(
event_name,
same_repository=same_repository,
changes=changes,
)
failures: list[str] = []
for job, expected_result in expected.items():
actual_result = results.get(job) or "missing"
if actual_result != expected_result:
failures.append(f"{job}: expected {expected_result}, got {actual_result}")
return failures
def _exact_openvmm_test(test: str) -> str:
return f"test(/^{re.escape(test)}$/)"
OPENVMM_REQUIRED_MICROVM_TESTS = (
"ttrpc::test_ttrpc_microvm_linux_direct_lifecycle_and_snapshot",
"x86_64::microvm::openvmm_linux_x64_phase_1_lifecycle",
)
OPENVMM_LINUX_TEST_FILTER = "test(openvmm) | test(ttrpc) | " + " | ".join(
_exact_openvmm_test(test) for test in OPENVMM_REQUIRED_MICROVM_TESTS
)
# A nested test runs inside an L1 VM host whose test function, vm_host or a
# variant, appears in its name. The Linux runners support neither nested
# virtualization nor the aarch64-unknown-linux-musl archive of aarch64 hosts.
OPENVMM_NESTED_TEST_EXCLUSION = " & !test(vm_host)"
def _exclude_openvmm_tests(
test_filter: str,
excluded_tests: tuple[str, ...],
) -> str:
exclusions = "".join(f" & !{_exact_openvmm_test(test)}" for test in excluded_tests)
return f"({test_filter}){exclusions}"
# KVM runs Linux guests; the prepped Windows test also stages a 30 GiB VHD.
OPENVMM_KVM_EXCLUDED_TESTS = (
"multiarch::openvmm_pcat_x64_freebsd_13_2_x64_boot_no_agent",
"multiarch::openvmm_pcat_x64_freebsd_13_2_x64_iso_boot_no_agent",
"multiarch::openvmm_pcat_x64_ubuntu_2404_server_x64_boot",
"multiarch::openvmm_pcat_x64_ubuntu_2504_server_x64_boot",
"multiarch::openvmm_pcat_x64_ubuntu_2504_server_x64_boot_heavy",
)
OPENVMM_KVM_TEST_FILTER = _exclude_openvmm_tests(
f"({OPENVMM_LINUX_TEST_FILTER})"
f"{OPENVMM_NESTED_TEST_EXCLUSION}"
" & !test(no_vmbus_prepped_boot_no_vmbus_windows)"
" & !test(windows_datacenter_core_2022_x64)"
" & !test(virtio_net_windows)",
OPENVMM_KVM_EXCLUDED_TESTS,
)
# MSHV runs Linux guests; the remaining exact exclusions fail on the runner.
OPENVMM_MSHV_EXCLUDED_TESTS = (
"multiarch::openvmm_pcat_x64_freebsd_13_2_x64_boot_no_agent",
"multiarch::openvmm_pcat_x64_freebsd_13_2_x64_iso_boot_no_agent",
"multiarch::openvmm_pcat_x64_ubuntu_2404_server_x64_boot",
"multiarch::openvmm_pcat_x64_ubuntu_2504_server_x64_boot",
"multiarch::openvmm_pcat_x64_ubuntu_2504_server_x64_boot_heavy",
"multiarch::pcie::openvmm_linux_x64_pcie_save_restore",
)
OPENVMM_MSHV_TEST_FILTER = _exclude_openvmm_tests(
f"({OPENVMM_LINUX_TEST_FILTER}){OPENVMM_NESTED_TEST_EXCLUSION}"
" & !test(windows_datacenter_core_2022_x64)",
OPENVMM_MSHV_EXCLUDED_TESTS,
)
# ttrpc::test_ttrpc_interface stays on the Linux backends: it boots Linux
# pipette, which flowey can build only on a Linux host. No test needs Hyper-V,
# which flowey reads from "hyperv" in a test's name, because test-openvmm runs
# the WHP tests that a build hands over without vmm-tests-run-target, which
# checks for Hyper-V on Windows.
OPENVMM_WHP_TESTS = (
"multiarch::hibernate::openvmm_uefi_x64_guest_test_x64_hibernate_halts",
"multiarch::ic::openvmm_uefi_x64_windows_datacenter_core_2022_x64_kvp_ic",
"multiarch::ic::openvmm_uefi_x64_windows_datacenter_core_2022_x64_timesync_ic",
"multiarch::openvmm_pcat_x64_freebsd_13_2_x64_boot_no_agent",
"multiarch::openvmm_pcat_x64_freebsd_13_2_x64_iso_boot_no_agent",
"multiarch::openvmm_pcat_x64_windows_datacenter_core_2022_x64_boot",
"multiarch::openvmm_pcat_x64_windows_datacenter_core_2022_x64_boot_heavy",
"multiarch::openvmm_uefi_x64_frontpage",
"multiarch::openvmm_uefi_x64_guest_test_x64_guest_test_uefi",
"multiarch::openvmm_uefi_x64_ubuntu_2504_server_x64_secure_boot_mismatched_template",
"multiarch::openvmm_uefi_x64_windows_datacenter_core_2022_x64_boot",
"multiarch::openvmm_uefi_x64_windows_datacenter_core_2022_x64_boot_heavy",
"multiarch::openvmm_uefi_x64_windows_datacenter_core_2022_x64_boot_nvme",
"multiarch::openvmm_uefi_x64_windows_datacenter_core_2022_x64_secure_boot",
"multiarch::openvmm_uefi_x64_windows_datacenter_core_2022_x64_secure_boot_mismatched_template",
"multiarch::pcie::openvmm_uefi_x64_windows_datacenter_core_2022_x64_pcie_hotplug",
"multiarch::pcie::openvmm_uefi_x64_windows_datacenter_core_2022_x64_pcie_nvme_boot",
"multiarch::pcie::openvmm_uefi_x64_windows_datacenter_core_2022_x64_pcie_root_emulation_multi_segment",
"multiarch::pcie::openvmm_uefi_x64_windows_datacenter_core_2022_x64_pcie_root_emulation_single_segment",
"multiarch::pcie::openvmm_uefi_x64_windows_datacenter_core_2022_x64_pcie_switches",
"multiarch::vmgs::openvmm_uefi_x64_ubuntu_2504_server_x64_invalid_boot_entries",
"multiarch::vmgs::openvmm_uefi_x64_windows_datacenter_core_2022_x64_clear_vmgs",
"multiarch::vmgs::openvmm_uefi_x64_windows_datacenter_core_2022_x64_default_boot",
"multiarch::vmgs::openvmm_uefi_x64_windows_datacenter_core_2022_x64_invalid_boot_entries",
*OPENVMM_REQUIRED_MICROVM_TESTS,
"ttrpc::test_ttrpc_uefi_boot",
"x86_64::openvmm_uefi_x64_guest_test_x64_crash_dump_on_triple_fault",
"x86_64::openvmm_uefi_x64_windows_datacenter_core_2022_x64_battery_capacity",
)
OPENVMM_WHP_EXCLUDED_TESTS: tuple[str, ...] = ()
def _join_openvmm_tests(
tests: tuple[str, ...],
excluded_tests: tuple[str, ...] = (),
) -> str:
test_filter = " | ".join(_exact_openvmm_test(test) for test in tests)
exclusions = "".join(f" & !{_exact_openvmm_test(test)}" for test in excluded_tests)
return f"({test_filter}){exclusions}"
def _select_openvmm_tests(
tests: tuple[str, ...],
excluded_tests: tuple[str, ...],
) -> tuple[str, ...]:
return tuple(test for test in tests if test not in excluded_tests)
OPENVMM_TEST_FILTERS = {
"kvm": OPENVMM_KVM_TEST_FILTER,
"mshv": OPENVMM_MSHV_TEST_FILTER,
"whp": _join_openvmm_tests(OPENVMM_WHP_TESTS, OPENVMM_WHP_EXCLUDED_TESTS),
}
# The tests that a backend's filter selects by their exact names, which is
# what a partition of its tests into shards lists. The KVM and MSHV filters
# select tests by pattern, so only the WHP tests have shards
# (doc/ci.md#openvmm-vmm-test-shards).
OPENVMM_TEST_SELECTIONS = {
"whp": _select_openvmm_tests(OPENVMM_WHP_TESTS, OPENVMM_WHP_EXCLUDED_TESTS),
}
@dataclass(frozen=True)
class OpenVMMTestShard:
"""Shard INDEX of the COUNT shards of a backend's VMM tests, with the
tests that it runs."""
index: int
count: int
tests: tuple[str, ...]
def openvmm_test_filter(backend: str, shard: OpenVMMTestShard | None = None) -> str:
"""Return the filter of the backend's tests, or of those of its tests that
a shard runs: only tests that the backend's filter selects, whatever the
shard lists."""
test_filter = OPENVMM_TEST_FILTERS[backend]
if shard is None:
return test_filter
if not shard.tests:
raise ScriptError(
f"shard {shard.index} of {shard.count} of the {backend.upper()} "
"OpenVMM tests runs no test"
)
return f"({test_filter}) & {_join_openvmm_tests(shard.tests)}"
def validate_openvmm_test_backend(backend: str) -> None:
if backend not in OPENVMM_TEST_BACKENDS:
choices = ", ".join(OPENVMM_TEST_BACKENDS)
raise ScriptError(
f"unsupported OpenVMM test backend {backend!r}; choose {choices}"
)
if backend == "whp":
if os.name != "nt":
raise ScriptError("WHP OpenVMM tests require Windows")
else:
if os.name == "nt":
raise ScriptError(f"{backend.upper()} OpenVMM tests require Linux")
device = Path("/dev") / backend
if not os.access(device, os.R_OK | os.W_OK):
raise ScriptError(f"OpenVMM tests require read/write access to {device}")
if backend == "kvm" and Path("/dev/mshv").exists():
raise ScriptError(
"/dev/mshv is present, so OpenVMM would select MSHV instead of KVM"
)
# MSVC's link.exe stamps every DLL and executable with the time of the link
# and a random PDB signature unless /Brepro derives both from the image's
# contents. Each Windows build therefore relinked every proc macro with new
# bytes, and sccache, which hashes the files that a compilation names with
# --extern, missed every crate that uses a proc macro directly (#693). The
# linker appends _LINK_ to each of its command lines; RUSTFLAGS would miss the
# proc macros of the builds that flowey runs with --target.
MSVC_REPRODUCIBLE_LINK_OPTION = "/Brepro"
def _link_reproducibly_with_msvc(environment: dict[str, str]) -> None:
options = environment.get("_LINK_", "")
if all(option.lstrip("/-").casefold() != "brepro" for option in options.split()):
environment["_LINK_"] = f"{options} {MSVC_REPRODUCIBLE_LINK_OPTION}".strip()
def _prepare_openvmm_test_environment(
backends: Sequence[str],
rustup: str,
) -> dict[str, str]:
toolchain = OpenVMMBuildConstants.RUST_TOOLCHAIN
targets = list(
dict.fromkeys(
target
for backend in backends
for target in OpenVMMBuildConstants.TEST_RUST_TARGETS[backend]
)
)
installed = run_capture(
[
rustup,
"target",
"list",
"--installed",
"--toolchain",
toolchain,
]
)
# rustup rejects the query when the pinned toolchain is not installed.
toolchain_installed = installed.returncode == 0
installed_targets: set[str] = set()
if toolchain_installed:
installed_targets.update(installed.stdout.decode("utf-8").splitlines())
environment = os.environ.copy()
environment["RUSTUP_TOOLCHAIN"] = toolchain
if os.name == "nt":
_link_reproducibly_with_msvc(environment)
runner_temp_value = os.environ.get("RUNNER_TEMP")
if not installed_targets.issuperset(targets):
if runner_temp_value:
environment["RUSTUP_HOME"] = os.fspath(
Path(runner_temp_value) / OpenVMMBuildConstants.RUSTUP_DIRECTORY_NAME
)
if runner_temp_value or not toolchain_installed:
run_checked(
[
rustup,
"toolchain",
"install",
toolchain,
"--profile",
"minimal",
],
env=environment,
)
run_checked(
[
rustup,
"target",
"add",
*targets,
"--toolchain",
toolchain,
],
env=environment,
)
_set_openvmm_test_cache(environment, backends)
return environment
def _set_openvmm_test_cache(
environment: dict[str, str],
backends: Sequence[str],
) -> None:
# Petri streams and layers guest disks in the XDG cache, and the Linux
# runner account has no home directory to hold it.
runner_temp_value = os.environ.get("RUNNER_TEMP")
if "whp" not in backends and runner_temp_value:
environment["XDG_CACHE_HOME"] = os.fspath(
Path(runner_temp_value) / OpenVMMBuildConstants.CACHE_DIRECTORY_NAME
)
def run_openvmm_unit_tests() -> None:
require_file(
OpenVMMBuildConstants.DIRECTORY / "Cargo.toml", "initialized OpenVMM submodule"
)
cargo = require_tool("cargo")
environment = os.environ.copy()
if os.name == "nt":
_link_reproducibly_with_msvc(environment)
fuzz_crates = run_capture(
[cargo, "xtask", "fuzz", "list", "--crates"],
cwd=OpenVMMBuildConstants.DIRECTORY,
env=environment,
)
require_success(fuzz_crates, "OpenVMM fuzz crate query")
run_checked(
[cargo, "xflowey", "restore-packages", "--no-compat-igvm"],
cwd=OpenVMMBuildConstants.DIRECTORY,
env=environment,
)
command = [
cargo,
"nextest",
"run",
"--profile",
"agent",
"--workspace",
"--tests",
"--bins",
"--features",
"ci",
]
excluded_packages = (
*OPENVMM_UNIT_TEST_EXCLUDED_PACKAGES,
*fuzz_crates.stdout.decode("utf-8").splitlines(),
)
for package in excluded_packages:
command.extend(("--exclude", package))
run_checked(command, cwd=OpenVMMBuildConstants.DIRECTORY, env=environment)
run_checked(
[
cargo,
"test",
"--locked",
"--doc",
"--workspace",
"--no-fail-fast",
],
cwd=OpenVMMBuildConstants.DIRECTORY,
env=environment,
)
def _openvmm_microvm_test_guest() -> tuple[Path, Path]:
kernel = require_file(
artifact_path(KernelBuildConstants.BINARY_NAME),
"microVM Linux direct kernel",
)
initrd = require_file(
artifact_path(AlpineBuildConstants.INITRAMFS_NAME),
"microVM Alpine initramfs",
)
return kernel, initrd
def run_openvmm_tests(
backend: str,
content: Path | None = None,
*,
shard: OpenVMMTestShard | None = None,
) -> None:
"""Run the backend's VMM tests, or those of a shard of them, building them
unless ``content`` names a test content directory that
``build_openvmm_tests`` built."""
validate_openvmm_test_backend(backend)
test_filter = openvmm_test_filter(backend, shard)
if content is not None:
_run_openvmm_test_content(backend, content, shard, test_filter)
return
require_file(
OpenVMMBuildConstants.DIRECTORY / "Cargo.toml", "initialized OpenVMM submodule"
)