Skip to content

Commit

Permalink
unit tests for hostnetwork
Browse files Browse the repository at this point in the history
  • Loading branch information
cofyc committed Nov 1, 2019
1 parent 02e166e commit a58fbba
Show file tree
Hide file tree
Showing 7 changed files with 285 additions and 13 deletions.
6 changes: 3 additions & 3 deletions pkg/manager/member/pd_member_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/intstr"
"k8s.io/apimachinery/pkg/util/uuid"
"k8s.io/client-go/listers/apps/v1"
v1 "k8s.io/client-go/listers/apps/v1"
corelisters "k8s.io/client-go/listers/core/v1"
glog "k8s.io/klog"
)
Expand Down Expand Up @@ -174,7 +174,7 @@ func (pmm *pdMemberManager) syncPDStatefulSetForTidbCluster(tc *v1alpha1.TidbClu
ns := tc.GetNamespace()
tcName := tc.GetName()

newPDSet, err := pmm.getNewPDSetForTidbCluster(tc)
newPDSet, err := getNewPDSetForTidbCluster(tc)
if err != nil {
return err
}
Expand Down Expand Up @@ -426,7 +426,7 @@ func (pmm *pdMemberManager) pdStatefulSetIsUpgrading(set *apps.StatefulSet, tc *
return false, nil
}

func (pmm *pdMemberManager) getNewPDSetForTidbCluster(tc *v1alpha1.TidbCluster) (*apps.StatefulSet, error) {
func getNewPDSetForTidbCluster(tc *v1alpha1.TidbCluster) (*apps.StatefulSet, error) {
ns := tc.Namespace
tcName := tc.Name
instanceName := tc.GetLabels()[label.InstanceLabelKey]
Expand Down
96 changes: 95 additions & 1 deletion pkg/manager/member/pd_member_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"github.com/pingcap/tidb-operator/pkg/pdapi"
apps "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
Expand Down Expand Up @@ -913,8 +914,101 @@ func TestGetNewPDHeadlessServiceForTidbCluster(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
svc := getNewPDHeadlessServiceForTidbCluster(&tt.tc)
if diff := cmp.Diff(tt.expected, *svc); diff != "" {
t.Errorf("unexpected plugin configuration (-want, +got): %s", diff)
t.Errorf("unexpected Service (-want, +got): %s", diff)
}
})
}
}

func testHostNetwork(t *testing.T, hostNetwork bool, dnsPolicy v1.DNSPolicy) func(sts *apps.StatefulSet) {
return func(sts *apps.StatefulSet) {
if hostNetwork != sts.Spec.Template.Spec.HostNetwork {
t.Errorf("unexpected hostNetwork %v, want %v", sts.Spec.Template.Spec.HostNetwork, hostNetwork)
}
if dnsPolicy != sts.Spec.Template.Spec.DNSPolicy {
t.Errorf("unexpected dnsPolicy %v, want %v", sts.Spec.Template.Spec.DNSPolicy, dnsPolicy)
}
}
}

