9e3fcc349fddfd2bca5835700b8c2c1922f22051
[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         associationManager := NewE2TAssociationManager(log, rnibDataService, e2tInstancesManager, rmClient)
59         //kubernetesManager := initKubernetesManagerTest(t)
60
61         /*shutdownManager := NewE2TShutdownManager(log, config, rnibDataService, e2tInstancesManager, associationManager, kubernetesManager)
62
63         return shutdownManager, readerMock, writerMock, httpClientMock, kubernetesManager*/
64         shutdownManager := NewE2TShutdownManager(log, config, rnibDataService, e2tInstancesManager, associationManager, nil)
65
66         return shutdownManager, readerMock, writerMock, httpClientMock, nil
67 }
68
69 func TestShutdownSuccess1OutOf3Instances(t *testing.T) {
70         shutdownManager, readerMock, writerMock, httpClientMock,_ := initE2TShutdownManagerTest(t)
71
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)
82
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)
89
90         e2tAddresses := []string{E2TAddress, E2TAddress2,E2TAddress3}
91         readerMock.On("GetE2TAddresses").Return(e2tAddresses, nil)
92
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)
98
99         writerMock.On("RemoveE2TInstance", E2TAddress).Return(nil)
100         writerMock.On("SaveE2TAddresses", []string{E2TAddress2,E2TAddress3}).Return(nil)
101
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)
114
115         err := shutdownManager.Shutdown(e2tInstance1)
116
117         assert.Nil(t, err)
118         readerMock.AssertExpectations(t)
119         writerMock.AssertExpectations(t)
120         httpClientMock.AssertExpectations(t)
121 }
122
123 func TestShutdownSuccess1InstanceWithoutRans(t *testing.T) {
124         shutdownManager, readerMock, writerMock, httpClientMock,_  := initE2TShutdownManagerTest(t)
125
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)
130
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)
136
137         writerMock.On("RemoveE2TInstance", E2TAddress).Return(nil)
138         readerMock.On("GetE2TAddresses").Return([]string{E2TAddress}, nil)
139         writerMock.On("SaveE2TAddresses", []string{}).Return(nil)
140
141         err := shutdownManager.Shutdown(e2tInstance1)
142
143         assert.Nil(t, err)
144         readerMock.AssertExpectations(t)
145         writerMock.AssertExpectations(t)
146         httpClientMock.AssertExpectations(t)
147 }
148
149 func TestShutdownSuccess1Instance2Rans(t *testing.T) {
150         shutdownManager, readerMock, writerMock, httpClientMock,_  := initE2TShutdownManagerTest(t)
151
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)
156
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)
161
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)
167
168         writerMock.On("RemoveE2TInstance", E2TAddress).Return(nil)
169         readerMock.On("GetE2TAddresses").Return([]string{E2TAddress}, nil)
170         writerMock.On("SaveE2TAddresses", []string{}).Return(nil)
171
172         nodeb1new := *nodeb1
173         nodeb1new.AssociatedE2TInstanceAddress = ""
174         nodeb1new.ConnectionStatus = entities.ConnectionStatus_DISCONNECTED
175         writerMock.On("UpdateNodebInfo", &nodeb1new).Return(nil)
176         nodeb2new := *nodeb2
177         nodeb2new.AssociatedE2TInstanceAddress = ""
178         nodeb2new.ConnectionStatus = entities.ConnectionStatus_DISCONNECTED
179         writerMock.On("UpdateNodebInfo", &nodeb2new).Return(nil)
180
181         err := shutdownManager.Shutdown(e2tInstance1)
182
183         assert.Nil(t, err)
184         readerMock.AssertExpectations(t)
185         writerMock.AssertExpectations(t)
186         httpClientMock.AssertExpectations(t)
187
188 }
189
190 func TestShutdownE2tInstanceAlreadyBeingDeleted(t *testing.T) {
191         shutdownManager, readerMock, writerMock, httpClientMock,_  := initE2TShutdownManagerTest(t)
192
193         e2tInstance1 := entities.NewE2TInstance(E2TAddress, PodName)
194         e2tInstance1.State = entities.ToBeDeleted
195         e2tInstance1.AssociatedRanList = []string{"test1"}
196         e2tInstance1.DeletionTimestamp = time.Now().UnixNano()
197
198         err := shutdownManager.Shutdown(e2tInstance1)
199
200         assert.Nil(t, err)
201         readerMock.AssertExpectations(t)
202         writerMock.AssertExpectations(t)
203         httpClientMock.AssertExpectations(t)
204
205 }
206
207 func TestShutdownFailureMarkInstanceAsToBeDeleted(t *testing.T) {
208         shutdownManager, readerMock, writerMock, httpClientMock,_  := initE2TShutdownManagerTest(t)
209
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())
214
215         err := shutdownManager.Shutdown(e2tInstance1)
216
217         assert.NotNil(t, err)
218         readerMock.AssertExpectations(t)
219         writerMock.AssertExpectations(t)
220         httpClientMock.AssertExpectations(t)
221
222 }
223
224 func TestShutdownFailureRoutingManagerError(t *testing.T) {
225         shutdownManager, readerMock, writerMock, httpClientMock,_  := initE2TShutdownManagerTest(t)
226
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)
237
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)
244
245         e2tAddresses := []string{E2TAddress, E2TAddress2,E2TAddress3}
246         readerMock.On("GetE2TAddresses").Return(e2tAddresses, nil)
247
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)
253
254         writerMock.On("RemoveE2TInstance", E2TAddress).Return(nil)
255         writerMock.On("SaveE2TAddresses", []string{E2TAddress2,E2TAddress3}).Return(nil)
256
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)
269
270         err := shutdownManager.Shutdown(e2tInstance1)
271
272         assert.Nil(t, err)
273         readerMock.AssertExpectations(t)
274         writerMock.AssertExpectations(t)
275         httpClientMock.AssertExpectations(t)
276
277 }
278
279 func TestShutdownFailureInClearNodebsAssociation(t *testing.T) {
280         shutdownManager, readerMock, writerMock, httpClientMock,_  := initE2TShutdownManagerTest(t)
281
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)
286
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)
289
290         nodeb1new := *nodeb1
291         nodeb1new.AssociatedE2TInstanceAddress = ""
292         nodeb1new.ConnectionStatus = entities.ConnectionStatus_DISCONNECTED
293         writerMock.On("UpdateNodebInfo", &nodeb1new).Return(common.NewInternalError(fmt.Errorf("for tests")))
294
295         err := shutdownManager.Shutdown(e2tInstance1)
296
297         assert.NotNil(t, err)
298         readerMock.AssertExpectations(t)
299         writerMock.AssertExpectations(t)
300         httpClientMock.AssertExpectations(t)
301 }
302
303 func TestShutdownResourceNotFoundErrorInGetNodeb(t *testing.T) {
304         shutdownManager, readerMock, writerMock, httpClientMock,_  := initE2TShutdownManagerTest(t)
305
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)
310
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"))
315
316         nodeb1new := *nodeb1
317         nodeb1new.AssociatedE2TInstanceAddress = ""
318         nodeb1new.ConnectionStatus = entities.ConnectionStatus_DISCONNECTED
319         writerMock.On("UpdateNodebInfo", &nodeb1new).Return(nil)
320
321         err := shutdownManager.Shutdown(e2tInstance1)
322
323         assert.NotNil(t, err)
324         readerMock.AssertExpectations(t)
325         writerMock.AssertExpectations(t)
326         httpClientMock.AssertExpectations(t)
327 }
328
329 func TestShutdownResourceGeneralErrorInGetNodeb(t *testing.T) {
330         shutdownManager, readerMock, writerMock, httpClientMock,_  := initE2TShutdownManagerTest(t)
331
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)
336
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)
341
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)
347
348         writerMock.On("RemoveE2TInstance", E2TAddress).Return(nil)
349         readerMock.On("GetE2TAddresses").Return([]string{E2TAddress}, nil)
350         writerMock.On("SaveE2TAddresses", []string{}).Return(nil)
351
352         nodeb2new := *nodeb2
353         nodeb2new.AssociatedE2TInstanceAddress = ""
354         nodeb2new.ConnectionStatus = entities.ConnectionStatus_DISCONNECTED
355         writerMock.On("UpdateNodebInfo", &nodeb2new).Return(nil)
356
357         err := shutdownManager.Shutdown(e2tInstance1)
358
359         assert.Nil(t, err)
360         readerMock.AssertExpectations(t)
361         writerMock.AssertExpectations(t)
362         httpClientMock.AssertExpectations(t)
363
364 }
365
366 func TestShutdownFailureInRemoveE2TInstance(t *testing.T) {
367         shutdownManager, readerMock, writerMock, httpClientMock,_  := initE2TShutdownManagerTest(t)
368
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)
379
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)
386
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)
392
393         writerMock.On("RemoveE2TInstance", E2TAddress).Return(common.NewInternalError(fmt.Errorf("for tests")))
394
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)
407
408         err := shutdownManager.Shutdown(e2tInstance1)
409
410         assert.IsType(t, &e2managererrors.RnibDbError{}, err)
411         readerMock.AssertExpectations(t)
412         writerMock.AssertExpectations(t)
413         httpClientMock.AssertExpectations(t)
414 }
415 /*
416 func TestShutdownSuccess2Instance2Rans(t *testing.T) {
417         shutdownManager, readerMock, writerMock, httpClientMock,kubernetesManager  := initE2TShutdownManagerTest(t)
418
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)
423
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)
427
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)
433
434         writerMock.On("RemoveE2TInstance", E2TAddress).Return(nil)
435         readerMock.On("GetE2TAddresses").Return([]string{E2TAddress}, nil)
436         writerMock.On("SaveE2TAddresses", []string{}).Return(nil)
437
438         nodeb1new := *nodeb1
439         nodeb1new.AssociatedE2TInstanceAddress = ""
440         nodeb1new.ConnectionStatus = entities.ConnectionStatus_DISCONNECTED
441         nodeb2new := *nodeb2
442         nodeb2new.AssociatedE2TInstanceAddress = ""
443         nodeb2new.ConnectionStatus = entities.ConnectionStatus_DISCONNECTED
444         writerMock.On("UpdateNodebInfo", &nodeb2new).Return(nil)
445
446         test := TestStruct{
447                 description: "namespace, 2 pods in Oran",
448                 namespace:   "oran",
449                 objs:        []runtime.Object{pod("oran", PodName), pod("oran", "e2t_2"), pod("some-namespace", "POD_Test_1")},
450         }
451
452         t.Run(test.description, func(t *testing.T) {
453                 kubernetesManager.ClientSet = fake.NewSimpleClientset(test.objs...)
454
455                 err := shutdownManager.Shutdown(e2tInstance1)
456
457                 assert.Nil(t, err)
458                 readerMock.AssertExpectations(t)
459                 writerMock.AssertExpectations(t)
460                 httpClientMock.AssertExpectations(t)
461         })
462 }
463
464 func TestShutdownSuccess2Instance2RansNoPod(t *testing.T) {
465         shutdownManager, readerMock, writerMock, httpClientMock,kubernetesManager  := initE2TShutdownManagerTest(t)
466
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)
471
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)
475
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)
481
482         writerMock.On("RemoveE2TInstance", E2TAddress).Return(nil)
483         readerMock.On("GetE2TAddresses").Return([]string{E2TAddress}, nil)
484         writerMock.On("SaveE2TAddresses", []string{}).Return(nil)
485
486         nodeb1new := *nodeb1
487         nodeb1new.AssociatedE2TInstanceAddress = ""
488         nodeb1new.ConnectionStatus = entities.ConnectionStatus_DISCONNECTED
489         nodeb2new := *nodeb2
490         nodeb2new.AssociatedE2TInstanceAddress = ""
491         nodeb2new.ConnectionStatus = entities.ConnectionStatus_DISCONNECTED
492         writerMock.On("UpdateNodebInfo", &nodeb2new).Return(nil)
493
494         test := TestStruct{
495                 description: "namespace, 2 pods in Oran",
496                 namespace:   "oran",
497                 objs:        []runtime.Object{pod("oran", "e2t_2"), pod("some-namespace", "POD_Test_1")},
498         }
499
500         t.Run(test.description, func(t *testing.T) {
501                 kubernetesManager.ClientSet = fake.NewSimpleClientset(test.objs...)
502
503                 err := shutdownManager.Shutdown(e2tInstance1)
504
505                 assert.Nil(t, err)
506                 readerMock.AssertExpectations(t)
507                 writerMock.AssertExpectations(t)
508                 httpClientMock.AssertExpectations(t)
509         })
510 }*/