[RIC-475] [RIC-507] Inject RanStatusChangeManager | Enhance E2 Setup flow | Remove...
[ric-plt/e2mgr.git] / E2Manager / managers / e2t_shutdown_manager_test.go
1 //
2 // Copyright 2019 AT&T Intellectual Property
3 // Copyright 2019 Nokia
4 //
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
8 //
9 //      http://www.apache.org/licenses/LICENSE-2.0
10 //
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.
16 //
17
18 //  This source code is part of the near-RT RIC (RAN Intelligent Controller)
19 //  platform project (RICP).
20
21 package managers
22
23 import (
24         "bytes"
25         "e2mgr/clients"
26         "e2mgr/configuration"
27         "e2mgr/e2managererrors"
28         "e2mgr/mocks"
29         "e2mgr/models"
30         "e2mgr/services"
31         "encoding/json"
32         "fmt"
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"
37         "io/ioutil"
38         //"k8s.io/apimachinery/pkg/runtime"
39         //"k8s.io/client-go/kubernetes/fake"
40         "net/http"
41         "testing"
42         "time"
43 )
44
45 const E2TAddress3 = "10.10.2.17:9800"
46
47 func initE2TShutdownManagerTest(t *testing.T) (*E2TShutdownManager, *mocks.RnibReaderMock, *mocks.RnibWriterMock, *mocks.HttpClientMock, *KubernetesManager) {
48         log := initLog(t)
49         config := &configuration.Configuration{RnibRetryIntervalMs: 10, MaxRnibConnectionAttempts: 3, E2TInstanceDeletionTimeoutMs: 15000}
50
51         readerMock := &mocks.RnibReaderMock{}
52         writerMock := &mocks.RnibWriterMock{}
53         rnibDataService := services.NewRnibDataService(log, config, readerMock, writerMock)
54
55         e2tInstancesManager := NewE2TInstancesManager(rnibDataService, log)
56         httpClientMock := &mocks.HttpClientMock{}
57         rmClient := clients.NewRoutingManagerClient(log, config, httpClientMock)
58
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)
64
65         /*shutdownManager := NewE2TShutdownManager(log, config, rnibDataService, e2tInstancesManager, associationManager, kubernetesManager)
66
67         return shutdownManager, readerMock, writerMock, httpClientMock, kubernetesManager*/
68         shutdownManager := NewE2TShutdownManager(log, config, rnibDataService, e2tInstancesManager, associationManager, nil)
69
70         return shutdownManager, readerMock, writerMock, httpClientMock, nil
71 }
72
73 func TestShutdownSuccess1OutOf3Instances(t *testing.T) {
74         shutdownManager, readerMock, writerMock, httpClientMock,_ := initE2TShutdownManagerTest(t)
75
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)
86
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)
93
94         e2tAddresses := []string{E2TAddress, E2TAddress2,E2TAddress3}
95         readerMock.On("GetE2TAddresses").Return(e2tAddresses, nil)
96
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)
102
103         writerMock.On("RemoveE2TInstance", E2TAddress).Return(nil)
104         writerMock.On("SaveE2TAddresses", []string{E2TAddress2,E2TAddress3}).Return(nil)
105
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)
118
119         err := shutdownManager.Shutdown(e2tInstance1)
120
121         assert.Nil(t, err)
122         readerMock.AssertExpectations(t)
123         writerMock.AssertExpectations(t)
124         httpClientMock.AssertExpectations(t)
125 }
126
127 func TestShutdownSuccess1InstanceWithoutRans(t *testing.T) {
128         shutdownManager, readerMock, writerMock, httpClientMock,_  := initE2TShutdownManagerTest(t)
129
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)
134
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)
140
141         writerMock.On("RemoveE2TInstance", E2TAddress).Return(nil)
142         readerMock.On("GetE2TAddresses").Return([]string{E2TAddress}, nil)
143         writerMock.On("SaveE2TAddresses", []string{}).Return(nil)
144
145         err := shutdownManager.Shutdown(e2tInstance1)
146
147         assert.Nil(t, err)
148         readerMock.AssertExpectations(t)
149         writerMock.AssertExpectations(t)
150         httpClientMock.AssertExpectations(t)
151 }
152
153 func TestShutdownSuccess1Instance2Rans(t *testing.T) {
154         shutdownManager, readerMock, writerMock, httpClientMock,_  := initE2TShutdownManagerTest(t)
155
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)
160
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)
165
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)
171
172         writerMock.On("RemoveE2TInstance", E2TAddress).Return(nil)
173         readerMock.On("GetE2TAddresses").Return([]string{E2TAddress}, nil)
174         writerMock.On("SaveE2TAddresses", []string{}).Return(nil)
175
176         nodeb1new := *nodeb1
177         nodeb1new.AssociatedE2TInstanceAddress = ""
178         nodeb1new.ConnectionStatus = entities.ConnectionStatus_DISCONNECTED
179         writerMock.On("UpdateNodebInfo", &nodeb1new).Return(nil)
180         nodeb2new := *nodeb2
181         nodeb2new.AssociatedE2TInstanceAddress = ""
182         nodeb2new.ConnectionStatus = entities.ConnectionStatus_DISCONNECTED
183         writerMock.On("UpdateNodebInfo", &nodeb2new).Return(nil)
184
185         err := shutdownManager.Shutdown(e2tInstance1)
186
187         assert.Nil(t, err)
188         readerMock.AssertExpectations(t)
189         writerMock.AssertExpectations(t)
190         httpClientMock.AssertExpectations(t)
191
192 }
193
194 func TestShutdownE2tInstanceAlreadyBeingDeleted(t *testing.T) {
195         shutdownManager, readerMock, writerMock, httpClientMock,_  := initE2TShutdownManagerTest(t)
196
197         e2tInstance1 := entities.NewE2TInstance(E2TAddress, PodName)
198         e2tInstance1.State = entities.ToBeDeleted
199         e2tInstance1.AssociatedRanList = []string{"test1"}
200         e2tInstance1.DeletionTimestamp = time.Now().UnixNano()
201
202         err := shutdownManager.Shutdown(e2tInstance1)
203
204         assert.Nil(t, err)
205         readerMock.AssertExpectations(t)
206         writerMock.AssertExpectations(t)
207         httpClientMock.AssertExpectations(t)
208
209 }
210
211 func TestShutdownFailureMarkInstanceAsToBeDeleted(t *testing.T) {
212         shutdownManager, readerMock, writerMock, httpClientMock,_  := initE2TShutdownManagerTest(t)
213
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())
218
219         err := shutdownManager.Shutdown(e2tInstance1)
220
221         assert.NotNil(t, err)
222         readerMock.AssertExpectations(t)
223         writerMock.AssertExpectations(t)
224         httpClientMock.AssertExpectations(t)
225
226 }
227
228 func TestShutdownFailureRoutingManagerError(t *testing.T) {
229         shutdownManager, readerMock, writerMock, httpClientMock,_  := initE2TShutdownManagerTest(t)
230
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)
241
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)
248
249         e2tAddresses := []string{E2TAddress, E2TAddress2,E2TAddress3}
250         readerMock.On("GetE2TAddresses").Return(e2tAddresses, nil)
251
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)
257
258         writerMock.On("RemoveE2TInstance", E2TAddress).Return(nil)
259         writerMock.On("SaveE2TAddresses", []string{E2TAddress2,E2TAddress3}).Return(nil)
260
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)
273
274         err := shutdownManager.Shutdown(e2tInstance1)
275
276         assert.Nil(t, err)
277         readerMock.AssertExpectations(t)
278         writerMock.AssertExpectations(t)
279         httpClientMock.AssertExpectations(t)
280
281 }
282
283 func TestShutdownFailureInClearNodebsAssociation(t *testing.T) {
284         shutdownManager, readerMock, writerMock, httpClientMock,_  := initE2TShutdownManagerTest(t)
285
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)
290
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)
293
294         nodeb1new := *nodeb1
295         nodeb1new.AssociatedE2TInstanceAddress = ""
296         nodeb1new.ConnectionStatus = entities.ConnectionStatus_DISCONNECTED
297         writerMock.On("UpdateNodebInfo", &nodeb1new).Return(common.NewInternalError(fmt.Errorf("for tests")))
298
299         err := shutdownManager.Shutdown(e2tInstance1)
300
301         assert.NotNil(t, err)
302         readerMock.AssertExpectations(t)
303         writerMock.AssertExpectations(t)
304         httpClientMock.AssertExpectations(t)
305 }
306
307 func TestShutdownResourceNotFoundErrorInGetNodeb(t *testing.T) {
308         shutdownManager, readerMock, writerMock, httpClientMock,_  := initE2TShutdownManagerTest(t)
309
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)
314
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"))
319
320         nodeb1new := *nodeb1
321         nodeb1new.AssociatedE2TInstanceAddress = ""
322         nodeb1new.ConnectionStatus = entities.ConnectionStatus_DISCONNECTED
323         writerMock.On("UpdateNodebInfo", &nodeb1new).Return(nil)
324
325         err := shutdownManager.Shutdown(e2tInstance1)
326
327         assert.NotNil(t, err)
328         readerMock.AssertExpectations(t)
329         writerMock.AssertExpectations(t)
330         httpClientMock.AssertExpectations(t)
331 }
332
333 func TestShutdownResourceGeneralErrorInGetNodeb(t *testing.T) {
334         shutdownManager, readerMock, writerMock, httpClientMock,_  := initE2TShutdownManagerTest(t)
335
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)
340
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)
345
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)
351
352         writerMock.On("RemoveE2TInstance", E2TAddress).Return(nil)
353         readerMock.On("GetE2TAddresses").Return([]string{E2TAddress}, nil)
354         writerMock.On("SaveE2TAddresses", []string{}).Return(nil)
355
356         nodeb2new := *nodeb2
357         nodeb2new.AssociatedE2TInstanceAddress = ""
358         nodeb2new.ConnectionStatus = entities.ConnectionStatus_DISCONNECTED
359         writerMock.On("UpdateNodebInfo", &nodeb2new).Return(nil)
360
361         err := shutdownManager.Shutdown(e2tInstance1)
362
363         assert.Nil(t, err)
364         readerMock.AssertExpectations(t)
365         writerMock.AssertExpectations(t)
366         httpClientMock.AssertExpectations(t)
367
368 }
369
370 func TestShutdownFailureInRemoveE2TInstance(t *testing.T) {
371         shutdownManager, readerMock, writerMock, httpClientMock,_  := initE2TShutdownManagerTest(t)
372
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)
383
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)
390
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)
396
397         writerMock.On("RemoveE2TInstance", E2TAddress).Return(common.NewInternalError(fmt.Errorf("for tests")))
398
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)
411
412         err := shutdownManager.Shutdown(e2tInstance1)
413
414         assert.IsType(t, &e2managererrors.RnibDbError{}, err)
415         readerMock.AssertExpectations(t)
416         writerMock.AssertExpectations(t)
417         httpClientMock.AssertExpectations(t)
418 }
419 /*
420 func TestShutdownSuccess2Instance2Rans(t *testing.T) {
421         shutdownManager, readerMock, writerMock, httpClientMock,kubernetesManager  := initE2TShutdownManagerTest(t)
422
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)
427
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)
431
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)
437
438         writerMock.On("RemoveE2TInstance", E2TAddress).Return(nil)
439         readerMock.On("GetE2TAddresses").Return([]string{E2TAddress}, nil)
440         writerMock.On("SaveE2TAddresses", []string{}).Return(nil)
441
442         nodeb1new := *nodeb1
443         nodeb1new.AssociatedE2TInstanceAddress = ""
444         nodeb1new.ConnectionStatus = entities.ConnectionStatus_DISCONNECTED
445         nodeb2new := *nodeb2
446         nodeb2new.AssociatedE2TInstanceAddress = ""
447         nodeb2new.ConnectionStatus = entities.ConnectionStatus_DISCONNECTED
448         writerMock.On("UpdateNodebInfo", &nodeb2new).Return(nil)
449
450         test := TestStruct{
451                 description: "namespace, 2 pods in Oran",
452                 namespace:   "oran",
453                 objs:        []runtime.Object{pod("oran", PodName), pod("oran", "e2t_2"), pod("some-namespace", "POD_Test_1")},
454         }
455
456         t.Run(test.description, func(t *testing.T) {
457                 kubernetesManager.ClientSet = fake.NewSimpleClientset(test.objs...)
458
459                 err := shutdownManager.Shutdown(e2tInstance1)
460
461                 assert.Nil(t, err)
462                 readerMock.AssertExpectations(t)
463                 writerMock.AssertExpectations(t)
464                 httpClientMock.AssertExpectations(t)
465         })
466 }
467
468 func TestShutdownSuccess2Instance2RansNoPod(t *testing.T) {
469         shutdownManager, readerMock, writerMock, httpClientMock,kubernetesManager  := initE2TShutdownManagerTest(t)
470
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)
475
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)
479
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)
485
486         writerMock.On("RemoveE2TInstance", E2TAddress).Return(nil)
487         readerMock.On("GetE2TAddresses").Return([]string{E2TAddress}, nil)
488         writerMock.On("SaveE2TAddresses", []string{}).Return(nil)
489
490         nodeb1new := *nodeb1
491         nodeb1new.AssociatedE2TInstanceAddress = ""
492         nodeb1new.ConnectionStatus = entities.ConnectionStatus_DISCONNECTED
493         nodeb2new := *nodeb2
494         nodeb2new.AssociatedE2TInstanceAddress = ""
495         nodeb2new.ConnectionStatus = entities.ConnectionStatus_DISCONNECTED
496         writerMock.On("UpdateNodebInfo", &nodeb2new).Return(nil)
497
498         test := TestStruct{
499                 description: "namespace, 2 pods in Oran",
500                 namespace:   "oran",
501                 objs:        []runtime.Object{pod("oran", "e2t_2"), pod("some-namespace", "POD_Test_1")},
502         }
503
504         t.Run(test.description, func(t *testing.T) {
505                 kubernetesManager.ClientSet = fake.NewSimpleClientset(test.objs...)
506
507                 err := shutdownManager.Shutdown(e2tInstance1)
508
509                 assert.Nil(t, err)
510                 readerMock.AssertExpectations(t)
511                 writerMock.AssertExpectations(t)
512                 httpClientMock.AssertExpectations(t)
513         })
514 }*/