Skip to content
This repository has been archived by the owner on Jul 11, 2023. It is now read-only.

Commit

Permalink
endpoint/k8s: Fix unit test sync (#2153)
Browse files Browse the repository at this point in the history
Since addition of PubSub, class announcement channels are not
reliable to get k8s update notifications; pubsub has to be used instead
for accuracy.
  • Loading branch information
eduser25 authored Dec 7, 2020
1 parent e66668d commit 6eb6974
Showing 1 changed file with 40 additions and 9 deletions.
49 changes: 40 additions & 9 deletions pkg/endpoint/providers/kube/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
fake "k8s.io/client-go/kubernetes/fake"

"github.com/openservicemesh/osm/pkg/announcements"
"github.com/openservicemesh/osm/pkg/configurator"
"github.com/openservicemesh/osm/pkg/endpoint"
k8s "github.com/openservicemesh/osm/pkg/kubernetes"
"github.com/openservicemesh/osm/pkg/kubernetes/events"
"github.com/openservicemesh/osm/pkg/service"
"github.com/openservicemesh/osm/pkg/tests"
"github.com/openservicemesh/osm/pkg/utils"
Expand Down Expand Up @@ -54,6 +56,11 @@ var _ = Describe("Test Kube Client Provider", func() {

It("should correctly return a list of endpoints for a service", func() {
// Should be empty for now
endpointChannel := events.GetPubSubInstance().Subscribe(announcements.EndpointAdded,
announcements.EndpointDeleted,
announcements.EndpointUpdated)
defer events.GetPubSubInstance().Unsub(endpointChannel)

Expect(provider.ListEndpointsForService(tests.BookbuyerService)).To(BeNil())

// Create bookbuyer endpoint in Bookbuyer namespace
Expand Down Expand Up @@ -86,7 +93,7 @@ var _ = Describe("Test Kube Client Provider", func() {

_, err = fakeClientSet.CoreV1().Endpoints(tests.BookbuyerService.Namespace).Create(context.TODO(), endp, metav1.CreateOptions{})
Expect(err).To(BeNil())
<-provider.GetAnnouncementsChannel()
<-endpointChannel

Expect(provider.ListEndpointsForService(tests.BookbuyerService)).To(Equal([]endpoint.Endpoint{
{
Expand Down Expand Up @@ -125,6 +132,11 @@ var _ = Describe("Test Kube Client Provider", func() {
})

It("GetResolvableEndpoints should properly return actual endpoints without ClusterIP when ClusterIP is not set", func() {
endpointChannel := events.GetPubSubInstance().Subscribe(announcements.EndpointAdded,
announcements.EndpointDeleted,
announcements.EndpointUpdated)
defer events.GetPubSubInstance().Unsub(endpointChannel)

// Expect the individual pod endpoints, when no cluster IP is assigned to the service
mockKubeController.EXPECT().GetService(tests.BookbuyerService).Return(&corev1.Service{
ObjectMeta: metav1.ObjectMeta{
Expand Down Expand Up @@ -173,7 +185,7 @@ var _ = Describe("Test Kube Client Provider", func() {

_, err = fakeClientSet.CoreV1().Endpoints(tests.BookbuyerService.Namespace).Create(context.TODO(), endp, metav1.CreateOptions{})
Expect(err).To(BeNil())
<-provider.GetAnnouncementsChannel()
<-endpointChannel

Expect(provider.GetResolvableEndpointsForService(tests.BookbuyerService)).To(Equal([]endpoint.Endpoint{
{
Expand All @@ -184,6 +196,11 @@ var _ = Describe("Test Kube Client Provider", func() {
})

It("should correctly return the port to protocol mapping for a service's endpoints", func() {
endpointChannel := events.GetPubSubInstance().Subscribe(announcements.EndpointAdded,
announcements.EndpointDeleted,
announcements.EndpointUpdated)
defer events.GetPubSubInstance().Unsub(endpointChannel)

appProtoHTTP := "http"
appProtoTCP := "tcp"

Expand Down Expand Up @@ -226,7 +243,7 @@ var _ = Describe("Test Kube Client Provider", func() {
Create(context.TODO(), endp, metav1.CreateOptions{})
Expect(err).To(BeNil())

<-provider.GetAnnouncementsChannel()
<-endpointChannel
portToProtocolMap, err := provider.GetPortToProtocolMappingForService(tests.BookbuyerService)
Expect(err).To(BeNil())

Expand Down Expand Up @@ -330,6 +347,11 @@ var _ = Describe("When getting a Service associated with a ServiceAccount", func
})

It("should return a service that matches the ServiceAccount associated with the Pod", func() {
podsChannel := events.GetPubSubInstance().Subscribe(announcements.PodAdded,
announcements.PodDeleted,
announcements.PodUpdated)
defer events.GetPubSubInstance().Unsub(podsChannel)

// Create a Service
svc := &corev1.Service{
ObjectMeta: metav1.ObjectMeta{
Expand Down Expand Up @@ -379,7 +401,7 @@ var _ = Describe("When getting a Service associated with a ServiceAccount", func

_, err = fakeClientSet.CoreV1().Pods(testNamespace).Create(context.Background(), pod, metav1.CreateOptions{})
Expect(err).ToNot(HaveOccurred())
<-provider.GetAnnouncementsChannel()
<-podsChannel

givenSvcAccount := service.K8sServiceAccount{
Namespace: testNamespace,
Expand All @@ -396,10 +418,15 @@ var _ = Describe("When getting a Service associated with a ServiceAccount", func

err = fakeClientSet.CoreV1().Pods(testNamespace).Delete(context.Background(), pod.Name, metav1.DeleteOptions{})
Expect(err).ToNot(HaveOccurred())
<-provider.GetAnnouncementsChannel()
<-podsChannel
})

It("should return an error when the Service selector doesn't match the pod", func() {
podsChannel := events.GetPubSubInstance().Subscribe(announcements.PodAdded,
announcements.PodDeleted,
announcements.PodUpdated)
defer events.GetPubSubInstance().Unsub(podsChannel)

// Create a Service
svc := &corev1.Service{
ObjectMeta: metav1.ObjectMeta{
Expand Down Expand Up @@ -449,7 +476,7 @@ var _ = Describe("When getting a Service associated with a ServiceAccount", func

_, err = fakeClientSet.CoreV1().Pods(testNamespace).Create(context.Background(), pod, metav1.CreateOptions{})
Expect(err).ToNot(HaveOccurred())
<-provider.GetAnnouncementsChannel()
<-podsChannel

givenSvcAccount := service.K8sServiceAccount{
Namespace: testNamespace,
Expand All @@ -465,13 +492,17 @@ var _ = Describe("When getting a Service associated with a ServiceAccount", func

err = fakeClientSet.CoreV1().Pods(testNamespace).Delete(context.Background(), pod.Name, metav1.DeleteOptions{})
Expect(err).ToNot(HaveOccurred())
<-provider.GetAnnouncementsChannel()
<-podsChannel
})

It("should return all services when multiple services match the same Pod", func() {
// This test is meant to ensure the
// service selector logic works as expected when multiple services
// have the same selector match.
podsChannel := events.GetPubSubInstance().Subscribe(announcements.PodAdded,
announcements.PodDeleted,
announcements.PodUpdated)
defer events.GetPubSubInstance().Unsub(podsChannel)

// Create a Service
svc := &corev1.Service{
Expand Down Expand Up @@ -528,7 +559,7 @@ var _ = Describe("When getting a Service associated with a ServiceAccount", func

_, err = fakeClientSet.CoreV1().Pods(testNamespace).Create(context.Background(), pod, metav1.CreateOptions{})
Expect(err).ToNot(HaveOccurred())
<-provider.GetAnnouncementsChannel()
<-podsChannel

givenSvcAccount := service.K8sServiceAccount{
Namespace: testNamespace,
Expand All @@ -547,6 +578,6 @@ var _ = Describe("When getting a Service associated with a ServiceAccount", func

err = fakeClientSet.CoreV1().Pods(testNamespace).Delete(context.Background(), pod.Name, metav1.DeleteOptions{})
Expect(err).ToNot(HaveOccurred())
<-provider.GetAnnouncementsChannel()
<-podsChannel
})
})

0 comments on commit 6eb6974

Please sign in to comment.