@@ -12,6 +12,9 @@ import (
1212 "strconv"
1313 "testing"
1414
15+ nnc "github.com/Azure/azure-container-networking/nodenetworkconfig/api/v1alpha"
16+ metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
17+
1518 "github.com/Azure/azure-container-networking/cns"
1619 "github.com/Azure/azure-container-networking/cns/common"
1720 "github.com/Azure/azure-container-networking/cns/fakes"
@@ -129,7 +132,38 @@ func TestMain(m *testing.M) {
129132 httpRestService , err := restserver .NewHTTPRestService (& config , fakes .NewFakeImdsClient (), fakes .NewFakeNMAgentClient ())
130133 svc = httpRestService .(* restserver.HTTPRestService )
131134 svc .Name = "cns-test-server"
132- svc .IPAMPoolMonitor = fakes .NewIPAMPoolMonitorFake ()
135+ fakeNNC := nnc.NodeNetworkConfig {
136+ TypeMeta : metav1.TypeMeta {},
137+ ObjectMeta : metav1.ObjectMeta {},
138+ Spec : nnc.NodeNetworkConfigSpec {
139+ RequestedIPCount : 16 ,
140+ IPsNotInUse : []string {"abc" },
141+ },
142+ Status : nnc.NodeNetworkConfigStatus {
143+ Scaler : nnc.Scaler {
144+ BatchSize : 10 ,
145+ ReleaseThresholdPercent : 50 ,
146+ RequestThresholdPercent : 40 ,
147+ },
148+ NetworkContainers : []nnc.NetworkContainer {
149+ {
150+ ID : "nc1" ,
151+ PrimaryIP : "10.0.0.11" ,
152+ SubnetName : "sub1" ,
153+ IPAssignments : []nnc.IPAssignment {
154+ {
155+ Name : "ip1" ,
156+ IP : "10.0.0.10" ,
157+ },
158+ },
159+ DefaultGateway : "10.0.0.1" ,
160+ SubnetAddressSpace : "10.0.0.0/24" ,
161+ Version : 2 ,
162+ },
163+ },
164+ },
165+ }
166+ svc .IPAMPoolMonitor = & fakes.IPAMPoolMonitorFake {FakeMinimumIps : 10 , FakeMaximumIps : 20 , FakeIpsNotInUseCount : 13 , FakecachedNNC : fakeNNC }
133167
134168 if err != nil {
135169 logger .Errorf ("Failed to create CNS object, err:%v.\n " , err )
@@ -254,8 +288,7 @@ func TestCNSClientPodContextApi(t *testing.T) {
254288 podNamespace := "testpodnamespace"
255289 desiredIpAddress := "10.0.0.5"
256290
257- secondaryIps := make ([]string , 0 )
258- secondaryIps = append (secondaryIps , desiredIpAddress )
291+ secondaryIps := []string {desiredIpAddress }
259292 cnsClient , _ := InitCnsClient ("" )
260293
261294 addTestStateToRestServer (t , secondaryIps )
@@ -282,4 +315,79 @@ func TestCNSClientPodContextApi(t *testing.T) {
282315 }
283316
284317 t .Log (podcontext )
318+
319+ // release requested IP address, expect success
320+ err = cnsClient .ReleaseIPAddress (orchestratorContext )
321+ if err != nil {
322+ t .Fatalf ("Expected to not fail when releasing IP reservation found with context: %+v" , err )
323+ }
324+ }
325+
326+ func TestCNSClientDebugAPI (t * testing.T ) {
327+ podName := "testpodname"
328+ podNamespace := "testpodnamespace"
329+ desiredIpAddress := "10.0.0.5"
330+
331+ secondaryIps := []string {desiredIpAddress }
332+ cnsClient , _ := InitCnsClient ("" )
333+
334+ addTestStateToRestServer (t , secondaryIps )
335+
336+ podInfo := cns.KubernetesPodInfo {PodName : podName , PodNamespace : podNamespace }
337+ orchestratorContext , err := json .Marshal (podInfo )
338+ if err != nil {
339+ t .Fatal (err )
340+ }
341+
342+ // request IP address
343+ _ , err1 := cnsClient .RequestIPAddress (orchestratorContext )
344+ if err1 != nil {
345+ t .Fatalf ("get IP from CNS failed with %+v" , err1 )
346+ }
347+
348+ //test for debug api/cmd to get inmemory data from HTTPRestService
349+ inmemory , err := cnsClient .GetHTTPServiceData ()
350+ if err != nil {
351+ t .Errorf ("Get in-memory http REST Struct failed %+v" , err )
352+ }
353+
354+ if len (inmemory .HttpRestServiceData .PodIPIDByOrchestratorContext ) < 1 {
355+ t .Errorf ("OrchestratorContext map is expected but not returned" )
356+ }
357+
358+ //testing Pod IP Configuration Status values set for test
359+ podConfig := inmemory .HttpRestServiceData .PodIPConfigState
360+ for _ , v := range podConfig {
361+ if v .IPAddress != "10.0.0.5" || v .State != "Allocated" || v .NCID != "testNcId1" {
362+ t .Errorf ("Not the expected set values for testing IPConfigurationStatus, %+v" , podConfig )
363+ }
364+ }
365+ if len (inmemory .HttpRestServiceData .PodIPConfigState ) < 1 {
366+ t .Errorf ("PodIpConfigState with atleast 1 entry expected but not returned." )
367+ }
368+
369+ testIpamPoolMonitor := inmemory .HttpRestServiceData .IPAMPoolMonitor
370+ if testIpamPoolMonitor .MinimumFreeIps != 10 || testIpamPoolMonitor .MaximumFreeIps != 20 || testIpamPoolMonitor .UpdatingIpsNotInUseCount != 13 {
371+ t .Errorf ("IPAMPoolMonitor state is not reflecting the initial set values, %+v" , testIpamPoolMonitor )
372+ }
373+
374+ //check for cached NNC Spec struct values
375+ if testIpamPoolMonitor .CachedNNC .Spec .RequestedIPCount != 16 || len (testIpamPoolMonitor .CachedNNC .Spec .IPsNotInUse ) != 1 {
376+ t .Errorf ("IPAMPoolMonitor cached NNC Spec is not reflecting the initial set values, %+v" , testIpamPoolMonitor .CachedNNC .Spec )
377+ }
378+
379+ //check for cached NNC Status struct values
380+ if testIpamPoolMonitor .CachedNNC .Status .Scaler .BatchSize != 10 || testIpamPoolMonitor .CachedNNC .Status .Scaler .ReleaseThresholdPercent != 50 || testIpamPoolMonitor .CachedNNC .Status .Scaler .RequestThresholdPercent != 40 {
381+ t .Errorf ("IPAMPoolMonitor cached NNC Status is not reflecting the initial set values, %+v" , testIpamPoolMonitor .CachedNNC .Status .Scaler )
382+ }
383+
384+ if len (testIpamPoolMonitor .CachedNNC .Status .NetworkContainers ) != 1 {
385+ t .Errorf ("Expected only one Network Container in the list, %+v" , testIpamPoolMonitor .CachedNNC .Status .NetworkContainers )
386+ }
387+
388+ t .Logf ("In-memory Data: " )
389+ t .Logf ("PodIPIDByOrchestratorContext: %+v" , inmemory .HttpRestServiceData .PodIPIDByOrchestratorContext )
390+ t .Logf ("PodIPConfigState: %+v" , inmemory .HttpRestServiceData .PodIPConfigState )
391+ t .Logf ("IPAMPoolMonitor: %+v" , inmemory .HttpRestServiceData .IPAMPoolMonitor )
392+
285393}
0 commit comments