7490c42b72d650f2c6c35f2c591b7d5e2867e409
[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 }
65
66 func TestShutdownSuccess1OutOf3Instances(t *testing.T) {
67         shutdownManager, readerMock, writerMock, httpClientMock,_ := initE2TShutdownManagerTest(t)
68
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)
79
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)
86
87         e2tAddresses := []string{E2TAddress, E2TAddress2,E2TAddress3}
88         readerMock.On("GetE2TAddresses").Return(e2tAddresses, nil)
89
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)
95
96         writerMock.On("RemoveE2TInstance", E2TAddress).Return(nil)
97         writerMock.On("SaveE2TAddresses", []string{E2TAddress2,E2TAddress3}).Return(nil)
98
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)
111
112         err := shutdownManager.Shutdown(e2tInstance1)
113
114         assert.Nil(t, err)
115         readerMock.AssertExpectations(t)
116         writerMock.AssertExpectations(t)
117         httpClientMock.AssertExpectations(t)
118 }
119
120 func TestShutdownSuccess1InstanceWithoutRans(t *testing.T) {
121         shutdownManager, readerMock, writerMock, httpClientMock,_  := initE2TShutdownManagerTest(t)
122
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)
127
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)
133
134         writerMock.On("RemoveE2TInstance", E2TAddress).Return(nil)
135         readerMock.On("GetE2TAddresses").Return([]string{E2TAddress}, nil)
136         writerMock.On("SaveE2TAddresses", []string{}).Return(nil)
137
138         err := shutdownManager.Shutdown(e2tInstance1)
139
140         assert.Nil(t, err)
141         readerMock.AssertExpectations(t)
142         writerMock.AssertExpectations(t)
143         httpClientMock.AssertExpectations(t)
144 }
145
146 func TestShutdownSuccess1Instance2Rans(t *testing.T) {
147         shutdownManager, readerMock, writerMock, httpClientMock,_  := initE2TShutdownManagerTest(t)
148
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)
153
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)
158
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)
164
165         writerMock.On("RemoveE2TInstance", E2TAddress).Return(nil)
166         readerMock.On("GetE2TAddresses").Return([]string{E2TAddress}, nil)
167         writerMock.On("SaveE2TAddresses", []string{}).Return(nil)
168
169         nodeb1new := *nodeb1
170         nodeb1new.AssociatedE2TInstanceAddress = ""
171         nodeb1new.ConnectionStatus = entities.ConnectionStatus_DISCONNECTED
172         writerMock.On("UpdateNodebInfo", &nodeb1new).Return(nil)
173         nodeb2new := *nodeb2
174         nodeb2new.AssociatedE2TInstanceAddress = ""
175         nodeb2new.ConnectionStatus = entities.ConnectionStatus_DISCONNECTED
176         writerMock.On("UpdateNodebInfo", &nodeb2new).Return(nil)
177
178         err := shutdownManager.Shutdown(e2tInstance1)
179
180         assert.Nil(t, err)
181         readerMock.AssertExpectations(t)
182         writerMock.AssertExpectations(t)
183         httpClientMock.AssertExpectations(t)
184
185 }
186
187 func TestShutdownE2tInstanceAlreadyBeingDeleted(t *testing.T) {
188         shutdownManager, readerMock, writerMock, httpClientMock,_  := initE2TShutdownManagerTest(t)
189
190         e2tInstance1 := entities.NewE2TInstance(E2TAddress, PodName)
191         e2tInstance1.State = entities.ToBeDeleted
192         e2tInstance1.AssociatedRanList = []string{"test1"}
193         e2tInstance1.DeletionTimestamp = time.Now().UnixNano()
194
195         err := shutdownManager.Shutdown(e2tInstance1)
196
197         assert.Nil(t, err)
198         readerMock.AssertExpectations(t)
199         writerMock.AssertExpectations(t)
200         httpClientMock.AssertExpectations(t)
201
202 }
203
204 func TestShutdownFailureMarkInstanceAsToBeDeleted(t *testing.T) {
205         shutdownManager, readerMock, writerMock, httpClientMock,_  := initE2TShutdownManagerTest(t)
206
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())
211
212         err := shutdownManager.Shutdown(e2tInstance1)
213
214         assert.NotNil(t, err)
215         readerMock.AssertExpectations(t)
216         writerMock.AssertExpectations(t)
217         httpClientMock.AssertExpectations(t)
218
219 }
220
221 func TestShutdownFailureRoutingManagerError(t *testing.T) {
222         shutdownManager, readerMock, writerMock, httpClientMock,_  := initE2TShutdownManagerTest(t)
223
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)
234
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)
241
242         e2tAddresses := []string{E2TAddress, E2TAddress2,E2TAddress3}
243         readerMock.On("GetE2TAddresses").Return(e2tAddresses, nil)
244
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)
250
251         writerMock.On("RemoveE2TInstance", E2TAddress).Return(nil)
252         writerMock.On("SaveE2TAddresses", []string{E2TAddress2,E2TAddress3}).Return(nil)
253
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)
266
267         err := shutdownManager.Shutdown(e2tInstance1)
268
269         assert.Nil(t, err)
270         readerMock.AssertExpectations(t)
271         writerMock.AssertExpectations(t)
272         httpClientMock.AssertExpectations(t)
273
274 }
275
276 func TestShutdownFailureInClearNodebsAssociation(t *testing.T) {
277         shutdownManager, readerMock, writerMock, httpClientMock,_  := initE2TShutdownManagerTest(t)
278
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)
283
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)
286
287         nodeb1new := *nodeb1
288         nodeb1new.AssociatedE2TInstanceAddress = ""
289         nodeb1new.ConnectionStatus = entities.ConnectionStatus_DISCONNECTED
290         writerMock.On("UpdateNodebInfo", &nodeb1new).Return(common.NewInternalError(fmt.Errorf("for tests")))
291
292         err := shutdownManager.Shutdown(e2tInstance1)
293
294         assert.NotNil(t, err)
295         readerMock.AssertExpectations(t)
296         writerMock.AssertExpectations(t)
297         httpClientMock.AssertExpectations(t)
298 }
299
300 func TestShutdownResourceNotFoundErrorInGetNodeb(t *testing.T) {
301         shutdownManager, readerMock, writerMock, httpClientMock,_  := initE2TShutdownManagerTest(t)
302
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)
307
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"))
312
313         nodeb1new := *nodeb1
314         nodeb1new.AssociatedE2TInstanceAddress = ""
315         nodeb1new.ConnectionStatus = entities.ConnectionStatus_DISCONNECTED
316         writerMock.On("UpdateNodebInfo", &nodeb1new).Return(nil)
317
318         err := shutdownManager.Shutdown(e2tInstance1)
319
320         assert.NotNil(t, err)
321         readerMock.AssertExpectations(t)
322         writerMock.AssertExpectations(t)
323         httpClientMock.AssertExpectations(t)
324 }
325
326 func TestShutdownResourceGeneralErrorInGetNodeb(t *testing.T) {
327         shutdownManager, readerMock, writerMock, httpClientMock,_  := initE2TShutdownManagerTest(t)
328
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)
333
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)
338
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)
344
345         writerMock.On("RemoveE2TInstance", E2TAddress).Return(nil)
346         readerMock.On("GetE2TAddresses").Return([]string{E2TAddress}, nil)
347         writerMock.On("SaveE2TAddresses", []string{}).Return(nil)
348
349         nodeb2new := *nodeb2
350         nodeb2new.AssociatedE2TInstanceAddress = ""
351         nodeb2new.ConnectionStatus = entities.ConnectionStatus_DISCONNECTED
352         writerMock.On("UpdateNodebInfo", &nodeb2new).Return(nil)
353
354         err := shutdownManager.Shutdown(e2tInstance1)
355
356         assert.Nil(t, err)
357         readerMock.AssertExpectations(t)
358         writerMock.AssertExpectations(t)
359         httpClientMock.AssertExpectations(t)
360
361 }
362
363 func TestShutdownFailureInRemoveE2TInstance(t *testing.T) {
364         shutdownManager, readerMock, writerMock, httpClientMock,_  := initE2TShutdownManagerTest(t)
365
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)
376
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)
383
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)
389
390         writerMock.On("RemoveE2TInstance", E2TAddress).Return(common.NewInternalError(fmt.Errorf("for tests")))
391
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)
404
405         err := shutdownManager.Shutdown(e2tInstance1)
406
407         assert.IsType(t, &e2managererrors.RnibDbError{}, err)
408         readerMock.AssertExpectations(t)
409         writerMock.AssertExpectations(t)
410         httpClientMock.AssertExpectations(t)
411 }
412
413 func TestShutdownSuccess2Instance2Rans(t *testing.T) {
414         shutdownManager, readerMock, writerMock, httpClientMock,kubernetesManager  := initE2TShutdownManagerTest(t)
415
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)
420
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)
424
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)
430
431         writerMock.On("RemoveE2TInstance", E2TAddress).Return(nil)
432         readerMock.On("GetE2TAddresses").Return([]string{E2TAddress}, nil)
433         writerMock.On("SaveE2TAddresses", []string{}).Return(nil)
434
435         nodeb1new := *nodeb1
436         nodeb1new.AssociatedE2TInstanceAddress = ""
437         nodeb1new.ConnectionStatus = entities.ConnectionStatus_DISCONNECTED
438         nodeb2new := *nodeb2
439         nodeb2new.AssociatedE2TInstanceAddress = ""
440         nodeb2new.ConnectionStatus = entities.ConnectionStatus_DISCONNECTED
441         writerMock.On("UpdateNodebInfo", &nodeb2new).Return(nil)
442
443         test := TestStruct{
444                 description: "namespace, 2 pods in Oran",
445                 namespace:   "oran",
446                 objs:        []runtime.Object{pod("oran", PodName), pod("oran", "e2t_2"), pod("some-namespace", "POD_Test_1")},
447         }
448
449         t.Run(test.description, func(t *testing.T) {
450                 kubernetesManager.ClientSet = fake.NewSimpleClientset(test.objs...)
451
452                 err := shutdownManager.Shutdown(e2tInstance1)
453
454                 assert.Nil(t, err)
455                 readerMock.AssertExpectations(t)
456                 writerMock.AssertExpectations(t)
457                 httpClientMock.AssertExpectations(t)
458         })
459 }
460
461 func TestShutdownSuccess2Instance2RansNoPod(t *testing.T) {
462         shutdownManager, readerMock, writerMock, httpClientMock,kubernetesManager  := initE2TShutdownManagerTest(t)
463
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)
468
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)
472
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)
478
479         writerMock.On("RemoveE2TInstance", E2TAddress).Return(nil)
480         readerMock.On("GetE2TAddresses").Return([]string{E2TAddress}, nil)
481         writerMock.On("SaveE2TAddresses", []string{}).Return(nil)
482
483         nodeb1new := *nodeb1
484         nodeb1new.AssociatedE2TInstanceAddress = ""
485         nodeb1new.ConnectionStatus = entities.ConnectionStatus_DISCONNECTED
486         nodeb2new := *nodeb2
487         nodeb2new.AssociatedE2TInstanceAddress = ""
488         nodeb2new.ConnectionStatus = entities.ConnectionStatus_DISCONNECTED
489         writerMock.On("UpdateNodebInfo", &nodeb2new).Return(nil)
490
491         test := TestStruct{
492                 description: "namespace, 2 pods in Oran",
493                 namespace:   "oran",
494                 objs:        []runtime.Object{pod("oran", "e2t_2"), pod("some-namespace", "POD_Test_1")},
495         }
496
497         t.Run(test.description, func(t *testing.T) {
498                 kubernetesManager.ClientSet = fake.NewSimpleClientset(test.objs...)
499
500                 err := shutdownManager.Shutdown(e2tInstance1)
501
502                 assert.Nil(t, err)
503                 readerMock.AssertExpectations(t)
504                 writerMock.AssertExpectations(t)
505                 httpClientMock.AssertExpectations(t)
506         })
507 }