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)
58 associationManager := NewE2TAssociationManager(log, rnibDataService, e2tInstancesManager, rmClient)
59 //kubernetesManager := initKubernetesManagerTest(t)
61 /*shutdownManager := NewE2TShutdownManager(log, config, rnibDataService, e2tInstancesManager, associationManager, kubernetesManager)
63 return shutdownManager, readerMock, writerMock, httpClientMock, kubernetesManager*/
64 shutdownManager := NewE2TShutdownManager(log, config, rnibDataService, e2tInstancesManager, associationManager, nil)
66 return shutdownManager, readerMock, writerMock, httpClientMock, nil
69 func TestShutdownSuccess1OutOf3Instances(t *testing.T) {
70 shutdownManager, readerMock, writerMock, httpClientMock,_ := initE2TShutdownManagerTest(t)
72 e2tInstance1 := entities.NewE2TInstance(E2TAddress, PodName)
73 e2tInstance1.State = entities.Active
74 e2tInstance1.AssociatedRanList = []string{"test1", "test2", "test5"}
75 e2tInstance2 := entities.NewE2TInstance(E2TAddress2, PodName)
76 e2tInstance2.State = entities.Active
77 e2tInstance2.AssociatedRanList = []string{"test3"}
78 e2tInstance3 := entities.NewE2TInstance(E2TAddress3, PodName)
79 e2tInstance3.State = entities.Active
80 e2tInstance3.AssociatedRanList = []string{"test4"}
81 writerMock.On("SaveE2TInstance", mock.MatchedBy(func(e2tInstance *entities.E2TInstance) bool { return e2tInstance.Address == E2TAddress && e2tInstance.State == entities.ToBeDeleted })).Return(nil)
83 nodeb1 := &entities.NodebInfo{RanName:"test1", AssociatedE2TInstanceAddress:E2TAddress, ConnectionStatus:entities.ConnectionStatus_CONNECTED, E2ApplicationProtocol:entities.E2ApplicationProtocol_X2_SETUP_REQUEST}
84 readerMock.On("GetNodeb", "test1").Return(nodeb1, nil)
85 nodeb2 := &entities.NodebInfo{RanName:"test2", AssociatedE2TInstanceAddress:E2TAddress, ConnectionStatus:entities.ConnectionStatus_SHUTTING_DOWN, E2ApplicationProtocol:entities.E2ApplicationProtocol_X2_SETUP_REQUEST}
86 readerMock.On("GetNodeb", "test2").Return(nodeb2, nil)
87 nodeb5 := &entities.NodebInfo{RanName:"test5", AssociatedE2TInstanceAddress:E2TAddress, ConnectionStatus:entities.ConnectionStatus_CONNECTED, E2ApplicationProtocol:entities.E2ApplicationProtocol_X2_SETUP_REQUEST}
88 readerMock.On("GetNodeb", "test5").Return(nodeb5, nil)
90 e2tAddresses := []string{E2TAddress, E2TAddress2,E2TAddress3}
91 readerMock.On("GetE2TAddresses").Return(e2tAddresses, nil)
93 data := models.NewRoutingManagerDeleteRequestModel(E2TAddress, e2tInstance1.AssociatedRanList, nil)
94 marshaled, _ := json.Marshal(data)
95 body := bytes.NewBuffer(marshaled)
96 respBody := ioutil.NopCloser(bytes.NewBufferString(""))
97 httpClientMock.On("Delete", "e2t", "application/json", body).Return(&http.Response{StatusCode: http.StatusCreated, Body: respBody}, nil)
99 writerMock.On("RemoveE2TInstance", E2TAddress).Return(nil)
100 writerMock.On("SaveE2TAddresses", []string{E2TAddress2,E2TAddress3}).Return(nil)
102 nodeb1connected := *nodeb1
103 nodeb1connected.AssociatedE2TInstanceAddress = ""
104 nodeb1connected.ConnectionStatus = entities.ConnectionStatus_DISCONNECTED
105 writerMock.On("UpdateNodebInfo", &nodeb1connected).Return(nil)
106 nodeb2connected := *nodeb2
107 nodeb2connected.AssociatedE2TInstanceAddress = ""
108 nodeb2connected.ConnectionStatus = entities.ConnectionStatus_DISCONNECTED
109 writerMock.On("UpdateNodebInfo", &nodeb2connected).Return(nil)
110 nodeb5connected := *nodeb5
111 nodeb5connected.AssociatedE2TInstanceAddress = ""
112 nodeb5connected.ConnectionStatus = entities.ConnectionStatus_DISCONNECTED
113 writerMock.On("UpdateNodebInfo", &nodeb5connected).Return(nil)
115 err := shutdownManager.Shutdown(e2tInstance1)
118 readerMock.AssertExpectations(t)
119 writerMock.AssertExpectations(t)
120 httpClientMock.AssertExpectations(t)
123 func TestShutdownSuccess1InstanceWithoutRans(t *testing.T) {
124 shutdownManager, readerMock, writerMock, httpClientMock,_ := initE2TShutdownManagerTest(t)
126 e2tInstance1 := entities.NewE2TInstance(E2TAddress, PodName)
127 e2tInstance1.State = entities.Active
128 e2tInstance1.AssociatedRanList = []string{}
129 writerMock.On("SaveE2TInstance", mock.MatchedBy(func(e2tInstance *entities.E2TInstance) bool { return e2tInstance.Address == E2TAddress && e2tInstance.State == entities.ToBeDeleted })).Return(nil)
131 data := models.NewRoutingManagerDeleteRequestModel(E2TAddress, nil, nil)
132 marshaled, _ := json.Marshal(data)
133 body := bytes.NewBuffer(marshaled)
134 respBody := ioutil.NopCloser(bytes.NewBufferString(""))
135 httpClientMock.On("Delete", "e2t", "application/json", body).Return(&http.Response{StatusCode: http.StatusCreated, Body: respBody}, nil)
137 writerMock.On("RemoveE2TInstance", E2TAddress).Return(nil)
138 readerMock.On("GetE2TAddresses").Return([]string{E2TAddress}, nil)
139 writerMock.On("SaveE2TAddresses", []string{}).Return(nil)
141 err := shutdownManager.Shutdown(e2tInstance1)
144 readerMock.AssertExpectations(t)
145 writerMock.AssertExpectations(t)
146 httpClientMock.AssertExpectations(t)
149 func TestShutdownSuccess1Instance2Rans(t *testing.T) {
150 shutdownManager, readerMock, writerMock, httpClientMock,_ := initE2TShutdownManagerTest(t)
152 e2tInstance1 := entities.NewE2TInstance(E2TAddress, PodName)
153 e2tInstance1.State = entities.Active
154 e2tInstance1.AssociatedRanList = []string{"test1", "test2"}
155 writerMock.On("SaveE2TInstance", mock.MatchedBy(func(e2tInstance *entities.E2TInstance) bool { return e2tInstance.Address == E2TAddress && e2tInstance.State == entities.ToBeDeleted })).Return(nil)
157 nodeb1 := &entities.NodebInfo{RanName:"test1", AssociatedE2TInstanceAddress:E2TAddress, ConnectionStatus:entities.ConnectionStatus_CONNECTED, E2ApplicationProtocol:entities.E2ApplicationProtocol_X2_SETUP_REQUEST}
158 readerMock.On("GetNodeb", "test1").Return(nodeb1, nil)
159 nodeb2 := &entities.NodebInfo{RanName:"test2", AssociatedE2TInstanceAddress:E2TAddress, ConnectionStatus:entities.ConnectionStatus_DISCONNECTED, E2ApplicationProtocol:entities.E2ApplicationProtocol_X2_SETUP_REQUEST}
160 readerMock.On("GetNodeb", "test2").Return(nodeb2, nil)
162 data := models.NewRoutingManagerDeleteRequestModel(E2TAddress, []string{"test1", "test2"}, nil)
163 marshaled, _ := json.Marshal(data)
164 body := bytes.NewBuffer(marshaled)
165 respBody := ioutil.NopCloser(bytes.NewBufferString(""))
166 httpClientMock.On("Delete", "e2t", "application/json", body).Return(&http.Response{StatusCode: http.StatusCreated, Body: respBody}, nil)
168 writerMock.On("RemoveE2TInstance", E2TAddress).Return(nil)
169 readerMock.On("GetE2TAddresses").Return([]string{E2TAddress}, nil)
170 writerMock.On("SaveE2TAddresses", []string{}).Return(nil)
173 nodeb1new.AssociatedE2TInstanceAddress = ""
174 nodeb1new.ConnectionStatus = entities.ConnectionStatus_DISCONNECTED
175 writerMock.On("UpdateNodebInfo", &nodeb1new).Return(nil)
177 nodeb2new.AssociatedE2TInstanceAddress = ""
178 nodeb2new.ConnectionStatus = entities.ConnectionStatus_DISCONNECTED
179 writerMock.On("UpdateNodebInfo", &nodeb2new).Return(nil)
181 err := shutdownManager.Shutdown(e2tInstance1)
184 readerMock.AssertExpectations(t)
185 writerMock.AssertExpectations(t)
186 httpClientMock.AssertExpectations(t)
190 func TestShutdownE2tInstanceAlreadyBeingDeleted(t *testing.T) {
191 shutdownManager, readerMock, writerMock, httpClientMock,_ := initE2TShutdownManagerTest(t)
193 e2tInstance1 := entities.NewE2TInstance(E2TAddress, PodName)
194 e2tInstance1.State = entities.ToBeDeleted
195 e2tInstance1.AssociatedRanList = []string{"test1"}
196 e2tInstance1.DeletionTimestamp = time.Now().UnixNano()
198 err := shutdownManager.Shutdown(e2tInstance1)
201 readerMock.AssertExpectations(t)
202 writerMock.AssertExpectations(t)
203 httpClientMock.AssertExpectations(t)
207 func TestShutdownFailureMarkInstanceAsToBeDeleted(t *testing.T) {
208 shutdownManager, readerMock, writerMock, httpClientMock,_ := initE2TShutdownManagerTest(t)
210 e2tInstance1 := entities.NewE2TInstance(E2TAddress, PodName)
211 e2tInstance1.State = entities.Active
212 e2tInstance1.AssociatedRanList = []string{"test1", "test2", "test5"}
213 writerMock.On("SaveE2TInstance", mock.MatchedBy(func(e2tInstance *entities.E2TInstance) bool { return e2tInstance.Address == E2TAddress && e2tInstance.State == entities.ToBeDeleted })).Return(e2managererrors.NewRnibDbError())
215 err := shutdownManager.Shutdown(e2tInstance1)
217 assert.NotNil(t, err)
218 readerMock.AssertExpectations(t)
219 writerMock.AssertExpectations(t)
220 httpClientMock.AssertExpectations(t)
224 func TestShutdownFailureRoutingManagerError(t *testing.T) {
225 shutdownManager, readerMock, writerMock, httpClientMock,_ := initE2TShutdownManagerTest(t)
227 e2tInstance1 := entities.NewE2TInstance(E2TAddress, PodName)
228 e2tInstance1.State = entities.Active
229 e2tInstance1.AssociatedRanList = []string{"test1", "test2", "test5"}
230 e2tInstance2 := entities.NewE2TInstance(E2TAddress2, PodName)
231 e2tInstance2.State = entities.Active
232 e2tInstance2.AssociatedRanList = []string{"test3"}
233 e2tInstance3 := entities.NewE2TInstance(E2TAddress3, PodName)
234 e2tInstance3.State = entities.Active
235 e2tInstance3.AssociatedRanList = []string{"test4"}
236 writerMock.On("SaveE2TInstance", mock.MatchedBy(func(e2tInstance *entities.E2TInstance) bool { return e2tInstance.Address == E2TAddress && e2tInstance.State == entities.ToBeDeleted })).Return(nil)
238 nodeb1 := &entities.NodebInfo{RanName:"test1", AssociatedE2TInstanceAddress:E2TAddress, ConnectionStatus:entities.ConnectionStatus_CONNECTED, E2ApplicationProtocol:entities.E2ApplicationProtocol_X2_SETUP_REQUEST}
239 readerMock.On("GetNodeb", "test1").Return(nodeb1, nil)
240 nodeb2 := &entities.NodebInfo{RanName:"test2", AssociatedE2TInstanceAddress:E2TAddress, ConnectionStatus:entities.ConnectionStatus_SHUTTING_DOWN, E2ApplicationProtocol:entities.E2ApplicationProtocol_X2_SETUP_REQUEST}
241 readerMock.On("GetNodeb", "test2").Return(nodeb2, nil)
242 nodeb5 := &entities.NodebInfo{RanName:"test5", AssociatedE2TInstanceAddress:E2TAddress, ConnectionStatus:entities.ConnectionStatus_CONNECTED, E2ApplicationProtocol:entities.E2ApplicationProtocol_X2_SETUP_REQUEST}
243 readerMock.On("GetNodeb", "test5").Return(nodeb5, nil)
245 e2tAddresses := []string{E2TAddress, E2TAddress2,E2TAddress3}
246 readerMock.On("GetE2TAddresses").Return(e2tAddresses, nil)
248 data := models.NewRoutingManagerDeleteRequestModel(E2TAddress, e2tInstance1.AssociatedRanList, nil)
249 marshaled, _ := json.Marshal(data)
250 body := bytes.NewBuffer(marshaled)
251 respBody := ioutil.NopCloser(bytes.NewBufferString(""))
252 httpClientMock.On("Delete", "e2t", "application/json", body).Return(&http.Response{StatusCode: http.StatusBadRequest, Body: respBody}, nil)
254 writerMock.On("RemoveE2TInstance", E2TAddress).Return(nil)
255 writerMock.On("SaveE2TAddresses", []string{E2TAddress2,E2TAddress3}).Return(nil)
257 nodeb1connected := *nodeb1
258 nodeb1connected.AssociatedE2TInstanceAddress = ""
259 nodeb1connected.ConnectionStatus = entities.ConnectionStatus_DISCONNECTED
260 writerMock.On("UpdateNodebInfo", &nodeb1connected).Return(nil)
261 nodeb2connected := *nodeb2
262 nodeb2connected.AssociatedE2TInstanceAddress = ""
263 nodeb2connected.ConnectionStatus = entities.ConnectionStatus_DISCONNECTED
264 writerMock.On("UpdateNodebInfo", &nodeb2connected).Return(nil)
265 nodeb5connected := *nodeb5
266 nodeb5connected.AssociatedE2TInstanceAddress = ""
267 nodeb5connected.ConnectionStatus = entities.ConnectionStatus_DISCONNECTED
268 writerMock.On("UpdateNodebInfo", &nodeb5connected).Return(nil)
270 err := shutdownManager.Shutdown(e2tInstance1)
273 readerMock.AssertExpectations(t)
274 writerMock.AssertExpectations(t)
275 httpClientMock.AssertExpectations(t)
279 func TestShutdownFailureInClearNodebsAssociation(t *testing.T) {
280 shutdownManager, readerMock, writerMock, httpClientMock,_ := initE2TShutdownManagerTest(t)
282 e2tInstance1 := entities.NewE2TInstance(E2TAddress, PodName)
283 e2tInstance1.State = entities.Active
284 e2tInstance1.AssociatedRanList = []string{"test1", "test2"}
285 writerMock.On("SaveE2TInstance", mock.MatchedBy(func(e2tInstance *entities.E2TInstance) bool { return e2tInstance.Address == E2TAddress && e2tInstance.State == entities.ToBeDeleted })).Return(nil)
287 nodeb1 := &entities.NodebInfo{RanName:"test1", AssociatedE2TInstanceAddress:E2TAddress, ConnectionStatus:entities.ConnectionStatus_CONNECTED, E2ApplicationProtocol:entities.E2ApplicationProtocol_X2_SETUP_REQUEST}
288 readerMock.On("GetNodeb", "test1").Return(nodeb1, nil)
291 nodeb1new.AssociatedE2TInstanceAddress = ""
292 nodeb1new.ConnectionStatus = entities.ConnectionStatus_DISCONNECTED
293 writerMock.On("UpdateNodebInfo", &nodeb1new).Return(common.NewInternalError(fmt.Errorf("for tests")))
295 err := shutdownManager.Shutdown(e2tInstance1)
297 assert.NotNil(t, err)
298 readerMock.AssertExpectations(t)
299 writerMock.AssertExpectations(t)
300 httpClientMock.AssertExpectations(t)
303 func TestShutdownResourceNotFoundErrorInGetNodeb(t *testing.T) {
304 shutdownManager, readerMock, writerMock, httpClientMock,_ := initE2TShutdownManagerTest(t)
306 e2tInstance1 := entities.NewE2TInstance(E2TAddress, PodName)
307 e2tInstance1.State = entities.Active
308 e2tInstance1.AssociatedRanList = []string{"test1", "test2"}
309 writerMock.On("SaveE2TInstance", mock.MatchedBy(func(e2tInstance *entities.E2TInstance) bool { return e2tInstance.Address == E2TAddress && e2tInstance.State == entities.ToBeDeleted })).Return(nil)
311 nodeb1 := &entities.NodebInfo{RanName:"test1", AssociatedE2TInstanceAddress:E2TAddress, ConnectionStatus:entities.ConnectionStatus_CONNECTED, E2ApplicationProtocol:entities.E2ApplicationProtocol_X2_SETUP_REQUEST}
312 readerMock.On("GetNodeb", "test1").Return(nodeb1, nil)
313 var nodeb2 *entities.NodebInfo
314 readerMock.On("GetNodeb", "test2").Return(nodeb2, common.NewResourceNotFoundError("for testing"))
317 nodeb1new.AssociatedE2TInstanceAddress = ""
318 nodeb1new.ConnectionStatus = entities.ConnectionStatus_DISCONNECTED
319 writerMock.On("UpdateNodebInfo", &nodeb1new).Return(nil)
321 err := shutdownManager.Shutdown(e2tInstance1)
323 assert.NotNil(t, err)
324 readerMock.AssertExpectations(t)
325 writerMock.AssertExpectations(t)
326 httpClientMock.AssertExpectations(t)
329 func TestShutdownResourceGeneralErrorInGetNodeb(t *testing.T) {
330 shutdownManager, readerMock, writerMock, httpClientMock,_ := initE2TShutdownManagerTest(t)
332 e2tInstance1 := entities.NewE2TInstance(E2TAddress, PodName)
333 e2tInstance1.State = entities.Active
334 e2tInstance1.AssociatedRanList = []string{"test1", "test2"}
335 writerMock.On("SaveE2TInstance", mock.MatchedBy(func(e2tInstance *entities.E2TInstance) bool { return e2tInstance.Address == E2TAddress && e2tInstance.State == entities.ToBeDeleted })).Return(nil)
337 var nodeb1 *entities.NodebInfo
338 readerMock.On("GetNodeb", "test1").Return(nodeb1, common.NewInternalError(fmt.Errorf("for testing")))
339 nodeb2 := &entities.NodebInfo{RanName:"test2", AssociatedE2TInstanceAddress:E2TAddress, ConnectionStatus:entities.ConnectionStatus_DISCONNECTED, E2ApplicationProtocol:entities.E2ApplicationProtocol_X2_SETUP_REQUEST}
340 readerMock.On("GetNodeb", "test2").Return(nodeb2, nil)
342 data := models.NewRoutingManagerDeleteRequestModel(E2TAddress, []string{"test1", "test2"}, nil)
343 marshaled, _ := json.Marshal(data)
344 body := bytes.NewBuffer(marshaled)
345 respBody := ioutil.NopCloser(bytes.NewBufferString(""))
346 httpClientMock.On("Delete", "e2t", "application/json", body).Return(&http.Response{StatusCode: http.StatusCreated, Body: respBody}, nil)
348 writerMock.On("RemoveE2TInstance", E2TAddress).Return(nil)
349 readerMock.On("GetE2TAddresses").Return([]string{E2TAddress}, nil)
350 writerMock.On("SaveE2TAddresses", []string{}).Return(nil)
353 nodeb2new.AssociatedE2TInstanceAddress = ""
354 nodeb2new.ConnectionStatus = entities.ConnectionStatus_DISCONNECTED
355 writerMock.On("UpdateNodebInfo", &nodeb2new).Return(nil)
357 err := shutdownManager.Shutdown(e2tInstance1)
360 readerMock.AssertExpectations(t)
361 writerMock.AssertExpectations(t)
362 httpClientMock.AssertExpectations(t)
366 func TestShutdownFailureInRemoveE2TInstance(t *testing.T) {
367 shutdownManager, readerMock, writerMock, httpClientMock,_ := initE2TShutdownManagerTest(t)
369 e2tInstance1 := entities.NewE2TInstance(E2TAddress, PodName)
370 e2tInstance1.State = entities.Active
371 e2tInstance1.AssociatedRanList = []string{"test1", "test2", "test5"}
372 e2tInstance2 := entities.NewE2TInstance(E2TAddress2, PodName)
373 e2tInstance2.State = entities.Active
374 e2tInstance2.AssociatedRanList = []string{"test3"}
375 e2tInstance3 := entities.NewE2TInstance(E2TAddress3, PodName)
376 e2tInstance3.State = entities.Active
377 e2tInstance3.AssociatedRanList = []string{"test4"}
378 writerMock.On("SaveE2TInstance", mock.MatchedBy(func(e2tInstance *entities.E2TInstance) bool { return e2tInstance.Address == E2TAddress && e2tInstance.State == entities.ToBeDeleted })).Return(nil)
380 nodeb1 := &entities.NodebInfo{RanName:"test1", AssociatedE2TInstanceAddress:E2TAddress, ConnectionStatus:entities.ConnectionStatus_CONNECTED, E2ApplicationProtocol:entities.E2ApplicationProtocol_X2_SETUP_REQUEST}
381 readerMock.On("GetNodeb", "test1").Return(nodeb1, nil)
382 nodeb2 := &entities.NodebInfo{RanName:"test2", AssociatedE2TInstanceAddress:E2TAddress, ConnectionStatus:entities.ConnectionStatus_SHUTTING_DOWN, E2ApplicationProtocol:entities.E2ApplicationProtocol_X2_SETUP_REQUEST}
383 readerMock.On("GetNodeb", "test2").Return(nodeb2, nil)
384 nodeb5 := &entities.NodebInfo{RanName:"test5", AssociatedE2TInstanceAddress:E2TAddress, ConnectionStatus:entities.ConnectionStatus_CONNECTED, E2ApplicationProtocol:entities.E2ApplicationProtocol_X2_SETUP_REQUEST}
385 readerMock.On("GetNodeb", "test5").Return(nodeb5, nil)
387 data := models.NewRoutingManagerDeleteRequestModel(E2TAddress, e2tInstance1.AssociatedRanList, nil)
388 marshaled, _ := json.Marshal(data)
389 body := bytes.NewBuffer(marshaled)
390 respBody := ioutil.NopCloser(bytes.NewBufferString(""))
391 httpClientMock.On("Delete", "e2t", "application/json", body).Return(&http.Response{StatusCode: http.StatusCreated, Body: respBody}, nil)
393 writerMock.On("RemoveE2TInstance", E2TAddress).Return(common.NewInternalError(fmt.Errorf("for tests")))
395 nodeb1connected := *nodeb1
396 nodeb1connected.AssociatedE2TInstanceAddress = ""
397 nodeb1connected.ConnectionStatus = entities.ConnectionStatus_DISCONNECTED
398 writerMock.On("UpdateNodebInfo", &nodeb1connected).Return(nil)
399 nodeb2connected := *nodeb2
400 nodeb2connected.AssociatedE2TInstanceAddress = ""
401 nodeb2connected.ConnectionStatus = entities.ConnectionStatus_DISCONNECTED
402 writerMock.On("UpdateNodebInfo", &nodeb2connected).Return(nil)
403 nodeb5connected := *nodeb5
404 nodeb5connected.AssociatedE2TInstanceAddress = ""
405 nodeb5connected.ConnectionStatus = entities.ConnectionStatus_DISCONNECTED
406 writerMock.On("UpdateNodebInfo", &nodeb5connected).Return(nil)
408 err := shutdownManager.Shutdown(e2tInstance1)
410 assert.IsType(t, &e2managererrors.RnibDbError{}, err)
411 readerMock.AssertExpectations(t)
412 writerMock.AssertExpectations(t)
413 httpClientMock.AssertExpectations(t)
416 func TestShutdownSuccess2Instance2Rans(t *testing.T) {
417 shutdownManager, readerMock, writerMock, httpClientMock,kubernetesManager := initE2TShutdownManagerTest(t)
419 e2tInstance1 := entities.NewE2TInstance(E2TAddress, PodName)
420 e2tInstance1.State = entities.Active
421 e2tInstance1.AssociatedRanList = []string{"test2"}
422 writerMock.On("SaveE2TInstance", mock.MatchedBy(func(e2tInstance *entities.E2TInstance) bool { return e2tInstance.Address == E2TAddress && e2tInstance.State == entities.ToBeDeleted })).Return(nil)
424 nodeb1 := &entities.NodebInfo{RanName:"test1", AssociatedE2TInstanceAddress:E2TAddress2, ConnectionStatus:entities.ConnectionStatus_CONNECTED, E2ApplicationProtocol:entities.E2ApplicationProtocol_X2_SETUP_REQUEST}
425 nodeb2 := &entities.NodebInfo{RanName:"test2", AssociatedE2TInstanceAddress:E2TAddress, ConnectionStatus:entities.ConnectionStatus_DISCONNECTED, E2ApplicationProtocol:entities.E2ApplicationProtocol_X2_SETUP_REQUEST}
426 readerMock.On("GetNodeb", "test2").Return(nodeb2, nil)
428 data := models.NewRoutingManagerDeleteRequestModel(E2TAddress, []string{"test2"}, nil)
429 marshaled, _ := json.Marshal(data)
430 body := bytes.NewBuffer(marshaled)
431 respBody := ioutil.NopCloser(bytes.NewBufferString(""))
432 httpClientMock.On("Delete", "e2t", "application/json", body).Return(&http.Response{StatusCode: http.StatusCreated, Body: respBody}, nil)
434 writerMock.On("RemoveE2TInstance", E2TAddress).Return(nil)
435 readerMock.On("GetE2TAddresses").Return([]string{E2TAddress}, nil)
436 writerMock.On("SaveE2TAddresses", []string{}).Return(nil)
439 nodeb1new.AssociatedE2TInstanceAddress = ""
440 nodeb1new.ConnectionStatus = entities.ConnectionStatus_DISCONNECTED
442 nodeb2new.AssociatedE2TInstanceAddress = ""
443 nodeb2new.ConnectionStatus = entities.ConnectionStatus_DISCONNECTED
444 writerMock.On("UpdateNodebInfo", &nodeb2new).Return(nil)
447 description: "namespace, 2 pods in Oran",
449 objs: []runtime.Object{pod("oran", PodName), pod("oran", "e2t_2"), pod("some-namespace", "POD_Test_1")},
452 t.Run(test.description, func(t *testing.T) {
453 kubernetesManager.ClientSet = fake.NewSimpleClientset(test.objs...)
455 err := shutdownManager.Shutdown(e2tInstance1)
458 readerMock.AssertExpectations(t)
459 writerMock.AssertExpectations(t)
460 httpClientMock.AssertExpectations(t)
464 func TestShutdownSuccess2Instance2RansNoPod(t *testing.T) {
465 shutdownManager, readerMock, writerMock, httpClientMock,kubernetesManager := initE2TShutdownManagerTest(t)
467 e2tInstance1 := entities.NewE2TInstance(E2TAddress, PodName)
468 e2tInstance1.State = entities.Active
469 e2tInstance1.AssociatedRanList = []string{"test2"}
470 writerMock.On("SaveE2TInstance", mock.MatchedBy(func(e2tInstance *entities.E2TInstance) bool { return e2tInstance.Address == E2TAddress && e2tInstance.State == entities.ToBeDeleted })).Return(nil)
472 nodeb1 := &entities.NodebInfo{RanName:"test1", AssociatedE2TInstanceAddress:E2TAddress2, ConnectionStatus:entities.ConnectionStatus_CONNECTED, E2ApplicationProtocol:entities.E2ApplicationProtocol_X2_SETUP_REQUEST}
473 nodeb2 := &entities.NodebInfo{RanName:"test2", AssociatedE2TInstanceAddress:E2TAddress, ConnectionStatus:entities.ConnectionStatus_DISCONNECTED, E2ApplicationProtocol:entities.E2ApplicationProtocol_X2_SETUP_REQUEST}
474 readerMock.On("GetNodeb", "test2").Return(nodeb2, nil)
476 data := models.NewRoutingManagerDeleteRequestModel(E2TAddress, []string{"test2"}, nil)
477 marshaled, _ := json.Marshal(data)
478 body := bytes.NewBuffer(marshaled)
479 respBody := ioutil.NopCloser(bytes.NewBufferString(""))
480 httpClientMock.On("Delete", "e2t", "application/json", body).Return(&http.Response{StatusCode: http.StatusCreated, Body: respBody}, nil)
482 writerMock.On("RemoveE2TInstance", E2TAddress).Return(nil)
483 readerMock.On("GetE2TAddresses").Return([]string{E2TAddress}, nil)
484 writerMock.On("SaveE2TAddresses", []string{}).Return(nil)
487 nodeb1new.AssociatedE2TInstanceAddress = ""
488 nodeb1new.ConnectionStatus = entities.ConnectionStatus_DISCONNECTED
490 nodeb2new.AssociatedE2TInstanceAddress = ""
491 nodeb2new.ConnectionStatus = entities.ConnectionStatus_DISCONNECTED
492 writerMock.On("UpdateNodebInfo", &nodeb2new).Return(nil)
495 description: "namespace, 2 pods in Oran",
497 objs: []runtime.Object{pod("oran", "e2t_2"), pod("some-namespace", "POD_Test_1")},
500 t.Run(test.description, func(t *testing.T) {
501 kubernetesManager.ClientSet = fake.NewSimpleClientset(test.objs...)
503 err := shutdownManager.Shutdown(e2tInstance1)
506 readerMock.AssertExpectations(t)
507 writerMock.AssertExpectations(t)
508 httpClientMock.AssertExpectations(t)