@@ -45,6 +45,90 @@ func TestCreateOrUpdateNetworkContainerInternal(t *testing.T) {
4545 validateCreateOrUpdateNCInternal (t , 2 , "-1" )
4646}
4747
48+ // TestReconcileNCStatePrimaryIPChangeShouldFail tests that reconciling NC state with
49+ // a NC whose IP has changed should fail
50+ func TestReconcileNCStatePrimaryIPChangeShouldFail (t * testing.T ) {
51+ restartService ()
52+ setEnv (t )
53+ setOrchestratorTypeInternal (cns .KubernetesCRD )
54+ svc .state .ContainerStatus = make (map [string ]containerstatus )
55+
56+ // start with a NC in state
57+ ncID := "555ac5c9-89f2-4b5d-b8d0-616894d6d151"
58+ svc .state .ContainerStatus [ncID ] = containerstatus {
59+ ID : ncID ,
60+ VMVersion : "0" ,
61+ HostVersion : "0" ,
62+ CreateNetworkContainerRequest : cns.CreateNetworkContainerRequest {
63+ NetworkContainerid : ncID ,
64+ IPConfiguration : cns.IPConfiguration {
65+ IPSubnet : cns.IPSubnet {
66+ IPAddress : "10.0.1.0" ,
67+ PrefixLength : 24 ,
68+ },
69+ },
70+ },
71+ }
72+
73+ // now try to reconcile the state where the NC primary IP has changed
74+ resp := svc .ReconcileNCState (& cns.CreateNetworkContainerRequest {
75+ NetworkContainerid : ncID ,
76+ IPConfiguration : cns.IPConfiguration {
77+ IPSubnet : cns.IPSubnet {
78+ IPAddress : "10.0.2.0" , // note this IP has changed
79+ PrefixLength : 24 ,
80+ },
81+ },
82+ }, map [string ]cns.PodInfo {}, & v1alpha.NodeNetworkConfig {})
83+
84+ assert .Equal (t , types .PrimaryCANotSame , resp )
85+ }
86+
87+ // TestReconcileNCStateGatewayChange tests that NC state gets updated when reconciled
88+ // if the NC's gateway IP has changed
89+ func TestReconcileNCStateGatewayChange (t * testing.T ) {
90+ restartService ()
91+ setEnv (t )
92+ setOrchestratorTypeInternal (cns .KubernetesCRD )
93+ svc .state .ContainerStatus = make (map [string ]containerstatus )
94+
95+ // start with a NC in state
96+ ncID := "555ac5c9-89f2-4b5d-b8d0-616894d6d151"
97+ oldGW := "10.0.0.0"
98+ newGW := "10.0.1.0"
99+ ncPrimaryIP := cns.IPSubnet {
100+ IPAddress : "10.0.1.1" ,
101+ PrefixLength : 24 ,
102+ }
103+ svc .state .ContainerStatus [ncID ] = containerstatus {
104+ ID : ncID ,
105+ VMVersion : "0" ,
106+ HostVersion : "0" ,
107+ CreateNetworkContainerRequest : cns.CreateNetworkContainerRequest {
108+ NetworkContainerid : ncID ,
109+ NetworkContainerType : cns .Kubernetes ,
110+ IPConfiguration : cns.IPConfiguration {
111+ IPSubnet : ncPrimaryIP ,
112+ GatewayIPAddress : oldGW ,
113+ },
114+ },
115+ }
116+
117+ // now try to reconcile the state where the NC gateway has changed
118+ resp := svc .ReconcileNCState (& cns.CreateNetworkContainerRequest {
119+ NetworkContainerid : ncID ,
120+ NetworkContainerType : cns .Kubernetes ,
121+ IPConfiguration : cns.IPConfiguration {
122+ IPSubnet : ncPrimaryIP ,
123+ GatewayIPAddress : newGW , // note this IP has changed
124+ },
125+ }, map [string ]cns.PodInfo {}, & v1alpha.NodeNetworkConfig {})
126+
127+ assert .Equal (t , types .Success , resp )
128+ // assert the new state reflects the gateway update
129+ assert .Equal (t , newGW , svc .state .ContainerStatus [ncID ].CreateNetworkContainerRequest .IPConfiguration .GatewayIPAddress )
130+ }
131+
48132func TestCreateOrUpdateNCWithLargerVersionComparedToNMAgent (t * testing.T ) {
49133 restartService ()
50134
0 commit comments