@@ -504,13 +504,15 @@ pub mod test_util {
504
504
use std:: io:: Write ;
505
505
use std:: path:: { Path , PathBuf } ;
506
506
507
- use vmm_sys_util:: rand ;
507
+ use vmm_sys_util:: tempdir :: TempDir ;
508
508
509
509
#[ derive( Debug ) ]
510
510
pub struct MockCgroupFs {
511
511
mounts_file : File ,
512
- pub proc_mounts_path : String ,
513
- pub sys_cgroups_path : String ,
512
+ // kept to clean up on Drop
513
+ _mock_jailer_dir : TempDir ,
514
+ pub proc_mounts_path : PathBuf ,
515
+ pub sys_cgroups_path : PathBuf ,
514
516
}
515
517
516
518
// Helper object that simulates the layout of the cgroup file system
@@ -533,17 +535,12 @@ pub mod test_util {
533
535
}
534
536
535
537
pub fn new ( ) -> std:: result:: Result < MockCgroupFs , std:: io:: Error > {
536
- let mock_jailer_dir = format ! (
537
- "/tmp/firecracker/test/{}/jailer" ,
538
- rand:: rand_alphanumerics( 4 ) . into_string( ) . unwrap( )
539
- ) ;
540
- let mock_proc_mounts = format ! ( "{}/{}" , mock_jailer_dir, "proc/mounts" , ) ;
541
- let mock_sys_cgroups = format ! ( "{}/{}" , mock_jailer_dir, "sys_cgroup" , ) ;
542
-
543
- let mock_proc_dir = Path :: new ( & mock_proc_mounts) . parent ( ) . unwrap ( ) ;
538
+ let mock_jailer_dir = TempDir :: new ( ) . unwrap ( ) ;
539
+ let mock_proc_mounts = mock_jailer_dir. as_path ( ) . join ( "proc/mounts" ) ;
540
+ let mock_sys_cgroups = mock_jailer_dir. as_path ( ) . join ( "sys_cgroup" ) ;
544
541
545
542
// create a mock /proc/mounts file in a temporary directory
546
- fs:: create_dir_all ( mock_proc_dir ) ?;
543
+ fs:: create_dir_all ( mock_proc_mounts . parent ( ) . unwrap ( ) ) ?;
547
544
let file = OpenOptions :: new ( )
548
545
. read ( true )
549
546
. write ( true )
@@ -552,6 +549,7 @@ pub mod test_util {
552
549
. open ( mock_proc_mounts. clone ( ) ) ?;
553
550
Ok ( MockCgroupFs {
554
551
mounts_file : file,
552
+ _mock_jailer_dir : mock_jailer_dir,
555
553
proc_mounts_path : mock_proc_mounts,
556
554
sys_cgroups_path : mock_sys_cgroups,
557
555
} )
@@ -563,9 +561,9 @@ pub mod test_util {
563
561
writeln ! (
564
562
self . mounts_file,
565
563
"cgroupv2 {}/unified cgroup2 rw,nosuid,nodev,noexec,relatime,nsdelegate 0 0" ,
566
- self . sys_cgroups_path,
564
+ self . sys_cgroups_path. to_str ( ) . unwrap ( ) ,
567
565
) ?;
568
- let cg_unified_path = PathBuf :: from ( format ! ( "{}/ unified", self . sys_cgroups_path ) ) ;
566
+ let cg_unified_path = self . sys_cgroups_path . join ( " unified") ;
569
567
fs:: create_dir_all ( & cg_unified_path) ?;
570
568
Self :: create_file_with_contents (
571
569
cg_unified_path. join ( "cgroup.controllers" ) ,
@@ -589,26 +587,14 @@ pub mod test_util {
589
587
writeln ! (
590
588
self . mounts_file,
591
589
"cgroup {}/{} cgroup rw,nosuid,nodev,noexec,relatime,{} 0 0" ,
592
- self . sys_cgroups_path, c, c,
590
+ self . sys_cgroups_path. to_str( ) . unwrap( ) ,
591
+ c,
592
+ c,
593
593
) ?;
594
594
}
595
595
Ok ( ( ) )
596
596
}
597
597
}
598
-
599
- // Cleanup created files when object goes out of scope
600
- impl Drop for MockCgroupFs {
601
- fn drop ( & mut self ) {
602
- let tmp_dir = Path :: new ( self . proc_mounts_path . as_str ( ) )
603
- . parent ( )
604
- . unwrap ( )
605
- . parent ( )
606
- . unwrap ( )
607
- . parent ( )
608
- . unwrap ( ) ;
609
- let _ = fs:: remove_dir_all ( tmp_dir) ;
610
- }
611
- }
612
598
}
613
599
614
600
#[ cfg( test) ]
@@ -639,53 +625,60 @@ mod tests {
639
625
#[ test]
640
626
fn test_cgroup_conf_builder_invalid_version ( ) {
641
627
let mock_cgroups = MockCgroupFs :: new ( ) . unwrap ( ) ;
642
- let builder = CgroupConfigurationBuilder :: new ( 0 , mock_cgroups. proc_mounts_path . as_str ( ) ) ;
628
+ let builder =
629
+ CgroupConfigurationBuilder :: new ( 0 , mock_cgroups. proc_mounts_path . to_str ( ) . unwrap ( ) ) ;
643
630
builder. unwrap_err ( ) ;
644
631
}
645
632
646
633
#[ test]
647
634
fn test_cgroup_conf_builder_no_mounts ( ) {
648
635
let mock_cgroups = MockCgroupFs :: new ( ) . unwrap ( ) ;
649
- let builder = CgroupConfigurationBuilder :: new ( 1 , mock_cgroups. proc_mounts_path . as_str ( ) ) ;
636
+ let builder =
637
+ CgroupConfigurationBuilder :: new ( 1 , mock_cgroups. proc_mounts_path . to_str ( ) . unwrap ( ) ) ;
650
638
builder. unwrap_err ( ) ;
651
639
}
652
640
653
641
#[ test]
654
642
fn test_cgroup_conf_builder_v1 ( ) {
655
643
let mut mock_cgroups = MockCgroupFs :: new ( ) . unwrap ( ) ;
656
644
mock_cgroups. add_v1_mounts ( ) . unwrap ( ) ;
657
- let builder = CgroupConfigurationBuilder :: new ( 1 , mock_cgroups. proc_mounts_path . as_str ( ) ) ;
645
+ let builder =
646
+ CgroupConfigurationBuilder :: new ( 1 , mock_cgroups. proc_mounts_path . to_str ( ) . unwrap ( ) ) ;
658
647
builder. unwrap ( ) ;
659
648
}
660
649
661
650
#[ test]
662
651
fn test_cgroup_conf_builder_v2 ( ) {
663
652
let mut mock_cgroups = MockCgroupFs :: new ( ) . unwrap ( ) ;
664
653
mock_cgroups. add_v2_mounts ( ) . unwrap ( ) ;
665
- let builder = CgroupConfigurationBuilder :: new ( 2 , mock_cgroups. proc_mounts_path . as_str ( ) ) ;
654
+ let builder =
655
+ CgroupConfigurationBuilder :: new ( 2 , mock_cgroups. proc_mounts_path . to_str ( ) . unwrap ( ) ) ;
666
656
builder. unwrap ( ) ;
667
657
}
668
658
669
659
#[ test]
670
660
fn test_cgroup_conf_builder_v2_with_v1_mounts ( ) {
671
661
let mut mock_cgroups = MockCgroupFs :: new ( ) . unwrap ( ) ;
672
662
mock_cgroups. add_v1_mounts ( ) . unwrap ( ) ;
673
- let builder = CgroupConfigurationBuilder :: new ( 2 , mock_cgroups. proc_mounts_path . as_str ( ) ) ;
663
+ let builder =
664
+ CgroupConfigurationBuilder :: new ( 2 , mock_cgroups. proc_mounts_path . to_str ( ) . unwrap ( ) ) ;
674
665
builder. unwrap_err ( ) ;
675
666
}
676
667
677
668
#[ test]
678
669
fn test_cgroup_conf_builder_v2_no_mounts ( ) {
679
670
let mock_cgroups = MockCgroupFs :: new ( ) . unwrap ( ) ;
680
- let builder = CgroupConfigurationBuilder :: new ( 2 , mock_cgroups. proc_mounts_path . as_str ( ) ) ;
671
+ let builder =
672
+ CgroupConfigurationBuilder :: new ( 2 , mock_cgroups. proc_mounts_path . to_str ( ) . unwrap ( ) ) ;
681
673
builder. unwrap_err ( ) ;
682
674
}
683
675
684
676
#[ test]
685
677
fn test_cgroup_conf_builder_v1_with_v2_mounts ( ) {
686
678
let mut mock_cgroups = MockCgroupFs :: new ( ) . unwrap ( ) ;
687
679
mock_cgroups. add_v2_mounts ( ) . unwrap ( ) ;
688
- let builder = CgroupConfigurationBuilder :: new ( 1 , mock_cgroups. proc_mounts_path . as_str ( ) ) ;
680
+ let builder =
681
+ CgroupConfigurationBuilder :: new ( 1 , mock_cgroups. proc_mounts_path . to_str ( ) . unwrap ( ) ) ;
689
682
builder. unwrap_err ( ) ;
690
683
}
691
684
@@ -696,9 +689,11 @@ mod tests {
696
689
mock_cgroups. add_v2_mounts ( ) . unwrap ( ) ;
697
690
698
691
for v in & [ 1 , 2 ] {
699
- let mut builder =
700
- CgroupConfigurationBuilder :: new ( * v, mock_cgroups. proc_mounts_path . as_str ( ) )
701
- . unwrap ( ) ;
692
+ let mut builder = CgroupConfigurationBuilder :: new (
693
+ * v,
694
+ mock_cgroups. proc_mounts_path . to_str ( ) . unwrap ( ) ,
695
+ )
696
+ . unwrap ( ) ;
702
697
703
698
builder
704
699
. add_cgroup_property (
@@ -719,9 +714,11 @@ mod tests {
719
714
mock_cgroups. add_v2_mounts ( ) . unwrap ( ) ;
720
715
721
716
for v in & [ 1 , 2 ] {
722
- let mut builder =
723
- CgroupConfigurationBuilder :: new ( * v, mock_cgroups. proc_mounts_path . as_str ( ) )
724
- . unwrap ( ) ;
717
+ let mut builder = CgroupConfigurationBuilder :: new (
718
+ * v,
719
+ mock_cgroups. proc_mounts_path . to_str ( ) . unwrap ( ) ,
720
+ )
721
+ . unwrap ( ) ;
725
722
builder
726
723
. add_cgroup_property (
727
724
"invalid.cg" . to_string ( ) ,
@@ -739,7 +736,8 @@ mod tests {
739
736
mock_cgroups. add_v1_mounts ( ) . unwrap ( ) ;
740
737
741
738
let mut builder =
742
- CgroupConfigurationBuilder :: new ( 1 , mock_cgroups. proc_mounts_path . as_str ( ) ) . unwrap ( ) ;
739
+ CgroupConfigurationBuilder :: new ( 1 , mock_cgroups. proc_mounts_path . to_str ( ) . unwrap ( ) )
740
+ . unwrap ( ) ;
743
741
builder
744
742
. add_cgroup_property (
745
743
"cpuset.mems" . to_string ( ) ,
@@ -750,7 +748,7 @@ mod tests {
750
748
. unwrap ( ) ;
751
749
let cg_conf = builder. build ( ) ;
752
750
753
- let cg_root = PathBuf :: from ( format ! ( "{}/ cpuset", mock_cgroups . sys_cgroups_path ) ) ;
751
+ let cg_root = mock_cgroups . sys_cgroups_path . join ( " cpuset") ;
754
752
755
753
// with real cgroups these files are created automatically
756
754
// since the mock will not do it automatically, we create it here
@@ -773,11 +771,13 @@ mod tests {
773
771
fn test_cgroup_conf_v2_write_value ( ) {
774
772
let mut mock_cgroups = MockCgroupFs :: new ( ) . unwrap ( ) ;
775
773
mock_cgroups. add_v2_mounts ( ) . unwrap ( ) ;
776
- let builder = CgroupConfigurationBuilder :: new ( 2 , mock_cgroups. proc_mounts_path . as_str ( ) ) ;
774
+ let builder =
775
+ CgroupConfigurationBuilder :: new ( 2 , mock_cgroups. proc_mounts_path . to_str ( ) . unwrap ( ) ) ;
777
776
builder. unwrap ( ) ;
778
777
779
778
let mut builder =
780
- CgroupConfigurationBuilder :: new ( 2 , mock_cgroups. proc_mounts_path . as_str ( ) ) . unwrap ( ) ;
779
+ CgroupConfigurationBuilder :: new ( 2 , mock_cgroups. proc_mounts_path . to_str ( ) . unwrap ( ) )
780
+ . unwrap ( ) ;
781
781
builder
782
782
. add_cgroup_property (
783
783
"cpuset.mems" . to_string ( ) ,
@@ -787,7 +787,7 @@ mod tests {
787
787
)
788
788
. unwrap ( ) ;
789
789
790
- let cg_root = PathBuf :: from ( format ! ( "{}/ unified", mock_cgroups . sys_cgroups_path ) ) ;
790
+ let cg_root = mock_cgroups . sys_cgroups_path . join ( " unified") ;
791
791
792
792
assert_eq ! ( builder. get_v2_hierarchy_path( ) . unwrap( ) , & cg_root) ;
793
793
0 commit comments