Skip to content

Commit

Permalink
Add unit test about endpoints.
Browse files Browse the repository at this point in the history
  • Loading branch information
tkanng committed Jun 4, 2019
1 parent fddbcd9 commit 4997aa9
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 5 deletions.
33 changes: 31 additions & 2 deletions pkg/controller/service_control.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
v1listers "github.com/pingcap/tidb-operator/pkg/client/listers/pingcap.com/v1alpha1"
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
coreinformers "k8s.io/client-go/informers/core/v1"
"k8s.io/client-go/kubernetes"
Expand Down Expand Up @@ -122,6 +123,7 @@ var _ ServiceControlInterface = &realServiceControl{}
type FakeServiceControl struct {
SvcLister corelisters.ServiceLister
SvcIndexer cache.Indexer
EpsIndexer cache.Indexer
TcLister v1listers.TidbClusterLister
TcIndexer cache.Indexer
createServiceTracker requestTracker
Expand All @@ -130,10 +132,11 @@ type FakeServiceControl struct {
}

// NewFakeServiceControl returns a FakeServiceControl
func NewFakeServiceControl(svcInformer coreinformers.ServiceInformer, tcInformer tcinformers.TidbClusterInformer) *FakeServiceControl {
func NewFakeServiceControl(svcInformer coreinformers.ServiceInformer, epsInformer coreinformers.EndpointsInformer, tcInformer tcinformers.TidbClusterInformer) *FakeServiceControl {
return &FakeServiceControl{
svcInformer.Lister(),
svcInformer.Informer().GetIndexer(),
epsInformer.Informer().GetIndexer(),
tcInformer.Lister(),
tcInformer.Informer().GetIndexer(),
requestTracker{0, nil, 0},
Expand Down Expand Up @@ -168,7 +171,21 @@ func (ssc *FakeServiceControl) CreateService(_ *v1alpha1.TidbCluster, svc *corev
return ssc.createServiceTracker.err
}

return ssc.SvcIndexer.Add(svc)
err := ssc.SvcIndexer.Add(svc)
if err != nil {
return err
}
// add a new endpoint to indexer if svc has selector
if svc.Spec.Selector != nil {
eps := &corev1.Endpoints{
ObjectMeta: metav1.ObjectMeta{
Name: svc.Name,
Namespace: svc.Namespace,
},
}
return ssc.EpsIndexer.Add(eps)
}
return nil
}

// UpdateService updates the service of SvcIndexer
Expand All @@ -179,6 +196,18 @@ func (ssc *FakeServiceControl) UpdateService(_ *v1alpha1.TidbCluster, svc *corev
return nil, ssc.updateServiceTracker.err
}

if svc.Spec.Selector != nil {
eps := &corev1.Endpoints{
ObjectMeta: metav1.ObjectMeta{
Name: svc.Name,
Namespace: svc.Namespace,
},
}
err := ssc.EpsIndexer.Update(eps)
if err != nil {
return nil, err
}
}
return svc, ssc.SvcIndexer.Update(svc)
}

Expand Down
16 changes: 15 additions & 1 deletion pkg/manager/member/pd_member_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,19 +78,27 @@ func TestPDMemberManagerSyncCreate(t *testing.T) {
g.Expect(tc.Spec).To(Equal(oldSpec))

svc1, err := pmm.svcLister.Services(ns).Get(controller.PDMemberName(tcName))
eps1, eperr := pmm.epsLister.Endpoints(ns).Get(controller.PDMemberName(tcName))
if test.pdSvcCreated {
g.Expect(err).NotTo(HaveOccurred())
g.Expect(svc1).NotTo(Equal(nil))
g.Expect(eperr).NotTo(HaveOccurred())
g.Expect(eps1).NotTo(Equal(nil))
} else {
expectErrIsNotFound(g, err)
expectErrIsNotFound(g, eperr)
}

svc2, err := pmm.svcLister.Services(ns).Get(controller.PDPeerMemberName(tcName))
eps2, eperr := pmm.epsLister.Endpoints(ns).Get(controller.PDPeerMemberName(tcName))
if test.pdPeerSvcCreated {
g.Expect(err).NotTo(HaveOccurred())
g.Expect(svc2).NotTo(Equal(nil))
g.Expect(eperr).NotTo(HaveOccurred())
g.Expect(eps2).NotTo(Equal(nil))
} else {
expectErrIsNotFound(g, err)
expectErrIsNotFound(g, eperr)
}

tc1, err := pmm.setLister.StatefulSets(ns).Get(controller.PDMemberName(tcName))
Expand Down Expand Up @@ -243,8 +251,14 @@ func TestPDMemberManagerSyncUpdate(t *testing.T) {

_, err = pmm.svcLister.Services(ns).Get(controller.PDMemberName(tcName))
g.Expect(err).NotTo(HaveOccurred())
_, err = pmm.epsLister.Endpoints(ns).Get(controller.PDMemberName(tcName))
g.Expect(err).NotTo(HaveOccurred())

_, err = pmm.svcLister.Services(ns).Get(controller.PDPeerMemberName(tcName))
g.Expect(err).NotTo(HaveOccurred())
_, err = pmm.epsLister.Endpoints(ns).Get(controller.PDPeerMemberName(tcName))
g.Expect(err).NotTo(HaveOccurred())

_, err = pmm.setLister.StatefulSets(ns).Get(controller.PDMemberName(tcName))
g.Expect(err).NotTo(HaveOccurred())

Expand Down Expand Up @@ -638,7 +652,7 @@ func newFakePDMemberManager() (*pdMemberManager, *controller.FakeStatefulSetCont
pvcInformer := kubeinformers.NewSharedInformerFactory(kubeCli, 0).Core().V1().PersistentVolumeClaims()
tcInformer := informers.NewSharedInformerFactory(cli, 0).Pingcap().V1alpha1().TidbClusters()
setControl := controller.NewFakeStatefulSetControl(setInformer, tcInformer)
svcControl := controller.NewFakeServiceControl(svcInformer, tcInformer)
svcControl := controller.NewFakeServiceControl(svcInformer, epsInformer, tcInformer)
podControl := controller.NewFakePodControl(podInformer)
pdControl := controller.NewFakePDControl()
pdScaler := NewFakePDScaler()
Expand Down
3 changes: 2 additions & 1 deletion pkg/manager/member/tidb_member_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -527,9 +527,10 @@ func newFakeTiDBMemberManager() (*tidbMemberManager, *controller.FakeStatefulSet
setInformer := kubeinformers.NewSharedInformerFactory(kubeCli, 0).Apps().V1beta1().StatefulSets()
tcInformer := informers.NewSharedInformerFactory(cli, 0).Pingcap().V1alpha1().TidbClusters()
svcInformer := kubeinformers.NewSharedInformerFactory(kubeCli, 0).Core().V1().Services()
epsInformer := kubeinformers.NewSharedInformerFactory(kubeCli, 0).Core().V1().Endpoints()
podInformer := kubeinformers.NewSharedInformerFactory(kubeCli, 0).Core().V1().Pods()
setControl := controller.NewFakeStatefulSetControl(setInformer, tcInformer)
svcControl := controller.NewFakeServiceControl(svcInformer, tcInformer)
svcControl := controller.NewFakeServiceControl(svcInformer, epsInformer, tcInformer)
tidbUpgrader := NewFakeTiDBUpgrader()
tidbFailover := NewFakeTiDBFailover()
tidbControl := controller.NewFakeTiDBControl()
Expand Down
3 changes: 2 additions & 1 deletion pkg/manager/member/tikv_member_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1411,9 +1411,10 @@ func newFakeTiKVMemberManager(tc *v1alpha1.TidbCluster) (
pdControl.SetPDClient(tc, pdClient)
setInformer := kubeinformers.NewSharedInformerFactory(kubeCli, 0).Apps().V1beta1().StatefulSets()
svcInformer := kubeinformers.NewSharedInformerFactory(kubeCli, 0).Core().V1().Services()
epsInformer := kubeinformers.NewSharedInformerFactory(kubeCli, 0).Core().V1().Endpoints()
tcInformer := informers.NewSharedInformerFactory(cli, 0).Pingcap().V1alpha1().TidbClusters()
setControl := controller.NewFakeStatefulSetControl(setInformer, tcInformer)
svcControl := controller.NewFakeServiceControl(svcInformer, tcInformer)
svcControl := controller.NewFakeServiceControl(svcInformer, epsInformer, tcInformer)
podInformer := kubeinformers.NewSharedInformerFactory(kubeCli, 0).Core().V1().Pods()
nodeInformer := kubeinformers.NewSharedInformerFactory(kubeCli, 0).Core().V1().Nodes()
tikvScaler := NewFakeTiKVScaler()
Expand Down

0 comments on commit 4997aa9

Please sign in to comment.