Skip to content

Commit

Permalink
unit tests for L4 ILB
Browse files Browse the repository at this point in the history
Most of the tests in l4_test.go are from gce_loadbalancer_internal_test.go
  • Loading branch information
prameshj committed Jan 17, 2020
1 parent 14d36e9 commit a39607f
Show file tree
Hide file tree
Showing 6 changed files with 935 additions and 2 deletions.
10 changes: 8 additions & 2 deletions pkg/backends/syncer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,12 @@ func (p *portset) check(fakeGCE *gce.Cloud) error {
return fmt.Errorf("backend for port %+v should exist, but got: %v", sp.NodePort, err)
}
} else {
if bs, err := composite.GetBackendService(fakeGCE, key, features.VersionFromServicePort(&sp)); !utils.IsHTTPErrorCode(err, http.StatusNotFound) {
bs, err := composite.GetBackendService(fakeGCE, key, features.VersionFromServicePort(&sp))
if err == nil || !utils.IsHTTPErrorCode(err, http.StatusNotFound) {
if sp.PrimaryIPNEGEnabled {
// It is expected that these Backends should not get cleaned up in the GC loop.
continue
}
return fmt.Errorf("backend for port %+v should not exist, but got %v", sp, bs)
}
}
Expand Down Expand Up @@ -333,7 +338,7 @@ func TestGC(t *testing.T) {
}
}

// Test GC with both ELB and ILBs
// Test GC with both ELB and ILBs. Add in an L4 ILB NEG which should not be deleted as part of GC.
func TestGCMixed(t *testing.T) {
fakeGCE := gce.NewFakeGCECloud(gce.DefaultTestClusterValues())
syncer := newTestSyncer(fakeGCE)
Expand All @@ -345,6 +350,7 @@ func TestGCMixed(t *testing.T) {
{NodePort: 84, Protocol: annotations.ProtocolHTTP, NEGEnabled: true, L7ILBEnabled: true, BackendNamer: defaultNamer},
{NodePort: 85, Protocol: annotations.ProtocolHTTPS, NEGEnabled: true, L7ILBEnabled: true, BackendNamer: defaultNamer},
{NodePort: 86, Protocol: annotations.ProtocolHTTP, NEGEnabled: true, L7ILBEnabled: true, BackendNamer: defaultNamer},
{ID: utils.ServicePortID{Service: types.NamespacedName{Name: "testsvc"}}, PrimaryIPNEGEnabled: true, BackendNamer: defaultNamer},
}
ps := newPortset(svcNodePorts)
if err := ps.add(svcNodePorts); err != nil {
Expand Down
200 changes: 200 additions & 0 deletions pkg/controller/service_controller_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,200 @@
/*
Copyright 2020 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package controller

import (
"k8s.io/ingress-gce/pkg/loadbalancers"
"k8s.io/ingress-gce/pkg/neg/types"
"testing"
"time"

"github.com/GoogleCloudPlatform/k8s-cloud-provider/pkg/cloud"
"github.com/GoogleCloudPlatform/k8s-cloud-provider/pkg/cloud/meta"
api_v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1"
//"k8s.io/ingress-gce/pkg/loadbalancers"
"k8s.io/client-go/kubernetes/fake"
"k8s.io/ingress-gce/pkg/composite"
"k8s.io/ingress-gce/pkg/context"
"k8s.io/ingress-gce/pkg/test"
"k8s.io/ingress-gce/pkg/utils/common"
"k8s.io/ingress-gce/pkg/utils/namer"
"k8s.io/legacy-cloud-providers/gce"
)

func newServiceController() *L4Controller {
kubeClient := fake.NewSimpleClientset()
fakeGCE := gce.NewFakeGCECloud(gce.DefaultTestClusterValues())
(fakeGCE.Compute().(*cloud.MockGCE)).MockForwardingRules.InsertHook = loadbalancers.InsertForwardingRuleHook

namer := namer.NewNamer(clusterUID, "")

stopCh := make(chan struct{})
ctxConfig := context.ControllerContextConfig{
Namespace: api_v1.NamespaceAll,
ResyncPeriod: 1 * time.Minute,
}
ctx := context.NewControllerContext(nil, kubeClient, nil, nil, fakeGCE, namer, "" /*kubeSystemUID*/, ctxConfig)
return NewL4Controller(ctx, stopCh)
}

