api_server: add unit tests to boot_source.rs
coverage: 92.9%
@@ -7,17 +7,17 @@ use http_service::{empty_response, json_fault_message, json_response};
use request::{ParsedRequest, SyncRequest};
use request::sync::GenerateResponse;
-#[derive(Debug, Deserialize, Serialize, PartialEq)]
+#[derive(Debug, Deserialize, PartialEq, Serialize)]
pub enum BootSourceType {
LocalImage,
}
-#[derive(Debug, Deserialize, Serialize, PartialEq)]
+#[derive(Debug, Deserialize, PartialEq, Serialize)]
pub struct LocalImage {
pub kernel_image_path: String,
}
-#[derive(Debug, Deserialize, Serialize, PartialEq)]
+#[derive(Debug, Deserialize, PartialEq, Serialize)]
pub struct BootSourceBody {
boot_source_id: String,
source_type: BootSourceType,
@@ -77,3 +77,45 @@ impl BootSourceBody {
))
}
}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn test_generate_response_put_boot_source_config_error() {
+ assert_eq!(PutBootSourceConfigError::InvalidKernelPath.generate_response().status(),
+ StatusCode::BadRequest);
+ assert_eq!(PutBootSourceConfigError::InvalidKernelCommandLine.generate_response().status(),
+ StatusCode::BadRequest);
+ }
+
+ #[test]
+ fn test_generate_response_put_boot_source_outcome() {
+ assert_eq!(PutBootSourceOutcome::Created.generate_response().status(),
+ StatusCode::Created);
+ assert_eq!(PutBootSourceOutcome::Updated.generate_response().status(),
+ StatusCode::NoContent);
+ assert_eq!(PutBootSourceOutcome::Error(PutBootSourceConfigError::InvalidKernelPath)
+ .generate_response().status(), StatusCode::BadRequest);
+ }
+
+ #[test]
+ fn test_into_parsed_request() {
+ let body = BootSourceBody {
+ boot_source_id: String::from("/foo/bar"),
+ source_type: BootSourceType::LocalImage,
+ local_image: Some(LocalImage { kernel_image_path: String::from("/foo/bar") }),
+ boot_args: Some(String::from("foobar"))
+ };
+ let same_body = BootSourceBody {
+ boot_source_id: String::from("/foo/bar"),
+ source_type: BootSourceType::LocalImage,
+ local_image: Some(LocalImage { kernel_image_path: String::from("/foo/bar") }),
+ boot_args: Some(String::from("foobar"))
+ };
+ let (sender, receiver) = oneshot::channel();
+ assert!(body.into_parsed_request().eq(&Ok(ParsedRequest::Sync(
+ SyncRequest::PutBootSource(same_body, sender), receiver))))
+ }
+}
