@@ -849,6 +849,57 @@ var _ = Describe("Podman kube generate", func() {
849849 Expect (inspect .OutputToString ()).To (ContainSubstring (vol1 ))
850850 })
851851
852+ It ("with subpath volume" , func () {
853+ // We want to verify that generating Volume's subPath is working properly
854+ // https://kubernetes.io/docs/concepts/storage/volumes/#using-subpath
855+ // by creating a volume with two directories and mounthing them separately
856+ volName := "vol-test1"
857+ pvcName := fmt .Sprintf ("%s-pvc" , volName )
858+ podmanTest .PodmanExitCleanly ("volume" , "create" , volName )
859+
860+ mountPoint := "/mnt"
861+
862+ etcFile := "etcfile"
863+ etcDirPath := filepath .Join (mountPoint , "etc" )
864+ etcSubPath := filepath .Join ("/etc" , etcFile )
865+ etcMountSubPath := filepath .Join (mountPoint , etcSubPath )
866+ populateEtcCmd := fmt .Sprintf ("mkdir -p %s; touch %s" , etcDirPath , etcMountSubPath )
867+
868+ varFile := "varfile"
869+ varDirPath := filepath .Join (mountPoint , "var" )
870+ varSubPath := filepath .Join ("/var" , varFile )
871+ varMountSubPath := filepath .Join (mountPoint , varSubPath )
872+ populateVarCmd := fmt .Sprintf ("mkdir -p %s; touch %s" , varDirPath , varMountSubPath )
873+
874+ cmd := fmt .Sprintf ("%s; %s" , populateEtcCmd , populateVarCmd )
875+ vol := fmt .Sprintf ("%s:%s" , volName , mountPoint )
876+ podmanTest .PodmanExitCleanly ("run" , "-v" , vol , ALPINE , "sh" , "-c" , cmd )
877+
878+ etcTargetPath := filepath .Join (mountPoint , etcFile )
879+ varTargetPath := filepath .Join (mountPoint , varFile )
880+ mountTemplate := "type=volume,source=%s,volume-subpath=%s,target=%s"
881+ podmanTest .PodmanExitCleanly ("run" , "-d" , "--pod" , "new:test1" , "--name" , "test-ctr" ,
882+ "--mount" , fmt .Sprintf (mountTemplate , volName , etcSubPath , etcTargetPath ),
883+ "--mount" , fmt .Sprintf (mountTemplate , volName , varSubPath , varTargetPath ),
884+ CITEST_IMAGE , "top" )
885+
886+ kube := podmanTest .PodmanExitCleanly ("kube" , "generate" , "test1" )
887+
888+ pod := new (v1.Pod )
889+ err = yaml .Unmarshal (kube .Out .Contents (), pod )
890+ Expect (err ).ToNot (HaveOccurred ())
891+ Expect (pod .Spec .Containers [0 ].VolumeMounts ).To (ContainElements (v1.VolumeMount {
892+ Name : pvcName ,
893+ MountPath : etcTargetPath ,
894+ SubPath : etcSubPath ,
895+ }, v1.VolumeMount {
896+ Name : pvcName ,
897+ MountPath : varTargetPath ,
898+ SubPath : varSubPath ,
899+ }),
900+ )
901+ })
902+
852903 It ("when bind-mounting '/' and '/root' at the same time " , func () {
853904 // Fixes https://github.com/containers/podman/issues/9764
854905
0 commit comments