Added Gomock and one test case with mock.
This commit is contained in:
		
							parent
							
								
									9e095e49d8
								
							
						
					
					
						commit
						b42013455f
					
				
							
								
								
									
										2
									
								
								go.mod
								
								
								
								
							
							
						
						
									
										2
									
								
								go.mod
								
								
								
								
							| 
						 | 
					@ -18,4 +18,6 @@ require (
 | 
				
			||||||
	k8s.io/apimachinery v0.19.3
 | 
						k8s.io/apimachinery v0.19.3
 | 
				
			||||||
	k8s.io/client-go v0.19.3
 | 
						k8s.io/client-go v0.19.3
 | 
				
			||||||
	k8s.io/code-generator v0.19.3
 | 
						k8s.io/code-generator v0.19.3
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						github.com/golang/mock v1.4.4
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
							
								
								
									
										5
									
								
								go.sum
								
								
								
								
							
							
						
						
									
										5
									
								
								go.sum
								
								
								
								
							| 
						 | 
					@ -165,9 +165,8 @@ github.com/golang/groupcache v0.0.0-20190129154638-5b532d6fd5ef/go.mod h1:cIg4er
 | 
				
			||||||
github.com/golang/groupcache v0.0.0-20190702054246-869f871628b6/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc=
 | 
					github.com/golang/groupcache v0.0.0-20190702054246-869f871628b6/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc=
 | 
				
			||||||
github.com/golang/groupcache v0.0.0-20191227052852-215e87163ea7 h1:5ZkaAPbicIKTF2I64qf5Fh8Aa83Q/dnOafMYV0OMwjA=
 | 
					github.com/golang/groupcache v0.0.0-20191227052852-215e87163ea7 h1:5ZkaAPbicIKTF2I64qf5Fh8Aa83Q/dnOafMYV0OMwjA=
 | 
				
			||||||
github.com/golang/groupcache v0.0.0-20191227052852-215e87163ea7/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc=
 | 
					github.com/golang/groupcache v0.0.0-20191227052852-215e87163ea7/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc=
 | 
				
			||||||
github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A=
 | 
					github.com/golang/mock v1.4.4 h1:l75CXGRSwbaYNpl/Z2X1XIIAMSCquvXgpVZDhwEIJsc=
 | 
				
			||||||
github.com/golang/mock v1.2.0/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A=
 | 
					github.com/golang/mock v1.4.4/go.mod h1:l3mdAwkq5BuhzHwde/uurv3sEJeZMXNpwsxVWU71h+4=
 | 
				
			||||||
github.com/golang/mock v1.3.1/go.mod h1:sBzyDLLjw3U8JLTeZvSv8jJB+tU5PVekmnlKIyFUx0Y=
 | 
					 | 
				
			||||||
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
 | 
					github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
 | 
				
			||||||
github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
 | 
					github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
 | 
				
			||||||
github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
 | 
					github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -271,7 +271,7 @@ func (c *Cluster) executeEBSMigration() error {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	for _, volume := range awsVolumes {
 | 
						for _, volume := range awsVolumes {
 | 
				
			||||||
		if volume.VolumeType == "gp2" && volume.Size < c.OpConfig.EnableEBSGp3MaxSize {
 | 
							if volume.VolumeType == "gp2" && volume.Size < c.OpConfig.EnableEBSGp3MaxSize {
 | 
				
			||||||
			c.logger.Info("Modifying EBS volume %s to type gp3 (%s)", volume.VolumeID, volume.Size)
 | 
								c.logger.Infof("Modifying EBS volume %s to type gp3 (%d)", volume.VolumeID, volume.Size)
 | 
				
			||||||
			c.VolumeResizer.ModifyVolume(volume.VolumeID, "gp3", volume.Size, 3000, 125)
 | 
								c.VolumeResizer.ModifyVolume(volume.VolumeID, "gp3", volume.Size, 3000, 125)
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		c.EBSVolumes[volume.VolumeID] = volume
 | 
							c.EBSVolumes[volume.VolumeID] = volume
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1,6 +1,7 @@
 | 
				
			||||||
package cluster
 | 
					package cluster
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import (
 | 
					import (
 | 
				
			||||||
 | 
						"fmt"
 | 
				
			||||||
	"testing"
 | 
						"testing"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	"context"
 | 
						"context"
 | 
				
			||||||
| 
						 | 
					@ -10,11 +11,13 @@ import (
 | 
				
			||||||
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
 | 
						metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
 | 
				
			||||||
	"k8s.io/apimachinery/pkg/labels"
 | 
						"k8s.io/apimachinery/pkg/labels"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						"github.com/golang/mock/gomock"
 | 
				
			||||||
	"github.com/stretchr/testify/assert"
 | 
						"github.com/stretchr/testify/assert"
 | 
				
			||||||
	acidv1 "github.com/zalando/postgres-operator/pkg/apis/acid.zalan.do/v1"
 | 
						acidv1 "github.com/zalando/postgres-operator/pkg/apis/acid.zalan.do/v1"
 | 
				
			||||||
	"github.com/zalando/postgres-operator/pkg/util/config"
 | 
						"github.com/zalando/postgres-operator/pkg/util/config"
 | 
				
			||||||
	"github.com/zalando/postgres-operator/pkg/util/constants"
 | 
						"github.com/zalando/postgres-operator/pkg/util/constants"
 | 
				
			||||||
	"github.com/zalando/postgres-operator/pkg/util/k8sutil"
 | 
						"github.com/zalando/postgres-operator/pkg/util/k8sutil"
 | 
				
			||||||
 | 
						"github.com/zalando/postgres-operator/pkg/util/volumes/mocks"
 | 
				
			||||||
	"k8s.io/client-go/kubernetes/fake"
 | 
						"k8s.io/client-go/kubernetes/fake"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -23,6 +26,8 @@ func NewFakeKubernetesClient() (k8sutil.KubernetesClient, *fake.Clientset) {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	return k8sutil.KubernetesClient{
 | 
						return k8sutil.KubernetesClient{
 | 
				
			||||||
		PersistentVolumeClaimsGetter: clientSet.CoreV1(),
 | 
							PersistentVolumeClaimsGetter: clientSet.CoreV1(),
 | 
				
			||||||
 | 
							PersistentVolumesGetter:      clientSet.CoreV1(),
 | 
				
			||||||
 | 
							PodsGetter:                   clientSet.CoreV1(),
 | 
				
			||||||
	}, clientSet
 | 
						}, clientSet
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -169,3 +174,99 @@ func TestQuantityToGigabyte(t *testing.T) {
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func CreatePVCs(namespace string, clusterName string, labels labels.Set, n int, size string) v1.PersistentVolumeClaimList {
 | 
				
			||||||
 | 
						// define and create PVCs for 1Gi volumes
 | 
				
			||||||
 | 
						storage1Gi, _ := resource.ParseQuantity("1Gi")
 | 
				
			||||||
 | 
						pvcList := v1.PersistentVolumeClaimList{
 | 
				
			||||||
 | 
							Items: []v1.PersistentVolumeClaim{},
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						for i := 0; i <= n; i++ {
 | 
				
			||||||
 | 
							pvc := v1.PersistentVolumeClaim{
 | 
				
			||||||
 | 
								ObjectMeta: metav1.ObjectMeta{
 | 
				
			||||||
 | 
									Name:      fmt.Sprintf("%s-%s-%d", constants.DataVolumeName, clusterName, i),
 | 
				
			||||||
 | 
									Namespace: namespace,
 | 
				
			||||||
 | 
									Labels:    labels,
 | 
				
			||||||
 | 
								},
 | 
				
			||||||
 | 
								Spec: v1.PersistentVolumeClaimSpec{
 | 
				
			||||||
 | 
									Resources: v1.ResourceRequirements{
 | 
				
			||||||
 | 
										Requests: v1.ResourceList{
 | 
				
			||||||
 | 
											v1.ResourceStorage: storage1Gi,
 | 
				
			||||||
 | 
										},
 | 
				
			||||||
 | 
									},
 | 
				
			||||||
 | 
									VolumeName: fmt.Sprintf("persistent-volume-%d", i),
 | 
				
			||||||
 | 
								},
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							pvcList.Items = append(pvcList.Items, pvc)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						return pvcList
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func TestMigrateEBS(t *testing.T) {
 | 
				
			||||||
 | 
						client, _ := NewFakeKubernetesClient()
 | 
				
			||||||
 | 
						clusterName := "acid-test-cluster"
 | 
				
			||||||
 | 
						namespace := "default"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// new cluster with pvc storage resize mode and configured labels
 | 
				
			||||||
 | 
						var cluster = New(
 | 
				
			||||||
 | 
							Config{
 | 
				
			||||||
 | 
								OpConfig: config.Config{
 | 
				
			||||||
 | 
									Resources: config.Resources{
 | 
				
			||||||
 | 
										ClusterLabels:    map[string]string{"application": "spilo"},
 | 
				
			||||||
 | 
										ClusterNameLabel: "cluster-name",
 | 
				
			||||||
 | 
									},
 | 
				
			||||||
 | 
									StorageResizeMode:     "pvc",
 | 
				
			||||||
 | 
									EnableEBSGp3Migration: true,
 | 
				
			||||||
 | 
								},
 | 
				
			||||||
 | 
							}, client, acidv1.Postgresql{}, logger, eventRecorder)
 | 
				
			||||||
 | 
						cluster.Spec.Volume.Size = "1Gi"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// set metadata, so that labels will get correct values
 | 
				
			||||||
 | 
						cluster.Name = clusterName
 | 
				
			||||||
 | 
						cluster.Namespace = namespace
 | 
				
			||||||
 | 
						filterLabels := cluster.labelsSet(false)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						pvcList := CreatePVCs(namespace, clusterName, filterLabels, 2, "1Gi")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						ps := v1.PersistentVolumeSpec{}
 | 
				
			||||||
 | 
						ps.AWSElasticBlockStore = &v1.AWSElasticBlockStoreVolumeSource{}
 | 
				
			||||||
 | 
						ps.AWSElasticBlockStore.VolumeID = "vol-1111"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						pvList := &v1.PersistentVolumeList{
 | 
				
			||||||
 | 
							Items: []v1.PersistentVolume{
 | 
				
			||||||
 | 
								{
 | 
				
			||||||
 | 
									ObjectMeta: metav1.ObjectMeta{
 | 
				
			||||||
 | 
										Name: "persistent-volume-0",
 | 
				
			||||||
 | 
									},
 | 
				
			||||||
 | 
									Spec: ps,
 | 
				
			||||||
 | 
								},
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						for _, pvc := range pvcList.Items {
 | 
				
			||||||
 | 
							cluster.KubeClient.PersistentVolumeClaims(namespace).Create(context.TODO(), &pvc, metav1.CreateOptions{})
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						cluster.KubeClient.PersistentVolumes().Create(context.TODO(), &pvList.Items[0], metav1.CreateOptions{})
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						pod := v1.Pod{
 | 
				
			||||||
 | 
							ObjectMeta: metav1.ObjectMeta{
 | 
				
			||||||
 | 
								Name:   clusterName + "-0",
 | 
				
			||||||
 | 
								Labels: filterLabels,
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
							Spec: v1.PodSpec{},
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						cluster.KubeClient.Pods(namespace).Create(context.TODO(), &pod, metav1.CreateOptions{})
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						ctrl := gomock.NewController(t)
 | 
				
			||||||
 | 
						defer ctrl.Finish()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						resizer := mocks.NewMockVolumeResizer(ctrl)
 | 
				
			||||||
 | 
						resizer.EXPECT().DescribeVolumes(gomock.Eq([]string{"vol-1111"})).Return(nil, nil)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						cluster.VolumeResizer = resizer
 | 
				
			||||||
 | 
						cluster.executeEBSMigration()
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1,5 +1,7 @@
 | 
				
			||||||
package volumes
 | 
					package volumes
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					//go:generate go run -mod=mod github.com/golang/mock/mockgen -package mocks -destination=$PWD/mocks/$GOFILE -source=$GOFILE -build_flags=-mod=mod
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import v1 "k8s.io/api/core/v1"
 | 
					import v1 "k8s.io/api/core/v1"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// VolumeProperties ...
 | 
					// VolumeProperties ...
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue