@@ -608,17 +608,25 @@ mod tests {
608
608
// these resources, it is considered an invalid json and the test will crash.
609
609
610
610
// 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
+ ) ;
615
619
616
620
// Valid JSON string without the configuration for kernel or rootfs
617
621
// 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
+ ) ;
622
630
623
631
// Invalid kernel path.
624
632
let mut json = format ! (
@@ -639,15 +647,21 @@ mod tests {
639
647
rootfs_file. as_path( ) . to_str( ) . unwrap( )
640
648
) ;
641
649
642
- match VmResources :: from_json (
650
+ let error = VmResources :: from_json (
643
651
json. as_str ( ) ,
644
652
& default_instance_info,
645
653
HTTP_MAX_PAYLOAD_SIZE ,
646
654
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
+ ) ;
651
665
652
666
// Invalid rootfs path.
653
667
json = format ! (
@@ -668,18 +682,23 @@ mod tests {
668
682
kernel_file. as_path( ) . to_str( ) . unwrap( )
669
683
) ;
670
684
671
- match VmResources :: from_json (
685
+ let error = VmResources :: from_json (
672
686
json. as_str ( ) ,
673
687
& default_instance_info,
674
688
HTTP_MAX_PAYLOAD_SIZE ,
675
689
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
+ ) ;
683
702
// Valid config for x86 but invalid on aarch64 since it uses cpu_template.
684
703
json = format ! (
685
704
r#"{{
@@ -745,15 +764,21 @@ mod tests {
745
764
rootfs_file. as_path( ) . to_str( ) . unwrap( )
746
765
) ;
747
766
748
- match VmResources :: from_json (
767
+ let error = VmResources :: from_json (
749
768
json. as_str ( ) ,
750
769
& default_instance_info,
751
770
HTTP_MAX_PAYLOAD_SIZE ,
752
771
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
+ ) ;
757
782
758
783
// Invalid path for logger pipe.
759
784
json = format ! (
@@ -778,15 +803,21 @@ mod tests {
778
803
rootfs_file. as_path( ) . to_str( ) . unwrap( )
779
804
) ;
780
805
781
- match VmResources :: from_json (
806
+ let error = VmResources :: from_json (
782
807
json. as_str ( ) ,
783
808
& default_instance_info,
784
809
HTTP_MAX_PAYLOAD_SIZE ,
785
810
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
+ ) ;
790
821
791
822
// Invalid path for metrics pipe.
792
823
json = format ! (
@@ -811,15 +842,21 @@ mod tests {
811
842
rootfs_file. as_path( ) . to_str( ) . unwrap( )
812
843
) ;
813
844
814
- match VmResources :: from_json (
845
+ let error = VmResources :: from_json (
815
846
json. as_str ( ) ,
816
847
& default_instance_info,
817
848
HTTP_MAX_PAYLOAD_SIZE ,
818
849
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
+ ) ;
823
860
824
861
// Reuse of a host name.
825
862
json = format ! (
@@ -851,17 +888,24 @@ mod tests {
851
888
rootfs_file. as_path( ) . to_str( ) . unwrap( )
852
889
) ;
853
890
854
- match VmResources :: from_json (
891
+ let error = VmResources :: from_json (
855
892
json. as_str ( ) ,
856
893
& default_instance_info,
857
894
HTTP_MAX_PAYLOAD_SIZE ,
858
895
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
+ ) ;
865
909
866
910
// Let's try now passing a valid configuration. We won't include any logger
867
911
// or metrics configuration because these were already initialized in other
@@ -992,15 +1036,14 @@ mod tests {
992
1036
rootfs_file. as_path( ) . to_str( ) . unwrap( ) ,
993
1037
) ;
994
1038
995
- match VmResources :: from_json (
1039
+ let error = VmResources :: from_json (
996
1040
json. as_str ( ) ,
997
1041
& default_instance_info,
998
1042
HTTP_MAX_PAYLOAD_SIZE ,
999
1043
None ,
1000
- ) {
1001
- Err ( ResourcesError :: File ( _) ) => ( ) ,
1002
- _ => unreachable ! ( ) ,
1003
- }
1044
+ )
1045
+ . unwrap_err ( ) ;
1046
+ assert ! ( matches!( error, ResourcesError :: File ( _) ) , "{:?}" , error) ;
1004
1047
}
1005
1048
1006
1049
#[ test]
0 commit comments