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

Commit

Permalink
test(split): Refactor tests to use two traffic split backends (#1821)
Browse files Browse the repository at this point in the history
  • Loading branch information
ksubrmnn authored Oct 13, 2020
1 parent ecc5dc6 commit c1265f5
Show file tree
Hide file tree
Showing 21 changed files with 186 additions and 136 deletions.
4 changes: 2 additions & 2 deletions pkg/catalog/debugger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ var _ = Describe("Test catalog proxy register/unregister", func() {
It("lists monitored namespaces", func() {
actual := mc.ListMonitoredNamespaces()
listExpectedNs := tests.GetUnique([]string{
tests.BookstoreService.Namespace,
tests.BookstoreV1Service.Namespace,
tests.BookbuyerService.Namespace,
tests.BookwarehouseService.Namespace,
})
Expand All @@ -96,7 +96,7 @@ var _ = Describe("Test catalog proxy register/unregister", func() {

Expect(trafficSplits[0].Spec.Service).To(Equal("bookstore-apex"))
Expect(weightedServices[0]).To(Equal(service.WeightedService{
Service: tests.BookstoreService,
Service: tests.BookstoreV1Service,
Weight: 100,
RootService: "bookstore-apex"}))
Expect(serviceAccounts[0].String()).To(Equal("default/bookstore"))
Expand Down
4 changes: 2 additions & 2 deletions pkg/catalog/endpoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ var _ = Describe("Test catalog functions", func() {
mc := newFakeMeshCatalog()
Context("Testing ListEndpointsForService()", func() {
It("lists endpoints for a given service", func() {
actual, err := mc.ListEndpointsForService(tests.BookstoreService)
actual, err := mc.ListEndpointsForService(tests.BookstoreV1Service)
Expect(err).ToNot(HaveOccurred())

expected := []endpoint.Endpoint{
Expand All @@ -24,7 +24,7 @@ var _ = Describe("Test catalog functions", func() {

Context("Testing GetResolvableServiceEndpoints()", func() {
It("returns the endpoint for the service", func() {
actual, err := mc.GetResolvableServiceEndpoints(tests.BookstoreService)
actual, err := mc.GetResolvableServiceEndpoints(tests.BookstoreV1Service)
Expect(err).ToNot(HaveOccurred())

expected := []endpoint.Endpoint{
Expand Down
3 changes: 2 additions & 1 deletion pkg/catalog/fake.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,8 @@ func NewFakeMeshCatalog(kubeClient kubernetes.Interface) *MeshCatalog {

mockKubeController.EXPECT().GetAnnouncementsChannel(k8s.Namespaces).Return(testChan).AnyTimes()
mockKubeController.EXPECT().GetAnnouncementsChannel(k8s.Services).Return(testChan).AnyTimes()
mockKubeController.EXPECT().IsMonitoredNamespace(tests.BookstoreService.Namespace).Return(true).AnyTimes()
mockKubeController.EXPECT().IsMonitoredNamespace(tests.BookstoreV1Service.Namespace).Return(true).AnyTimes()
mockKubeController.EXPECT().IsMonitoredNamespace(tests.BookstoreV2Service.Namespace).Return(true).AnyTimes()
mockKubeController.EXPECT().IsMonitoredNamespace(tests.BookbuyerService.Namespace).Return(true).AnyTimes()
mockKubeController.EXPECT().IsMonitoredNamespace(tests.BookwarehouseService.Namespace).Return(true).AnyTimes()
mockKubeController.EXPECT().GetAnnouncementsChannel(k8s.Namespaces).Return(testChan).AnyTimes()
Expand Down
17 changes: 12 additions & 5 deletions pkg/catalog/ingress_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,16 @@ func newFakeMeshCatalog() *MeshCatalog {
GinkgoT().Fatalf("Error creating new fake Mesh Catalog: %s", err.Error())
}

// Create Bookstore Service
// Create Bookstore-v1 Service
selector := map[string]string{tests.SelectorKey: tests.SelectorValue}
svc := tests.NewServiceFixture(tests.BookstoreService.Name, tests.BookstoreService.Namespace, selector)
if _, err := kubeClient.CoreV1().Services(tests.BookstoreService.Namespace).Create(context.TODO(), svc, metav1.CreateOptions{}); err != nil {
svc := tests.NewServiceFixture(tests.BookstoreV1Service.Name, tests.BookstoreV1Service.Namespace, selector)
if _, err := kubeClient.CoreV1().Services(tests.BookstoreV1Service.Namespace).Create(context.TODO(), svc, metav1.CreateOptions{}); err != nil {
GinkgoT().Fatalf("Error creating new Bookstore service: %s", err.Error())
}

// Create Bookstore-v2 Service
svc = tests.NewServiceFixture(tests.BookstoreV2Service.Name, tests.BookstoreV2Service.Namespace, selector)
if _, err := kubeClient.CoreV1().Services(tests.BookstoreV2Service.Namespace).Create(context.TODO(), svc, metav1.CreateOptions{}); err != nil {
GinkgoT().Fatalf("Error creating new Bookstore service: %s", err.Error())
}

Expand All @@ -101,7 +107,7 @@ func newFakeMeshCatalog() *MeshCatalog {

// Monitored namespaces is made a set to make sure we don't repeat namespaces on mock
listExpectedNs := tests.GetUnique([]string{
tests.BookstoreService.Namespace,
tests.BookstoreV1Service.Namespace,
tests.BookbuyerService.Namespace,
tests.BookstoreApexService.Namespace,
})
Expand Down Expand Up @@ -133,7 +139,8 @@ func newFakeMeshCatalog() *MeshCatalog {
}).AnyTimes()
mockKubeController.EXPECT().GetAnnouncementsChannel(k8s.Namespaces).Return(testChan).AnyTimes()
mockKubeController.EXPECT().GetAnnouncementsChannel(k8s.Services).Return(testChan).AnyTimes()
mockKubeController.EXPECT().IsMonitoredNamespace(tests.BookstoreService.Namespace).Return(true).AnyTimes()
mockKubeController.EXPECT().IsMonitoredNamespace(tests.BookstoreV1Service.Namespace).Return(true).AnyTimes()
mockKubeController.EXPECT().IsMonitoredNamespace(tests.BookstoreV2Service.Namespace).Return(true).AnyTimes()
mockKubeController.EXPECT().IsMonitoredNamespace(tests.BookbuyerService.Namespace).Return(true).AnyTimes()
mockKubeController.EXPECT().IsMonitoredNamespace(tests.BookwarehouseService.Namespace).Return(true).AnyTimes()
mockKubeController.EXPECT().ListMonitoredNamespaces().Return(listExpectedNs, nil).AnyTimes()
Expand Down
106 changes: 44 additions & 62 deletions pkg/catalog/routes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ func TestListTrafficPolicies(t *testing.T) {

listTrafficPoliciesTests := []listTrafficPoliciesTest{
{
input: tests.BookstoreService,
output: []trafficpolicy.TrafficTarget{tests.BookstoreTrafficPolicy},
input: tests.BookstoreV1Service,
output: []trafficpolicy.TrafficTarget{tests.BookstoreV1TrafficPolicy},
},
{
input: tests.BookbuyerService,
output: []trafficpolicy.TrafficTarget{tests.BookstoreTrafficPolicy, tests.BookstoreApexTrafficPolicy},
output: []trafficpolicy.TrafficTarget{tests.BookstoreV1TrafficPolicy, tests.BookstoreV2TrafficPolicy, tests.BookstoreApexTrafficPolicy},
},
}

Expand All @@ -57,46 +57,22 @@ func TestGetTrafficPoliciesForService(t *testing.T) {
input: tests.BookbuyerService,
output: []trafficpolicy.TrafficTarget{
{
Name: utils.GetTrafficTargetName(tests.TrafficTargetName, tests.BookbuyerService, tests.BookstoreService),
Destination: tests.BookstoreService,
Name: utils.GetTrafficTargetName(tests.TrafficTargetName, tests.BookbuyerService, tests.BookstoreV1Service),
Destination: tests.BookstoreV1Service,
Source: tests.BookbuyerService,
HTTPRoutes: []trafficpolicy.HTTPRoute{
{
PathRegex: tests.BookstoreBuyPath,
Methods: []string{"GET"},
Headers: map[string]string{
"user-agent": tests.HTTPUserAgent,
},
},
{
PathRegex: tests.BookstoreSellPath,
Methods: []string{"GET"},
Headers: map[string]string{
"user-agent": tests.HTTPUserAgent,
},
},
},
HTTPRoutes: tests.BookstoreV1TrafficPolicy.HTTPRoutes,
},
{
Name: utils.GetTrafficTargetName(tests.TrafficTargetName, tests.BookbuyerService, tests.BookstoreV2Service),
Destination: tests.BookstoreV2Service,
Source: tests.BookbuyerService,
HTTPRoutes: tests.BookstoreV2TrafficPolicy.HTTPRoutes,
},
{
Name: utils.GetTrafficTargetName(tests.TrafficTargetName, tests.BookbuyerService, tests.BookstoreApexService),
Destination: tests.BookstoreApexService,
Source: tests.BookbuyerService,
HTTPRoutes: []trafficpolicy.HTTPRoute{
{
PathRegex: tests.BookstoreBuyPath,
Methods: []string{"GET"},
Headers: map[string]string{
"user-agent": tests.HTTPUserAgent,
},
},
{
PathRegex: tests.BookstoreSellPath,
Methods: []string{"GET"},
Headers: map[string]string{
"user-agent": tests.HTTPUserAgent,
},
},
},
HTTPRoutes: tests.BookstoreApexTrafficPolicy.HTTPRoutes,
},
},
},
Expand Down Expand Up @@ -163,7 +139,7 @@ func TestListAllowedInboundServices(t *testing.T) {

mc := newFakeMeshCatalog()

actualList, err := mc.ListAllowedInboundServices(tests.BookstoreService)
actualList, err := mc.ListAllowedInboundServices(tests.BookstoreV1Service)
assert.Nil(err)
expectedList := []service.MeshService{tests.BookbuyerService}
assert.ElementsMatch(actualList, expectedList)
Expand All @@ -179,7 +155,7 @@ func TestBuildAllowPolicyForSourceToDest(t *testing.T) {
}
source := tests.NewServiceFixture(tests.BookbuyerServiceName, tests.Namespace, selectors)
expectedSourceTrafficResource := utils.K8sSvcToMeshSvc(source)
destination := tests.NewServiceFixture(tests.BookstoreServiceName, tests.Namespace, selectors)
destination := tests.NewServiceFixture(tests.BookstoreV1ServiceName, tests.Namespace, selectors)
expectedDestinationTrafficResource := utils.K8sSvcToMeshSvc(destination)

expectedHostHeaders := map[string]string{"user-agent": tests.HTTPUserAgent}
Expand All @@ -203,19 +179,19 @@ func TestListAllowedOutboundServices(t *testing.T) {
actualList, err := mc.ListAllowedOutboundServices(tests.BookbuyerService)
assert.Nil(err)

expectedList := []service.MeshService{tests.BookstoreService, tests.BookstoreApexService}
expectedList := []service.MeshService{tests.BookstoreV1Service, tests.BookstoreV2Service, tests.BookstoreApexService}
assert.ElementsMatch(actualList, expectedList)
}

func TestGetWeightedClusterForService(t *testing.T) {
assert := assert.New(t)

mc := newFakeMeshCatalog()
weightedCluster, err := mc.GetWeightedClusterForService(tests.BookstoreService)
weightedCluster, err := mc.GetWeightedClusterForService(tests.BookstoreV1Service)
assert.Nil(err)

expected := service.WeightedCluster{
ClusterName: "default/bookstore",
ClusterName: "default/bookstore-v1",
Weight: 100,
}
assert.Equal(weightedCluster, expected)
Expand All @@ -225,20 +201,20 @@ func TestGetServiceHostnames(t *testing.T) {
assert := assert.New(t)

mc := newFakeMeshCatalog()
actual, err := mc.getServiceHostnames(tests.BookstoreService)
actual, err := mc.getServiceHostnames(tests.BookstoreV1Service)
assert.Nil(err)

expected := []string{
"bookstore",
"bookstore.default",
"bookstore.default.svc",
"bookstore.default.svc.cluster",
"bookstore.default.svc.cluster.local",
"bookstore:8888",
"bookstore.default:8888",
"bookstore.default.svc:8888",
"bookstore.default.svc.cluster:8888",
"bookstore.default.svc.cluster.local:8888",
"bookstore-v1",
"bookstore-v1.default",
"bookstore-v1.default.svc",
"bookstore-v1.default.svc.cluster",
"bookstore-v1.default.svc.cluster.local",
"bookstore-v1:8888",
"bookstore-v1.default:8888",
"bookstore-v1.default.svc:8888",
"bookstore-v1.default.svc.cluster:8888",
"bookstore-v1.default.svc.cluster.local:8888",
}
assert.ElementsMatch(actual, expected)
}
Expand All @@ -253,9 +229,9 @@ func TestHostnamesTostr(t *testing.T) {
func TestGetDefaultWeightedClusterForService(t *testing.T) {
assert := assert.New(t)

actual := getDefaultWeightedClusterForService(tests.BookstoreService)
actual := getDefaultWeightedClusterForService(tests.BookstoreV1Service)
expected := service.WeightedCluster{
ClusterName: "default/bookstore",
ClusterName: "default/bookstore-v1",
Weight: 100,
}
assert.Equal(actual, expected)
Expand All @@ -266,7 +242,7 @@ func TestGetHostnamesForService(t *testing.T) {

mc := newFakeMeshCatalog()

actual, err := mc.GetHostnamesForService(tests.BookstoreService)
actual, err := mc.GetHostnamesForService(tests.BookstoreV1Service)
assert.Nil(err)

expected := strings.Join(
Expand All @@ -292,27 +268,33 @@ func TestBuildAllowAllTrafficPolicies(t *testing.T) {

mc := newFakeMeshCatalog()

actual := mc.buildAllowAllTrafficPolicies(tests.BookstoreService)
actual := mc.buildAllowAllTrafficPolicies(tests.BookstoreV1Service)
var actualTargetNames []string
for _, target := range actual {
actualTargetNames = append(actualTargetNames, target.Name)
}

expected := []string{
"default/bookstore->default/bookbuyer",
"default/bookstore->default/bookstore-apex",
"default/bookbuyer->default/bookstore",
"default/bookstore-v1->default/bookbuyer",
"default/bookstore-v1->default/bookstore-apex",
"default/bookstore-v2->default/bookbuyer",
"default/bookstore-v2->default/bookstore-apex",
"default/bookbuyer->default/bookstore-v1",
"default/bookbuyer->default/bookstore-apex",
"default/bookstore-apex->default/bookstore",
"default/bookstore-apex->default/bookstore-v1",
"default/bookbuyer->default/bookstore-v2",
"default/bookstore-apex->default/bookstore-v2",
"default/bookstore-apex->default/bookbuyer",
"default/bookstore-v1->default/bookstore-v2",
"default/bookstore-v2->default/bookstore-v1",
}
assert.ElementsMatch(actualTargetNames, expected)
}

func TestListTrafficTargetPermutations(t *testing.T) {
assert := assert.New(t)

destList := []service.MeshService{tests.BookstoreService, tests.BookstoreApexService}
destList := []service.MeshService{tests.BookstoreV1Service, tests.BookstoreApexService}
srcList := []service.MeshService{tests.BookbuyerService, tests.BookwarehouseService}
trafficTargets := listTrafficTargetPermutations(tests.TrafficTargetName, srcList, destList)

Expand Down
4 changes: 2 additions & 2 deletions pkg/debugger/namespace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ func TestMonitoredNamespaceHandler(t *testing.T) {
monitoredNamespacesHandler := ds.getMonitoredNamespacesHandler()

uniqueNs := tests.GetUnique([]string{
tests.BookbuyerService.Namespace, // default
tests.BookstoreService.Namespace, // default
tests.BookbuyerService.Namespace, // default
tests.BookstoreV1Service.Namespace, // default
})

mock.EXPECT().ListMonitoredNamespaces().Return(uniqueNs)
Expand Down
5 changes: 3 additions & 2 deletions pkg/debugger/policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ func TestGetSMIPolicies(t *testing.T) {
}},
},
[]service.WeightedService{
tests.WeightedService,
tests.BookstoreV1WeightedService,
tests.BookstoreV2WeightedService,
},
[]service.K8sServiceAccount{
tests.BookbuyerServiceAccount,
Expand All @@ -52,6 +53,6 @@ func TestGetSMIPolicies(t *testing.T) {
responseRecorder := httptest.NewRecorder()
smiPoliciesHandler.ServeHTTP(responseRecorder, nil)
actualResponseBody := responseRecorder.Body.String()
expectedResponseBody := `{"traffic_splits":[{"metadata":{"name":"bar","namespace":"foo","creationTimestamp":null},"spec":{}}],"weighted_services":[{"service_name:omitempty":{"Namespace":"default","Name":"bookstore"},"weight:omitempty":100,"root_service:omitempty":"bookstore-apex"}],"service_accounts":[{"Namespace":"default","Name":"bookbuyer"}],"route_groups":[{"kind":"HTTPRouteGroup","apiVersion":"specs.smi-spec.io/v1alpha2","metadata":{"name":"bookstore-service-routes","namespace":"default","creationTimestamp":null},"spec":{"matches":[{"name":"buy-books","methods":["GET"],"pathRegex":"/buy","headers":[{"user-agent":"test-UA"}]},{"name":"sell-books","methods":["GET"],"pathRegex":"/sell","headers":[{"user-agent":"test-UA"}]},{"name":"allow-everything-on-header","headers":[{"user-agent":"test-UA"}]}]}}],"traffic_targets":[{"kind":"TrafficTarget","apiVersion":"access.smi-spec.io/v1alpha2","metadata":{"name":"bookbuyer-access-bookstore","namespace":"default","creationTimestamp":null},"spec":{"destination":{"kind":"Name","name":"bookstore","namespace":"default"},"sources":[{"kind":"Name","name":"bookbuyer","namespace":"default"}],"rules":[{"kind":"HTTPRouteGroup","name":"bookstore-service-routes","matches":["buy-books","sell-books"]}]}}]}`
expectedResponseBody := `{"traffic_splits":[{"metadata":{"name":"bar","namespace":"foo","creationTimestamp":null},"spec":{}}],"weighted_services":[{"service_name:omitempty":{"Namespace":"default","Name":"bookstore-v1"},"weight:omitempty":100,"root_service:omitempty":"bookstore-apex"},{"service_name:omitempty":{"Namespace":"default","Name":"bookstore-v2"},"weight:omitempty":100,"root_service:omitempty":"bookstore-apex"}],"service_accounts":[{"Namespace":"default","Name":"bookbuyer"}],"route_groups":[{"kind":"HTTPRouteGroup","apiVersion":"specs.smi-spec.io/v1alpha2","metadata":{"name":"bookstore-service-routes","namespace":"default","creationTimestamp":null},"spec":{"matches":[{"name":"buy-books","methods":["GET"],"pathRegex":"/buy","headers":[{"user-agent":"test-UA"}]},{"name":"sell-books","methods":["GET"],"pathRegex":"/sell","headers":[{"user-agent":"test-UA"}]},{"name":"allow-everything-on-header","headers":[{"user-agent":"test-UA"}]}]}}],"traffic_targets":[{"kind":"TrafficTarget","apiVersion":"access.smi-spec.io/v1alpha2","metadata":{"name":"bookbuyer-access-bookstore","namespace":"default","creationTimestamp":null},"spec":{"destination":{"kind":"Name","name":"bookstore","namespace":"default"},"sources":[{"kind":"Name","name":"bookbuyer","namespace":"default"}],"rules":[{"kind":"HTTPRouteGroup","name":"bookstore-service-routes","matches":["buy-books","sell-books"]}]}}]}`
assert.Equal(actualResponseBody, expectedResponseBody, "Actual value did not match expectations:\n%s", actualResponseBody)
}
4 changes: 2 additions & 2 deletions pkg/endpoint/providers/kube/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ var _ = Describe("Test Kube Client Provider", func() {

It("returns the services for a given service account", func() {
c := NewFakeProvider()
actual := c.ListEndpointsForService(tests.BookstoreService)
actual := c.ListEndpointsForService(tests.BookstoreV1Service)
expected := []endpoint.Endpoint{{
IP: net.ParseIP("8.8.8.8"),
Port: 8888,
Expand All @@ -177,7 +177,7 @@ var _ = Describe("Test Kube Client Provider", func() {

sMesh, err := c.GetServicesForServiceAccount(tests.BookstoreServiceAccount)
Expect(err).To(BeNil())
expectedServices := []service.MeshService{tests.BookstoreService, tests.BookstoreApexService}
expectedServices := []service.MeshService{tests.BookstoreV1Service, tests.BookstoreV2Service, tests.BookstoreApexService}
Expect(sMesh).To(Equal(expectedServices))

sMesh2, err := c.GetServicesForServiceAccount(tests.BookbuyerServiceAccount)
Expand Down
5 changes: 3 additions & 2 deletions pkg/endpoint/providers/kube/fake.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@ import (
func NewFakeProvider() endpoint.Provider {
return &fakeClient{
endpoints: map[string][]endpoint.Endpoint{
tests.BookstoreService.String(): {tests.Endpoint},
tests.BookstoreV1Service.String(): {tests.Endpoint},
tests.BookstoreV2Service.String(): {tests.Endpoint},
tests.BookbuyerService.String(): {tests.Endpoint},
tests.BookstoreApexService.String(): {tests.Endpoint},
},
services: map[service.K8sServiceAccount][]service.MeshService{
tests.BookstoreServiceAccount: {tests.BookstoreService, tests.BookstoreApexService},
tests.BookstoreServiceAccount: {tests.BookstoreV1Service, tests.BookstoreV2Service, tests.BookstoreApexService},
tests.BookbuyerServiceAccount: {tests.BookbuyerService},
},
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/envoy/ads/response_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ var _ = Describe("Test ADS response functions", func() {
kubeClient := testclient.NewSimpleClientset()
namespace := tests.Namespace
envoyUID := tests.EnvoyUID
serviceName := tests.BookstoreServiceName
serviceName := tests.BookstoreV1ServiceName
serviceAccountName := tests.BookstoreServiceAccountName

labels := map[string]string{constants.EnvoyUniqueIDLabelName: tests.EnvoyUID}
Expand Down
2 changes: 1 addition & 1 deletion pkg/envoy/cds/cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ var _ = Describe("Cluster configurations", func() {
mockConfigurator = configurator.NewMockConfigurator(mockCtrl)

localService := tests.BookbuyerService
remoteService := tests.BookstoreService
remoteService := tests.BookstoreV1Service
Context("Test getRemoteServiceCluster", func() {
It("Returns an EDS based cluster when permissive mode is disabled", func() {
mockConfigurator.EXPECT().IsPermissiveTrafficPolicyMode().Return(false).Times(1)
Expand Down
Loading

0 comments on commit c1265f5

Please sign in to comment.