From 0690010524001b606f67c1a65c67f3c27883183f Mon Sep 17 00:00:00 2001 From: George Pisaltu Date: Fri, 9 Jul 2021 15:41:25 +0300 Subject: [PATCH] made config file JSON error more explicit Signed-off-by: George Pisaltu --- src/vmm/src/resources.rs | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/src/vmm/src/resources.rs b/src/vmm/src/resources.rs index 475ea0f4a08..e5b4458e4df 100644 --- a/src/vmm/src/resources.rs +++ b/src/vmm/src/resources.rs @@ -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. @@ -99,7 +99,7 @@ impl VmResources { instance_info: &InstanceInfo, ) -> std::result::Result { let vmm_config: VmmConfig = serde_json::from_slice::(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)?; @@ -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#"{{