func addILBService(l4c *L4Controller, svc *api_v1.Service) {
l4c.ctx.KubeClient.CoreV1().Services(svc.Namespace).Create(svc)
l4c.ctx.ServiceInformer.GetIndexer().Add(svc)
}

func updateILBService(l4c *L4Controller, svc *api_v1.Service) {
l4c.ctx.KubeClient.CoreV1().Services(svc.Namespace).Update(svc)
l4c.ctx.ServiceInformer.GetIndexer().Update(svc)
}

func deleteILBService(l4c *L4Controller, svc *api_v1.Service) {
l4c.ctx.KubeClient.CoreV1().Services(svc.Namespace).Delete(svc.Name, &v1.DeleteOptions{})
l4c.ctx.ServiceInformer.GetIndexer().Delete(svc)
}

func addNEG(l4c *L4Controller, svc *api_v1.Service) {
// Also create a fake NEG for this service since the sync code will try to link the backend service to NEG
negName := l4c.ctx.ClusterNamer.PrimaryIPNEG(svc.Namespace, svc.Name)
neg := &composite.NetworkEndpointGroup{Name: negName}
key := meta.ZonalKey(negName, types.TestZone1)
composite.CreateNetworkEndpointGroup(l4c.ctx.Cloud, key, neg)
}

func getKeyForSvc(svc *api_v1.Service, t *testing.T) string {
key, err := common.KeyFunc(svc)
if err != nil {
t.Fatalf("Failed to get key for service %v, err : %v", svc, err)
}
return key
}

func validateSvcStatus(svc *api_v1.Service, expectStatus bool, t *testing.T) {
if common.HasGivenFinalizer(svc.ObjectMeta, common.ILBFinalizerV2) != expectStatus {
t.Fatalf("Expected L4 finalizer present to be %v, but it was %v", expectStatus, !expectStatus)
}
if len(svc.Status.LoadBalancer.Ingress) == 0 || svc.Status.LoadBalancer.Ingress[0].IP == "" {
if expectStatus {
t.Fatalf("Invalid LoadBalancer status field in service - %+v", svc.Status.LoadBalancer)
}
}
if len(svc.Status.LoadBalancer.Ingress) > 0 && !expectStatus {
t.Fatalf("Expected LoadBalancer status to be empty, Got %v", svc.Status.LoadBalancer)
}
}

// TestProcessCreateOrUpdate verifies the processing loop in L4Controller.
// This test adds a new service, then performs a valid update and then modifies the service type to External and ensures
// that the status field is as expected in each case.
func TestProcessCreateOrUpdate(t *testing.T) {
l4c := newServiceController()
newSvc := test.NewL4ILBService(false, 8080)
addILBService(l4c, newSvc)
addNEG(l4c, newSvc)
err := l4c.sync(getKeyForSvc(newSvc, t))
if err != nil {
t.Errorf("Failed to sync newly added service %s, err %v", newSvc.Name, err)
}
// List the service and ensure that it contains the finalizer as well as Status field.
svc, err := l4c.client.CoreV1().Services(newSvc.Namespace).Get(newSvc.Name, v1.GetOptions{})
if err != nil {
t.Errorf("Failed to lookup service %s, err: %v", newSvc.Name, err)
}
validateSvcStatus(svc, true, t)

// set the TrafficPolicy of the service to Local
newSvc.Spec.ExternalTrafficPolicy = api_v1.ServiceExternalTrafficPolicyTypeLocal
updateILBService(l4c, newSvc)
err = l4c.sync(getKeyForSvc(newSvc, t))
if err != nil {
t.Errorf("Failed to sync updated service %s, err %v", newSvc.Name, err)
}
// List the service and ensure that it contains the finalizer as well as Status field.
svc, err = l4c.client.CoreV1().Services(newSvc.Namespace).Get(newSvc.Name, v1.GetOptions{})
if err != nil {
t.Errorf("Failed to lookup service %s, err: %v", newSvc.Name, err)
}
validateSvcStatus(svc, true, t)

// Remove the Internal LoadBalancer annotation, this should trigger a cleanup.
delete(newSvc.Annotations, gce.ServiceAnnotationLoadBalancerType)
updateILBService(l4c, newSvc)
err = l4c.sync(getKeyForSvc(newSvc, t))
if err != nil {
t.Errorf("Failed to sync updated service %s, err %v", newSvc.Name, err)
}
// List the service and ensure that it contains the finalizer as well as Status field.
svc, err = l4c.client.CoreV1().Services(newSvc.Namespace).Get(newSvc.Name, v1.GetOptions{})
if err != nil {
t.Errorf("Failed to lookup service %s, err: %v", newSvc.Name, err)
}
validateSvcStatus(svc, false, t)
}

