github.com/jmclong/azuredisk-csi-driver@v0.7.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/azuredisk-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  	IsWindows bool
    35  }
    36  
    37  type VolumeDetails struct {
    38  	VolumeType            string
    39  	FSType                string
    40  	Encrypted             bool
    41  	MountOptions          []string
    42  	ClaimSize             string
    43  	ReclaimPolicy         *v1.PersistentVolumeReclaimPolicy
    44  	VolumeBindingMode     *storagev1.VolumeBindingMode
    45  	AllowedTopologyValues []string
    46  	VolumeMode            VolumeMode
    47  	VolumeMount           VolumeMountDetails
    48  	VolumeDevice          VolumeDeviceDetails
    49  	// Optional, used with pre-provisioned volumes
    50  	VolumeID string
    51  	// Optional, used with PVCs created from snapshots or pvc
    52  	DataSource *DataSource
    53  	// Optional, used with specified StorageClass
    54  	StorageClass *storagev1.StorageClass
    55  }
    56  
    57  type VolumeMode int
    58  
    59  const (
    60  	FileSystem VolumeMode = iota
    61  	Block
    62  )
    63  
    64  const (
    65  	VolumeSnapshotKind = "VolumeSnapshot"
    66  	VolumePVCKind      = "PersistentVolumeClaim"
    67  	APIVersionv1beta1  = "v1beta1"
    68  	SnapshotAPIVersion = "snapshot.storage.k8s.io/" + APIVersionv1beta1
    69  )
    70  
    71  var (
    72  	SnapshotAPIGroup = "snapshot.storage.k8s.io"
    73  )
    74  
    75  type VolumeMountDetails struct {
    76  	NameGenerate      string
    77  	MountPathGenerate string
    78  	ReadOnly          bool
    79  }
    80  
    81  type VolumeDeviceDetails struct {
    82  	NameGenerate string
    83  	DevicePath   string
    84  }
    85  
    86  type DataSource struct {
    87  	Kind string
    88  	Name string
    89  }
    90  
    91  func (pod *PodDetails) SetupWithDynamicVolumes(client clientset.Interface, namespace *v1.Namespace, csiDriver driver.DynamicPVTestDriver) (*TestPod, []func()) {
    92  	tpod := NewTestPod(client, namespace, pod.Cmd, pod.IsWindows)
    93  	cleanupFuncs := make([]func(), 0)
    94  	for n, v := range pod.Volumes {
    95  		tpvc, funcs := v.SetupDynamicPersistentVolumeClaim(client, namespace, csiDriver)
    96  		cleanupFuncs = append(cleanupFuncs, funcs...)
    97  		ginkgo.By("setting up the pod")
    98  		if v.VolumeMode == Block {
    99  			tpod.SetupRawBlockVolume(tpvc.persistentVolumeClaim, fmt.Sprintf("%s%d", v.VolumeDevice.NameGenerate, n+1), v.VolumeDevice.DevicePath)
   100  		} else {
   101  			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)
   102  		}
   103  	}
   104  	return tpod, cleanupFuncs
   105  }
   106  
   107  func (pod *PodDetails) SetupWithPreProvisionedVolumes(client clientset.Interface, namespace *v1.Namespace, csiDriver driver.PreProvisionedVolumeTestDriver) (*TestPod, []func()) {
   108  	tpod := NewTestPod(client, namespace, pod.Cmd, pod.IsWindows)
   109  	cleanupFuncs := make([]func(), 0)
   110  	for n, v := range pod.Volumes {
   111  		tpvc, funcs := v.SetupPreProvisionedPersistentVolumeClaim(client, namespace, csiDriver)
   112  		cleanupFuncs = append(cleanupFuncs, funcs...)
   113  
   114  		if v.VolumeMode == Block {
   115  			tpod.SetupRawBlockVolume(tpvc.persistentVolumeClaim, fmt.Sprintf("%s%d", v.VolumeDevice.NameGenerate, n+1), v.VolumeDevice.DevicePath)
   116  		} else {
   117  			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)
   118  		}
   119  	}
   120  	return tpod, cleanupFuncs
   121  }
   122  
   123  func (pod *PodDetails) SetupDeployment(client clientset.Interface, namespace *v1.Namespace, csiDriver driver.DynamicPVTestDriver) (*TestDeployment, []func()) {
   124  	cleanupFuncs := make([]func(), 0)
   125  	volume := pod.Volumes[0]
   126  	ginkgo.By("setting up the StorageClass")
   127  	storageClass := csiDriver.GetDynamicProvisionStorageClass(driver.GetParameters(), volume.MountOptions, volume.ReclaimPolicy, volume.VolumeBindingMode, volume.AllowedTopologyValues, namespace.Name)
   128  	tsc := NewTestStorageClass(client, namespace, storageClass)
   129  	createdStorageClass := tsc.Create()
   130  	cleanupFuncs = append(cleanupFuncs, tsc.Cleanup)
   131  	ginkgo.By("setting up the PVC")
   132  	tpvc := NewTestPersistentVolumeClaim(client, namespace, volume.ClaimSize, volume.VolumeMode, &createdStorageClass)
   133  	tpvc.Create()
   134  	if volume.VolumeBindingMode == nil || *volume.VolumeBindingMode == storagev1.VolumeBindingImmediate {
   135  		tpvc.WaitForBound()
   136  		tpvc.ValidateProvisionedPersistentVolume()
   137  	}
   138  	cleanupFuncs = append(cleanupFuncs, tpvc.Cleanup)
   139  	ginkgo.By("setting up the Deployment")
   140  	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, pod.IsWindows)
   141  
   142  	cleanupFuncs = append(cleanupFuncs, tDeployment.Cleanup)
   143  	return tDeployment, cleanupFuncs
   144  }
   145  
   146  func (volume *VolumeDetails) SetupDynamicPersistentVolumeClaim(client clientset.Interface, namespace *v1.Namespace, csiDriver driver.DynamicPVTestDriver) (*TestPersistentVolumeClaim, []func()) {
   147  	cleanupFuncs := make([]func(), 0)
   148  	storageClass := volume.StorageClass
   149  	if storageClass == nil {
   150  		tsc, tscCleanup := volume.CreateStorageClass(client, namespace, csiDriver)
   151  		cleanupFuncs = append(cleanupFuncs, tscCleanup)
   152  		storageClass = tsc.storageClass
   153  	}
   154  	ginkgo.By("setting up the PVC and PV")
   155  	var tpvc *TestPersistentVolumeClaim
   156  	if volume.DataSource != nil {
   157  		dataSource := &v1.TypedLocalObjectReference{
   158  			Name: volume.DataSource.Name,
   159  			Kind: volume.DataSource.Kind,
   160  		}
   161  		if volume.DataSource.Kind == VolumeSnapshotKind {
   162  			dataSource.APIGroup = &SnapshotAPIGroup
   163  		}
   164  		tpvc = NewTestPersistentVolumeClaimWithDataSource(client, namespace, volume.ClaimSize, volume.VolumeMode, storageClass, dataSource)
   165  	} else {
   166  		tpvc = NewTestPersistentVolumeClaim(client, namespace, volume.ClaimSize, volume.VolumeMode, storageClass)
   167  	}
   168  	tpvc.Create()
   169  	cleanupFuncs = append(cleanupFuncs, tpvc.Cleanup)
   170  	// PV will not be ready until PVC is used in a pod when volumeBindingMode: WaitForFirstConsumer
   171  	if volume.VolumeBindingMode == nil || *volume.VolumeBindingMode == storagev1.VolumeBindingImmediate {
   172  		tpvc.WaitForBound()
   173  		tpvc.ValidateProvisionedPersistentVolume()
   174  	}
   175  
   176  	return tpvc, cleanupFuncs
   177  }
   178  
   179  func (volume *VolumeDetails) SetupPreProvisionedPersistentVolumeClaim(client clientset.Interface, namespace *v1.Namespace, csiDriver driver.PreProvisionedVolumeTestDriver) (*TestPersistentVolumeClaim, []func()) {
   180  	cleanupFuncs := make([]func(), 0)
   181  	ginkgo.By("setting up the PV")
   182  	pv := csiDriver.GetPersistentVolume(volume.VolumeID, volume.FSType, volume.ClaimSize, volume.ReclaimPolicy, namespace.Name)
   183  	tpv := NewTestPreProvisionedPersistentVolume(client, pv)
   184  	tpv.Create()
   185  	ginkgo.By("setting up the PVC")
   186  	tpvc := NewTestPersistentVolumeClaim(client, namespace, volume.ClaimSize, volume.VolumeMode, nil)
   187  	tpvc.Create()
   188  	cleanupFuncs = append(cleanupFuncs, tpvc.DeleteBoundPersistentVolume)
   189  	cleanupFuncs = append(cleanupFuncs, tpvc.Cleanup)
   190  	tpvc.WaitForBound()
   191  	tpvc.ValidateProvisionedPersistentVolume()
   192  
   193  	return tpvc, cleanupFuncs
   194  }
   195  
   196  func (volume *VolumeDetails) CreateStorageClass(client clientset.Interface, namespace *v1.Namespace, csiDriver driver.DynamicPVTestDriver) (*TestStorageClass, func()) {
   197  	ginkgo.By("setting up the StorageClass")
   198  	storageClass := csiDriver.GetDynamicProvisionStorageClass(driver.GetParameters(), volume.MountOptions, volume.ReclaimPolicy, volume.VolumeBindingMode, volume.AllowedTopologyValues, namespace.Name)
   199  	tsc := NewTestStorageClass(client, namespace, storageClass)
   200  	tsc.Create()
   201  	return tsc, tsc.Cleanup
   202  }
   203  
   204  func CreateVolumeSnapshotClass(client restclientset.Interface, namespace *v1.Namespace, csiDriver driver.VolumeSnapshotTestDriver) (*TestVolumeSnapshotClass, func()) {
   205  	ginkgo.By("setting up the VolumeSnapshotClass")
   206  	volumeSnapshotClass := csiDriver.GetVolumeSnapshotClass(namespace.Name)
   207  	tvsc := NewTestVolumeSnapshotClass(client, namespace, volumeSnapshotClass)
   208  	tvsc.Create()
   209  
   210  	return tvsc, tvsc.Cleanup
   211  }