@@ -22,15 +22,27 @@ import (
2222 datav1alpha1 "github.com/fluid-cloudnative/fluid/api/v1alpha1"
2323 "github.com/fluid-cloudnative/fluid/pkg/common"
2424 "github.com/fluid-cloudnative/fluid/pkg/utils/fake"
25- "sigs.k8s.io/controller-runtime/pkg/client"
26-
2725 appsv1 "k8s.io/api/apps/v1"
26+ batchv1 "k8s.io/api/batch/v1"
2827 corev1 "k8s.io/api/core/v1"
28+ rbacv1 "k8s.io/api/rbac/v1"
2929 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
30-
3130 "k8s.io/apimachinery/pkg/runtime"
31+ "sigs.k8s.io/controller-runtime/pkg/client"
32+ )
33+
34+ var (
35+ testScheme * runtime.Scheme
3236)
3337
38+ func init () {
39+ testScheme = runtime .NewScheme ()
40+ _ = corev1 .AddToScheme (testScheme )
41+ _ = rbacv1 .AddToScheme (testScheme )
42+ _ = appsv1 .AddToScheme (testScheme )
43+ _ = batchv1 .AddToScheme (testScheme )
44+ }
45+
3446// func TestGetTemplateToInjectForFuse(t *testing.T) {
3547// type runtimeInfo struct {
3648// name string
@@ -1169,3 +1181,180 @@ func TestGetFuseDaemonset(t *testing.T) {
11691181 }
11701182 }
11711183}
1184+
1185+ func TestGetMountInfoFromVolumeClaim (t * testing.T ) {
1186+ namespace := "default"
1187+ testPVCInputs := []* corev1.PersistentVolumeClaim {{
1188+ ObjectMeta : metav1.ObjectMeta {Name : "fluidpvc" ,
1189+ Namespace : namespace },
1190+ Spec : corev1.PersistentVolumeClaimSpec {
1191+ VolumeName : "fluidpv" ,
1192+ },
1193+ }, {
1194+ ObjectMeta : metav1.ObjectMeta {Name : "nonfluidpvc" ,
1195+ Annotations : common .ExpectedFluidAnnotations ,
1196+ Namespace : namespace },
1197+ Spec : corev1.PersistentVolumeClaimSpec {
1198+ VolumeName : "nonfluidpv" ,
1199+ },
1200+ }, {
1201+ ObjectMeta : metav1.ObjectMeta {Name : "nopv" ,
1202+ Annotations : common .ExpectedFluidAnnotations ,
1203+ Namespace : namespace },
1204+ Spec : corev1.PersistentVolumeClaimSpec {
1205+ VolumeName : "nopv" ,
1206+ },
1207+ }, {
1208+ ObjectMeta : metav1.ObjectMeta {Name : "subpvc" ,
1209+ Annotations : common .ExpectedFluidAnnotations ,
1210+ Namespace : namespace },
1211+ Spec : corev1.PersistentVolumeClaimSpec {
1212+ VolumeName : "subpv" ,
1213+ },
1214+ }}
1215+
1216+ objs := []runtime.Object {}
1217+
1218+ for _ , pvc := range testPVCInputs {
1219+ objs = append (objs , pvc .DeepCopy ())
1220+ }
1221+
1222+ testPVInputs := []* corev1.PersistentVolume {{
1223+ ObjectMeta : metav1.ObjectMeta {Name : "fluidpv" },
1224+ Spec : corev1.PersistentVolumeSpec {
1225+ PersistentVolumeSource : corev1.PersistentVolumeSource {
1226+ CSI : & corev1.CSIPersistentVolumeSource {
1227+ Driver : "fuse.csi.fluid.io" ,
1228+ VolumeAttributes : map [string ]string {
1229+ common .VolumeAttrFluidPath : "/runtime-mnt/jindo/big-data/nofounddataset/jindofs-fuse" ,
1230+ common .VolumeAttrMountType : common .JindoRuntime ,
1231+ },
1232+ },
1233+ },
1234+ },
1235+ }, {
1236+ ObjectMeta : metav1.ObjectMeta {Name : "nonfluidpv" , Annotations : common .ExpectedFluidAnnotations },
1237+ Spec : corev1.PersistentVolumeSpec {},
1238+ }, {
1239+ ObjectMeta : metav1.ObjectMeta {Name : "subpv" },
1240+ Spec : corev1.PersistentVolumeSpec {
1241+ PersistentVolumeSource : corev1.PersistentVolumeSource {
1242+ CSI : & corev1.CSIPersistentVolumeSource {
1243+ Driver : "fuse.csi.fluid.io" ,
1244+ VolumeAttributes : map [string ]string {
1245+ common .VolumeAttrFluidPath : "/runtime-mnt/jindo/big-data/nofounddataset/jindofs-fuse" ,
1246+ common .VolumeAttrMountType : common .JindoRuntime ,
1247+ common .VolumeAttrFluidSubPath : "subtest" ,
1248+ },
1249+ },
1250+ },
1251+ },
1252+ }}
1253+
1254+ for _ , pv := range testPVInputs {
1255+ objs = append (objs , pv .DeepCopy ())
1256+ }
1257+
1258+ client := fake .NewFakeClientWithScheme (testScheme , objs ... )
1259+
1260+ type args struct {
1261+ name string
1262+ namespace string
1263+ }
1264+ tests := []struct {
1265+ name string
1266+ args args
1267+ wantError bool
1268+ wantPath string
1269+ wantType string
1270+ wantSubPath string
1271+ }{{
1272+ name : "volumeClaim doesn't exist" ,
1273+ args : args {
1274+ name : "notExist" ,
1275+ namespace : namespace ,
1276+ },
1277+ wantError : true ,
1278+ }, {
1279+ name : "non fluid pv" ,
1280+ args : args {
1281+ name : "nonfluidpvc" ,
1282+ namespace : namespace ,
1283+ },
1284+ wantError : true ,
1285+ }, {
1286+ name : " fluid pv" ,
1287+ args : args {
1288+ name : "fluidpvc" ,
1289+ namespace : namespace ,
1290+ },
1291+ wantError : false ,
1292+ wantPath : "/runtime-mnt/jindo/big-data/nofounddataset/jindofs-fuse" ,
1293+ wantType : common .JindoRuntime ,
1294+ }, {
1295+ name : "no pv" ,
1296+ args : args {
1297+ name : "nopv" ,
1298+ namespace : namespace ,
1299+ },
1300+ wantError : true ,
1301+ }, {
1302+ name : "sub pv" ,
1303+ args : args {
1304+ name : "subpvc" ,
1305+ namespace : namespace ,
1306+ },
1307+ wantError : false ,
1308+ wantPath : "/runtime-mnt/jindo/big-data/nofounddataset/jindofs-fuse" ,
1309+ wantType : common .JindoRuntime ,
1310+ wantSubPath : "subtest" ,
1311+ }}
1312+
1313+ for _ , tt := range tests {
1314+ t .Run (tt .name , func (t * testing.T ) {
1315+ runtimeInfo := RuntimeInfo {
1316+ name : tt .args .name ,
1317+ namespace : tt .args .namespace ,
1318+ runtimeType : common .JindoRuntime ,
1319+ client : client ,
1320+ }
1321+
1322+ path , mountType , subpath , err := runtimeInfo .getMountInfo (false )
1323+ got := err != nil
1324+
1325+ if got != tt .wantError {
1326+ t .Errorf ("testcase %v GetMountInfoFromVolumeClaim() for %v in %v = %v, err = %v" , tt .name ,
1327+ tt .args .name ,
1328+ tt .args .namespace ,
1329+ got ,
1330+ err )
1331+ }
1332+
1333+ if path != tt .wantPath {
1334+ t .Errorf ("testcase %v GetMountInfoFromVolumeClaim() for %v in %v got path %v, want path = %v" , tt .name ,
1335+ tt .args .name ,
1336+ tt .args .namespace ,
1337+ path ,
1338+ tt .wantPath )
1339+ }
1340+
1341+ if mountType != tt .wantType {
1342+ t .Errorf ("testcase %v GetMountInfoFromVolumeClaim() for %v in %v got mountType %v, want mountType = %v" , tt .name ,
1343+ tt .args .name ,
1344+ tt .args .namespace ,
1345+ mountType ,
1346+ tt .wantType )
1347+ }
1348+
1349+ if subpath != tt .wantSubPath {
1350+ t .Errorf ("testcase %v GetMountInfoFromVolumeClaim() for %v in %v got subpath %v, want subpath = %v" , tt .name ,
1351+ tt .args .name ,
1352+ tt .args .namespace ,
1353+ subpath ,
1354+ tt .wantSubPath )
1355+ }
1356+
1357+ })
1358+ }
1359+
1360+ }
0 commit comments