Skip to content

Commit

Permalink
unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
prameshj committed Jul 24, 2020
1 parent 49bcef6 commit eb37f88
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 19 deletions.
88 changes: 71 additions & 17 deletions pkg/neg/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -282,28 +282,58 @@ func TestEnableNEGServiceWithIngress(t *testing.T) {
}

//TestEnableNEGSeviceWithL4ILB tests L4 ILB service with NEGs enabled.
//Also verifies that modifying the TrafficPolicy on the service will
//take effect.
func TestEnableNEGServiceWithL4ILB(t *testing.T) {
controller := newTestController(fake.NewSimpleClientset())
controller.runL4 = true
defer controller.stop()
for _, randomize := range []bool{false, true} {
controller.serviceLister.Add(newTestILBService(controller, !randomize, 80))
svcClient := controller.client.CoreV1().Services(testServiceNamespace)
svcKey := utils.ServiceKeyFunc(testServiceNamespace, testServiceName)
err := controller.processService(svcKey)
if err != nil {
t.Fatalf("Failed to process service: %v", err)
}
svc, err := svcClient.Get(context2.TODO(), testServiceName, metav1.GetOptions{})
if err != nil {
t.Fatalf("Service was not created.(*apiv1.Service) successfully, err: %v", err)
}
validateSyncers(t, controller, 1, false)
expectedPortInfoMap := negtypes.NewPortInfoMapForVMIPNEG(testServiceNamespace, testServiceName,
controller.namer, randomize)
validateSyncerManagerWithPortInfoMap(t, controller, testServiceNamespace, testServiceName, expectedPortInfoMap)
validateServiceAnnotationWithPortInfoMap(t, svc, expectedPortInfoMap)
var prevSyncerKey, updatedSyncerKey negtypes.NegSyncerKey
randomize := false
t.Logf("Creating L4 ILB service with ExternalTrafficPolicy:Cluster")
controller.serviceLister.Add(newTestILBService(controller, !randomize, 80))
svcClient := controller.client.CoreV1().Services(testServiceNamespace)
svcKey := utils.ServiceKeyFunc(testServiceNamespace, testServiceName)
err := controller.processService(svcKey)
if err != nil {
t.Fatalf("Failed to process service: %v", err)
}
svc, err := svcClient.Get(context2.TODO(), testServiceName, metav1.GetOptions{})
if err != nil {
t.Fatalf("Service was not created.(*apiv1.Service) successfully, err: %v", err)
}
expectedPortInfoMap := negtypes.NewPortInfoMapForVMIPNEG(testServiceNamespace, testServiceName,
controller.namer, randomize)
// There will be only one entry in the map
for key, val := range expectedPortInfoMap {
prevSyncerKey = getSyncerKey(testServiceNamespace, testServiceName, key, val)
}
ValidateSyncerByKey(t, controller, 1, prevSyncerKey, false)
validateSyncerManagerWithPortInfoMap(t, controller, testServiceNamespace, testServiceName, expectedPortInfoMap)
validateServiceAnnotationWithPortInfoMap(t, svc, expectedPortInfoMap)
// Now Update the service to change the TrafficPolicy
t.Logf("Updating L4 ILB service from ExternalTrafficPolicy:Cluster to Local")
randomize = !randomize
if err = controller.serviceLister.Update(updateTestILBService(controller, !randomize, svc)); err != nil {
t.Fatalf("Failed to update test L4 ILB service: %v", err)
}
if err = controller.processService(svcKey); err != nil {
t.Fatalf("Failed to process updated L4 ILB srvice: %v", err)
}
expectedPortInfoMap = negtypes.NewPortInfoMapForVMIPNEG(testServiceNamespace, testServiceName,
controller.namer, randomize)
// There will be only one entry in the map
for key, val := range expectedPortInfoMap {
updatedSyncerKey = getSyncerKey(testServiceNamespace, testServiceName, key, val)
}
// there should only be 2 syncers - one stopped and one running.
ValidateSyncerByKey(t, controller, 2, updatedSyncerKey, false)
ValidateSyncerByKey(t, controller, 2, prevSyncerKey, true)
time.Sleep(1 * time.Second)
controller.manager.(*syncerManager).GC()
// check the port info map after all stale syncers have been deleted.
validateSyncerManagerWithPortInfoMap(t, controller, testServiceNamespace, testServiceName, expectedPortInfoMap)
validateServiceAnnotationWithPortInfoMap(t, svc, expectedPortInfoMap)
}

// TestEnableNEGServiceWithILBIngress tests ILB service with NEG enabled
Expand Down Expand Up @@ -882,6 +912,20 @@ func validateSyncers(t *testing.T, controller *Controller, num int, stopped bool
}
}

func ValidateSyncerByKey(t *testing.T, controller *Controller, num int, expectedKey negtypes.NegSyncerKey, stopped bool) {
t.Helper()
if len(controller.manager.(*syncerManager).syncerMap) != num {
t.Errorf("got %v syncer, want %v.", len(controller.manager.(*syncerManager).syncerMap), num)
}
if syncer, ok := controller.manager.(*syncerManager).syncerMap[expectedKey]; ok {
if syncer.IsStopped() == stopped {
return
}
t.Errorf("got syncer %q IsStopped() == %v, want %v.", expectedKey, syncer.IsStopped(), stopped)
}
t.Errorf("got %v syncer map, want syncer key %q", controller.manager.(*syncerManager).syncerMap, expectedKey)
}

func validateSyncerManagerWithPortInfoMap(t *testing.T, controller *Controller, namespace, svcName string, expectedPortInfoMap negtypes.PortInfoMap) {
t.Helper()
if len(controller.manager.(*syncerManager).syncerMap) != len(expectedPortInfoMap) {
Expand Down Expand Up @@ -1126,6 +1170,16 @@ func newTestILBService(c *Controller, onlyLocal bool, port int) *apiv1.Service {
return svc
}

func updateTestILBService(c *Controller, onlyLocal bool, svc *apiv1.Service) *apiv1.Service {
if onlyLocal {
svc.Spec.ExternalTrafficPolicy = apiv1.ServiceExternalTrafficPolicyTypeLocal
} else {
svc.Spec.ExternalTrafficPolicy = apiv1.ServiceExternalTrafficPolicyTypeCluster
}
c.client.CoreV1().Services(svc.Namespace).Update(context2.TODO(), svc, metav1.UpdateOptions{})
return svc
}

func newTestService(c *Controller, negIngress bool, negSvcPorts []int32) *apiv1.Service {
svcAnnotations := map[string]string{}
if negIngress || len(negSvcPorts) > 0 {
Expand Down
7 changes: 5 additions & 2 deletions pkg/neg/syncers/endpoints_calculator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,11 @@ func TestLocalGetEndpointSet(t *testing.T) {
{
desc: "no endpoints",
endpoints: &v1.Endpoints{},
// No nodes are picked as there are no service endpoints.
endpointSets: nil,
// 3 nodes are picked at random when there are no service endpoints.
endpointSets: map[string]negtypes.NetworkEndpointSet{
negtypes.TestZone1: negtypes.NewNetworkEndpointSet(negtypes.NetworkEndpoint{IP: "1.2.3.2", Node: testInstance2}),
negtypes.TestZone2: negtypes.NewNetworkEndpointSet(negtypes.NetworkEndpoint{IP: "1.2.3.4", Node: testInstance4}, negtypes.NetworkEndpoint{IP: "1.2.3.6", Node: testInstance6}),
},
networkEndpointType: negtypes.VmIpEndpointType,
},
}
Expand Down

0 comments on commit eb37f88

Please sign in to comment.