Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add unit-tests for dm-master/worker failover/scaler/upgrader and some other components #3326

Merged
merged 9 commits into from
Sep 28, 2020
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