Skip to content

Commit

Permalink
test(volume package): Add unit tests for cstor, zfs cas and refactors. (
Browse files Browse the repository at this point in the history
#62)

* golanci-lint checks corrected
* Update pkg/volume/zfs_locapv_test.go

Signed-off-by: Abhinandan-Purkait <abhinandan.purkait@mayadata.io>
  • Loading branch information
Abhinandan-Purkait authored Jul 21, 2021
1 parent e182f4a commit 3d90504
Show file tree
Hide file tree
Showing 11 changed files with 1,097 additions and 132 deletions.
8 changes: 4 additions & 4 deletions pkg/client/k8s.go
Original file line number Diff line number Diff line change
Expand Up @@ -339,8 +339,8 @@ func (k K8sClient) GetCV(volName string) (*cstorv1.CStorVolume, error) {
// Only one type can be returned at a time, please define the other type as '_' while calling.
func (k K8sClient) GetCVs(volNames []string, rType util.ReturnType, labelSelector string, options util.MapOptions) (*cstorv1.CStorVolumeList, map[string]cstorv1.CStorVolume, error) {
cVols, err := k.OpenebsCS.CstorV1().CStorVolumes("").List(context.TODO(), metav1.ListOptions{LabelSelector: labelSelector})
if err != nil {
return nil, nil, errors.Wrapf(err, "Error while getting volumes")
if len(cVols.Items) == 0 {
return nil, nil, errors.Errorf("Error while getting volumes%v", err)
}
var list []cstorv1.CStorVolume
if len(volNames) == 0 {
Expand Down Expand Up @@ -404,8 +404,8 @@ func (k K8sClient) GetCVA(labelSelector string) (*cstorv1.CStorVolumeAttachment,
// Only one type can be returned at a time, please define the other type as '_' while calling.
func (k K8sClient) GetCVAs(rType util.ReturnType, labelSelector string, options util.MapOptions) (*cstorv1.CStorVolumeAttachmentList, map[string]cstorv1.CStorVolumeAttachment, error) {
cvaList, err := k.OpenebsCS.CstorV1().CStorVolumeAttachments("").List(context.TODO(), metav1.ListOptions{LabelSelector: labelSelector})
if err != nil {
return nil, nil, err
if len(cvaList.Items) == 0 {
return nil, nil, errors.Errorf("No CVA found for %s, %v", labelSelector, err)
}
if rType == util.List {
return cvaList, nil, nil
Expand Down
2 changes: 1 addition & 1 deletion pkg/client/lvmlocalpv.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func getLVMclient(kubeconfig string) (*lvmclient.Clientset, error) {
// GetLVMvol returns a list or a map of LVMVolume depending upon rType & options
func (k K8sClient) GetLVMvol(lVols []string, rType util.ReturnType, labelSelector string, options util.MapOptions) (*lvm.LVMVolumeList, map[string]lvm.LVMVolume, error) {
// NOTE: The resource name must be plural and the API-group should be present for getting CRs
lvs, err := k.LVMCS.LocalV1alpha1().LVMVolumes(k.Ns).List(context.TODO(), v1.ListOptions{LabelSelector: labelSelector})
lvs, err := k.LVMCS.LocalV1alpha1().LVMVolumes("").List(context.TODO(), v1.ListOptions{LabelSelector: labelSelector})
if err != nil {
return nil, nil, err
}
Expand Down
2 changes: 2 additions & 0 deletions pkg/storage/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ func Get(pools []string, openebsNS string, casType string) error {
if f, ok := CasListMap()[casType]; ok {
// if a cas-type is found, run it and return the error
return f(k, pools)
} else if casType != "" {
return fmt.Errorf("cas-type %s is not supported", casType)
}
// 3. Call all functions & exit
var separator bool
Expand Down
24 changes: 9 additions & 15 deletions pkg/volume/cstor.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ func GetCStor(c *client.K8sClient, pvList *corev1.PersistentVolumeList, openebsN
accessMode := pv.Spec.AccessModes[0]
rows = append(rows, metav1.TableRow{
Cells: []interface{}{
ns, pv.Name, customStatus, storageVersion, pv.Spec.Capacity.Storage(), pv.Spec.StorageClassName, pv.Status.Phase,
ns, pv.Name, customStatus, storageVersion, util.ConvertToIBytes(pv.Spec.Capacity.Storage().String()), pv.Spec.StorageClassName, pv.Status.Phase,
accessMode, attachedNode}})
}
}
Expand Down Expand Up @@ -139,8 +139,8 @@ func DescribeCstorVolume(c *client.K8sClient, vol *corev1.PersistentVolume) erro
}

// 5. cStor Volume Replicas
cvrInfo, err := c.GetCVRs(cstortypes.PersistentVolumeLabelKey + "=" + vol.Name)
if err != nil {
cvrInfo, _ := c.GetCVRs(cstortypes.PersistentVolumeLabelKey + "=" + vol.Name)
if len(cvrInfo.Items) == 0 {
_, _ = fmt.Fprintf(os.Stderr, "failed to get cStor Volume Replicas for %s\n", vol.Name)
}

Expand All @@ -163,10 +163,7 @@ func DescribeCstorVolume(c *client.K8sClient, vol *corev1.PersistentVolume) erro
}

// Print the output for the portal status info
err = util.PrintByTemplate("volume", cstorVolInfoTemplate, volume)
if err != nil {
return err
}
_ = util.PrintByTemplate("volume", cstorVolInfoTemplate, volume)

portalInfo := util.PortalInfo{
IQN: volumeInfo.Spec.Iqn,
Expand All @@ -177,16 +174,13 @@ func DescribeCstorVolume(c *client.K8sClient, vol *corev1.PersistentVolume) erro
}

// Print the output for the portal status info
err = util.PrintByTemplate("PortalInfo", cstorPortalTemplate, portalInfo)
if err != nil {
return err
}
_ = util.PrintByTemplate("PortalInfo", cstorPortalTemplate, portalInfo)

replicaCount := volumeInfo.Spec.ReplicationFactor
// This case will occur only if user has manually specified zero replica.
// or if none of the replicas are healthy & running
if replicaCount == 0 || len(volumeInfo.Status.ReplicaStatuses) == 0 {
fmt.Printf("None of the replicas are running\n")
fmt.Printf("\nNone of the replicas are running\n")
//please check the volume pod's status by running [kubectl describe pvc -l=openebs/replica --all-namespaces]\Oor try again later.")
return nil
}
Expand All @@ -206,7 +200,7 @@ func DescribeCstorVolume(c *client.K8sClient, vol *corev1.PersistentVolume) erro
util.TablePrinter(util.CstorReplicaColumnDefinations, rows, printers.PrintOptions{Wide: true})
}

cStorBackupList, _ := c.GetCVBackups(vol.Name)
cStorBackupList, _ := c.GetCVBackups(cstortypes.PersistentVolumeLabelKey + "=" + vol.Name)
if cStorBackupList != nil {
fmt.Printf("\nCstor Backup Details :\n" + "---------------------\n")
var rows []metav1.TableRow
Expand All @@ -223,7 +217,7 @@ func DescribeCstorVolume(c *client.K8sClient, vol *corev1.PersistentVolume) erro
util.TablePrinter(util.CstorBackupColumnDefinations, rows, printers.PrintOptions{Wide: true})
}

cstorCompletedBackupList, _ := c.GetCVCompletedBackups(vol.Name)
cstorCompletedBackupList, _ := c.GetCVCompletedBackups(cstortypes.PersistentVolumeLabelKey + "=" + vol.Name)
if cstorCompletedBackupList != nil {
fmt.Printf("\nCstor Completed Backup Details :" + "\n-------------------------------\n")
var rows []metav1.TableRow
Expand All @@ -238,7 +232,7 @@ func DescribeCstorVolume(c *client.K8sClient, vol *corev1.PersistentVolume) erro
util.TablePrinter(util.CstorCompletedBackupColumnDefinations, rows, printers.PrintOptions{Wide: true})
}

cStorRestoreList, _ := c.GetCVRestores(vol.Name)
cStorRestoreList, _ := c.GetCVRestores(cstortypes.PersistentVolumeLabelKey + "=" + vol.Name)
if cStorRestoreList != nil {
fmt.Printf("\nCstor Restores Details :" + "\n-----------------------\n")
var rows []metav1.TableRow
Expand Down
Loading

0 comments on commit 3d90504

Please sign in to comment.