Skip to content

Commit

Permalink
Add unit-tests for dm-master/worker failover/scaler/upgrader and some…
Browse files Browse the repository at this point in the history
… other components (pingcap#3326)

* add UT for dmcluster configurations and dmapi
  • Loading branch information
lichunzhu authored and cvvz committed Oct 18, 2020
1 parent ba2f812 commit 8b968d2
Show file tree
Hide file tree
Showing 15 changed files with 2,407 additions and 44 deletions.
6 changes: 6 additions & 0 deletions pkg/controller/dmmaster_control.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,9 @@ func NewFakeMasterClient(dmControl *dmapi.FakeMasterControl, dc *v1alpha1.DMClus
dmControl.SetMasterClient(dc.GetNamespace(), dc.GetName(), masterClient)
return masterClient
}

func NewFakeMasterPeerClient(dmControl *dmapi.FakeMasterControl, dc *v1alpha1.DMCluster, podName string) *dmapi.FakeMasterClient {
masterClient := dmapi.NewFakeMasterClient()
dmControl.SetMasterPeerClient(dc.GetNamespace(), dc.GetName(), podName, masterClient)
return masterClient
}
24 changes: 21 additions & 3 deletions pkg/dmapi/master_control.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,10 @@ func masterClientKey(scheme, namespace, clusterName string) string {
return fmt.Sprintf("%s.%s.%s", scheme, clusterName, namespace)
}

func masterPeerClientKey(schema, namespace, clusterName, podName string) string {
return fmt.Sprintf("%s.%s.%s.%s", schema, clusterName, namespace, podName)
}

// MasterClientURL builds the url of master client
func MasterClientURL(namespace, clusterName, scheme string) string {
return fmt.Sprintf("%s://%s-dm-master.%s:8261", scheme, clusterName, namespace)
Expand All @@ -111,14 +115,28 @@ func MasterPeerClientURL(namespace, clusterName, podName, scheme string) string
// FakeMasterControl implements a fake version of MasterControlInterface.
type FakeMasterControl struct {
defaultMasterControl
masterPeerClients map[string]MasterClient
}

func NewFakeMasterControl(kubeCli kubernetes.Interface) *FakeMasterControl {
return &FakeMasterControl{
defaultMasterControl{kubeCli: kubeCli, masterClients: map[string]MasterClient{}},
defaultMasterControl: defaultMasterControl{kubeCli: kubeCli, masterClients: map[string]MasterClient{}},
masterPeerClients: map[string]MasterClient{},
}
}

func (fmc *FakeMasterControl) SetMasterClient(namespace string, tcName string, masterClient MasterClient) {
fmc.defaultMasterControl.masterClients[masterClientKey("http", namespace, tcName)] = masterClient
func (fmc *FakeMasterControl) SetMasterClient(namespace, dcName string, masterClient MasterClient) {
fmc.defaultMasterControl.masterClients[masterClientKey("http", namespace, dcName)] = masterClient
}

func (fmc *FakeMasterControl) SetMasterPeerClient(namespace, dcName, podName string, masterPeerClient MasterClient) {
fmc.masterPeerClients[masterPeerClientKey("http", namespace, dcName, podName)] = masterPeerClient
}

func (fmc *FakeMasterControl) GetMasterClient(namespace string, dcName string, tlsEnabled bool) MasterClient {
return fmc.defaultMasterControl.GetMasterClient(namespace, dcName, tlsEnabled)
}

func (fmc *FakeMasterControl) GetMasterPeerClient(namespace, dcName, podName string, tlsEnabled bool) MasterClient {
return fmc.masterPeerClients[masterPeerClientKey("http", namespace, dcName, podName)]
}
2 changes: 1 addition & 1 deletion pkg/manager/member/dm_master_failover.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func (mf *masterFailover) Failover(dc *v1alpha1.DMCluster) error {
}
inQuorum := healthCount > len(dc.Status.Master.Members)/2
if !inQuorum {
return fmt.Errorf("DMCluster: %s/%s's dm-master cluster is not health: %d/%d, "+
return fmt.Errorf("DMCluster: %s/%s's dm-master cluster is not healthy: %d/%d, "+
"replicas: %d, failureCount: %d, can't failover",
ns, dcName, healthCount, dc.MasterStsDesiredReplicas(), dc.Spec.Master.Replicas, len(dc.Status.Master.FailureMembers))
}
Expand Down
Loading

0 comments on commit 8b968d2

Please sign in to comment.