Skip to content

Commit 8945939

Browse files
committed
refactor: Eliminate use of unreachable! from resources.rs tests
The `match`/`unreachable!` pattern results in extremely unhelpful test failures message ("entered unreachable code"). Replace with a pattern that will at least print out _what_ the unexpected result was. Signed-off-by: Patrick Roy <[email protected]>
1 parent 14d13c1 commit 8945939

File tree

1 file changed

+91
-48
lines changed

1 file changed

+91
-48
lines changed

src/vmm/src/resources.rs

Lines changed: 91 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -608,17 +608,25 @@ mod tests {
608608
// these resources, it is considered an invalid json and the test will crash.
609609

610610
// Invalid JSON string must yield a `serde_json` error.
611-
match VmResources::from_json(r#"}"#, &default_instance_info, HTTP_MAX_PAYLOAD_SIZE, None) {
612-
Err(ResourcesError::InvalidJson(_)) => (),
613-
_ => unreachable!(),
614-
}
611+
let error =
612+
VmResources::from_json(r#"}"#, &default_instance_info, HTTP_MAX_PAYLOAD_SIZE, None)
613+
.unwrap_err();
614+
assert!(
615+
matches!(error, ResourcesError::InvalidJson(_)),
616+
"{:?}",
617+
error
618+
);
615619

616620
// Valid JSON string without the configuration for kernel or rootfs
617621
// result in an invalid JSON error.
618-
match VmResources::from_json(r#"{}"#, &default_instance_info, HTTP_MAX_PAYLOAD_SIZE, None) {
619-
Err(ResourcesError::InvalidJson(_)) => (),
620-
_ => unreachable!(),
621-
}
622+
let error =
623+
VmResources::from_json(r#"{}"#, &default_instance_info, HTTP_MAX_PAYLOAD_SIZE, None)
624+
.unwrap_err();
625+
assert!(
626+
matches!(error, ResourcesError::InvalidJson(_)),
627+
"{:?}",
628+
error
629+
);
622630

623631
// Invalid kernel path.
624632
let mut json = format!(
@@ -639,15 +647,21 @@ mod tests {
639647
rootfs_file.as_path().to_str().unwrap()
640648
);
641649

642-
match VmResources::from_json(
650+
let error = VmResources::from_json(
643651
json.as_str(),
644652
&default_instance_info,
645653
HTTP_MAX_PAYLOAD_SIZE,
646654
None,
647-
) {
648-
Err(ResourcesError::BootSource(BootSourceConfigError::InvalidKernelPath(_))) => (),
649-
_ => unreachable!(),
650-
}
655+
)
656+
.unwrap_err();
657+
assert!(
658+
matches!(
659+
error,
660+
ResourcesError::BootSource(BootSourceConfigError::InvalidKernelPath(_))
661+
),
662+
"{:?}",
663+
error
664+
);
651665

652666
// Invalid rootfs path.
653667
json = format!(
@@ -668,18 +682,23 @@ mod tests {
668682
kernel_file.as_path().to_str().unwrap()
669683
);
670684

671-
match VmResources::from_json(
685+
let error = VmResources::from_json(
672686
json.as_str(),
673687
&default_instance_info,
674688
HTTP_MAX_PAYLOAD_SIZE,
675689
None,
676-
) {
677-
Err(ResourcesError::BlockDevice(DriveError::CreateVirtioBlockDevice(
678-
VirtioBlockError::BackingFile(_, _),
679-
))) => (),
680-
_ => unreachable!(),
681-
}
682-
690+
)
691+
.unwrap_err();
692+
assert!(
693+
matches!(
694+
error,
695+
ResourcesError::BlockDevice(DriveError::CreateVirtioBlockDevice(
696+
VirtioBlockError::BackingFile(_, _),
697+
))
698+
),
699+
"{:?}",
700+
error
701+
);
683702
// Valid config for x86 but invalid on aarch64 since it uses cpu_template.
684703
json = format!(
685704
r#"{{
@@ -745,15 +764,21 @@ mod tests {
745764
rootfs_file.as_path().to_str().unwrap()
746765
);
747766

748-
match VmResources::from_json(
767+
let error = VmResources::from_json(
749768
json.as_str(),
750769
&default_instance_info,
751770
HTTP_MAX_PAYLOAD_SIZE,
752771
None,
753-
) {
754-
Err(ResourcesError::VmConfig(VmConfigError::InvalidMemorySize)) => (),
755-
_ => unreachable!(),
756-
}
772+
)
773+
.unwrap_err();
774+
assert!(
775+
matches!(
776+
error,
777+
ResourcesError::VmConfig(VmConfigError::InvalidMemorySize)
778+
),
779+
"{:?}",
780+
error
781+
);
757782

758783
// Invalid path for logger pipe.
759784
json = format!(
@@ -778,15 +803,21 @@ mod tests {
778803
rootfs_file.as_path().to_str().unwrap()
779804
);
780805

781-
match VmResources::from_json(
806+
let error = VmResources::from_json(
782807
json.as_str(),
783808
&default_instance_info,
784809
HTTP_MAX_PAYLOAD_SIZE,
785810
None,
786-
) {
787-
Err(ResourcesError::Logger(crate::logger::LoggerUpdateError(_))) => (),
788-
_ => unreachable!(),
789-
}
811+
)
812+
.unwrap_err();
813+
assert!(
814+
matches!(
815+
error,
816+
ResourcesError::Logger(crate::logger::LoggerUpdateError(_))
817+
),
818+
"{:?}",
819+
error
820+
);
790821

791822
// Invalid path for metrics pipe.
792823
json = format!(
@@ -811,15 +842,21 @@ mod tests {
811842
rootfs_file.as_path().to_str().unwrap()
812843
);
813844

814-
match VmResources::from_json(
845+
let error = VmResources::from_json(
815846
json.as_str(),
816847
&default_instance_info,
817848
HTTP_MAX_PAYLOAD_SIZE,
818849
None,
819-
) {
820-
Err(ResourcesError::Metrics(MetricsConfigError::InitializationFailure { .. })) => (),
821-
_ => unreachable!(),
822-
}
850+
)
851+
.unwrap_err();
852+
assert!(
853+
matches!(
854+
error,
855+
ResourcesError::Metrics(MetricsConfigError::InitializationFailure { .. })
856+
),
857+
"{:?}",
858+
error
859+
);
823860

824861
// Reuse of a host name.
825862
json = format!(
@@ -851,17 +888,24 @@ mod tests {
851888
rootfs_file.as_path().to_str().unwrap()
852889
);
853890

854-
match VmResources::from_json(
891+
let error = VmResources::from_json(
855892
json.as_str(),
856893
&default_instance_info,
857894
HTTP_MAX_PAYLOAD_SIZE,
858895
None,
859-
) {
860-
Err(ResourcesError::NetDevice(NetworkInterfaceError::CreateNetworkDevice(
861-
crate::devices::virtio::net::NetError::TapOpen { .. },
862-
))) => (),
863-
_ => unreachable!(),
864-
}
896+
)
897+
.unwrap_err();
898+
899+
assert!(
900+
matches!(
901+
error,
902+
ResourcesError::NetDevice(NetworkInterfaceError::CreateNetworkDevice(
903+
crate::devices::virtio::net::NetError::TapOpen { .. },
904+
))
905+
),
906+
"{:?}",
907+
error
908+
);
865909

866910
// Let's try now passing a valid configuration. We won't include any logger
867911
// or metrics configuration because these were already initialized in other
@@ -992,15 +1036,14 @@ mod tests {
9921036
rootfs_file.as_path().to_str().unwrap(),
9931037
);
9941038

995-
match VmResources::from_json(
1039+
let error = VmResources::from_json(
9961040
json.as_str(),
9971041
&default_instance_info,
9981042
HTTP_MAX_PAYLOAD_SIZE,
9991043
None,
1000-
) {
1001-
Err(ResourcesError::File(_)) => (),
1002-
_ => unreachable!(),
1003-
}
1044+
)
1045+
.unwrap_err();
1046+
assert!(matches!(error, ResourcesError::File(_)), "{:?}", error);
10041047
}
10051048

10061049
#[test]

0 commit comments

Comments
 (0)