Skip to content

Commit 9724750

Browse files
authored
Improve error when guest binary not found (#55)
* Improve error when file not found, and avoid unnecessary call to try_exists, and fix unnecessary recreation of matched pattern Signed-off-by: Ludvig Liljenberg <[email protected]>
1 parent 94512d8 commit 9724750

File tree

1 file changed

+31
-13
lines changed

1 file changed

+31
-13
lines changed

src/hyperlight_host/src/sandbox/uninitialized.rs

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -143,14 +143,18 @@ impl UninitializedSandbox {
143143
log_build_details();
144144

145145
// If the guest binary is a file make sure it exists
146-
147146
let guest_binary = match guest_binary {
148147
GuestBinary::FilePath(binary_path) => {
149-
let path = Path::new(&binary_path).canonicalize()?;
150-
path.try_exists()?;
151-
GuestBinary::FilePath(path.to_str().unwrap().to_string())
148+
let path = Path::new(&binary_path)
149+
.canonicalize()
150+
.map_err(|e| new_error!("GuestBinary not found: '{}': {}", binary_path, e))?;
151+
GuestBinary::FilePath(
152+
path.into_os_string()
153+
.into_string()
154+
.map_err(|e| new_error!("Error converting OsString to String: {:?}", e))?,
155+
)
152156
}
153-
GuestBinary::Buffer(buffer) => GuestBinary::Buffer(buffer),
157+
buffer @ GuestBinary::Buffer(_) => buffer,
154158
};
155159

156160
let run_opts = sandbox_run_options.unwrap_or_default();
@@ -921,11 +925,7 @@ mod tests {
921925
event_values.get("metadata").unwrap().as_object().unwrap();
922926
let event_values_map = event_values.as_object().unwrap();
923927

924-
#[cfg(target_os = "windows")]
925-
let expected_error =
926-
"IOError(Os { code: 2, kind: NotFound, message: \"The system cannot find the file specified.\" }";
927-
#[cfg(not(target_os = "windows"))]
928-
let expected_error = "IOError(Os { code: 2, kind: NotFound, message: \"No such file or directory\" }";
928+
let expected_error_start = "Error(\"GuestBinary not found:";
929929

930930
let err_vals_res = try_to_strings([
931931
(metadata_values_map, "level"),
@@ -935,7 +935,7 @@ mod tests {
935935
]);
936936
if let Ok(err_vals) = err_vals_res {
937937
if err_vals[0] == "ERROR"
938-
&& err_vals[1].starts_with(expected_error)
938+
&& err_vals[1].starts_with(expected_error_start)
939939
&& err_vals[2] == "hyperlight_host::sandbox::uninitialized"
940940
&& err_vals[3] == "hyperlight_host::sandbox::uninitialized"
941941
{
@@ -1013,7 +1013,9 @@ mod tests {
10131013

10141014
let logcall = TEST_LOGGER.get_log_call(16).unwrap();
10151015
assert_eq!(Level::Error, logcall.level);
1016-
assert!(logcall.args.starts_with("error=IOError(Os { code"));
1016+
assert!(logcall
1017+
.args
1018+
.starts_with("error=Error(\"GuestBinary not found:"));
10171019
assert_eq!("hyperlight_host::sandbox::uninitialized", logcall.target);
10181020

10191021
// Log record 18
@@ -1065,7 +1067,9 @@ mod tests {
10651067

10661068
let logcall = TEST_LOGGER.get_log_call(1).unwrap();
10671069
assert_eq!(Level::Error, logcall.level);
1068-
assert!(logcall.args.starts_with("error=IOError"));
1070+
assert!(logcall
1071+
.args
1072+
.starts_with("error=Error(\"GuestBinary not found:"));
10691073
assert_eq!("hyperlight_host::sandbox::uninitialized", logcall.target);
10701074
}
10711075
{
@@ -1088,4 +1092,18 @@ mod tests {
10881092
assert_eq!(0, num_calls);
10891093
}
10901094
}
1095+
1096+
#[test]
1097+
fn test_invalid_path() {
1098+
let invalid_path = "some/path/that/does/not/exist";
1099+
let sbox = UninitializedSandbox::new(
1100+
GuestBinary::FilePath(invalid_path.to_string()),
1101+
None,
1102+
None,
1103+
None,
1104+
);
1105+
assert!(
1106+
matches!(sbox, Err(e) if e.to_string().contains("GuestBinary not found: 'some/path/that/does/not/exist': No such file or directory (os error 2)"))
1107+
);
1108+
}
10911109
}

0 commit comments

Comments
 (0)