func TestProcessDeletion(t *testing.T) {
l4c := newServiceController()
newSvc := test.NewL4ILBService(false, 8080)
addILBService(l4c, newSvc)
addNEG(l4c, newSvc)
err := l4c.sync(getKeyForSvc(newSvc, t))
if err != nil {
t.Errorf("Failed to sync newly added service %s, err %v", newSvc.Name, err)
}
// List the service and ensure that it contains the finalizer as well as Status field.
svc, err := l4c.client.CoreV1().Services(newSvc.Namespace).Get(newSvc.Name, v1.GetOptions{})
if err != nil {
t.Errorf("Failed to lookup service %s, err: %v", newSvc.Name, err)
}
validateSvcStatus(svc, true, t)

// Mark the service for deletion by updating timestamp. Use svc instead of newSvc since that has the finalizer.
// TODO use patch instead of update here as well.
svc.DeletionTimestamp = &v1.Time{}
updateILBService(l4c, svc)
err = l4c.sync(getKeyForSvc(svc, t))
if err != nil {
t.Errorf("Failed to sync updated service %s, err %v", svc.Name, err)
}
svc, err = l4c.client.CoreV1().Services(svc.Namespace).Get(svc.Name, v1.GetOptions{})
if err != nil {
t.Errorf("Failed to lookup service %s, err: %v", newSvc.Name, err)
}
validateSvcStatus(svc, false, t)
deleteILBService(l4c, svc)
svc, err = l4c.client.CoreV1().Services(newSvc.Namespace).Get(svc.Name, v1.GetOptions{})
if svc != nil {
t.Errorf("Expected service to be deleted, but was found - %v", svc)
}
}

func TestProcessCreateLegacyService(t *testing.T) {
l4c := newServiceController()
newSvc := test.NewL4ILBService(false, 8080)
// Set the legacy finalizer
newSvc.Finalizers = append(newSvc.Finalizers, common.LegacyILBFinalizer)
addILBService(l4c, newSvc)
err := l4c.sync(getKeyForSvc(newSvc, t))
if err != nil {
t.Errorf("Failed to sync newly added service %s, err %v", newSvc.Name, err)
}
// List the service and ensure that the status field is not updated.
svc, err := l4c.client.CoreV1().Services(newSvc.Namespace).Get(newSvc.Name, v1.GetOptions{})
if err != nil {
t.Errorf("Failed to lookup service %s, err: %v", newSvc.Name, err)
}
validateSvcStatus(svc, false, t)
}
7 changes: 7 additions & 0 deletions pkg/loadbalancers/fakes.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,10 @@ func InsertGlobalForwardingRuleHook(ctx context.Context, key *meta.Key, obj *com
}
return false, nil
}

func InsertForwardingRuleHook(ctx context.Context, key *meta.Key, obj *compute.ForwardingRule, m *cloud.MockForwardingRules) (b bool, e error) {
if obj.IPAddress == "" {
obj.IPAddress = "10.0.0.1"
}
return false, nil
}
Loading

0 comments on commit a39607f

Please sign in to comment.