2 // Copyright 2019 AT&T Intellectual Property
3 // Copyright 2019 Nokia
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
9 // http://www.apache.org/licenses/LICENSE-2.0
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
18 // This source code is part of the near-RT RIC (RAN Intelligent Controller)
19 // platform project (RICP).
27 "e2mgr/e2managererrors"
33 "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/common"
34 "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/entities"
35 "github.com/stretchr/testify/assert"
36 "github.com/stretchr/testify/mock"
38 //"k8s.io/apimachinery/pkg/runtime"
39 //"k8s.io/client-go/kubernetes/fake"
45 const E2TAddress3 = "10.10.2.17:9800"
47 func initE2TShutdownManagerTest(t *testing.T) (*E2TShutdownManager, *mocks.RnibReaderMock, *mocks.RnibWriterMock, *mocks.HttpClientMock, *KubernetesManager) {
49 config := &configuration.Configuration{RnibRetryIntervalMs: 10, MaxRnibConnectionAttempts: 3, E2TInstanceDeletionTimeoutMs: 15000}
51 readerMock := &mocks.RnibReaderMock{}
52 writerMock := &mocks.RnibWriterMock{}
53 rnibDataService := services.NewRnibDataService(log, config, readerMock, writerMock)
55 e2tInstancesManager := NewE2TInstancesManager(rnibDataService, log)
56 httpClientMock := &mocks.HttpClientMock{}
57 rmClient := clients.NewRoutingManagerClient(log, config, httpClientMock)
59 ranListManager := NewRanListManager(log)
60 ranAlarmService := services.NewRanAlarmService(log, config)
61 ranConnectStatusChangeManager := NewRanConnectStatusChangeManager(log, rnibDataService,ranListManager, ranAlarmService)
62 associationManager := NewE2TAssociationManager(log, rnibDataService, e2tInstancesManager, rmClient, ranConnectStatusChangeManager)
63 //kubernetesManager := initKubernetesManagerTest(t)
65 /*shutdownManager := NewE2TShutdownManager(log, config, rnibDataService, e2tInstancesManager, associationManager, kubernetesManager)
67 return shutdownManager, readerMock, writerMock, httpClientMock, kubernetesManager*/
68 shutdownManager := NewE2TShutdownManager(log, config, rnibDataService, e2tInstancesManager, associationManager, nil)
70 return shutdownManager, readerMock, writerMock, httpClientMock, nil
73 func TestShutdownSuccess1OutOf3Instances(t *testing.T) {
74 shutdownManager, readerMock, writerMock, httpClientMock,_ := initE2TShutdownManagerTest(t)
76 e2tInstance1 := entities.NewE2TInstance(E2TAddress, PodName)
77 e2tInstance1.State = entities.Active
78 e2tInstance1.AssociatedRanList = []string{"test1", "test2", "test5"}
79 e2tInstance2 := entities.NewE2TInstance(E2TAddress2, PodName)
80 e2tInstance2.State = entities.Active
81 e2tInstance2.AssociatedRanList = []string{"test3"}
82 e2tInstance3 := entities.NewE2TInstance(E2TAddress3, PodName)
83 e2tInstance3.State = entities.Active
84 e2tInstance3.AssociatedRanList = []string{"test4"}
85 writerMock.On("SaveE2TInstance", mock.MatchedBy(func(e2tInstance *entities.E2TInstance) bool { return e2tInstance.Address == E2TAddress && e2tInstance.State == entities.ToBeDeleted })).Return(nil)
87 nodeb1 := &entities.NodebInfo{RanName:"test1", AssociatedE2TInstanceAddress:E2TAddress, ConnectionStatus:entities.ConnectionStatus_CONNECTED, E2ApplicationProtocol:entities.E2ApplicationProtocol_X2_SETUP_REQUEST}
88 readerMock.On("GetNodeb", "test1").Return(nodeb1, nil)
89 nodeb2 := &entities.NodebInfo{RanName:"test2", AssociatedE2TInstanceAddress:E2TAddress, ConnectionStatus:entities.ConnectionStatus_SHUTTING_DOWN, E2ApplicationProtocol:entities.E2ApplicationProtocol_X2_SETUP_REQUEST}
90 readerMock.On("GetNodeb", "test2").Return(nodeb2, nil)
91 nodeb5 := &entities.NodebInfo{RanName:"test5", AssociatedE2TInstanceAddress:E2TAddress, ConnectionStatus:entities.ConnectionStatus_CONNECTED, E2ApplicationProtocol:entities.E2ApplicationProtocol_X2_SETUP_REQUEST}
92 readerMock.On("GetNodeb", "test5").Return(nodeb5, nil)
94 e2tAddresses := []string{E2TAddress, E2TAddress2,E2TAddress3}
95 readerMock.On("GetE2TAddresses").Return(e2tAddresses, nil)
97 data := models.NewRoutingManagerDeleteRequestModel(E2TAddress, e2tInstance1.AssociatedRanList, nil)
98 marshaled, _ := json.Marshal(data)
99 body := bytes.NewBuffer(marshaled)
100 respBody := ioutil.NopCloser(bytes.NewBufferString(""))
101 httpClientMock.On("Delete", "e2t", "application/json", body).Return(&http.Response{StatusCode: http.StatusCreated, Body: respBody}, nil)
103 writerMock.On("RemoveE2TInstance", E2TAddress).Return(nil)
104 writerMock.On("SaveE2TAddresses", []string{E2TAddress2,E2TAddress3}).Return(nil)
106 nodeb1connected := *nodeb1
107 nodeb1connected.AssociatedE2TInstanceAddress = ""
108 nodeb1connected.ConnectionStatus = entities.ConnectionStatus_DISCONNECTED
109 writerMock.On("UpdateNodebInfo", &nodeb1connected).Return(nil)
110 nodeb2connected := *nodeb2
111 nodeb2connected.AssociatedE2TInstanceAddress = ""
112 nodeb2connected.ConnectionStatus = entities.ConnectionStatus_DISCONNECTED
113 writerMock.On("UpdateNodebInfo", &nodeb2connected).Return(nil)
114 nodeb5connected := *nodeb5
115 nodeb5connected.AssociatedE2TInstanceAddress = ""
116 nodeb5connected.ConnectionStatus = entities.ConnectionStatus_DISCONNECTED
117 writerMock.On("UpdateNodebInfo", &nodeb5connected).Return(nil)
119 err := shutdownManager.Shutdown(e2tInstance1)
122 readerMock.AssertExpectations(t)
123 writerMock.AssertExpectations(t)
124 httpClientMock.AssertExpectations(t)
127 func TestShutdownSuccess1InstanceWithoutRans(t *testing.T) {
128 shutdownManager, readerMock, writerMock, httpClientMock,_ := initE2TShutdownManagerTest(t)
130 e2tInstance1 := entities.NewE2TInstance(E2TAddress, PodName)
131 e2tInstance1.State = entities.Active
132 e2tInstance1.AssociatedRanList = []string{}
133 writerMock.On("SaveE2TInstance", mock.MatchedBy(func(e2tInstance *entities.E2TInstance) bool { return e2tInstance.Address == E2TAddress && e2tInstance.State == entities.ToBeDeleted })).Return(nil)
135 data := models.NewRoutingManagerDeleteRequestModel(E2TAddress, nil, nil)
136 marshaled, _ := json.Marshal(data)
137 body := bytes.NewBuffer(marshaled)
138 respBody := ioutil.NopCloser(bytes.NewBufferString(""))
139 httpClientMock.On("Delete", "e2t", "application/json", body).Return(&http.Response{StatusCode: http.StatusCreated, Body: respBody}, nil)
141 writerMock.On("RemoveE2TInstance", E2TAddress).Return(nil)
142 readerMock.On("GetE2TAddresses").Return([]string{E2TAddress}, nil)
143 writerMock.On("SaveE2TAddresses", []string{}).Return(nil)
145 err := shutdownManager.Shutdown(e2tInstance1)
148 readerMock.AssertExpectations(t)
149 writerMock.AssertExpectations(t)
150 httpClientMock.AssertExpectations(t)
153 func TestShutdownSuccess1Instance2Rans(t *testing.T) {
154 shutdownManager, readerMock, writerMock, httpClientMock,_ := initE2TShutdownManagerTest(t)
156 e2tInstance1 := entities.NewE2TInstance(E2TAddress, PodName)
157 e2tInstance1.State = entities.Active
158 e2tInstance1.AssociatedRanList = []string{"test1", "test2"}
159 writerMock.On("SaveE2TInstance", mock.MatchedBy(func(e2tInstance *entities.E2TInstance) bool { return e2tInstance.Address == E2TAddress && e2tInstance.State == entities.ToBeDeleted })).Return(nil)
161 nodeb1 := &entities.NodebInfo{RanName:"test1", AssociatedE2TInstanceAddress:E2TAddress, ConnectionStatus:entities.ConnectionStatus_CONNECTED, E2ApplicationProtocol:entities.E2ApplicationProtocol_X2_SETUP_REQUEST}
162 readerMock.On("GetNodeb", "test1").Return(nodeb1, nil)
163 nodeb2 := &entities.NodebInfo{RanName:"test2", AssociatedE2TInstanceAddress:E2TAddress, ConnectionStatus:entities.ConnectionStatus_DISCONNECTED, E2ApplicationProtocol:entities.E2ApplicationProtocol_X2_SETUP_REQUEST}
164 readerMock.On("GetNodeb", "test2").Return(nodeb2, nil)
166 data := models.NewRoutingManagerDeleteRequestModel(E2TAddress, []string{"test1", "test2"}, nil)
167 marshaled, _ := json.Marshal(data)
168 body := bytes.NewBuffer(marshaled)
169 respBody := ioutil.NopCloser(bytes.NewBufferString(""))
170 httpClientMock.On("Delete", "e2t", "application/json", body).Return(&http.Response{StatusCode: http.StatusCreated, Body: respBody}, nil)
172 writerMock.On("RemoveE2TInstance", E2TAddress).Return(nil)
173 readerMock.On("GetE2TAddresses").Return([]string{E2TAddress}, nil)
174 writerMock.On("SaveE2TAddresses", []string{}).Return(nil)
177 nodeb1new.AssociatedE2TInstanceAddress = ""
178 nodeb1new.ConnectionStatus = entities.ConnectionStatus_DISCONNECTED
179 writerMock.On("UpdateNodebInfo", &nodeb1new).Return(nil)
181 nodeb2new.AssociatedE2TInstanceAddress = ""
182 nodeb2new.ConnectionStatus = entities.ConnectionStatus_DISCONNECTED
183 writerMock.On("UpdateNodebInfo", &nodeb2new).Return(nil)
185 err := shutdownManager.Shutdown(e2tInstance1)
188 readerMock.AssertExpectations(t)
189 writerMock.AssertExpectations(t)
190 httpClientMock.AssertExpectations(t)
194 func TestShutdownE2tInstanceAlreadyBeingDeleted(t *testing.T) {
195 shutdownManager, readerMock, writerMock, httpClientMock,_ := initE2TShutdownManagerTest(t)
197 e2tInstance1 := entities.NewE2TInstance(E2TAddress, PodName)
198 e2tInstance1.State = entities.ToBeDeleted
199 e2tInstance1.AssociatedRanList = []string{"test1"}
200 e2tInstance1.DeletionTimestamp = time.Now().UnixNano()
202 err := shutdownManager.Shutdown(e2tInstance1)
205 readerMock.AssertExpectations(t)
206 writerMock.AssertExpectations(t)
207 httpClientMock.AssertExpectations(t)
211 func TestShutdownFailureMarkInstanceAsToBeDeleted(t *testing.T) {
212 shutdownManager, readerMock, writerMock, httpClientMock,_ := initE2TShutdownManagerTest(t)
214 e2tInstance1 := entities.NewE2TInstance(E2TAddress, PodName)
215 e2tInstance1.State = entities.Active
216 e2tInstance1.AssociatedRanList = []string{"test1", "test2", "test5"}
217 writerMock.On("SaveE2TInstance", mock.MatchedBy(func(e2tInstance *entities.E2TInstance) bool { return e2tInstance.Address == E2TAddress && e2tInstance.State == entities.ToBeDeleted })).Return(e2managererrors.NewRnibDbError())
219 err := shutdownManager.Shutdown(e2tInstance1)
221 assert.NotNil(t, err)
222 readerMock.AssertExpectations(t)
223 writerMock.AssertExpectations(t)
224 httpClientMock.AssertExpectations(t)
228 func TestShutdownFailureRoutingManagerError(t *testing.T) {
229 shutdownManager, readerMock, writerMock, httpClientMock,_ := initE2TShutdownManagerTest(t)
231 e2tInstance1 := entities.NewE2TInstance(E2TAddress, PodName)
232 e2tInstance1.State = entities.Active
233 e2tInstance1.AssociatedRanList = []string{"test1", "test2", "test5"}
234 e2tInstance2 := entities.NewE2TInstance(E2TAddress2, PodName)
235 e2tInstance2.State = entities.Active
236 e2tInstance2.AssociatedRanList = []string{"test3"}
237 e2tInstance3 := entities.NewE2TInstance(E2TAddress3, PodName)
238 e2tInstance3.State = entities.Active
239 e2tInstance3.AssociatedRanList = []string{"test4"}
240 writerMock.On("SaveE2TInstance", mock.MatchedBy(func(e2tInstance *entities.E2TInstance) bool { return e2tInstance.Address == E2TAddress && e2tInstance.State == entities.ToBeDeleted })).Return(nil)
242 nodeb1 := &entities.NodebInfo{RanName:"test1", AssociatedE2TInstanceAddress:E2TAddress, ConnectionStatus:entities.ConnectionStatus_CONNECTED, E2ApplicationProtocol:entities.E2ApplicationProtocol_X2_SETUP_REQUEST}
243 readerMock.On("GetNodeb", "test1").Return(nodeb1, nil)
244 nodeb2 := &entities.NodebInfo{RanName:"test2", AssociatedE2TInstanceAddress:E2TAddress, ConnectionStatus:entities.ConnectionStatus_SHUTTING_DOWN, E2ApplicationProtocol:entities.E2ApplicationProtocol_X2_SETUP_REQUEST}
245 readerMock.On("GetNodeb", "test2").Return(nodeb2, nil)
246 nodeb5 := &entities.NodebInfo{RanName:"test5", AssociatedE2TInstanceAddress:E2TAddress, ConnectionStatus:entities.ConnectionStatus_CONNECTED, E2ApplicationProtocol:entities.E2ApplicationProtocol_X2_SETUP_REQUEST}
247 readerMock.On("GetNodeb", "test5").Return(nodeb5, nil)
249 e2tAddresses := []string{E2TAddress, E2TAddress2,E2TAddress3}
250 readerMock.On("GetE2TAddresses").Return(e2tAddresses, nil)
252 data := models.NewRoutingManagerDeleteRequestModel(E2TAddress, e2tInstance1.AssociatedRanList, nil)
253 marshaled, _ := json.Marshal(data)
254 body := bytes.NewBuffer(marshaled)
255 respBody := ioutil.NopCloser(bytes.NewBufferString(""))
256 httpClientMock.On("Delete", "e2t", "application/json", body).Return(&http.Response{StatusCode: http.StatusBadRequest, Body: respBody}, nil)
258 writerMock.On("RemoveE2TInstance", E2TAddress).Return(nil)
259 writerMock.On("SaveE2TAddresses", []string{E2TAddress2,E2TAddress3}).Return(nil)
261 nodeb1connected := *nodeb1
262 nodeb1connected.AssociatedE2TInstanceAddress = ""
263 nodeb1connected.ConnectionStatus = entities.ConnectionStatus_DISCONNECTED
264 writerMock.On("UpdateNodebInfo", &nodeb1connected).Return(nil)
265 nodeb2connected := *nodeb2
266 nodeb2connected.AssociatedE2TInstanceAddress = ""
267 nodeb2connected.ConnectionStatus = entities.ConnectionStatus_DISCONNECTED
268 writerMock.On("UpdateNodebInfo", &nodeb2connected).Return(nil)
269 nodeb5connected := *nodeb5
270 nodeb5connected.AssociatedE2TInstanceAddress = ""
271 nodeb5connected.ConnectionStatus = entities.ConnectionStatus_DISCONNECTED
272 writerMock.On("UpdateNodebInfo", &nodeb5connected).Return(nil)
274 err := shutdownManager.Shutdown(e2tInstance1)
277 readerMock.AssertExpectations(t)
278 writerMock.AssertExpectations(t)
279 httpClientMock.AssertExpectations(t)
283 func TestShutdownFailureInClearNodebsAssociation(t *testing.T) {
284 shutdownManager, readerMock, writerMock, httpClientMock,_ := initE2TShutdownManagerTest(t)
286 e2tInstance1 := entities.NewE2TInstance(E2TAddress, PodName)
287 e2tInstance1.State = entities.Active
288 e2tInstance1.AssociatedRanList = []string{"test1", "test2"}
289 writerMock.On("SaveE2TInstance", mock.MatchedBy(func(e2tInstance *entities.E2TInstance) bool { return e2tInstance.Address == E2TAddress && e2tInstance.State == entities.ToBeDeleted })).Return(nil)
291 nodeb1 := &entities.NodebInfo{RanName:"test1", AssociatedE2TInstanceAddress:E2TAddress, ConnectionStatus:entities.ConnectionStatus_CONNECTED, E2ApplicationProtocol:entities.E2ApplicationProtocol_X2_SETUP_REQUEST}
292 readerMock.On("GetNodeb", "test1").Return(nodeb1, nil)
295 nodeb1new.AssociatedE2TInstanceAddress = ""
296 nodeb1new.ConnectionStatus = entities.ConnectionStatus_DISCONNECTED
297 writerMock.On("UpdateNodebInfo", &nodeb1new).Return(common.NewInternalError(fmt.Errorf("for tests")))
299 err := shutdownManager.Shutdown(e2tInstance1)
301 assert.NotNil(t, err)
302 readerMock.AssertExpectations(t)
303 writerMock.AssertExpectations(t)
304 httpClientMock.AssertExpectations(t)
307 func TestShutdownResourceNotFoundErrorInGetNodeb(t *testing.T) {
308 shutdownManager, readerMock, writerMock, httpClientMock,_ := initE2TShutdownManagerTest(t)
310 e2tInstance1 := entities.NewE2TInstance(E2TAddress, PodName)
311 e2tInstance1.State = entities.Active
312 e2tInstance1.AssociatedRanList = []string{"test1", "test2"}
313 writerMock.On("SaveE2TInstance", mock.MatchedBy(func(e2tInstance *entities.E2TInstance) bool { return e2tInstance.Address == E2TAddress && e2tInstance.State == entities.ToBeDeleted })).Return(nil)
315 nodeb1 := &entities.NodebInfo{RanName:"test1", AssociatedE2TInstanceAddress:E2TAddress, ConnectionStatus:entities.ConnectionStatus_CONNECTED, E2ApplicationProtocol:entities.E2ApplicationProtocol_X2_SETUP_REQUEST}
316 readerMock.On("GetNodeb", "test1").Return(nodeb1, nil)
317 var nodeb2 *entities.NodebInfo
318 readerMock.On("GetNodeb", "test2").Return(nodeb2, common.NewResourceNotFoundError("for testing"))
321 nodeb1new.AssociatedE2TInstanceAddress = ""
322 nodeb1new.ConnectionStatus = entities.ConnectionStatus_DISCONNECTED
323 writerMock.On("UpdateNodebInfo", &nodeb1new).Return(nil)
325 err := shutdownManager.Shutdown(e2tInstance1)
327 assert.NotNil(t, err)
328 readerMock.AssertExpectations(t)
329 writerMock.AssertExpectations(t)
330 httpClientMock.AssertExpectations(t)
333 func TestShutdownResourceGeneralErrorInGetNodeb(t *testing.T) {
334 shutdownManager, readerMock, writerMock, httpClientMock,_ := initE2TShutdownManagerTest(t)
336 e2tInstance1 := entities.NewE2TInstance(E2TAddress, PodName)
337 e2tInstance1.State = entities.Active
338 e2tInstance1.AssociatedRanList = []string{"test1", "test2"}
339 writerMock.On("SaveE2TInstance", mock.MatchedBy(func(e2tInstance *entities.E2TInstance) bool { return e2tInstance.Address == E2TAddress && e2tInstance.State == entities.ToBeDeleted })).Return(nil)
341 var nodeb1 *entities.NodebInfo
342 readerMock.On("GetNodeb", "test1").Return(nodeb1, common.NewInternalError(fmt.Errorf("for testing")))
343 nodeb2 := &entities.NodebInfo{RanName:"test2", AssociatedE2TInstanceAddress:E2TAddress, ConnectionStatus:entities.ConnectionStatus_DISCONNECTED, E2ApplicationProtocol:entities.E2ApplicationProtocol_X2_SETUP_REQUEST}
344 readerMock.On("GetNodeb", "test2").Return(nodeb2, nil)
346 data := models.NewRoutingManagerDeleteRequestModel(E2TAddress, []string{"test1", "test2"}, nil)
347 marshaled, _ := json.Marshal(data)
348 body := bytes.NewBuffer(marshaled)
349 respBody := ioutil.NopCloser(bytes.NewBufferString(""))
350 httpClientMock.On("Delete", "e2t", "application/json", body).Return(&http.Response{StatusCode: http.StatusCreated, Body: respBody}, nil)
352 writerMock.On("RemoveE2TInstance", E2TAddress).Return(nil)
353 readerMock.On("GetE2TAddresses").Return([]string{E2TAddress}, nil)
354 writerMock.On("SaveE2TAddresses", []string{}).Return(nil)
357 nodeb2new.AssociatedE2TInstanceAddress = ""
358 nodeb2new.ConnectionStatus = entities.ConnectionStatus_DISCONNECTED
359 writerMock.On("UpdateNodebInfo", &nodeb2new).Return(nil)
361 err := shutdownManager.Shutdown(e2tInstance1)
364 readerMock.AssertExpectations(t)
365 writerMock.AssertExpectations(t)
366 httpClientMock.AssertExpectations(t)
370 func TestShutdownFailureInRemoveE2TInstance(t *testing.T) {
371 shutdownManager, readerMock, writerMock, httpClientMock,_ := initE2TShutdownManagerTest(t)
373 e2tInstance1 := entities.NewE2TInstance(E2TAddress, PodName)
374 e2tInstance1.State = entities.Active
375 e2tInstance1.AssociatedRanList = []string{"test1", "test2", "test5"}
376 e2tInstance2 := entities.NewE2TInstance(E2TAddress2, PodName)
377 e2tInstance2.State = entities.Active
378 e2tInstance2.AssociatedRanList = []string{"test3"}
379 e2tInstance3 := entities.NewE2TInstance(E2TAddress3, PodName)
380 e2tInstance3.State = entities.Active
381 e2tInstance3.AssociatedRanList = []string{"test4"}
382 writerMock.On("SaveE2TInstance", mock.MatchedBy(func(e2tInstance *entities.E2TInstance) bool { return e2tInstance.Address == E2TAddress && e2tInstance.State == entities.ToBeDeleted })).Return(nil)
384 nodeb1 := &entities.NodebInfo{RanName:"test1", AssociatedE2TInstanceAddress:E2TAddress, ConnectionStatus:entities.ConnectionStatus_CONNECTED, E2ApplicationProtocol:entities.E2ApplicationProtocol_X2_SETUP_REQUEST}
385 readerMock.On("GetNodeb", "test1").Return(nodeb1, nil)
386 nodeb2 := &entities.NodebInfo{RanName:"test2", AssociatedE2TInstanceAddress:E2TAddress, ConnectionStatus:entities.ConnectionStatus_SHUTTING_DOWN, E2ApplicationProtocol:entities.E2ApplicationProtocol_X2_SETUP_REQUEST}
387 readerMock.On("GetNodeb", "test2").Return(nodeb2, nil)
388 nodeb5 := &entities.NodebInfo{RanName:"test5", AssociatedE2TInstanceAddress:E2TAddress, ConnectionStatus:entities.ConnectionStatus_CONNECTED, E2ApplicationProtocol:entities.E2ApplicationProtocol_X2_SETUP_REQUEST}
389 readerMock.On("GetNodeb", "test5").Return(nodeb5, nil)
391 data := models.NewRoutingManagerDeleteRequestModel(E2TAddress, e2tInstance1.AssociatedRanList, nil)
392 marshaled, _ := json.Marshal(data)
393 body := bytes.NewBuffer(marshaled)
394 respBody := ioutil.NopCloser(bytes.NewBufferString(""))
395 httpClientMock.On("Delete", "e2t", "application/json", body).Return(&http.Response{StatusCode: http.StatusCreated, Body: respBody}, nil)
397 writerMock.On("RemoveE2TInstance", E2TAddress).Return(common.NewInternalError(fmt.Errorf("for tests")))
399 nodeb1connected := *nodeb1
400 nodeb1connected.AssociatedE2TInstanceAddress = ""
401 nodeb1connected.ConnectionStatus = entities.ConnectionStatus_DISCONNECTED
402 writerMock.On("UpdateNodebInfo", &nodeb1connected).Return(nil)
403 nodeb2connected := *nodeb2
404 nodeb2connected.AssociatedE2TInstanceAddress = ""
405 nodeb2connected.ConnectionStatus = entities.ConnectionStatus_DISCONNECTED
406 writerMock.On("UpdateNodebInfo", &nodeb2connected).Return(nil)
407 nodeb5connected := *nodeb5
408 nodeb5connected.AssociatedE2TInstanceAddress = ""
409 nodeb5connected.ConnectionStatus = entities.ConnectionStatus_DISCONNECTED
410 writerMock.On("UpdateNodebInfo", &nodeb5connected).Return(nil)
412 err := shutdownManager.Shutdown(e2tInstance1)
414 assert.IsType(t, &e2managererrors.RnibDbError{}, err)
415 readerMock.AssertExpectations(t)
416 writerMock.AssertExpectations(t)
417 httpClientMock.AssertExpectations(t)
420 func TestShutdownSuccess2Instance2Rans(t *testing.T) {
421 shutdownManager, readerMock, writerMock, httpClientMock,kubernetesManager := initE2TShutdownManagerTest(t)
423 e2tInstance1 := entities.NewE2TInstance(E2TAddress, PodName)
424 e2tInstance1.State = entities.Active
425 e2tInstance1.AssociatedRanList = []string{"test2"}
426 writerMock.On("SaveE2TInstance", mock.MatchedBy(func(e2tInstance *entities.E2TInstance) bool { return e2tInstance.Address == E2TAddress && e2tInstance.State == entities.ToBeDeleted })).Return(nil)
428 nodeb1 := &entities.NodebInfo{RanName:"test1", AssociatedE2TInstanceAddress:E2TAddress2, ConnectionStatus:entities.ConnectionStatus_CONNECTED, E2ApplicationProtocol:entities.E2ApplicationProtocol_X2_SETUP_REQUEST}
429 nodeb2 := &entities.NodebInfo{RanName:"test2", AssociatedE2TInstanceAddress:E2TAddress, ConnectionStatus:entities.ConnectionStatus_DISCONNECTED, E2ApplicationProtocol:entities.E2ApplicationProtocol_X2_SETUP_REQUEST}
430 readerMock.On("GetNodeb", "test2").Return(nodeb2, nil)
432 data := models.NewRoutingManagerDeleteRequestModel(E2TAddress, []string{"test2"}, nil)
433 marshaled, _ := json.Marshal(data)
434 body := bytes.NewBuffer(marshaled)
435 respBody := ioutil.NopCloser(bytes.NewBufferString(""))
436 httpClientMock.On("Delete", "e2t", "application/json", body).Return(&http.Response{StatusCode: http.StatusCreated, Body: respBody}, nil)
438 writerMock.On("RemoveE2TInstance", E2TAddress).Return(nil)
439 readerMock.On("GetE2TAddresses").Return([]string{E2TAddress}, nil)
440 writerMock.On("SaveE2TAddresses", []string{}).Return(nil)
443 nodeb1new.AssociatedE2TInstanceAddress = ""
444 nodeb1new.ConnectionStatus = entities.ConnectionStatus_DISCONNECTED
446 nodeb2new.AssociatedE2TInstanceAddress = ""
447 nodeb2new.ConnectionStatus = entities.ConnectionStatus_DISCONNECTED
448 writerMock.On("UpdateNodebInfo", &nodeb2new).Return(nil)
451 description: "namespace, 2 pods in Oran",
453 objs: []runtime.Object{pod("oran", PodName), pod("oran", "e2t_2"), pod("some-namespace", "POD_Test_1")},
456 t.Run(test.description, func(t *testing.T) {
457 kubernetesManager.ClientSet = fake.NewSimpleClientset(test.objs...)
459 err := shutdownManager.Shutdown(e2tInstance1)
462 readerMock.AssertExpectations(t)
463 writerMock.AssertExpectations(t)
464 httpClientMock.AssertExpectations(t)
468 func TestShutdownSuccess2Instance2RansNoPod(t *testing.T) {
469 shutdownManager, readerMock, writerMock, httpClientMock,kubernetesManager := initE2TShutdownManagerTest(t)
471 e2tInstance1 := entities.NewE2TInstance(E2TAddress, PodName)
472 e2tInstance1.State = entities.Active
473 e2tInstance1.AssociatedRanList = []string{"test2"}
474 writerMock.On("SaveE2TInstance", mock.MatchedBy(func(e2tInstance *entities.E2TInstance) bool { return e2tInstance.Address == E2TAddress && e2tInstance.State == entities.ToBeDeleted })).Return(nil)
476 nodeb1 := &entities.NodebInfo{RanName:"test1", AssociatedE2TInstanceAddress:E2TAddress2, ConnectionStatus:entities.ConnectionStatus_CONNECTED, E2ApplicationProtocol:entities.E2ApplicationProtocol_X2_SETUP_REQUEST}
477 nodeb2 := &entities.NodebInfo{RanName:"test2", AssociatedE2TInstanceAddress:E2TAddress, ConnectionStatus:entities.ConnectionStatus_DISCONNECTED, E2ApplicationProtocol:entities.E2ApplicationProtocol_X2_SETUP_REQUEST}
478 readerMock.On("GetNodeb", "test2").Return(nodeb2, nil)
480 data := models.NewRoutingManagerDeleteRequestModel(E2TAddress, []string{"test2"}, nil)
481 marshaled, _ := json.Marshal(data)
482 body := bytes.NewBuffer(marshaled)
483 respBody := ioutil.NopCloser(bytes.NewBufferString(""))
484 httpClientMock.On("Delete", "e2t", "application/json", body).Return(&http.Response{StatusCode: http.StatusCreated, Body: respBody}, nil)
486 writerMock.On("RemoveE2TInstance", E2TAddress).Return(nil)
487 readerMock.On("GetE2TAddresses").Return([]string{E2TAddress}, nil)
488 writerMock.On("SaveE2TAddresses", []string{}).Return(nil)
491 nodeb1new.AssociatedE2TInstanceAddress = ""
492 nodeb1new.ConnectionStatus = entities.ConnectionStatus_DISCONNECTED
494 nodeb2new.AssociatedE2TInstanceAddress = ""
495 nodeb2new.ConnectionStatus = entities.ConnectionStatus_DISCONNECTED
496 writerMock.On("UpdateNodebInfo", &nodeb2new).Return(nil)
499 description: "namespace, 2 pods in Oran",
501 objs: []runtime.Object{pod("oran", "e2t_2"), pod("some-namespace", "POD_Test_1")},
504 t.Run(test.description, func(t *testing.T) {
505 kubernetesManager.ClientSet = fake.NewSimpleClientset(test.objs...)
507 err := shutdownManager.Shutdown(e2tInstance1)
510 readerMock.AssertExpectations(t)
511 writerMock.AssertExpectations(t)
512 httpClientMock.AssertExpectations(t)