func TestGetNewPDSetForTidbCluster(t *testing.T) {
tests := []struct {
name string
tc v1alpha1.TidbCluster
wantErr bool
testSts func(sts *apps.StatefulSet)
}{
{
name: "pd network is not host",
tc: v1alpha1.TidbCluster{
ObjectMeta: metav1.ObjectMeta{
Name: "tc",
Namespace: "ns",
},
},
testSts: testHostNetwork(t, false, v1.DNSClusterFirst),
},
{
name: "pd network is host",
tc: v1alpha1.TidbCluster{
ObjectMeta: metav1.ObjectMeta{
Name: "tc",
Namespace: "ns",
},
Spec: v1alpha1.TidbClusterSpec{
PD: v1alpha1.PDSpec{
PodAttributesSpec: v1alpha1.PodAttributesSpec{
HostNetwork: true,
},
},
},
},
testSts: testHostNetwork(t, true, v1.DNSClusterFirstWithHostNet),
},
{
name: "pd network is not host when tidb is host",
tc: v1alpha1.TidbCluster{
ObjectMeta: metav1.ObjectMeta{
Name: "tc",
Namespace: "ns",
},
Spec: v1alpha1.TidbClusterSpec{
TiDB: v1alpha1.TiDBSpec{
PodAttributesSpec: v1alpha1.PodAttributesSpec{
HostNetwork: true,
},
},
},
},
testSts: testHostNetwork(t, false, v1.DNSClusterFirst),
},
{
name: "pd network is not host when tikv is host",
tc: v1alpha1.TidbCluster{
ObjectMeta: metav1.ObjectMeta{
Name: "tc",
Namespace: "ns",
},
Spec: v1alpha1.TidbClusterSpec{
TiKV: v1alpha1.TiKVSpec{
PodAttributesSpec: v1alpha1.PodAttributesSpec{
HostNetwork: true,
},
},
},
},
testSts: testHostNetwork(t, false, v1.DNSClusterFirst),
},
// TODO add more tests
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
sts, err := getNewPDSetForTidbCluster(&tt.tc)
if (err != nil) != tt.wantErr {
t.Errorf("error %v, wantErr %v", err, tt.wantErr)
}
tt.testSts(sts)
})
}
}
6 changes: 3 additions & 3 deletions pkg/manager/member/tidb_member_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/intstr"
"k8s.io/apimachinery/pkg/util/uuid"
"k8s.io/client-go/listers/apps/v1"
v1 "k8s.io/client-go/listers/apps/v1"
corelisters "k8s.io/client-go/listers/core/v1"
)

Expand Down Expand Up @@ -134,7 +134,7 @@ func (tmm *tidbMemberManager) syncTiDBStatefulSetForTidbCluster(tc *v1alpha1.Tid
ns := tc.GetNamespace()
tcName := tc.GetName()

newTiDBSet := tmm.getNewTiDBSetForTidbCluster(tc)
newTiDBSet := getNewTiDBSetForTidbCluster(tc)
oldTiDBSetTemp, err := tmm.setLister.StatefulSets(ns).Get(controller.TiDBMemberName(tcName))
if errors.IsNotFound(err) {
err = SetLastAppliedConfigAnnotation(newTiDBSet)
Expand Down Expand Up @@ -219,7 +219,7 @@ func getNewTiDBHeadlessServiceForTidbCluster(tc *v1alpha1.TidbCluster) *corev1.S
}
}

func (tmm *tidbMemberManager) getNewTiDBSetForTidbCluster(tc *v1alpha1.TidbCluster) *apps.StatefulSet {
func getNewTiDBSetForTidbCluster(tc *v1alpha1.TidbCluster) *apps.StatefulSet {
ns := tc.GetNamespace()
tcName := tc.GetName()
instanceName := tc.GetLabels()[label.InstanceLabelKey]
Expand Down
80 changes: 80 additions & 0 deletions pkg/manager/member/tidb_member_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"github.com/pingcap/tidb-operator/pkg/label"
apps "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
Expand Down Expand Up @@ -648,3 +649,82 @@ func TestGetNewTiDBHeadlessServiceForTidbCluster(t *testing.T) {
})
}
}

