Skip to content

Commit

Permalink
add ut
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielZhangQD committed Jun 16, 2020
1 parent 713caf5 commit e07a472
Show file tree
Hide file tree
Showing 4 changed files with 184 additions and 6 deletions.
36 changes: 36 additions & 0 deletions pkg/manager/member/tikv_member_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -910,6 +910,42 @@ func TestTiKVMemberManagerSyncTidbClusterStatus(t *testing.T) {
g.Expect(tc.Status.TiKV.Phase).To(Equal(v1alpha1.NormalPhase))
},
},
{
name: "statefulset is scaling out",
updateTC: func(tc *v1alpha1.TidbCluster) {
tc.Status.TiKV.Phase = v1alpha1.ScaleOutPhase
},
upgradingFn: func(lister corelisters.PodLister, controlInterface pdapi.PDControlInterface, set *apps.StatefulSet, cluster *v1alpha1.TidbCluster) (bool, error) {
return false, nil
},
errWhenGetStores: false,
storeInfo: nil,
errWhenGetTombstoneStores: false,
tombstoneStoreInfo: nil,
errExpectFn: nil,
tcExpectFn: func(g *GomegaWithT, tc *v1alpha1.TidbCluster) {
g.Expect(tc.Status.TiKV.StatefulSet.Replicas).To(Equal(int32(3)))
g.Expect(tc.Status.TiKV.Phase).To(Equal(v1alpha1.ScaleOutPhase))
},
},
{
name: "statefulset is scaling in",
updateTC: func(tc *v1alpha1.TidbCluster) {
tc.Status.TiKV.Phase = v1alpha1.ScaleInPhase
},
upgradingFn: func(lister corelisters.PodLister, controlInterface pdapi.PDControlInterface, set *apps.StatefulSet, cluster *v1alpha1.TidbCluster) (bool, error) {
return false, nil
},
errWhenGetStores: false,
storeInfo: nil,
errWhenGetTombstoneStores: false,
tombstoneStoreInfo: nil,
errExpectFn: nil,
tcExpectFn: func(g *GomegaWithT, tc *v1alpha1.TidbCluster) {
g.Expect(tc.Status.TiKV.StatefulSet.Replicas).To(Equal(int32(3)))
g.Expect(tc.Status.TiKV.Phase).To(Equal(v1alpha1.ScaleInPhase))
},
},
{
name: "get stores failed",
updateTC: nil,
Expand Down
10 changes: 5 additions & 5 deletions pkg/manager/member/tikv_scaler.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,13 @@ func (tsd *tikvScaler) ScaleOut(tc *v1alpha1.TidbCluster, oldSet *apps.StatefulS
return nil
}

tc.Status.TiKV.Phase = v1alpha1.ScaleOutPhase

klog.Infof("scaling out tikv statefulset %s/%s, ordinal: %d (replicas: %d, delete slots: %v)", oldSet.Namespace, oldSet.Name, ordinal, replicas, deleteSlots.List())
_, err := tsd.deleteDeferDeletingPVC(tc, oldSet.GetName(), v1alpha1.TiKVMemberType, ordinal)
if err != nil {
return err
}

tc.Status.TiKV.Phase = v1alpha1.ScaleOutPhase
setReplicasAndDeleteSlots(newSet, replicas, deleteSlots)
return nil
}
Expand Down Expand Up @@ -114,8 +113,6 @@ func (tsd *tikvScaler) ScaleIn(tc *v1alpha1.TidbCluster, oldSet *apps.StatefulSe
return nil
}

tc.Status.TiKV.Phase = v1alpha1.ScaleInPhase

klog.Infof("scaling in tikv statefulset %s/%s, ordinal: %d (replicas: %d, delete slots: %v)", oldSet.Namespace, oldSet.Name, ordinal, replicas, deleteSlots.List())
// We need remove member from cluster before reducing statefulset replicas
podName := ordinalPodName(v1alpha1.TiKVMemberType, tcName, ordinal)
Expand All @@ -125,6 +122,7 @@ func (tsd *tikvScaler) ScaleIn(tc *v1alpha1.TidbCluster, oldSet *apps.StatefulSe
}

