@@ -2,18 +2,54 @@ package restserver_test
22
33import (
44 "context"
5+ "net"
56 "testing"
67
7- "github.com/Azure/azure-container-networking/cns"
8+ "github.com/Azure/azure-container-networking/cns/cnireconciler "
89 "github.com/Azure/azure-container-networking/cns/logger"
910 "github.com/Azure/azure-container-networking/cns/restserver"
11+ "github.com/Azure/azure-container-networking/cns/types"
12+ "github.com/Azure/azure-container-networking/store"
1013)
1114
12- // Mock implementation of PodInfoByIPProvider
13- type MockPodInfoByIPProvider struct {}
15+ func getMockStore () store.KeyValueStore {
16+ mockStore := store .NewMockStore ("" )
17+ endpointState := map [string ]* restserver.EndpointInfo {
18+ "12e65d89e58cb23c784e97840cf76866bfc9902089bdc8e87e9f64032e312b0b" : {
19+ PodName : "coredns-54b69f46b8-ldmwr" ,
20+ PodNamespace : "kube-system" ,
21+ IfnameToIPMap : map [string ]* restserver.IPInfo {
22+ "eth0" : {
23+ IPv4 : []net.IPNet {
24+ {
25+ IP : net .IPv4 (10 , 0 , 0 , 52 ),
26+ Mask : net .CIDRMask (24 , 32 ),
27+ },
28+ },
29+ },
30+ },
31+ },
32+ "1fc5176913a3a1a7facfb823dde3b4ded404041134fef4f4a0c8bba140fc0413" : {
33+ PodName : "load-test-7f7d49687d-wxc9p" ,
34+ PodNamespace : "load-test" ,
35+ IfnameToIPMap : map [string ]* restserver.IPInfo {
36+ "eth0" : {
37+ IPv4 : []net.IPNet {
38+ {
39+ IP : net .IPv4 (10 , 0 , 0 , 63 ),
40+ Mask : net .CIDRMask (24 , 32 ),
41+ },
42+ },
43+ },
44+ },
45+ },
46+ }
1447
15- func (m * MockPodInfoByIPProvider ) PodInfoByIP () (res map [string ]cns.PodInfo , err error ) {
16- return res , nil
48+ err := mockStore .Write (restserver .EndpointStoreKey , endpointState )
49+ if err != nil {
50+ return nil
51+ }
52+ return mockStore
1753}
1854
1955// Mock implementation of CNIConflistGenerator
@@ -32,7 +68,10 @@ func (m *MockCNIConflistGenerator) Close() error {
3268}
3369
3470func TestNodeSubnet (t * testing.T ) {
35- mockPodInfoProvider := & MockPodInfoByIPProvider {}
71+ podInfoByIPProvider , err := cnireconciler .NewCNSPodInfoProvider (getMockStore ())
72+ if err != nil {
73+ t .Fatalf ("NewCNSPodInfoProvider returned an error: %v" , err )
74+ }
3675
3776 // Create a real HTTPRestService object
3877 mockCNIConflistGenerator := & MockCNIConflistGenerator {
@@ -42,15 +81,22 @@ func TestNodeSubnet(t *testing.T) {
4281 ctx , cancel := testContext (t )
4382 defer cancel ()
4483
45- err := service .InitializeNodeSubnet (ctx , mockPodInfoProvider )
46- service .StartNodeSubnet (ctx )
84+ err = service .InitializeNodeSubnet (ctx , podInfoByIPProvider )
85+ if err != nil {
86+ t .Fatalf ("InitializeNodeSubnet returned an error: %v" , err )
87+ }
4788
48- if service .GetNodesubnetIPFetcher () == nil {
49- t .Error ("NodeSubnetIPFetcher is not initialized" )
89+ expectedIPs := map [string ]types.IPState {
90+ "10.0.0.52" : types .Assigned ,
91+ "10.0.0.63" : types .Assigned ,
5092 }
5193
52- if err != nil {
53- t .Fatalf ("InitializeNodeSubnet returned an error: %v" , err )
94+ checkIPassignment (t , service , expectedIPs )
95+
96+ service .StartNodeSubnet (ctx )
97+
98+ if service .GetNodesubnetIPFetcher () == nil {
99+ t .Fatal ("NodeSubnetIPFetcher is not initialized" )
54100 }
55101
56102 select {
@@ -60,6 +106,23 @@ func TestNodeSubnet(t *testing.T) {
60106 case <- mockCNIConflistGenerator .GenerateCalled :
61107 break
62108 }
109+
110+ expectedIPs ["10.0.0.45" ] = types .Available
111+ checkIPassignment (t , service , expectedIPs )
112+ }
113+
114+ func checkIPassignment (t * testing.T , service * restserver.HTTPRestService , expectedIPs map [string ]types.IPState ) {
115+ if len (service .PodIPConfigState ) != len (expectedIPs ) {
116+ t .Fatalf ("expected 2 entries in PodIPConfigState, got %d" , len (service .PodIPConfigState ))
117+ }
118+
119+ for ip , config := range service .GetPodIPConfigState () {
120+ if assignmentState , exists := expectedIPs [ip ]; ! exists {
121+ t .Fatalf ("unexpected IP %s in PodIPConfigState" , ip )
122+ } else if config .GetState () != assignmentState {
123+ t .Fatalf ("expected state 'Assigned' for IP %s, got %s" , ip , config .GetState ())
124+ }
125+ }
63126}
64127
65128// testContext creates a context from the provided testing.T that will be
0 commit comments