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

Commit

Permalink
[tests] : Adding unit test for the entire envoy listener configuration (
Browse files Browse the repository at this point in the history
#2358)

This PR adds a unit test for the entire listener configuration

Signed-off-by: Sneha Chhabria <snchh@microsoft.com>
  • Loading branch information
snehachhabria authored Jan 22, 2021
1 parent e109d6a commit 1c00c37
Show file tree
Hide file tree
Showing 4 changed files with 178 additions and 69 deletions.
109 changes: 109 additions & 0 deletions pkg/envoy/lds/response_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
package lds

import (
"fmt"
"testing"

xds_core "github.com/envoyproxy/go-control-plane/envoy/config/core/v3"
xds_listener "github.com/envoyproxy/go-control-plane/envoy/config/listener/v3"
"github.com/envoyproxy/go-control-plane/pkg/wellknown"
"github.com/golang/mock/gomock"
"github.com/golang/protobuf/ptypes"
"github.com/google/uuid"
tassert "github.com/stretchr/testify/assert"
"k8s.io/client-go/kubernetes"
testclient "k8s.io/client-go/kubernetes/fake"

"github.com/openservicemesh/osm/pkg/catalog"
"github.com/openservicemesh/osm/pkg/certificate"
"github.com/openservicemesh/osm/pkg/configurator"
"github.com/openservicemesh/osm/pkg/envoy"
"github.com/openservicemesh/osm/pkg/tests"
)

func getProxy(kubeClient kubernetes.Interface) (*envoy.Proxy, error) {
if _, err := tests.MakePod(kubeClient, tests.Namespace, tests.BookbuyerServiceName, tests.BookbuyerServiceAccountName, tests.ProxyUUID); err != nil {
return nil, err
}
if _, err := tests.MakePod(kubeClient, tests.Namespace, "bookstore", tests.BookstoreServiceAccountName, uuid.New().String()); err != nil {
return nil, err
}

for _, svcName := range []string{tests.BookbuyerServiceName, tests.BookstoreApexServiceName, tests.BookstoreV1ServiceName, tests.BookstoreV2ServiceName} {
if _, err := tests.MakeService(kubeClient, svcName); err != nil {
return nil, err
}
}

certCommonName := certificate.CommonName(fmt.Sprintf("%s.%s.%s", tests.ProxyUUID, tests.BookbuyerServiceAccountName, tests.Namespace))
certSerialNumber := certificate.SerialNumber("123456")
proxy := envoy.NewProxy(certCommonName, certSerialNumber, nil)
return proxy, nil
}

func TestListenerConfiguration(t *testing.T) {
assert := tassert.New(t)
mockCtrl := gomock.NewController(t)
mockConfigurator := configurator.NewMockConfigurator(mockCtrl)
kubeClient := testclient.NewSimpleClientset()
meshCatalog := catalog.NewFakeMeshCatalog(kubeClient)

proxy, err := getProxy(kubeClient)
assert.Empty(err)
assert.NotNil(meshCatalog)
assert.NotNil(proxy)

mockConfigurator.EXPECT().IsPermissiveTrafficPolicyMode().Return(false).AnyTimes()
mockConfigurator.EXPECT().IsPrometheusScrapingEnabled().Return(true).AnyTimes()
mockConfigurator.EXPECT().IsTracingEnabled().Return(false).AnyTimes()
mockConfigurator.EXPECT().IsEgressEnabled().Return(true).AnyTimes()

actual, err := NewResponse(meshCatalog, proxy, nil, mockConfigurator, nil)
assert.Empty(err)
assert.NotNil(actual)
// There are 3 listeners configured based on the configuration:
// 1. Outbound listener (outbound-listener)
// 2. inbound listener (inbound-listener)
// 3. Prometheus listener (inbound-prometheus-listener)
assert.Len(actual.Resources, 3)

listener := xds_listener.Listener{}

// validating outbound listener
err = ptypes.UnmarshalAny(actual.Resources[0], &listener)
assert.Empty(err)
assert.Equal(listener.Name, outboundListenerName)
assert.Equal(listener.TrafficDirection, xds_core.TrafficDirection_OUTBOUND)
assert.Len(listener.ListenerFilters, 1)
assert.Equal(listener.ListenerFilters[0].Name, wellknown.OriginalDestination)
assert.NotNil(listener.FilterChains)
// There are 3 filter chains configured on the outbound-listner based on the configuration:
// 1. Filter chanin for bookstore-v1
// 2. Filter chanin for bookstore-v2
// 3. Egress filter chain
assert.Len(listener.FilterChains, 3)
assert.NotNil(listener.DefaultFilterChain)
assert.Equal(listener.DefaultFilterChain.Name, outboundEgressFilterChainName)
assert.Equal(listener.DefaultFilterChain.Filters[0].Name, wellknown.TCPProxy)

// validating inbound listener
err = ptypes.UnmarshalAny(actual.Resources[1], &listener)
assert.Empty(err)
assert.Equal(listener.Name, inboundListenerName)
assert.Equal(listener.TrafficDirection, xds_core.TrafficDirection_INBOUND)
assert.Len(listener.ListenerFilters, 2)
assert.Equal(listener.ListenerFilters[0].Name, wellknown.TlsInspector)
assert.Equal(listener.ListenerFilters[1].Name, wellknown.OriginalDestination)
assert.NotNil(listener.FilterChains)
// There is 1 filter chains configured on the inbound-listner based on the configuration:
// 1. Filter chanin for bookbuyer
assert.Len(listener.FilterChains, 1)

// validating prometheus listener
err = ptypes.UnmarshalAny(actual.Resources[2], &listener)
assert.Empty(err)
assert.Equal(listener.Name, prometheusListenerName)
assert.Equal(listener.TrafficDirection, xds_core.TrafficDirection_INBOUND)
assert.NotNil(listener.FilterChains)
assert.Len(listener.FilterChains, 1)
}
39 changes: 39 additions & 0 deletions pkg/tests/helpers.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
package tests

import (
"context"

v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"

"github.com/openservicemesh/osm/pkg/constants"
)

// GetUnique gets a slice of strings and returns a slice with the unique strings
func GetUnique(slice []string) []string {
// Map as a set
Expand All @@ -16,3 +26,32 @@ func GetUnique(slice []string) []string {

return uniqueList
}

// MakeService creates a new service for a set of pods with matching selectors
func MakeService(kubeClient kubernetes.Interface, svcName string) (*v1.Service, error) {
// These selectors must match the POD(s) created
selectors := map[string]string{
SelectorKey: SelectorValue,
}

// The serviceName must match the SMI
service := NewServiceFixture(svcName, Namespace, selectors)
createdService, err := kubeClient.CoreV1().Services(Namespace).Create(context.TODO(), service, metav1.CreateOptions{})
if err != nil {
return nil, err
}
return createdService, nil
}

// MakePod creates a pod
func MakePod(kubeClient kubernetes.Interface, namespace, podName, serviceAccountName, proxyUUID string) (*v1.Pod, error) {
requestedPod := NewPodTestFixtureWithOptions(namespace, podName, serviceAccountName)

// The proxyUUID links the Pod and the Certificate created for it
requestedPod.Labels[constants.EnvoyUniqueIDLabelName] = proxyUUID
createdPod, err := kubeClient.CoreV1().Pods(namespace).Create(context.TODO(), &requestedPod, metav1.CreateOptions{})
if err != nil {
return nil, err
}
return createdPod, nil
}
68 changes: 0 additions & 68 deletions tests/scenarios/helpers.go

This file was deleted.

31 changes: 30 additions & 1 deletion tests/scenarios/traffic_split_with_apex_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,21 @@ package scenarios
import (
"fmt"

"github.com/google/uuid"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"k8s.io/client-go/kubernetes"
testclient "k8s.io/client-go/kubernetes/fake"

xds_route "github.com/envoyproxy/go-control-plane/envoy/config/route/v3"
"github.com/golang/protobuf/ptypes"
"github.com/golang/protobuf/ptypes/wrappers"

"github.com/openservicemesh/osm/pkg/catalog"
"github.com/openservicemesh/osm/pkg/certificate"
"github.com/openservicemesh/osm/pkg/envoy"
"github.com/openservicemesh/osm/pkg/envoy/rds"
"github.com/openservicemesh/osm/pkg/tests"
)

var _ = Describe(``+
Expand All @@ -23,7 +30,9 @@ var _ = Describe(``+
Context("Test rds.NewResponse()", func() {

// ---[ Setup the test context ]---------
meshCatalog, proxy, err := getMeshCatalogAndProxy()
kubeClient := testclient.NewSimpleClientset()
meshCatalog := catalog.NewFakeMeshCatalog(kubeClient)
proxy, err := getProxy(kubeClient)
It("sets up test context - SMI policies, Services, Pods etc.", func() {
Expect(err).ToNot(HaveOccurred())
Expect(meshCatalog).ToNot(BeNil())
Expand Down Expand Up @@ -179,3 +188,23 @@ func weightedCluster(serviceName string, weight uint32) *xds_route.WeightedClust
Weight: toInt(weight),
}
}

func getProxy(kubeClient kubernetes.Interface) (*envoy.Proxy, error) {
if _, err := tests.MakePod(kubeClient, tests.Namespace, tests.BookbuyerServiceName, tests.BookbuyerServiceAccountName, tests.ProxyUUID); err != nil {
return nil, err
}
if _, err := tests.MakePod(kubeClient, tests.Namespace, "bookstore", tests.BookstoreServiceAccountName, uuid.New().String()); err != nil {
return nil, err
}

for _, svcName := range []string{tests.BookbuyerServiceName, tests.BookstoreApexServiceName, tests.BookstoreV1ServiceName, tests.BookstoreV2ServiceName} {
if _, err := tests.MakeService(kubeClient, svcName); err != nil {
return nil, err
}
}

certCommonName := certificate.CommonName(fmt.Sprintf("%s.%s.%s", tests.ProxyUUID, tests.BookbuyerServiceAccountName, tests.Namespace))
certSerialNumber := certificate.SerialNumber("123456")
proxy := envoy.NewProxy(certCommonName, certSerialNumber, nil)
return proxy, nil
}

0 comments on commit 1c00c37

Please sign in to comment.