@@ -781,6 +781,8 @@ fn driver_config_rejects_bind_mounts_unless_enabled() {
781781
782782#[ test]
783783fn build_container_create_body_includes_bind_mounts_when_enabled ( ) {
784+ let bind_src = TempDir :: new ( ) . unwrap ( ) ;
785+ let src_path = bind_src. path ( ) . to_str ( ) . unwrap ( ) ;
784786 let mut sandbox = test_sandbox ( ) ;
785787 sandbox
786788 . spec
@@ -792,7 +794,7 @@ fn build_container_create_body_includes_bind_mounts_when_enabled() {
792794 . driver_config = Some ( json_struct ( serde_json:: json!( {
793795 "mounts" : [ {
794796 "type" : "bind" ,
795- "source" : "/host/path" ,
797+ "source" : src_path ,
796798 "target" : "/sandbox/host" ,
797799 "read_only" : true
798800 } ]
@@ -810,9 +812,10 @@ fn build_container_create_body_includes_bind_mounts_when_enabled() {
810812 . expect ( "binds should be set" ) ;
811813
812814 // User bind mount appears after the system binds.
815+ let expected = format ! ( "{src_path}:/sandbox/host:ro" ) ;
813816 assert ! (
814- binds. iter( ) . any( |b| b == "/host/path:/sandbox/host:ro" ) ,
815- "expected bind entry '/host/path:/sandbox/host:ro ', got {binds:?}"
817+ binds. iter( ) . any( |b| b == & expected ) ,
818+ "expected bind entry '{expected} ', got {binds:?}"
816819 ) ;
817820 // Bind mounts must not appear in the structured mounts vec.
818821 let mounts = body. host_config . unwrap ( ) . mounts . unwrap_or_default ( ) ;
@@ -824,6 +827,8 @@ fn build_container_create_body_includes_bind_mounts_when_enabled() {
824827
825828#[ test]
826829fn driver_config_defaults_enabled_bind_mounts_to_read_only ( ) {
830+ let bind_src = TempDir :: new ( ) . unwrap ( ) ;
831+ let src_path = bind_src. path ( ) . to_str ( ) . unwrap ( ) ;
827832 let mut sandbox = test_sandbox ( ) ;
828833 sandbox
829834 . spec
@@ -835,7 +840,7 @@ fn driver_config_defaults_enabled_bind_mounts_to_read_only() {
835840 . driver_config = Some ( json_struct ( serde_json:: json!( {
836841 "mounts" : [ {
837842 "type" : "bind" ,
838- "source" : "/host/path" ,
843+ "source" : src_path ,
839844 "target" : "/sandbox/host"
840845 } ]
841846 } ) ) ) ;
@@ -849,16 +854,17 @@ fn driver_config_defaults_enabled_bind_mounts_to_read_only() {
849854 . binds
850855 . expect ( "binds should be set" ) ;
851856
857+ let expected = format ! ( "{src_path}:/sandbox/host:ro" ) ;
852858 assert ! (
853- binds
854- . iter( )
855- . any( |b| b. contains( "/host/path:/sandbox/host:ro" ) ) ,
859+ binds. iter( ) . any( |b| b == & expected) ,
856860 "default bind mount should be read-only, got {binds:?}"
857861 ) ;
858862}
859863
860864#[ test]
861865fn bind_mount_selinux_shared_label ( ) {
866+ let bind_src = TempDir :: new ( ) . unwrap ( ) ;
867+ let src_path = bind_src. path ( ) . to_str ( ) . unwrap ( ) ;
862868 let mut sandbox = test_sandbox ( ) ;
863869 sandbox
864870 . spec
@@ -870,7 +876,7 @@ fn bind_mount_selinux_shared_label() {
870876 . driver_config = Some ( json_struct ( serde_json:: json!( {
871877 "mounts" : [ {
872878 "type" : "bind" ,
873- "source" : "/data/shared" ,
879+ "source" : src_path ,
874880 "target" : "/sandbox/data" ,
875881 "read_only" : true ,
876882 "selinux_label" : "shared"
@@ -886,14 +892,17 @@ fn bind_mount_selinux_shared_label() {
886892 . binds
887893 . expect ( "binds should be set" ) ;
888894
895+ let expected = format ! ( "{src_path}:/sandbox/data:ro,z" ) ;
889896 assert ! (
890- binds. iter( ) . any( |b| b == "/data/shared:/sandbox/data:ro,z" ) ,
897+ binds. iter( ) . any( |b| b == & expected ) ,
891898 "expected ':ro,z' label, got {binds:?}"
892899 ) ;
893900}
894901
895902#[ test]
896903fn bind_mount_selinux_private_label ( ) {
904+ let bind_src = TempDir :: new ( ) . unwrap ( ) ;
905+ let src_path = bind_src. path ( ) . to_str ( ) . unwrap ( ) ;
897906 let mut sandbox = test_sandbox ( ) ;
898907 sandbox
899908 . spec
@@ -905,7 +914,7 @@ fn bind_mount_selinux_private_label() {
905914 . driver_config = Some ( json_struct ( serde_json:: json!( {
906915 "mounts" : [ {
907916 "type" : "bind" ,
908- "source" : "/data/exclusive" ,
917+ "source" : src_path ,
909918 "target" : "/sandbox/data" ,
910919 "read_only" : false ,
911920 "selinux_label" : "private"
@@ -921,14 +930,17 @@ fn bind_mount_selinux_private_label() {
921930 . binds
922931 . expect ( "binds should be set" ) ;
923932
933+ let expected = format ! ( "{src_path}:/sandbox/data:Z" ) ;
924934 assert ! (
925- binds. iter( ) . any( |b| b == "/data/exclusive:/sandbox/data:Z" ) ,
935+ binds. iter( ) . any( |b| b == & expected ) ,
926936 "expected ':Z' label, got {binds:?}"
927937 ) ;
928938}
929939
930940#[ test]
931941fn bind_mount_without_selinux_label ( ) {
942+ let bind_src = TempDir :: new ( ) . unwrap ( ) ;
943+ let src_path = bind_src. path ( ) . to_str ( ) . unwrap ( ) ;
932944 let mut sandbox = test_sandbox ( ) ;
933945 sandbox
934946 . spec
@@ -940,7 +952,7 @@ fn bind_mount_without_selinux_label() {
940952 . driver_config = Some ( json_struct ( serde_json:: json!( {
941953 "mounts" : [ {
942954 "type" : "bind" ,
943- "source" : "/host/path" ,
955+ "source" : src_path ,
944956 "target" : "/sandbox/host" ,
945957 "read_only" : false
946958 } ]
@@ -955,12 +967,43 @@ fn bind_mount_without_selinux_label() {
955967 . binds
956968 . expect ( "binds should be set" ) ;
957969
970+ let expected = format ! ( "{src_path}:/sandbox/host" ) ;
958971 assert ! (
959- binds. iter( ) . any( |b| b == "/host/path:/sandbox/host" ) ,
972+ binds. iter( ) . any( |b| b == & expected ) ,
960973 "expected no options suffix, got {binds:?}"
961974 ) ;
962975}
963976
977+ #[ test]
978+ fn driver_config_rejects_missing_bind_source ( ) {
979+ let mut sandbox = test_sandbox ( ) ;
980+ sandbox
981+ . spec
982+ . as_mut ( )
983+ . unwrap ( )
984+ . template
985+ . as_mut ( )
986+ . unwrap ( )
987+ . driver_config = Some ( json_struct ( serde_json:: json!( {
988+ "mounts" : [ {
989+ "type" : "bind" ,
990+ "source" : "/no/such/path" ,
991+ "target" : "/sandbox/data"
992+ } ]
993+ } ) ) ) ;
994+ let mut config = runtime_config ( ) ;
995+ config. enable_bind_mounts = true ;
996+
997+ let err = build_container_create_body ( & sandbox, & config) . unwrap_err ( ) ;
998+
999+ assert_eq ! ( err. code( ) , tonic:: Code :: FailedPrecondition ) ;
1000+ assert ! (
1001+ err. message( ) . contains( "bind source path does not exist" ) ,
1002+ "expected missing-source error, got: {}" ,
1003+ err. message( )
1004+ ) ;
1005+ }
1006+
9641007#[ test]
9651008fn driver_config_rejects_relative_bind_sources_when_enabled ( ) {
9661009 let mut sandbox = test_sandbox ( ) ;
0 commit comments