1extern crate openvmm_hypervisors as _;
13
14pub mod disk_image;
15pub mod initrd;
16pub mod linux_bootstrap;
17mod linux_direct_serial_agent;
18pub mod mshv_boot;
19pub mod mshv_image;
20#[expect(missing_docs)]
23pub mod openhcl_diag;
24pub mod requirements;
25mod test;
26mod tracing;
27mod vm;
28mod worker;
29
30pub use petri_artifacts_core::ArtifactHandle;
31pub use petri_artifacts_core::ArtifactResolver;
32pub use petri_artifacts_core::ArtifactSource;
33pub use petri_artifacts_core::AsArtifactHandle;
34pub use petri_artifacts_core::ErasedArtifactHandle;
35pub use petri_artifacts_core::RemoteAccess;
36pub use petri_artifacts_core::ResolveTestArtifact;
37pub use petri_artifacts_core::ResolvedArtifact;
38pub use petri_artifacts_core::ResolvedArtifactSource;
39pub use petri_artifacts_core::ResolvedOptionalArtifact;
40pub use petri_artifacts_core::TestArtifactRequirements;
41pub use petri_artifacts_core::TestArtifacts;
42pub use pipette_client as pipette;
43pub use test::PetriTestParams;
44pub use test::RunTest;
45pub use test::SimpleTest;
46pub use test::TestCase;
47pub use test::test_macro_support;
48pub use test::test_main;
49pub use tracing::*;
50pub use vm::*;
51
52use jiff::Timestamp;
53use pal_async::process::PolledChild;
54use std::ops::Deref;
55use std::ops::DerefMut;
56use std::process::Command;
57use std::process::Stdio;
58use thiserror::Error;
59
60pub const SIZE_1_KB: u64 = 1024;
62pub const SIZE_1_MB: u64 = 1024 * SIZE_1_KB;
64pub const SIZE_1_GB: u64 = 1024 * SIZE_1_MB;
66
67#[expect(missing_docs)] pub enum ShutdownKind {
70 Shutdown,
71 Reboot,
72 Hibernate,
73}
74
75#[derive(Error, Debug)]
77pub enum CommandError {
78 #[error("failed to launch command")]
80 Launch(#[from] std::io::Error),
81 #[error("command exited with non-zero status ({0}): {1}")]
83 Command(std::process::ExitStatus, String),
84}
85
86pub async fn run_host_cmd(mut cmd: Command) -> Result<String, CommandError> {
88 cmd.stderr(Stdio::piped()).stdin(Stdio::null());
89
90 let cmd_debug = format!("{cmd:?}");
91 ::tracing::debug!(cmd = cmd_debug, "executing command");
92
93 let start = Timestamp::now();
94 let output = blocking::unblock(move || cmd.output()).await?;
95 let time_elapsed = Timestamp::now() - start;
96
97 let stdout_str = String::from_utf8_lossy(&output.stdout).to_string();
98 let stderr_str = String::from_utf8_lossy(&output.stderr).to_string();
99 ::tracing::debug!(
100 cmd = cmd_debug,
101 stdout_str,
102 stderr_str,
103 "command exited in {:.3}s with status {}",
104 time_elapsed.total(jiff::Unit::Second).unwrap(),
105 output.status
106 );
107
108 if !output.status.success() {
109 return Err(CommandError::Command(output.status, stderr_str));
110 }
111
112 Ok(stdout_str.trim().to_owned())
113}
114
115pub struct TestChild(PolledChild<std::process::Child>);
121
122impl TestChild {
123 pub fn new(child: PolledChild<std::process::Child>) -> Self {
125 Self(child)
126 }
127}
128
129impl Deref for TestChild {
130 type Target = PolledChild<std::process::Child>;
131
132 fn deref(&self) -> &Self::Target {
133 &self.0
134 }
135}
136
137impl DerefMut for TestChild {
138 fn deref_mut(&mut self) -> &mut Self::Target {
139 &mut self.0
140 }
141}
142
143impl Drop for TestChild {
144 fn drop(&mut self) {
145 let child = self.0.get_mut();
146 if let Ok(Some(_)) = child.try_wait() {
149 return;
150 }
151 ::tracing::warn!(
152 "killing test child process which was still running at the end of the test"
153 );
154 if let Err(err) = child.kill() {
155 ::tracing::warn!(
156 error = &err as &dyn std::error::Error,
157 "failed to kill test child process"
158 );
159 return;
160 }
161 let _ = child.wait();
164 }
165}