github.com/kubernetes-sigs/blobfuse-csi-driver@v0.5.0/test/e2e/testsuites/specs.go (about)

     1  /*
     2  Copyright 2019 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package testsuites
    18  
    19  import (
    20  	"fmt"
    21  
    22  	"sigs.k8s.io/blobfuse-csi-driver/test/e2e/driver"
    23  
    24  	"github.com/onsi/ginkgo"
    25  	v1 "k8s.io/api/core/v1"
    26  	storagev1 "k8s.io/api/storage/v1"
    27  	clientset "k8s.io/client-go/kubernetes"
    28  	restclientset "k8s.io/client-go/rest"
    29  )
    30  
    31  type PodDetails struct {
    32  	Cmd     string
    33  	Volumes []VolumeDetails
    34  }
    35  
    36  type VolumeDetails struct {
    37  	VolumeType            string
    38  	FSType                string
    39  	Encrypted             bool
    40  	MountOptions          []string
    41  	ClaimSize             string
    42  	ReclaimPolicy         *v1.PersistentVolumeReclaimPolicy
    43  	VolumeBindingMode     *storagev1.VolumeBindingMode
    44  	AllowedTopologyValues []string
    45  	VolumeMode            VolumeMode
    46  	VolumeMount           VolumeMountDetails
    47  	VolumeDevice          VolumeDeviceDetails
    48  	// Optional, used with pre-provisioned volumes
    49  	VolumeID string
    50  	// Optional, used with PVCs created from snapshots
    51  	DataSource         *DataSource
    52  	ContainerName      string
    53  	NodeStageSecretRef string
    54  }
    55  
    56  type VolumeMode int
    57  
    58  const (
    59  	FileSystem VolumeMode = iota
    60  	Block
    61  )
    62  
    63  const (
    64  	VolumeSnapshotKind = "VolumeSnapshot"
    65  	SnapshotAPIVersion = "snapshot.storage.k8s.io/v1alpha1"
    66  	APIVersionv1alpha1 = "v1alpha1"
    67  )
    68  
    69  var (
    70  	SnapshotAPIGroup = "snapshot.storage.k8s.io"
    71  )
    72  
    73  type VolumeMountDetails struct {
    74  	NameGenerate      string
    75  	MountPathGenerate string
    76  	ReadOnly          bool
    77  }
    78  
    79  type VolumeDeviceDetails struct {
    80  	NameGenerate string
    81  	DevicePath   string
    82  }
    83  
    84  type DataSource struct {
    85  	Name string
    86  }
    87  
    88  func (pod *PodDetails) SetupWithDynamicVolumes(client clientset.Interface, namespace *v1.Namespace, csiDriver driver.DynamicPVTestDriver) (*TestPod, []func()) {
    89  	tpod := NewTestPod(client, namespace, pod.Cmd)
    90  	cleanupFuncs := make([]func(), 0)
    91  	for n, v := range pod.Volumes {
    92  		tpvc, funcs := v.SetupDynamicPersistentVolumeClaim(client, namespace, csiDriver)
    93  		cleanupFuncs = append(cleanupFuncs, funcs...)
    94  		if v.VolumeMode == Block {
    95  			tpod.SetupRawBlockVolume(tpvc.persistentVolumeClaim, fmt.Sprintf("%s%d", v.VolumeDevice.NameGenerate, n+1), v.VolumeDevice.DevicePath)
    96  		} else {
    97  			tpod.SetupVolume(tpvc.persistentVolumeClaim, fmt.Sprintf("%s%d", v.VolumeMount.NameGenerate, n+1), fmt.Sprintf("%s%d", v.VolumeMount.MountPathGenerate, n+1), v.VolumeMount.ReadOnly)
    98  		}
    99  	}
   100  	return tpod, cleanupFuncs
   101  }
   102  
   103  func (pod *PodDetails) SetupWithPreProvisionedVolumes(client clientset.Interface, namespace *v1.Namespace, csiDriver driver.PreProvisionedVolumeTestDriver) (*TestPod, []func()) {
   104  	tpod := NewTestPod(client, namespace, pod.Cmd)
   105  	cleanupFuncs := make([]func(), 0)
   106  	for n, v := range pod.Volumes {
   107  		tpvc, funcs := v.SetupPreProvisionedPersistentVolumeClaim(client, namespace, csiDriver)
   108  		cleanupFuncs = append(cleanupFuncs, funcs...)
   109  
   110  		if v.VolumeMode == Block {
   111  			tpod.SetupRawBlockVolume(tpvc.persistentVolumeClaim, fmt.Sprintf("%s%d", v.VolumeDevice.NameGenerate, n+1), v.VolumeDevice.DevicePath)
   112  		} else {
   113  			tpod.SetupVolume(tpvc.persistentVolumeClaim, fmt.Sprintf("%s%d", v.VolumeMount.NameGenerate, n+1), fmt.Sprintf("%s%d", v.VolumeMount.MountPathGenerate, n+1), v.VolumeMount.ReadOnly)
   114  		}
   115  	}
   116  	return tpod, cleanupFuncs
   117  }
   118  
   119  func (pod *PodDetails) SetupDeployment(client clientset.Interface, namespace *v1.Namespace, csiDriver driver.DynamicPVTestDriver) (*TestDeployment, []func()) {
   120  	cleanupFuncs := make([]func(), 0)
   121  	volume := pod.Volumes[0]
   122  	ginkgo.By("setting up the StorageClass")
   123  	storageClass := csiDriver.GetDynamicProvisionStorageClass(driver.GetParameters(), volume.MountOptions, volume.ReclaimPolicy, volume.VolumeBindingMode, volume.AllowedTopologyValues, namespace.Name)
   124  	tsc := NewTestStorageClass(client, namespace, storageClass)
   125  	createdStorageClass := tsc.Create()
   126  	cleanupFuncs = append(cleanupFuncs, tsc.Cleanup)
   127  	ginkgo.By("setting up the PVC")
   128  	tpvc := NewTestPersistentVolumeClaim(client, namespace, volume.ClaimSize, volume.VolumeMode, &createdStorageClass)
   129  	tpvc.Create()
   130  	tpvc.WaitForBound()
   131  	tpvc.ValidateProvisionedPersistentVolume()
   132  	cleanupFuncs = append(cleanupFuncs, tpvc.Cleanup)
   133  	ginkgo.By("setting up the Deployment")
   134  	tDeployment := NewTestDeployment(client, namespace, pod.Cmd, tpvc.persistentVolumeClaim, fmt.Sprintf("%s%d", volume.VolumeMount.NameGenerate, 1), fmt.Sprintf("%s%d", volume.VolumeMount.MountPathGenerate, 1), volume.VolumeMount.ReadOnly)
   135  
   136  	cleanupFuncs = append(cleanupFuncs, tDeployment.Cleanup)
   137  	return tDeployment, cleanupFuncs
   138  }
   139  
   140  func (volume *VolumeDetails) SetupDynamicPersistentVolumeClaim(client clientset.Interface, namespace *v1.Namespace, csiDriver driver.DynamicPVTestDriver) (*TestPersistentVolumeClaim, []func()) {
   141  	cleanupFuncs := make([]func(), 0)
   142  	ginkgo.By("setting up the StorageClass")
   143  	storageClass := csiDriver.GetDynamicProvisionStorageClass(driver.GetParameters(), volume.MountOptions, volume.ReclaimPolicy, volume.VolumeBindingMode, volume.AllowedTopologyValues, namespace.Name)
   144  	tsc := NewTestStorageClass(client, namespace, storageClass)
   145  	createdStorageClass := tsc.Create()
   146  	cleanupFuncs = append(cleanupFuncs, tsc.Cleanup)
   147  	ginkgo.By("setting up the PVC and PV")
   148  	var tpvc *TestPersistentVolumeClaim
   149  	if volume.DataSource != nil {
   150  		dataSource := &v1.TypedLocalObjectReference{
   151  			Name:     volume.DataSource.Name,
   152  			Kind:     VolumeSnapshotKind,
   153  			APIGroup: &SnapshotAPIGroup,
   154  		}
   155  		tpvc = NewTestPersistentVolumeClaimWithDataSource(client, namespace, volume.ClaimSize, volume.VolumeMode, &createdStorageClass, dataSource)
   156  	} else {
   157  		tpvc = NewTestPersistentVolumeClaim(client, namespace, volume.ClaimSize, volume.VolumeMode, &createdStorageClass)
   158  	}
   159  	tpvc.Create()
   160  	cleanupFuncs = append(cleanupFuncs, tpvc.Cleanup)
   161  	// PV will not be ready until PVC is used in a pod when volumeBindingMode: WaitForFirstConsumer
   162  	if volume.VolumeBindingMode == nil || *volume.VolumeBindingMode == storagev1.VolumeBindingImmediate {
   163  		tpvc.WaitForBound()
   164  		tpvc.ValidateProvisionedPersistentVolume()
   165  	}
   166  
   167  	return tpvc, cleanupFuncs
   168  }
   169  
   170  func (volume *VolumeDetails) SetupPreProvisionedPersistentVolumeClaim(client clientset.Interface, namespace *v1.Namespace, csiDriver driver.PreProvisionedVolumeTestDriver) (*TestPersistentVolumeClaim, []func()) {
   171  	cleanupFuncs := make([]func(), 0)
   172  	ginkgo.By("setting up the PV")
   173  	attrib := make(map[string]string)
   174  	if volume.ContainerName != "" {
   175  		attrib["containerName"] = volume.ContainerName
   176  	}
   177  	nodeStageSecretRef := volume.NodeStageSecretRef
   178  	pv := csiDriver.GetPersistentVolume(volume.VolumeID, volume.FSType, volume.ClaimSize, volume.ReclaimPolicy, namespace.Name, attrib, nodeStageSecretRef)
   179  	tpv := NewTestPreProvisionedPersistentVolume(client, pv)
   180  	tpv.Create()
   181  	ginkgo.By("setting up the PVC")
   182  	tpvc := NewTestPersistentVolumeClaim(client, namespace, volume.ClaimSize, volume.VolumeMode, nil)
   183  	tpvc.Create()
   184  	cleanupFuncs = append(cleanupFuncs, tpvc.DeleteBoundPersistentVolume)
   185  	cleanupFuncs = append(cleanupFuncs, tpvc.Cleanup)
   186  	tpvc.WaitForBound()
   187  	tpvc.ValidateProvisionedPersistentVolume()
   188  
   189  	return tpvc, cleanupFuncs
   190  }
   191  
   192  func CreateVolumeSnapshotClass(client restclientset.Interface, namespace *v1.Namespace, csiDriver driver.VolumeSnapshotTestDriver) (*TestVolumeSnapshotClass, func()) {
   193  	ginkgo.By("setting up the VolumeSnapshotClass")
   194  	volumeSnapshotClass := csiDriver.GetVolumeSnapshotClass(namespace.Name)
   195  	tvsc := NewTestVolumeSnapshotClass(client, namespace, volumeSnapshotClass)
   196  	tvsc.Create()
   197  
   198  	return tvsc, tvsc.Cleanup
   199  }