Skip to content

Commit

Permalink
made config file JSON error more explicit
Browse files Browse the repository at this point in the history
Signed-off-by: George Pisaltu <gpl@amazon.com>
  • Loading branch information
georgepisaltu authored and acatangiu committed Jul 9, 2021
1 parent 23183e5 commit 0690010
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions src/vmm/src/resources.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ pub enum Error {
/// Boot source configuration error.
BootSource(BootSourceConfigError),
/// JSON is invalid.
InvalidJson,
InvalidJson(serde_json::Error),
/// Logger configuration error.
Logger(LoggerConfigError),
/// Metrics system configuration error.
Expand Down Expand Up @@ -99,7 +99,7 @@ impl VmResources {
instance_info: &InstanceInfo,
) -> std::result::Result<Self, Error> {
let vmm_config: VmmConfig = serde_json::from_slice::<VmmConfig>(config_json.as_bytes())
.map_err(|_| Error::InvalidJson)?;
.map_err(Error::InvalidJson)?;

if let Some(logger) = vmm_config.logger {
init_logger(logger, instance_info).map_err(Error::Logger)?;
Expand Down Expand Up @@ -469,6 +469,19 @@ mod tests {
// in every json because they are mandatory fields. If we don't configure
// these resources, it is considered an invalid json and the test will crash.

// Invalid JSON string must yield a `serde_json` error.
match VmResources::from_json(r#"}"#, &default_instance_info) {
Err(Error::InvalidJson(_)) => (),
_ => unreachable!(),
}

// Valid JSON string without the configuration for kernel or rootfs
// result in an invalid JSON error.
match VmResources::from_json(r#"{}"#, &default_instance_info) {
Err(Error::InvalidJson(_)) => (),
_ => unreachable!(),
}

// Invalid kernel path.
let mut json = format!(
r#"{{
Expand Down

0 comments on commit 0690010

Please sign in to comment.