petri/
lib.rs

1// Copyright (c) Microsoft Corporation.
2// Licensed under the MIT License.
3
4//! A Rust-based testing framework for VMMs.
5//!
6//! At this time - `petri` supports testing OpenVMM, OpenHCL,
7//! and Hyper-V based VMs.
8
9// TODO: Remove this dependency by adding a frontend worker that handles
10// hypervisor auto-detection in the spawned openvmm process instead of
11// requiring probes to be registered in the petri process.
12extern crate openvmm_hypervisors as _;
13
14mod cpio;
15pub mod disk_image;
16mod linux_direct_serial_agent;
17// TODO: Add docs and maybe a trait interface for this, or maybe this can
18// remain crate-local somehow without violating interface privacy.
19#[expect(missing_docs)]
20pub mod openhcl_diag;
21pub mod requirements;
22mod test;
23mod tracing;
24mod vm;
25mod worker;
26
27pub use petri_artifacts_core::ArtifactHandle;
28pub use petri_artifacts_core::ArtifactResolver;
29pub use petri_artifacts_core::AsArtifactHandle;
30pub use petri_artifacts_core::ErasedArtifactHandle;
31pub use petri_artifacts_core::ResolveTestArtifact;
32pub use petri_artifacts_core::ResolvedArtifact;
33pub use petri_artifacts_core::ResolvedOptionalArtifact;
34pub use petri_artifacts_core::TestArtifactRequirements;
35pub use petri_artifacts_core::TestArtifacts;
36pub use pipette_client as pipette;
37pub use test::PetriTestParams;
38pub use test::RunTest;
39pub use test::SimpleTest;
40pub use test::TestCase;
41pub use test::test_macro_support;
42pub use test::test_main;
43pub use tracing::*;
44pub use vm::*;
45
46use jiff::Timestamp;
47use std::process::Command;
48use std::process::Stdio;
49use thiserror::Error;
50
51/// 1 kibibyte's worth of bytes.
52pub const SIZE_1_KB: u64 = 1024;
53/// 1 mebibyte's worth of bytes.
54pub const SIZE_1_MB: u64 = 1024 * SIZE_1_KB;
55/// 1 gibibyte's worth of bytes.
56pub const SIZE_1_GB: u64 = 1024 * SIZE_1_MB;
57
58/// The kind of shutdown to perform.
59#[expect(missing_docs)] // Self-describing names.
60pub enum ShutdownKind {
61    Shutdown,
62    Reboot,
63    // TODO: Add hibernate?
64}
65
66/// Error running command
67#[derive(Error, Debug)]
68pub enum CommandError {
69    /// failed to launch command
70    #[error("failed to launch command")]
71    Launch(#[from] std::io::Error),
72    /// command exited with non-zero status
73    #[error("command exited with non-zero status ({0}): {1}")]
74    Command(std::process::ExitStatus, String),
75}
76
77/// Run a command on the host and return the output
78pub async fn run_host_cmd(mut cmd: Command) -> Result<String, CommandError> {
79    cmd.stderr(Stdio::piped()).stdin(Stdio::null());
80
81    let cmd_debug = format!("{cmd:?}");
82    ::tracing::debug!(cmd = cmd_debug, "executing command");
83
84    let start = Timestamp::now();
85    let output = blocking::unblock(move || cmd.output()).await?;
86    let time_elapsed = Timestamp::now() - start;
87
88    let stdout_str = String::from_utf8_lossy(&output.stdout).to_string();
89    let stderr_str = String::from_utf8_lossy(&output.stderr).to_string();
90    ::tracing::debug!(
91        cmd = cmd_debug,
92        stdout_str,
93        stderr_str,
94        "command exited in {:.3}s with status {}",
95        time_elapsed.total(jiff::Unit::Second).unwrap(),
96        output.status
97    );
98
99    if !output.status.success() {
100        return Err(CommandError::Command(output.status, stderr_str));
101    }
102
103    Ok(stdout_str.trim().to_owned())
104}