if controller.PodWebhookEnabled {
tc.Status.TiKV.Phase = v1alpha1.ScaleInPhase
setReplicasAndDeleteSlots(newSet, replicas, deleteSlots)
return nil
}
Expand All @@ -137,6 +135,7 @@ func (tsd *tikvScaler) ScaleIn(tc *v1alpha1.TidbCluster, oldSet *apps.StatefulSe
return err
}
if state != v1alpha1.TiKVStateOffline {
tc.Status.TiKV.Phase = v1alpha1.ScaleInPhase
if err := controller.GetPDClient(tsd.pdControl, tc).DeleteStore(id); err != nil {
klog.Errorf("tikv scale in: failed to delete store %d, %v", id, err)
return err
Expand Down Expand Up @@ -174,7 +173,7 @@ func (tsd *tikvScaler) ScaleIn(tc *v1alpha1.TidbCluster, oldSet *apps.StatefulSe
}
klog.Infof("tikv scale in: set pvc %s/%s annotation: %s to %s",
ns, pvcName, label.AnnPVCDeferDeleting, now)

tc.Status.TiKV.Phase = v1alpha1.ScaleInPhase
setReplicasAndDeleteSlots(newSet, replicas, deleteSlots)
return nil
}
Expand Down Expand Up @@ -217,6 +216,7 @@ func (tsd *tikvScaler) ScaleIn(tc *v1alpha1.TidbCluster, oldSet *apps.StatefulSe
}
klog.Infof("pod %s not ready, tikv scale in: set pvc %s/%s annotation: %s to %s",
podName, ns, pvcName, label.AnnPVCDeferDeleting, now)
tc.Status.TiKV.Phase = v1alpha1.ScaleInPhase
setReplicasAndDeleteSlots(newSet, replicas, deleteSlots)
return nil
}
Expand Down
100 changes: 99 additions & 1 deletion pkg/manager/member/tikv_scaler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,70 @@ import (
"k8s.io/client-go/tools/cache"
)

func TestTiKVScalerScale(t *testing.T) {
g := NewGomegaWithT(t)
type testcase struct {
name string
tikvPhase v1alpha1.MemberPhase
expectPhase v1alpha1.MemberPhase
}

testFn := func(test *testcase, t *testing.T) {
t.Log(test.name)
tc := newTidbClusterForPD()

tc.Status.TiKV.Phase = test.tikvPhase

oldSet := newStatefulSetForPDScale()
newSet := oldSet.DeepCopy()

scaler, _, _, _, _ := newFakeTiKVScaler()

_ = scaler.Scale(tc, oldSet, newSet)
g.Expect(tc.Status.TiKV.Phase).To(Equal(test.expectPhase))
}

tests := []testcase{
{
name: "normal",
tikvPhase: v1alpha1.NormalPhase,
expectPhase: v1alpha1.NormalPhase,
},
{
name: "upgrade",
tikvPhase: v1alpha1.UpgradePhase,
expectPhase: v1alpha1.UpgradePhase,
},
{
name: "scale in",
tikvPhase: v1alpha1.ScaleInPhase,
expectPhase: v1alpha1.NormalPhase,
},
{
name: "scale out",
tikvPhase: v1alpha1.ScaleOutPhase,
expectPhase: v1alpha1.NormalPhase,
},
}

for i := range tests {
testFn(&tests[i], t)
}
}

