petri_artifact_resolver_openvmm_known_paths/
lib.rs1#![forbid(unsafe_code)]
7
8use anyhow::Context;
9use petri_artifacts_common::artifacts::*;
10use petri_artifacts_core::ArtifactId;
11use petri_artifacts_core::ArtifactSource;
12use petri_artifacts_core::ErasedArtifactHandle;
13use petri_artifacts_vmm_test::artifacts::*;
14use petri_artifacts_vmm_test::env::*;
15use petri_artifacts_vmm_test::vmm_test_image_from_id;
16use std::env::consts::EXE_EXTENSION;
17use std::path::Path;
18use std::path::PathBuf;
19
20pub struct OpenvmmKnownPathsTestArtifactResolver<'a>(&'a str);
24
25impl<'a> OpenvmmKnownPathsTestArtifactResolver<'a> {
26 pub fn new(test_name: &'a str) -> Self {
28 Self(test_name)
29 }
30}
31
32impl petri_artifacts_core::ResolveTestArtifact for OpenvmmKnownPathsTestArtifactResolver<'_> {
33 fn resolve(&self, handle: ErasedArtifactHandle) -> anyhow::Result<PathBuf> {
34 match handle.global_unique_id() {
35 TEST_LOG_DIRECTORY::GLOBAL_UNIQUE_ID => test_log_directory_path(self.0),
36 _ => resolve_artifact(handle),
37 }
38 }
39
40 fn resolve_source(&self, handle: ErasedArtifactHandle) -> anyhow::Result<ArtifactSource> {
41 let local_err = match self.resolve(handle) {
43 Ok(path) => return Ok(ArtifactSource::Local(path)),
44 Err(e) => e,
45 };
46
47 if let Some(url) = vmm_test_image_from_id(handle.global_unique_id()).and_then(|i| i.url()) {
50 return Ok(ArtifactSource::Remote { url });
51 }
52
53 Err(local_err)
55 }
56}
57
58pub fn resolve_artifact(handle: ErasedArtifactHandle) -> anyhow::Result<PathBuf> {
60 if handle.is_image() {
61 return get_vmm_test_image_path(handle.relative_path(), handle.global_unique_id());
62 }
63
64 let test_content_dir_path = test_content_dir_artifact_path(handle.relative_path());
65
66 if test_content_dir_path.is_ok() {
67 return test_content_dir_path;
68 }
69
70 let magic_path = resolve_magic_path_artifact(handle);
71
72 if magic_path.is_ok() {
73 return magic_path;
74 }
75
76 let target = handle.target_triple();
77 let exe_artifact_path = target
78 .as_ref()
79 .context("no associated triple for artifact")
80 .and_then(|t| get_executable_path_artifact(t, None, handle.filename()));
81
82 if exe_artifact_path.is_ok() {
83 return exe_artifact_path;
84 }
85
86 let relative_path = get_executable_path_relative(handle.filename());
87
88 if relative_path.is_ok() {
89 return relative_path;
90 }
91
92 let fallback_target = handle.target_triple().and_then(|mut target| {
94 matches!(
95 target,
96 target_lexicon::Triple {
97 operating_system: target_lexicon::OperatingSystem::Linux,
98 environment: target_lexicon::Environment::Gnu | target_lexicon::Environment::Musl,
99 ..
100 }
101 )
102 .then(|| {
103 if target.environment == target_lexicon::Environment::Gnu {
104 target.environment = target_lexicon::Environment::Musl;
105 } else {
106 target.environment = target_lexicon::Environment::Gnu;
107 }
108 target
109 })
110 });
111
112 let fallback_test_content_dir_path = fallback_target
113 .as_ref()
114 .context("no fallback target")
115 .and_then(|t| {
116 test_content_dir_artifact_path(PathBuf::from(t.to_string()).join(handle.filename()))
117 });
118
119 if fallback_test_content_dir_path.is_ok() {
120 return fallback_test_content_dir_path;
121 }
122
123 let fallback_exe_artifact_path = fallback_target
124 .as_ref()
125 .context("no associated triple for artifact")
126 .and_then(|t| get_executable_path_artifact(t, None, handle.filename()));
127
128 if fallback_exe_artifact_path.is_ok() {
129 return fallback_exe_artifact_path;
130 }
131
132 Err(anyhow::anyhow!(
133 "unable to locate {}:\n\t{}\n\t{}\n\t{}\n\t{}\n\t{}\n\t{}",
134 handle.global_unique_id(),
135 test_content_dir_path.unwrap_err(),
136 magic_path.unwrap_err(),
137 exe_artifact_path.unwrap_err(),
138 relative_path.unwrap_err(),
139 fallback_test_content_dir_path.unwrap_err(),
140 fallback_exe_artifact_path.unwrap_err(),
141 ))
142}
143
144fn test_content_dir_artifact_path(relative_path: impl AsRef<Path>) -> anyhow::Result<PathBuf> {
145 let test_content_dir =
146 std::env::var(VMM_TESTS_CONTENT_DIR).context("test content dir env var not set")?;
147 let path = PathBuf::from(test_content_dir).join(relative_path.as_ref());
148 if !path.exists() {
149 anyhow::bail!("{} not found", path.display())
150 }
151 Ok(path)
152}
153
154fn test_log_directory_path(test_name: &str) -> anyhow::Result<PathBuf> {
156 let root = std::env::var_os(TEST_OUTPUT_PATH)
157 .map_or_else(|| get_repo_root().join("vmm_test_results"), PathBuf::from);
158 let test_name =
159 std::env::var(PETRI_NESTED_TEST_PARENT).unwrap_or_else(|_| test_name.to_string());
160 let path = root.join(test_name.replace("::", "__"));
163 fs_err::create_dir_all(&path)?;
164 Ok(path)
165}
166
167pub fn get_repo_root() -> PathBuf {
169 Path::new(env!("CARGO_MANIFEST_DIR")).join("../..")
170}
171
172pub fn cargo_build_profile() -> &'static str {
179 static PROFILE: std::sync::OnceLock<String> = std::sync::OnceLock::new();
180 PROFILE.get_or_init(|| {
181 if let Ok(exe) = std::env::current_exe() {
182 if exe.components().any(|c| c.as_os_str() == "release") {
183 return "release".to_string();
184 }
185 }
186 "debug".to_string()
187 })
188}
189
190pub fn get_executable_path_relative(name: &str) -> anyhow::Result<PathBuf> {
193 let mut current_exe = std::env::current_exe().context("unable to get current exe")?;
194 if current_exe.parent().and_then(|x| x.file_name()).unwrap() == "deps" {
197 current_exe.pop();
198 }
199
200 let exe_path = current_exe
201 .parent()
202 .context("current exe has no parent")?
203 .join(Path::new(name).with_extension(EXE_EXTENSION));
204 if !exe_path.exists() {
205 anyhow::bail!("{} not found", exe_path.display());
206 }
207 Ok(exe_path)
208}
209
210pub fn get_executable_path_artifact(
214 target: &target_lexicon::Triple,
215 build_profile: Option<&str>,
216 filename: &str,
217) -> anyhow::Result<PathBuf> {
218 let exe_path = get_repo_root()
219 .join("target")
220 .join(target.to_string())
221 .join(build_profile.unwrap_or_else(|| cargo_build_profile()))
222 .join(filename);
223 if !exe_path.exists() {
224 anyhow::bail!("{} not found", exe_path.display());
225 }
226 Ok(exe_path)
227}
228
229fn get_vmm_test_image_path(
230 relative_path: impl AsRef<Path>,
231 name: &str,
232) -> Result<PathBuf, anyhow::Error> {
233 let test_images_dir =
234 std::env::var(VMM_TEST_IMAGES).context("test images dir env var not set")?;
235 let path = PathBuf::from(test_images_dir).join(relative_path.as_ref());
236 if !path.exists() {
237 anyhow::bail!("missing {} at {}", name, path.display())
238 }
239 Ok(path)
240}
241
242fn resolve_magic_path_artifact(handle: ErasedArtifactHandle) -> anyhow::Result<PathBuf> {
245 let package_dir = match handle.global_unique_id() {
246 loadable::LINUX_DIRECT_TEST_KERNEL_X64::GLOBAL_UNIQUE_ID
247 | loadable::LINUX_DIRECT_TEST_BZIMAGE_X64::GLOBAL_UNIQUE_ID
248 | loadable::LINUX_DIRECT_TEST_INITRD_X64::GLOBAL_UNIQUE_ID
249 | petritools::PETRITOOLS_EROFS_X64::GLOBAL_UNIQUE_ID => "underhill-deps-private/x64",
250 loadable::LINUX_DIRECT_TEST_KERNEL_AARCH64::GLOBAL_UNIQUE_ID
251 | loadable::LINUX_DIRECT_TEST_INITRD_AARCH64::GLOBAL_UNIQUE_ID
252 | petritools::PETRITOOLS_EROFS_AARCH64::GLOBAL_UNIQUE_ID => {
253 "underhill-deps-private/aarch64"
254 }
255 loadable::UEFI_FIRMWARE_X64::GLOBAL_UNIQUE_ID => {
256 "hyperv.uefi.mscoreuefi.x64.RELEASE/MsvmX64/RELEASE_VS2022/FV"
257 }
258 loadable::UEFI_FIRMWARE_AARCH64::GLOBAL_UNIQUE_ID => {
259 "hyperv.uefi.mscoreuefi.AARCH64.RELEASE/MsvmAARCH64/RELEASE_CLANGPDB/FV"
260 }
261 loadable::PCAT_FIRMWARE_X64::GLOBAL_UNIQUE_ID => {
262 "Microsoft.Windows.VmFirmware.Pcat.amd64fre/content"
263 }
264 loadable::SVGA_FIRMWARE_X64::GLOBAL_UNIQUE_ID => {
265 "Microsoft.Windows.VmEmulatedDevices.amd64fre/content"
266 }
267 virtio_win::VIRTIO_WINDOWS_DRIVERS::GLOBAL_UNIQUE_ID => "",
268
269 _ => anyhow::bail!("not a magic path artifact"),
270 };
271
272 let magic_path = get_repo_root()
273 .join(".packages")
274 .join(package_dir)
275 .join(handle.filename());
276 if !magic_path.exists() {
277 anyhow::bail!("{} not found", magic_path.display());
278 }
279 Ok(magic_path)
280}