sync R3 content from Azure
[ric-plt/e2mgr.git] / E2Manager / managers / ran_reconnection_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 package managers
19
20 import (
21         "e2mgr/configuration"
22         "e2mgr/logger"
23         "e2mgr/mocks"
24         "e2mgr/rmrCgo"
25         "e2mgr/services"
26         "e2mgr/services/rmrsender"
27         "e2mgr/tests"
28         "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/common"
29         "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/entities"
30         "github.com/pkg/errors"
31         "github.com/stretchr/testify/assert"
32         "github.com/stretchr/testify/mock"
33         "testing"
34 )
35
36 func initRanLostConnectionTest(t *testing.T) (*logger.Logger, *mocks.RmrMessengerMock, *mocks.RnibReaderMock, *mocks.RnibWriterMock, *RanReconnectionManager) {
37         logger, err := logger.InitLogger(logger.DebugLevel)
38         if err != nil {
39                 t.Errorf("#... - failed to initialize logger, error: %s", err)
40         }
41         config := &configuration.Configuration{RnibRetryIntervalMs: 10, MaxRnibConnectionAttempts: 3}
42
43         rmrMessengerMock := &mocks.RmrMessengerMock{}
44         rmrSender := initRmrSender(rmrMessengerMock, logger)
45
46         readerMock := &mocks.RnibReaderMock{}
47
48         writerMock := &mocks.RnibWriterMock{}
49
50         rnibDataService := services.NewRnibDataService(logger, config, readerMock, writerMock)
51         ranSetupManager := NewRanSetupManager(logger, rmrSender, rnibDataService)
52         ranReconnectionManager := NewRanReconnectionManager(logger, configuration.ParseConfiguration(), rnibDataService, ranSetupManager)
53         return logger, rmrMessengerMock, readerMock, writerMock, ranReconnectionManager
54 }
55
56 func TestRanReconnectionGetNodebFailure(t *testing.T) {
57         _, _, readerMock, writerMock, ranReconnectionManager := initRanLostConnectionTest(t)
58         ranName := "test"
59         var nodebInfo *entities.NodebInfo
60         readerMock.On("GetNodeb", ranName).Return(nodebInfo, common.NewInternalError(errors.New("Error")))
61         err := ranReconnectionManager.ReconnectRan(ranName)
62         assert.NotNil(t, err)
63         readerMock.AssertCalled(t, "GetNodeb", ranName)
64         writerMock.AssertNotCalled(t, "UpdateNodebInfo")
65 }
66
67 func TestShutdownRanReconnection(t *testing.T) {
68         _, _, readerMock, writerMock, ranReconnectionManager := initRanLostConnectionTest(t)
69         ranName := "test"
70         origNodebInfo := &entities.NodebInfo{RanName: ranName, GlobalNbId: &entities.GlobalNbId{PlmnId: "xxx", NbId: "yyy"}, ConnectionStatus: entities.ConnectionStatus_SHUT_DOWN}
71         var rnibErr error
72         readerMock.On("GetNodeb", ranName).Return(origNodebInfo, rnibErr)
73         err := ranReconnectionManager.ReconnectRan(ranName)
74         assert.Nil(t, err)
75         readerMock.AssertCalled(t, "GetNodeb", ranName)
76         writerMock.AssertNotCalled(t, "UpdateNodebInfo")
77 }
78
79 func TestShuttingdownRanReconnection(t *testing.T) {
80         _, _, readerMock, writerMock, ranReconnectionManager := initRanLostConnectionTest(t)
81         ranName := "test"
82         origNodebInfo := &entities.NodebInfo{RanName: ranName, GlobalNbId: &entities.GlobalNbId{PlmnId: "xxx", NbId: "yyy"}, ConnectionStatus: entities.ConnectionStatus_SHUTTING_DOWN}
83         var rnibErr error
84         readerMock.On("GetNodeb", ranName).Return(origNodebInfo, rnibErr)
85         updatedNodebInfo := *origNodebInfo
86         updatedNodebInfo.ConnectionStatus = entities.ConnectionStatus_SHUT_DOWN
87         writerMock.On("UpdateNodebInfo", &updatedNodebInfo).Return(rnibErr)
88         err := ranReconnectionManager.ReconnectRan(ranName)
89         assert.Nil(t, err)
90         readerMock.AssertCalled(t, "GetNodeb", ranName)
91         writerMock.AssertNumberOfCalls(t, "UpdateNodebInfo", 1)
92 }
93
94 func TestConnectingRanWithMaxAttemptsReconnection(t *testing.T) {
95         _, _, readerMock, writerMock, ranReconnectionManager := initRanLostConnectionTest(t)
96         ranName := "test"
97         origNodebInfo := &entities.NodebInfo{RanName: ranName, GlobalNbId: &entities.GlobalNbId{PlmnId: "xxx", NbId: "yyy"}, ConnectionStatus: entities.ConnectionStatus_CONNECTING, ConnectionAttempts: 20}
98         var rnibErr error
99         readerMock.On("GetNodeb", ranName).Return(origNodebInfo, rnibErr)
100         updatedNodebInfo := *origNodebInfo
101         updatedNodebInfo.ConnectionStatus = entities.ConnectionStatus_DISCONNECTED
102         writerMock.On("UpdateNodebInfo", &updatedNodebInfo).Return(rnibErr)
103         err := ranReconnectionManager.ReconnectRan(ranName)
104         assert.Nil(t, err)
105         readerMock.AssertCalled(t, "GetNodeb", ranName)
106         writerMock.AssertNumberOfCalls(t, "UpdateNodebInfo", 1)
107 }
108
109 func TestUnconnectableRanUpdateNodebInfoFailure(t *testing.T) {
110         _, _, readerMock, writerMock, ranReconnectionManager := initRanLostConnectionTest(t)
111         ranName := "test"
112         origNodebInfo := &entities.NodebInfo{RanName: ranName, GlobalNbId: &entities.GlobalNbId{PlmnId: "xxx", NbId: "yyy"}, ConnectionStatus: entities.ConnectionStatus_SHUTTING_DOWN}
113         var rnibErr error
114         readerMock.On("GetNodeb", ranName).Return(origNodebInfo, rnibErr)
115         updatedNodebInfo := *origNodebInfo
116         updatedNodebInfo.ConnectionStatus = entities.ConnectionStatus_SHUT_DOWN
117         writerMock.On("UpdateNodebInfo", &updatedNodebInfo).Return(common.NewInternalError(errors.New("Error")))
118         err := ranReconnectionManager.ReconnectRan(ranName)
119         assert.NotNil(t, err)
120         readerMock.AssertCalled(t, "GetNodeb", ranName)
121         writerMock.AssertNumberOfCalls(t, "UpdateNodebInfo", 1)
122 }
123
124 func TestConnectedRanExecuteSetupSuccess(t *testing.T) {
125         _, rmrMessengerMock, readerMock, writerMock, ranReconnectionManager := initRanLostConnectionTest(t)
126         ranName := "test"
127         origNodebInfo := &entities.NodebInfo{RanName: ranName, GlobalNbId: &entities.GlobalNbId{PlmnId: "xxx", NbId: "yyy"}, ConnectionStatus: entities.ConnectionStatus_CONNECTED, E2ApplicationProtocol: entities.E2ApplicationProtocol_ENDC_X2_SETUP_REQUEST}
128         var rnibErr error
129         readerMock.On("GetNodeb", ranName).Return(origNodebInfo, rnibErr)
130         updatedNodebInfo := *origNodebInfo
131         updatedNodebInfo.ConnectionStatus = entities.ConnectionStatus_CONNECTING
132         updatedNodebInfo.ConnectionAttempts++
133         writerMock.On("UpdateNodebInfo", &updatedNodebInfo).Return(nil)
134         rmrMessengerMock.On("SendMsg", mock.Anything).Return(&rmrCgo.MBuf{}, nil)
135         err := ranReconnectionManager.ReconnectRan(ranName)
136         assert.Nil(t, err)
137         readerMock.AssertCalled(t, "GetNodeb", ranName)
138         writerMock.AssertNumberOfCalls(t, "UpdateNodebInfo", 1)
139         rmrMessengerMock.AssertNumberOfCalls(t, "SendMsg", 1)
140 }
141
142 func TestConnectedRanExecuteSetupFailure(t *testing.T) {
143         _, _, readerMock, writerMock, ranReconnectionManager := initRanLostConnectionTest(t)
144         ranName := "test"
145         origNodebInfo := &entities.NodebInfo{RanName: ranName, GlobalNbId: &entities.GlobalNbId{PlmnId: "xxx", NbId: "yyy"}, ConnectionStatus: entities.ConnectionStatus_CONNECTED}
146         var rnibErr error
147         readerMock.On("GetNodeb", ranName).Return(origNodebInfo, rnibErr)
148         updatedNodebInfo := *origNodebInfo
149         updatedNodebInfo.ConnectionStatus = entities.ConnectionStatus_CONNECTING
150         updatedNodebInfo.ConnectionAttempts++
151         writerMock.On("UpdateNodebInfo", &updatedNodebInfo).Return(common.NewInternalError(errors.New("Error")))
152         err := ranReconnectionManager.ReconnectRan(ranName)
153         assert.NotNil(t, err)
154         readerMock.AssertCalled(t, "GetNodeb", ranName)
155         writerMock.AssertNumberOfCalls(t, "UpdateNodebInfo", 1)
156 }
157
158 func TestUnnecessaryUpdateNodebInfoStatus(t *testing.T) {
159         _, _, _, _, ranReconnectionManager := initRanLostConnectionTest(t)
160         nodebInfo := &entities.NodebInfo{RanName: "ranName", GlobalNbId: &entities.GlobalNbId{PlmnId: "xxx", NbId: "yyy"}, ConnectionStatus: entities.ConnectionStatus_CONNECTED}
161         err := ranReconnectionManager.updateNodebInfoStatus(nodebInfo, entities.ConnectionStatus_CONNECTED)
162         assert.Nil(t, err)
163 }
164
165 func TestNoSetConnectionStatus(t *testing.T) {
166         _, _, _, _, ranReconnectionManager := initRanLostConnectionTest(t)
167         nodebInfo := &entities.NodebInfo{RanName: "ranName", GlobalNbId: &entities.GlobalNbId{PlmnId: "xxx", NbId: "yyy"}, ConnectionStatus: entities.ConnectionStatus_CONNECTED}
168         err := ranReconnectionManager.setConnectionStatusOfUnconnectableRan(nodebInfo)
169         assert.Nil(t, err)
170 }
171
172 func initRmrSender(rmrMessengerMock *mocks.RmrMessengerMock, log *logger.Logger) *rmrsender.RmrSender {
173         rmrMessenger := rmrCgo.RmrMessenger(rmrMessengerMock)
174         rmrMessengerMock.On("Init", tests.GetPort(), tests.MaxMsgSize, tests.Flags, log).Return(&rmrMessenger)
175         return rmrsender.NewRmrSender(log, rmrMessenger)
176 }