func TestTiKVScalerScaleOut(t *testing.T) {
g := NewGomegaWithT(t)
type testcase struct {
name string
tikvUpgrading bool
pdUpgrading bool
hasPVC bool
hasDeferAnn bool
pvcDeleteErr bool
annoIsNil bool
errExpectFn func(*GomegaWithT, error)
changed bool
tikvPhase v1alpha1.MemberPhase
}

testFn := func(test *testcase, t *testing.T) {
Expand All @@ -51,6 +104,9 @@ func TestTiKVScalerScaleOut(t *testing.T) {
if test.tikvUpgrading {
tc.Status.TiKV.Phase = v1alpha1.UpgradePhase
}
if test.pdUpgrading {
tc.Status.PD.Phase = v1alpha1.UpgradePhase
}

oldSet := newStatefulSetForPDScale()
newSet := oldSet.DeepCopy()
Expand Down Expand Up @@ -78,6 +134,7 @@ func TestTiKVScalerScaleOut(t *testing.T) {

err := scaler.ScaleOut(tc, oldSet, newSet)
test.errExpectFn(g, err)
g.Expect(tc.Status.TiKV.Phase).To(Equal(test.tikvPhase))
if test.changed {
g.Expect(int(*newSet.Spec.Replicas)).To(Equal(6))
} else {
Expand All @@ -95,6 +152,7 @@ func TestTiKVScalerScaleOut(t *testing.T) {
pvcDeleteErr: false,
errExpectFn: errExpectNil,
changed: true,
tikvPhase: v1alpha1.ScaleOutPhase,
},
{
name: "tikv is upgrading",
Expand All @@ -105,6 +163,18 @@ func TestTiKVScalerScaleOut(t *testing.T) {
pvcDeleteErr: false,
errExpectFn: errExpectNil,
changed: false,
tikvPhase: v1alpha1.UpgradePhase,
},
{
name: "pd is upgrading",
tikvUpgrading: false,
pdUpgrading: true,
hasPVC: true,
hasDeferAnn: false,
annoIsNil: true,
pvcDeleteErr: false,
errExpectFn: errExpectNil,
changed: false,
},
{
name: "cache don't have pvc",
Expand All @@ -115,6 +185,7 @@ func TestTiKVScalerScaleOut(t *testing.T) {
pvcDeleteErr: false,
errExpectFn: errExpectNil,
changed: true,
tikvPhase: v1alpha1.ScaleOutPhase,
},
{
name: "pvc annotation is not nil but doesn't contain defer deletion annotation",
Expand All @@ -125,6 +196,7 @@ func TestTiKVScalerScaleOut(t *testing.T) {
pvcDeleteErr: false,
errExpectFn: errExpectNil,
changed: true,
tikvPhase: v1alpha1.ScaleOutPhase,
},
{
name: "pvc annotations defer deletion is not nil, pvc delete failed",
Expand All @@ -147,6 +219,7 @@ func TestTiKVScalerScaleIn(t *testing.T) {
type testcase struct {
name string
tikvUpgrading bool
pdUpgrading bool
storeFun func(tc *v1alpha1.TidbCluster)
delStoreErr bool
hasPVC bool
Expand All @@ -156,6 +229,7 @@ func TestTiKVScalerScaleIn(t *testing.T) {
pvcUpdateErr bool
errExpectFn func(*GomegaWithT, error)
changed bool
tikvPhase v1alpha1.MemberPhase
}

controller.ResyncDuration = 0
Expand All @@ -168,6 +242,9 @@ func TestTiKVScalerScaleIn(t *testing.T) {
if test.tikvUpgrading {
tc.Status.TiKV.Phase = v1alpha1.UpgradePhase
}
if test.pdUpgrading {
tc.Status.PD.Phase = v1alpha1.UpgradePhase
}

oldSet := newStatefulSetForPDScale()
newSet := oldSet.DeepCopy()
Expand Down Expand Up @@ -217,6 +294,7 @@ func TestTiKVScalerScaleIn(t *testing.T) {

err := scaler.ScaleIn(tc, oldSet, newSet)
test.errExpectFn(g, err)
g.Expect(tc.Status.TiKV.Phase).To(Equal(test.tikvPhase))
if test.changed {
g.Expect(int(*newSet.Spec.Replicas)).To(Equal(4))
} else {
Expand All @@ -229,14 +307,15 @@ func TestTiKVScalerScaleIn(t *testing.T) {
name: "store is up, delete store failed",
tikvUpgrading: false,
storeFun: normalStoreFun,
delStoreErr: false,
delStoreErr: true,
hasPVC: true,
storeIDSynced: true,
isPodReady: true,
hasSynced: true,
pvcUpdateErr: false,
errExpectFn: errExpectNotNil,
changed: false,
tikvPhase: v1alpha1.ScaleInPhase,
},
{
name: "store state is up, delete store success",
Expand All @@ -250,6 +329,7 @@ func TestTiKVScalerScaleIn(t *testing.T) {
pvcUpdateErr: false,
errExpectFn: errExpectRequeue,
changed: false,
tikvPhase: v1alpha1.ScaleInPhase,
},
{
name: "tikv is upgrading",
Expand All @@ -263,6 +343,21 @@ func TestTiKVScalerScaleIn(t *testing.T) {
pvcUpdateErr: false,
errExpectFn: errExpectNil,
changed: false,
tikvPhase: v1alpha1.UpgradePhase,
},
{
name: "pd is upgrading",
tikvUpgrading: false,
pdUpgrading: true,
storeFun: normalStoreFun,
delStoreErr: false,
hasPVC: true,
storeIDSynced: true,
isPodReady: true,
hasSynced: true,
pvcUpdateErr: false,
errExpectFn: errExpectNil,
changed: false,
},
{
name: "status.TiKV.Stores is empty",
Expand Down Expand Up @@ -308,6 +403,7 @@ func TestTiKVScalerScaleIn(t *testing.T) {
pvcUpdateErr: false,
errExpectFn: errExpectNil,
changed: true,
tikvPhase: v1alpha1.ScaleInPhase,
},
{
name: "podName not match",
Expand Down Expand Up @@ -375,6 +471,7 @@ func TestTiKVScalerScaleIn(t *testing.T) {
pvcUpdateErr: false,
errExpectFn: errExpectNil,
changed: true,
tikvPhase: v1alpha1.ScaleInPhase,
},
{
name: "store state is tombstone and store id not match",
Expand All @@ -388,6 +485,7 @@ func TestTiKVScalerScaleIn(t *testing.T) {
pvcUpdateErr: false,
errExpectFn: errExpectNotNil,
changed: false,
tikvPhase: v1alpha1.ScaleInPhase,
},
{
name: "store state is tombstone, id is not integer",
Expand Down
44 changes: 44 additions & 0 deletions pkg/manager/member/tikv_upgrader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,50 @@ func TestTiKVUpgraderUpgrade(t *testing.T) {
g.Expect(*newSet.Spec.UpdateStrategy.RollingUpdate.Partition).To(Equal(int32(3)))
},
},
{
name: "tikv can not upgrade when it is scaling in",
changeFn: func(tc *v1alpha1.TidbCluster) {
tc.Status.PD.Phase = v1alpha1.NormalPhase
tc.Status.TiKV.Phase = v1alpha1.ScaleInPhase
tc.Status.TiKV.Synced = true
},
changeOldSet: func(oldSet *apps.StatefulSet) {
SetStatefulSetLastAppliedConfigAnnotation(oldSet)
},
changePods: nil,
beginEvictLeaderErr: false,
endEvictLeaderErr: false,
updatePodErr: false,
errExpectFn: func(g *GomegaWithT, err error) {
g.Expect(err).NotTo(HaveOccurred())
},
expectFn: func(g *GomegaWithT, tc *v1alpha1.TidbCluster, newSet *apps.StatefulSet, pods map[string]*corev1.Pod) {
g.Expect(tc.Status.TiKV.Phase).To(Equal(v1alpha1.ScaleInPhase))
g.Expect(*newSet.Spec.UpdateStrategy.RollingUpdate.Partition).To(Equal(int32(3)))
},
},
{
name: "tikv can not upgrade when it is scaling out",
changeFn: func(tc *v1alpha1.TidbCluster) {
tc.Status.PD.Phase = v1alpha1.NormalPhase
tc.Status.TiKV.Phase = v1alpha1.ScaleOutPhase
tc.Status.TiKV.Synced = true
},
changeOldSet: func(oldSet *apps.StatefulSet) {
SetStatefulSetLastAppliedConfigAnnotation(oldSet)
},
changePods: nil,
beginEvictLeaderErr: false,
endEvictLeaderErr: false,
updatePodErr: false,
errExpectFn: func(g *GomegaWithT, err error) {
g.Expect(err).NotTo(HaveOccurred())
},
expectFn: func(g *GomegaWithT, tc *v1alpha1.TidbCluster, newSet *apps.StatefulSet, pods map[string]*corev1.Pod) {
g.Expect(tc.Status.TiKV.Phase).To(Equal(v1alpha1.ScaleOutPhase))
g.Expect(*newSet.Spec.UpdateStrategy.RollingUpdate.Partition).To(Equal(int32(3)))
},
},
{
name: "get last apply config error",
changeFn: func(tc *v1alpha1.TidbCluster) {
Expand Down

0 comments on commit e07a472

Please sign in to comment.