func TestGetNewTiDBSetForTidbCluster(t *testing.T) {
tests := []struct {
name string
tc v1alpha1.TidbCluster
wantErr bool
testSts func(sts *apps.StatefulSet)
}{
{
name: "tidb network is not host",
tc: v1alpha1.TidbCluster{
ObjectMeta: metav1.ObjectMeta{
Name: "tc",
Namespace: "ns",
},
},
testSts: testHostNetwork(t, false, v1.DNSClusterFirst),
},
{
name: "tidb network is host",
tc: v1alpha1.TidbCluster{
ObjectMeta: metav1.ObjectMeta{
Name: "tc",
Namespace: "ns",
},
Spec: v1alpha1.TidbClusterSpec{
TiDB: v1alpha1.TiDBSpec{
PodAttributesSpec: v1alpha1.PodAttributesSpec{
HostNetwork: true,
},
},
},
},
testSts: testHostNetwork(t, true, v1.DNSClusterFirstWithHostNet),
},
{
name: "tidb network is not host when pd is host",
tc: v1alpha1.TidbCluster{
ObjectMeta: metav1.ObjectMeta{
Name: "tc",
Namespace: "ns",
},
Spec: v1alpha1.TidbClusterSpec{
PD: v1alpha1.PDSpec{
PodAttributesSpec: v1alpha1.PodAttributesSpec{
HostNetwork: true,
},
},
},
},
testSts: testHostNetwork(t, false, v1.DNSClusterFirst),
},
{
name: "tidb network is not host when tikv is host",
tc: v1alpha1.TidbCluster{
ObjectMeta: metav1.ObjectMeta{
Name: "tc",
Namespace: "ns",
},
Spec: v1alpha1.TidbClusterSpec{
TiKV: v1alpha1.TiKVSpec{
PodAttributesSpec: v1alpha1.PodAttributesSpec{
HostNetwork: true,
},
},
},
},
testSts: testHostNetwork(t, false, v1.DNSClusterFirst),
},
// TODO add more tests
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
sts := getNewTiDBSetForTidbCluster(&tt.tc)
tt.testSts(sts)
})
}
}
12 changes: 6 additions & 6 deletions pkg/manager/member/tikv_member_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ func (tkmm *tikvMemberManager) syncStatefulSetForTidbCluster(tc *v1alpha1.TidbCl
ns := tc.GetNamespace()
tcName := tc.GetName()

newSet, err := tkmm.getNewSetForTidbCluster(tc)
newSet, err := getNewTiKVSetForTidbCluster(tc)
if err != nil {
return err
}
Expand Down Expand Up @@ -269,7 +269,7 @@ func getNewServiceForTidbCluster(tc *v1alpha1.TidbCluster, svcConfig SvcConfig)
return &svc
}

func (tkmm *tikvMemberManager) getNewSetForTidbCluster(tc *v1alpha1.TidbCluster) (*apps.StatefulSet, error) {
func getNewTiKVSetForTidbCluster(tc *v1alpha1.TidbCluster) (*apps.StatefulSet, error) {
ns := tc.GetNamespace()
tcName := tc.GetName()
tikvConfigMap := controller.MemberConfigMapName(tc, v1alpha1.TiKVMemberType)
Expand Down Expand Up @@ -326,7 +326,7 @@ func (tkmm *tikvMemberManager) getNewSetForTidbCluster(tc *v1alpha1.TidbCluster)
}
}

tikvLabel := tkmm.labelTiKV(tc)
tikvLabel := labelTiKV(tc)
setName := controller.TiKVMemberName(tcName)
podAnnotations := CombineAnnotations(controller.AnnProm(20180), tc.Spec.TiKV.Annotations)
capacity := controller.TiKVCapacity(tc.Spec.TiKV.Limits)
Expand Down Expand Up @@ -424,7 +424,7 @@ func (tkmm *tikvMemberManager) getNewSetForTidbCluster(tc *v1alpha1.TidbCluster)
},
},
VolumeClaimTemplates: []corev1.PersistentVolumeClaim{
tkmm.volumeClaimTemplate(q, v1alpha1.TiKVMemberType.String(), &storageClassName),
volumeClaimTemplate(q, v1alpha1.TiKVMemberType.String(), &storageClassName),
},
ServiceName: headlessSvcName,
PodManagementPolicy: apps.ParallelPodManagement,
Expand All @@ -439,7 +439,7 @@ func (tkmm *tikvMemberManager) getNewSetForTidbCluster(tc *v1alpha1.TidbCluster)
return tikvset, nil
}

func (tkmm *tikvMemberManager) volumeClaimTemplate(q resource.Quantity, metaName string, storageClassName *string) corev1.PersistentVolumeClaim {
func volumeClaimTemplate(q resource.Quantity, metaName string, storageClassName *string) corev1.PersistentVolumeClaim {
return corev1.PersistentVolumeClaim{
ObjectMeta: metav1.ObjectMeta{Name: metaName},
Spec: corev1.PersistentVolumeClaimSpec{
Expand All @@ -456,7 +456,7 @@ func (tkmm *tikvMemberManager) volumeClaimTemplate(q resource.Quantity, metaName
}
}

func (tkmm *tikvMemberManager) labelTiKV(tc *v1alpha1.TidbCluster) label.Label {
func labelTiKV(tc *v1alpha1.TidbCluster) label.Label {
instanceName := tc.GetLabels()[label.InstanceLabelKey]
return label.New().Instance(instanceName).TiKV()
}
Expand Down
Loading

0 comments on commit a58fbba

Please sign in to comment.