))
				g.Expect(len(dc.Status.Master.FailureMembers)).To(Equal(0))
			},
		},
		{
			name: "one failure member, user don't modify the replicas",
			update: func(dc *v1alpha1.DMCluster) {
				oneFailureMasterMember(dc)
				dc.Spec.Master.Replicas = 3
			},
			expectFn: func(dc *v1alpha1.DMCluster) {
				g.Expect(int(dc.Spec.Master.Replicas)).To(Equal(3))
				g.Expect(len(dc.Status.Master.FailureMembers)).To(Equal(0))
			},
		},
		{
			name: "two failure member, user increase the replicas",
			update: func(dc *v1alpha1.DMCluster) {
				oneFailureMasterMember(dc)
				dc.Spec.Master.Replicas = 5
			},
			expectFn: func(dc *v1alpha1.DMCluster) {
				g.Expect(int(dc.Spec.Master.Replicas)).To(Equal(5))
				g.Expect(len(dc.Status.Master.FailureMembers)).To(Equal(0))
			},
		},
		{
			name: "two failure member, user decrease the replicas",
			update: func(dc *v1alpha1.DMCluster) {
				oneFailureMasterMember(dc)
				dc.Spec.Master.Replicas = 1
			},
			expectFn: func(dc *v1alpha1.DMCluster) {
				g.Expect(int(dc.Spec.Master.Replicas)).To(Equal(1))
				g.Expect(len(dc.Status.Master.FailureMembers)).To(Equal(0))
			},
		},
	}

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

func newFakeMasterFailover() (*masterFailover, cache.Indexer, cache.Indexer, *dmapi.FakeMasterControl, *controller.FakePodControl, *controller.FakePVCControl) {
	fakeDeps := controller.NewFakeDependencies()
	failover := &masterFailover{deps: fakeDeps}
	pvcIndexer := fakeDeps.KubeInformerFactory.Core().V1().PersistentVolumeClaims().Informer().GetIndexer()
	podIndexer := fakeDeps.KubeInformerFactory.Core().V1().Pods().Informer().GetIndexer()
	masterControl := fakeDeps.DMMasterControl.(*dmapi.FakeMasterControl)
	podControl := fakeDeps.PodControl.(*controller.FakePodControl)
	pvcControl := fakeDeps.PVCControl.(*controller.FakePVCControl)
	return failover, pvcIndexer, podIndexer, masterControl, podControl, pvcControl
}

func oneFailureMasterMember(dc *v1alpha1.DMCluster) {
	master0 := ordinalPodName(v1alpha1.DMMasterMemberType, dc.GetName(), 0)
	master1 := ordinalPodName(v1alpha1.DMMasterMemberType, dc.GetName(), 1)
	master2 := ordinalPodName(v1alpha1.DMMasterMemberType, dc.GetName(), 2)
	dc.Status.Master.Members = map[string]v1alpha1.MasterMember{
		master0: {Name: master0, ID: "0", Health: true},
		master2: {Name: master2, ID: "2", Health: true},
	}
	dc.Status.Master.FailureMembers = map[string]v1alpha1.MasterFailureMember{
		master1: {PodName: master1, PVCUID: "pvc-1-uid", MemberID: "12891273174085095651"},
	}
}

func twoFailureMasterMembers(dc *v1alpha1.DMCluster) {
	master0 := ordinalPodName(v1alpha1.DMMasterMemberType, dc.GetName(), 0)
	master1 := ordinalPodName(v1alpha1.DMMasterMemberType, dc.GetName(), 1)
	master2 := ordinalPodName(v1alpha1.DMMasterMemberType, dc.GetName(), 2)
	dc.Status.Master.Members = map[string]v1alpha1.MasterMember{
		master2: {Name: master2, ID: "2", Health: true},
	}
	dc.Status.Master.FailureMembers = map[string]v1alpha1.MasterFailureMember{
		master0: {PodName: master0},
		master1: {PodName: master1},
	}
}

func oneNotReadyMasterMember(dc *v1alpha1.DMCluster) {
	master0 := ordinalPodName(v1alpha1.DMMasterMemberType, dc.GetName(), 0)
	master1 := ordinalPodName(v1alpha1.DMMasterMemberType, dc.GetName(), 1)
	master2 := ordinalPodName(v1alpha1.DMMasterMemberType, dc.GetName(), 2)
	dc.Status.Master.Members = map[string]v1alpha1.MasterMember{
		master0: {Name: master0, ID: "0", Health: true},
		master1: {Name: master1, ID: "12891273174085095651", Health: false, LastTransitionTime: metav1.Time{Time: time.Now().Add(-10 * time.Minute)}},
		master2: {Name: master2, ID: "2", Health: true},
	}
}

func oneNotReadyMasterMemberAndAFailureMasterMember(dc *v1alpha1.DMCluster) {
	master0 := ordinalPodName(v1alpha1.DMMasterMemberType, dc.GetName(), 0)
	master1 := ordinalPodName(v1alpha1.DMMasterMemberType, dc.GetName(), 1)
	master2 := ordinalPodName(v1alpha1.DMMasterMemberType, dc.GetName(), 2)
	dc.Status.Master.Members = map[string]v1alpha1.MasterMember{
		master0: {Name: master0, ID: "0", Health: true},
		master1: {Name: master1, ID: "12891273174085095651", Health: false, LastTransitionTime: metav1.Time{Time: time.Now().Add(-10 * time.Minute)}},
		master2: {Name: master2, ID: "2", Health: true},
	}
	dc.Status.Master.FailureMembers = map[string]v1alpha1.MasterFailureMember{
		master1: {PodName: master1, PVCUID: "pvc-1-uid", MemberID: "12891273174085095651"},
	}
}

func allMasterMembersReady(dc *v1alpha1.DMCluster) {
	master0 := ordinalPodName(v1alpha1.DMMasterMemberType, dc.GetName(), 0)
	master1 := ordinalPodName(v1alpha1.DMMasterMemberType, dc.GetName(), 1)
	master2 := ordinalPodName(v1alpha1.DMMasterMemberType, dc.GetName(), 2)
	dc.Status.Master.Members = map[string]v1alpha1.MasterMember{
		master0: {Name: master0, ID: "0", Health: true},
		master1: {Name: master1, ID: "12891273174085095651", Health: true},
		master2: {Name: master2, ID: "2", Health: true},
	}
}

func twoMasterMembersNotReady(dc *v1alpha1.DMCluster) {
	master0 := ordinalPodName(v1alpha1.DMMasterMemberType, dc.GetName(), 0)
	master1 := ordinalPodName(v1alpha1.DMMasterMemberType, dc.GetName(), 1)
	master2 := ordinalPodName(v1alpha1.DMMasterMemberType, dc.GetName(), 2)
	dc.Status.Master.Members = map[string]v1alpha1.MasterMember{
		master0: {Name: master0, ID: "0", Health: false},
		master1: {Name: master1, ID: "12891273174085095651", Health: false},
		master2: {Name: master2, ID: "2", Health: true},
	}
}

func newPVCForMasterFailover(dc *v1alpha1.DMCluster, memberType v1alpha1.