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
66 func TestShutdownSuccess1OutOf3Instances(t *testing.T) {
67 shutdownManager, readerMock, writerMock, httpClientMock,_ := initE2TShutdownManagerTest(t)
69 e2tInstance1 := entities.NewE2TInstance(E2TAddress, PodName)
70 e2tInstance1.State = entities.Active
71 e2tInstance1.AssociatedRanList = []string{"test1", "test2", "test5"}
72 e2tInstance2 := entities.NewE2TInstance(E2TAddress2, PodName)
73 e2tInstance2.State = entities.Active
74 e2tInstance2.AssociatedRanList = []string{"test3"}
75 e2tInstance3 := entities.NewE2TInstance(E2TAddress3, PodName)
76 e2tInstance3.State = entities.Active
77 e2tInstance3.AssociatedRanList = []string{"test4"}
78 writerMock.On("SaveE2TInstance", mock.MatchedBy(func(e2tInstance *entities.E2TInstance) bool { return e2tInstance.Address == E2TAddress && e2tInstance.State == entities.ToBeDeleted })).Return(nil)
80 nodeb1 := &entities.NodebInfo{RanName:"test1", AssociatedE2TInstanceAddress:E2TAddress, ConnectionStatus:entities.ConnectionStatus_CONNECTED, E2ApplicationProtocol:entities.E2ApplicationProtocol_X2_SETUP_REQUEST}
81 readerMock.On("GetNodeb", "test1").Return(nodeb1, nil)
82 nodeb2 := &entities.NodebInfo{RanName:"test2", AssociatedE2TInstanceAddress:E2TAddress, ConnectionStatus:entities.ConnectionStatus_SHUTTING_DOWN, E2ApplicationProtocol:entities.E2ApplicationProtocol_X2_SETUP_REQUEST}
83 readerMock.On("GetNodeb", "test2").Return(nodeb2, nil)
84 nodeb5 := &entities.NodebInfo{RanName:"test5", AssociatedE2TInstanceAddress:E2TAddress, ConnectionStatus:entities.ConnectionStatus_CONNECTED, E2ApplicationProtocol:entities.E2ApplicationProtocol_X2_SETUP_REQUEST}
85 readerMock.On("GetNodeb", "test5").Return(nodeb5, nil)
87 e2tAddresses := []string{E2TAddress, E2TAddress2,E2TAddress3}
88 readerMock.On("GetE2TAddresses").Return(e2tAddresses, nil)
90 data := models.NewRoutingManagerDeleteRequestModel(E2TAddress, e2tInstance1.AssociatedRanList, nil)
91 marshaled, _ := json.Marshal(data)
92 body := bytes.NewBuffer(marshaled)
93 respBody := ioutil.NopCloser(bytes.NewBufferString(""))
94 httpClientMock.On("Delete", "e2t", "application/json", body).Return(&http.Response{StatusCode: http.StatusCreated, Body: respBody}, nil)
96 writerMock.On("RemoveE2TInstance", E2TAddress).Return(nil)
97 writerMock.On("SaveE2TAddresses", []string{E2TAddress2,E2TAddress3}).Return(nil)
99 nodeb1connected := *nodeb1
100 nodeb1connected.AssociatedE2TInstanceAddress = ""
101 nodeb1connected.ConnectionStatus = entities.ConnectionStatus_DISCONNECTED
102 writerMock.On("UpdateNodebInfo", &nodeb1connected).Return(nil)
103 nodeb2connected := *nodeb2
104 nodeb2connected.AssociatedE2TInstanceAddress = ""
105 nodeb2connected.ConnectionStatus = entities.ConnectionStatus_DISCONNECTED
106 writerMock.On("UpdateNodebInfo", &nodeb2connected).Return(nil)
107 nodeb5connected := *nodeb5
108 nodeb5connected.AssociatedE2TInstanceAddress = ""
109 nodeb5connected.ConnectionStatus = entities.ConnectionStatus_DISCONNECTED
110 writerMock.On("UpdateNodebInfo", &nodeb5connected).Return(nil)
112 err := shutdownManager.Shutdown(e2tInstance1)
115 readerMock.AssertExpectations(t)
116 writerMock.AssertExpectations(t)
117 httpClientMock.AssertExpectations(t)
120 func TestShutdownSuccess1InstanceWithoutRans(t *testing.T) {
121 shutdownManager, readerMock, writerMock, httpClientMock,_ := initE2TShutdownManagerTest(t)
123 e2tInstance1 := entities.NewE2TInstance(E2TAddress, PodName)
124 e2tInstance1.State = entities.Active
125 e2tInstance1.AssociatedRanList = []string{}
126 writerMock.On("SaveE2TInstance", mock.MatchedBy(func(e2tInstance *entities.E2TInstance) bool { return e2tInstance.Address == E2TAddress && e2tInstance.State == entities.ToBeDeleted })).Return(nil)
128 data := models.NewRoutingManagerDeleteRequestModel(E2TAddress, nil, nil)
129 marshaled, _ := json.Marshal(data)
130 body := bytes.NewBuffer(marshaled)
131 respBody := ioutil.NopCloser(bytes.NewBufferString(""))
132 httpClientMock.On("Delete", "e2t", "application/json", body).Return(&http.Response{StatusCode: http.StatusCreated, Body: respBody}, nil)
134 writerMock.On("RemoveE2TInstance", E2TAddress).Return(nil)
135 readerMock.On("GetE2TAddresses").Return([]string{E2TAddress}, nil)
136 writerMock.On("SaveE2TAddresses", []string{}).Return(nil)
138 err := shutdownManager.Shutdown(e2tInstance1)
141 readerMock.AssertExpectations(t)
142 writerMock.AssertExpectations(t)
143 httpClientMock.AssertExpectations(t)
146 func TestShutdownSuccess1Instance2Rans(t *testing.T) {
147 shutdownManager, readerMock, writerMock, httpClientMock,_ := initE2TShutdownManagerTest(t)
149 e2tInstance1 := entities.NewE2TInstance(E2TAddress, PodName)
150 e2tInstance1.State = entities.Active
151 e2tInstance1.AssociatedRanList = []string{"test1", "test2"}
152 writerMock.On("SaveE2TInstance", mock.MatchedBy(func(e2tInstance *entities.E2TInstance) bool { return e2tInstance.Address == E2TAddress && e2tInstance.State == entities.ToBeDeleted })).Return(nil)
154 nodeb1 := &entities.NodebInfo{RanName:"test1", AssociatedE2TInstanceAddress:E2TAddress, ConnectionStatus:entities.ConnectionStatus_CONNECTED, E2ApplicationProtocol:entities.E2ApplicationProtocol_X2_SETUP_REQUEST}
155 readerMock.On("GetNodeb", "test1").Return(nodeb1, nil)
156 nodeb2 := &entities.NodebInfo{RanName:"test2", AssociatedE2TInstanceAddress:E2TAddress, ConnectionStatus:entities.ConnectionStatus_DISCONNECTED, E2ApplicationProtocol:entities.E2ApplicationProtocol_X2_SETUP_REQUEST}
157 readerMock.On("GetNodeb", "test2").Return(nodeb2, nil)
159 data := models.NewRoutingManagerDeleteRequestModel(E2TAddress, []string{"test1", "test2"}, nil)
160 marshaled, _ := json.Marshal(data)
161 body := bytes.NewBuffer(marshaled)
162 respBody := ioutil.NopCloser(bytes.NewBufferString(""))
163 httpClientMock.On("Delete", "e2t", "application/json", body).Return(&http.Response{StatusCode: http.StatusCreated, Body: respBody}, nil)
165 writerMock.On("RemoveE2TInstance", E2TAddress).Return(nil)
166 readerMock.On("GetE2TAddresses").Return([]string{E2TAddress}, nil)
167 writerMock.On("SaveE2TAddresses", []string{}).Return(nil)
170 nodeb1new.AssociatedE2TInstanceAddress = ""
171 nodeb1new.ConnectionStatus = entities.ConnectionStatus_DISCONNECTED
172 writerMock.On("UpdateNodebInfo", &nodeb1new).Return(nil)
174 nodeb2new.AssociatedE2TInstanceAddress = ""
175 nodeb2new.ConnectionStatus = entities.ConnectionStatus_DISCONNECTED
176 writerMock.On("UpdateNodebInfo", &nodeb2new).Return(nil)
178 err := shutdownManager.Shutdown(e2tInstance1)
181 readerMock.AssertExpectations(t)
182 writerMock.AssertExpectations(t)
183 httpClientMock.AssertExpectations(t)
187 func TestShutdownE2tInstanceAlreadyBeingDeleted(t *testing.T) {
188 shutdownManager, readerMock, writerMock, httpClientMock,_ := initE2TShutdownManagerTest(t)
190 e2tInstance1 := entities.NewE2TInstance(E2TAddress, PodName)
191 e2tInstance1.State = entities.ToBeDeleted
192 e2tInstance1.AssociatedRanList = []string{"test1"}
193 e2tInstance1.DeletionTimestamp = time.Now().UnixNano()
195 err := shutdownManager.Shutdown(e2tInstance1)
198 readerMock.AssertExpectations(t)
199 writerMock.AssertExpectations(t)
200 httpClientMock.AssertExpectations(t)
204 func TestShutdownFailureMarkInstanceAsToBeDeleted(t *testing.T) {
205 shutdownManager, readerMock, writerMock, httpClientMock,_ := initE2TShutdownManagerTest(t)
207 e2tInstance1 := entities.NewE2TInstance(E2TAddress, PodName)
208 e2tInstance1.State = entities.Active
209 e2tInstance1.AssociatedRanList = []string{"test1", "test2", "test5"}
210 writerMock.On("SaveE2TInstance", mock.MatchedBy(func(e2tInstance *entities.E2TInstance) bool { return e2tInstance.Address == E2TAddress && e2tInstance.State == entities.ToBeDeleted })).Return(e2managererrors.NewRnibDbError())
212 err := shutdownManager.Shutdown(e2tInstance1)
214 assert.NotNil(t, err)
215 readerMock.AssertExpectations(t)
216 writerMock.AssertExpectations(t)
217 httpClientMock.AssertExpectations(t)
221 func TestShutdownFailureRoutingManagerError(t *testing.T) {
222 shutdownManager, readerMock, writerMock, httpClientMock,_ := initE2TShutdownManagerTest(t)
224 e2tInstance1 := entities.NewE2TInstance(E2TAddress, PodName)
225 e2tInstance1.State = entities.Active
226 e2tInstance1.AssociatedRanList = []string{"test1", "test2", "test5"}
227 e2tInstance2 := entities.NewE2TInstance(E2TAddress2, PodName)
228 e2tInstance2.State = entities.Active
229 e2tInstance2.AssociatedRanList = []string{"test3"}
230 e2tInstance3 := entities.NewE2TInstance(E2TAddress3, PodName)
231 e2tInstance3.State = entities.Active
232 e2tInstance3.AssociatedRanList = []string{"test4"}
233 writerMock.On("SaveE2TInstance", mock.MatchedBy(func(e2tInstance *entities.E2TInstance) bool { return e2tInstance.Address == E2TAddress && e2tInstance.State == entities.ToBeDeleted })).Return(nil)
235 nodeb1 := &entities.NodebInfo{RanName:"test1", AssociatedE2TInstanceAddress:E2TAddress, ConnectionStatus:entities.ConnectionStatus_CONNECTED, E2ApplicationProtocol:entities.E2ApplicationProtocol_X2_SETUP_REQUEST}
236 readerMock.On("GetNodeb", "test1").Return(nodeb1, nil)
237 nodeb2 := &entities.NodebInfo{RanName:"test2", AssociatedE2TInstanceAddress:E2TAddress, ConnectionStatus:entities.ConnectionStatus_SHUTTING_DOWN, E2ApplicationProtocol:entities.E2ApplicationProtocol_X2_SETUP_REQUEST}
238 readerMock.On("GetNodeb", "test2").Return(nodeb2, nil)
239 nodeb5 := &entities.NodebInfo{RanName:"test5", AssociatedE2TInstanceAddress:E2TAddress, ConnectionStatus:entities.ConnectionStatus_CONNECTED, E2ApplicationProtocol:entities.E2ApplicationProtocol_X2_SETUP_REQUEST}
240 readerMock.On("GetNodeb", "test5").Return(nodeb5, nil)
242 e2tAddresses := []string{E2TAddress, E2TAddress2,E2TAddress3}
243 readerMock.On("GetE2TAddresses").Return(e2tAddresses, nil)
245 data := models.NewRoutingManagerDeleteRequestModel(E2TAddress, e2tInstance1.AssociatedRanList, nil)
246 marshaled, _ := json.Marshal(data)
247 body := bytes.NewBuffer(marshaled)
248 respBody := ioutil.NopCloser(bytes.NewBufferString(""))
249 httpClientMock.On("Delete", "e2t", "application/json", body).Return(&http.Response{StatusCode: http.StatusBadRequest, Body: respBody}, nil)
251 writerMock.On("RemoveE2TInstance", E2TAddress).Return(nil)
252 writerMock.On("SaveE2TAddresses", []string{E2TAddress2,E2TAddress3}).Return(nil)
254 nodeb1connected := *nodeb1
255 nodeb1connected.AssociatedE2TInstanceAddress = ""
256 nodeb1connected.ConnectionStatus = entities.ConnectionStatus_DISCONNECTED
257 writerMock.On("UpdateNodebInfo", &nodeb1connected).Return(nil)
258 nodeb2connected := *nodeb2
259 nodeb2connected.AssociatedE2TInstanceAddress = ""
260 nodeb2connected.ConnectionStatus = entities.ConnectionStatus_DISCONNECTED
261 writerMock.On("UpdateNodebInfo", &nodeb2connected).Return(nil)
262 nodeb5connected := *nodeb5
263 nodeb5connected.AssociatedE2TInstanceAddress = ""
264 nodeb5connected.ConnectionStatus = entities.ConnectionStatus_DISCONNECTED
265 writerMock.On("UpdateNodebInfo", &nodeb5connected).Return(nil)
267 err := shutdownManager.Shutdown(e2tInstance1)
270 readerMock.AssertExpectations(t)
271 writerMock.AssertExpectations(t)
272 httpClientMock.AssertExpectations(t)
276 func TestShutdownFailureInClearNodebsAssociation(t *testing.T) {
277 shutdownManager, readerMock, writerMock, httpClientMock,_ := initE2TShutdownManagerTest(t)
279 e2tInstance1 := entities.NewE2TInstance(E2TAddress, PodName)
280 e2tInstance1.State = entities.Active
281 e2tInstance1.AssociatedRanList = []string{"test1", "test2"}
282 writerMock.On("SaveE2TInstance", mock.MatchedBy(func(e2tInstance *entities.E2TInstance) bool { return e2tInstance.Address == E2TAddress && e2tInstance.State == entities.ToBeDeleted })).Return(nil)
284 nodeb1 := &entities.NodebInfo{RanName:"test1", AssociatedE2TInstanceAddress:E2TAddress, ConnectionStatus:entities.ConnectionStatus_CONNECTED, E2ApplicationProtocol:entities.E2ApplicationProtocol_X2_SETUP_REQUEST}
285 readerMock.On("GetNodeb", "test1").Return(nodeb1, nil)
288 nodeb1new.AssociatedE2TInstanceAddress = ""
289 nodeb1new.ConnectionStatus = entities.ConnectionStatus_DISCONNECTED
290 writerMock.On("UpdateNodebInfo", &nodeb1new).Return(common.NewInternalError(fmt.Errorf("for tests")))
292 err := shutdownManager.Shutdown(e2tInstance1)
294 assert.NotNil(t, err)
295 readerMock.AssertExpectations(t)
296 writerMock.AssertExpectations(t)
297 httpClientMock.AssertExpectations(t)
300 func TestShutdownResourceNotFoundErrorInGetNodeb(t *testing.T) {
301 shutdownManager, readerMock, writerMock, httpClientMock,_ := initE2TShutdownManagerTest(t)
303 e2tInstance1 := entities.NewE2TInstance(E2TAddress, PodName)
304 e2tInstance1.State = entities.Active
305 e2tInstance1.AssociatedRanList = []string{"test1", "test2"}
306 writerMock.On("SaveE2TInstance", mock.MatchedBy(func(e2tInstance *entities.E2TInstance) bool { return e2tInstance.Address == E2TAddress && e2tInstance.State == entities.ToBeDeleted })).Return(nil)
308 nodeb1 := &entities.NodebInfo{RanName:"test1", AssociatedE2TInstanceAddress:E2TAddress, ConnectionStatus:entities.ConnectionStatus_CONNECTED, E2ApplicationProtocol:entities.E2ApplicationProtocol_X2_SETUP_REQUEST}
309 readerMock.On("GetNodeb", "test1").Return(nodeb1, nil)
310 var nodeb2 *entities.NodebInfo
311 readerMock.On("GetNodeb", "test2").Return(nodeb2, common.NewResourceNotFoundError("for testing"))
314 nodeb1new.AssociatedE2TInstanceAddress = ""
315 nodeb1new.ConnectionStatus = entities.ConnectionStatus_DISCONNECTED
316 writerMock.On("UpdateNodebInfo", &nodeb1new).Return(nil)
318 err := shutdownManager.Shutdown(e2tInstance1)
320 assert.NotNil(t, err)
321 readerMock.AssertExpectations(t)
322 writerMock.AssertExpectations(t)
323 httpClientMock.AssertExpectations(t)
326 func TestShutdownResourceGeneralErrorInGetNodeb(t *testing.T) {
327 shutdownManager, readerMock, writerMock, httpClientMock,_ := initE2TShutdownManagerTest(t)
329 e2tInstance1 := entities.NewE2TInstance(E2TAddress, PodName)
330 e2tInstance1.State = entities.Active
331 e2tInstance1.AssociatedRanList = []string{"test1", "test2"}
332 writerMock.On("SaveE2TInstance", mock.MatchedBy(func(e2tInstance *entities.E2TInstance) bool { return e2tInstance.Address == E2TAddress && e2tInstance.State == entities.ToBeDeleted })).Return(nil)
334 var nodeb1 *entities.NodebInfo
335 readerMock.On("GetNodeb", "test1").Return(nodeb1, common.NewInternalError(fmt.Errorf("for testing")))
336 nodeb2 := &entities.NodebInfo{RanName:"test2", AssociatedE2TInstanceAddress:E2TAddress, ConnectionStatus:entities.ConnectionStatus_DISCONNECTED, E2ApplicationProtocol:entities.E2ApplicationProtocol_X2_SETUP_REQUEST}
337 readerMock.On("GetNodeb", "test2").Return(nodeb2, nil)
339 data := models.NewRoutingManagerDeleteRequestModel(E2TAddress, []string{"test1", "test2"}, nil)
340 marshaled, _ := json.Marshal(data)
341 body := bytes.NewBuffer(marshaled)
342 respBody := ioutil.NopCloser(bytes.NewBufferString(""))
343 httpClientMock.On("Delete", "e2t", "application/json", body).Return(&http.Response{StatusCode: http.StatusCreated, Body: respBody}, nil)
345 writerMock.On("RemoveE2TInstance", E2TAddress).Return(nil)
346 readerMock.On("GetE2TAddresses").Return([]string{E2TAddress}, nil)
347 writerMock.On("SaveE2TAddresses", []string{}).Return(nil)
350 nodeb2new.AssociatedE2TInstanceAddress = ""
351 nodeb2new.ConnectionStatus = entities.ConnectionStatus_DISCONNECTED
352 writerMock.On("UpdateNodebInfo", &nodeb2new).Return(nil)
354 err := shutdownManager.Shutdown(e2tInstance1)
357 readerMock.AssertExpectations(t)
358 writerMock.AssertExpectations(t)
359 httpClientMock.AssertExpectations(t)
363 func TestShutdownFailureInRemoveE2TInstance(t *testing.T) {
364 shutdownManager, readerMock, writerMock, httpClientMock,_ := initE2TShutdownManagerTest(t)
366 e2tInstance1 := entities.NewE2TInstance(E2TAddress, PodName)
367 e2tInstance1.State = entities.Active
368 e2tInstance1.AssociatedRanList = []string{"test1", "test2", "test5"}
369 e2tInstance2 := entities.NewE2TInstance(E2TAddress2, PodName)
370 e2tInstance2.State = entities.Active
371 e2tInstance2.AssociatedRanList = []string{"test3"}
372 e2tInstance3 := entities.NewE2TInstance(E2TAddress3, PodName)
373 e2tInstance3.State = entities.Active
374 e2tInstance3.AssociatedRanList = []string{"test4"}
375 writerMock.On("SaveE2TInstance", mock.MatchedBy(func(e2tInstance *entities.E2TInstance) bool { return e2tInstance.Address == E2TAddress && e2tInstance.State == entities.ToBeDeleted })).Return(nil)
377 nodeb1 := &entities.NodebInfo{RanName:"test1", AssociatedE2TInstanceAddress:E2TAddress, ConnectionStatus:entities.ConnectionStatus_CONNECTED, E2ApplicationProtocol:entities.E2ApplicationProtocol_X2_SETUP_REQUEST}
378 readerMock.On("GetNodeb", "test1").Return(nodeb1, nil)
379 nodeb2 := &entities.NodebInfo{RanName:"test2", AssociatedE2TInstanceAddress:E2TAddress, ConnectionStatus:entities.ConnectionStatus_SHUTTING_DOWN, E2ApplicationProtocol:entities.E2ApplicationProtocol_X2_SETUP_REQUEST}
380 readerMock.On("GetNodeb", "test2").Return(nodeb2, nil)
381 nodeb5 := &entities.NodebInfo{RanName:"test5", AssociatedE2TInstanceAddress:E2TAddress, ConnectionStatus:entities.ConnectionStatus_CONNECTED, E2ApplicationProtocol:entities.E2ApplicationProtocol_X2_SETUP_REQUEST}
382 readerMock.On("GetNodeb", "test5").Return(nodeb5, nil)
384 data := models.NewRoutingManagerDeleteRequestModel(E2TAddress, e2tInstance1.AssociatedRanList, nil)
385 marshaled, _ := json.Marshal(data)
386 body := bytes.NewBuffer(marshaled)
387 respBody := ioutil.NopCloser(bytes.NewBufferString(""))
388 httpClientMock.On("Delete", "e2t", "application/json", body).Return(&http.Response{StatusCode: http.StatusCreated, Body: respBody}, nil)
390 writerMock.On("RemoveE2TInstance", E2TAddress).Return(common.NewInternalError(fmt.Errorf("for tests")))
392 nodeb1connected := *nodeb1
393 nodeb1connected.AssociatedE2TInstanceAddress = ""
394 nodeb1connected.ConnectionStatus = entities.ConnectionStatus_DISCONNECTED
395 writerMock.On("UpdateNodebInfo", &nodeb1connected).Return(nil)
396 nodeb2connected := *nodeb2
397 nodeb2connected.AssociatedE2TInstanceAddress = ""
398 nodeb2connected.ConnectionStatus = entities.ConnectionStatus_DISCONNECTED
399 writerMock.On("UpdateNodebInfo", &nodeb2connected).Return(nil)
400 nodeb5connected := *nodeb5
401 nodeb5connected.AssociatedE2TInstanceAddress = ""
402 nodeb5connected.ConnectionStatus = entities.ConnectionStatus_DISCONNECTED
403 writerMock.On("UpdateNodebInfo", &nodeb5connected).Return(nil)
405 err := shutdownManager.Shutdown(e2tInstance1)
407 assert.IsType(t, &e2managererrors.RnibDbError{}, err)
408 readerMock.AssertExpectations(t)
409 writerMock.AssertExpectations(t)
410 httpClientMock.AssertExpectations(t)
413 func TestShutdownSuccess2Instance2Rans(t *testing.T) {
414 shutdownManager, readerMock, writerMock, httpClientMock,kubernetesManager := initE2TShutdownManagerTest(t)
416 e2tInstance1 := entities.NewE2TInstance(E2TAddress, PodName)
417 e2tInstance1.State = entities.Active
418 e2tInstance1.AssociatedRanList = []string{"test2"}
419 writerMock.On("SaveE2TInstance", mock.MatchedBy(func(e2tInstance *entities.E2TInstance) bool { return e2tInstance.Address == E2TAddress && e2tInstance.State == entities.ToBeDeleted })).Return(nil)
421 nodeb1 := &entities.NodebInfo{RanName:"test1", AssociatedE2TInstanceAddress:E2TAddress2, ConnectionStatus:entities.ConnectionStatus_CONNECTED, E2ApplicationProtocol:entities.E2ApplicationProtocol_X2_SETUP_REQUEST}
422 nodeb2 := &entities.NodebInfo{RanName:"test2", AssociatedE2TInstanceAddress:E2TAddress, ConnectionStatus:entities.ConnectionStatus_DISCONNECTED, E2ApplicationProtocol:entities.E2ApplicationProtocol_X2_SETUP_REQUEST}
423 readerMock.On("GetNodeb", "test2").Return(nodeb2, nil)
425 data := models.NewRoutingManagerDeleteRequestModel(E2TAddress, []string{"test2"}, nil)
426 marshaled, _ := json.Marshal(data)
427 body := bytes.NewBuffer(marshaled)
428 respBody := ioutil.NopCloser(bytes.NewBufferString(""))
429 httpClientMock.On("Delete", "e2t", "application/json", body).Return(&http.Response{StatusCode: http.StatusCreated, Body: respBody}, nil)
431 writerMock.On("RemoveE2TInstance", E2TAddress).Return(nil)
432 readerMock.On("GetE2TAddresses").Return([]string{E2TAddress}, nil)
433 writerMock.On("SaveE2TAddresses", []string{}).Return(nil)
436 nodeb1new.AssociatedE2TInstanceAddress = ""
437 nodeb1new.ConnectionStatus = entities.ConnectionStatus_DISCONNECTED
439 nodeb2new.AssociatedE2TInstanceAddress = ""
440 nodeb2new.ConnectionStatus = entities.ConnectionStatus_DISCONNECTED
441 writerMock.On("UpdateNodebInfo", &nodeb2new).Return(nil)
444 description: "namespace, 2 pods in Oran",
446 objs: []runtime.Object{pod("oran", PodName), pod("oran", "e2t_2"), pod("some-namespace", "POD_Test_1")},
449 t.Run(test.description, func(t *testing.T) {
450 kubernetesManager.ClientSet = fake.NewSimpleClientset(test.objs...)
452 err := shutdownManager.Shutdown(e2tInstance1)
455 readerMock.AssertExpectations(t)
456 writerMock.AssertExpectations(t)
457 httpClientMock.AssertExpectations(t)
461 func TestShutdownSuccess2Instance2RansNoPod(t *testing.T) {
462 shutdownManager, readerMock, writerMock, httpClientMock,kubernetesManager := initE2TShutdownManagerTest(t)
464 e2tInstance1 := entities.NewE2TInstance(E2TAddress, PodName)
465 e2tInstance1.State = entities.Active
466 e2tInstance1.AssociatedRanList = []string{"test2"}
467 writerMock.On("SaveE2TInstance", mock.MatchedBy(func(e2tInstance *entities.E2TInstance) bool { return e2tInstance.Address == E2TAddress && e2tInstance.State == entities.ToBeDeleted })).Return(nil)
469 nodeb1 := &entities.NodebInfo{RanName:"test1", AssociatedE2TInstanceAddress:E2TAddress2, ConnectionStatus:entities.ConnectionStatus_CONNECTED, E2ApplicationProtocol:entities.E2ApplicationProtocol_X2_SETUP_REQUEST}
470 nodeb2 := &entities.NodebInfo{RanName:"test2", AssociatedE2TInstanceAddress:E2TAddress, ConnectionStatus:entities.ConnectionStatus_DISCONNECTED, E2ApplicationProtocol:entities.E2ApplicationProtocol_X2_SETUP_REQUEST}
471 readerMock.On("GetNodeb", "test2").Return(nodeb2, nil)
473 data := models.NewRoutingManagerDeleteRequestModel(E2TAddress, []string{"test2"}, nil)
474 marshaled, _ := json.Marshal(data)
475 body := bytes.NewBuffer(marshaled)
476 respBody := ioutil.NopCloser(bytes.NewBufferString(""))
477 httpClientMock.On("Delete", "e2t", "application/json", body).Return(&http.Response{StatusCode: http.StatusCreated, Body: respBody}, nil)
479 writerMock.On("RemoveE2TInstance", E2TAddress).Return(nil)
480 readerMock.On("GetE2TAddresses").Return([]string{E2TAddress}, nil)
481 writerMock.On("SaveE2TAddresses", []string{}).Return(nil)
484 nodeb1new.AssociatedE2TInstanceAddress = ""
485 nodeb1new.ConnectionStatus = entities.ConnectionStatus_DISCONNECTED
487 nodeb2new.AssociatedE2TInstanceAddress = ""
488 nodeb2new.ConnectionStatus = entities.ConnectionStatus_DISCONNECTED
489 writerMock.On("UpdateNodebInfo", &nodeb2new).Return(nil)
492 description: "namespace, 2 pods in Oran",
494 objs: []runtime.Object{pod("oran", "e2t_2"), pod("some-namespace", "POD_Test_1")},
497 t.Run(test.description, func(t *testing.T) {
498 kubernetesManager.ClientSet = fake.NewSimpleClientset(test.objs...)
500 err := shutdownManager.Shutdown(e2tInstance1)
503 readerMock.AssertExpectations(t)
504 writerMock.AssertExpectations(t)
505 httpClientMock.AssertExpectations(t)