2 // Copyright 2019 AT&T Intellectual Property
3 // Copyright 2019 Nokia
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
9 // http://www.apache.org/licenses/LICENSE-2.0
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
30 "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/common"
31 "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/entities"
32 "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/reader"
33 "github.com/pkg/errors"
34 "github.com/stretchr/testify/assert"
35 "github.com/stretchr/testify/mock"
39 func initRanLostConnectionTest(t *testing.T) (*logger.Logger,*mocks.RmrMessengerMock, *mocks.RnibReaderMock, *mocks.RnibWriterMock, *RanReconnectionManager) {
40 logger, err := logger.InitLogger(logger.DebugLevel)
42 t.Errorf("#... - failed to initialize logger, error: %s", err)
45 rmrMessengerMock := &mocks.RmrMessengerMock{}
46 rmrService := getRmrService(rmrMessengerMock, logger)
48 readerMock := &mocks.RnibReaderMock{}
49 rnibReaderProvider := func() reader.RNibReader {
52 writerMock := &mocks.RnibWriterMock{}
53 rnibWriterProvider := func() rNibWriter.RNibWriter {
57 ranReconnectionManager := NewRanReconnectionManager(logger, configuration.ParseConfiguration(), rnibReaderProvider, rnibWriterProvider, rmrService)
58 return logger,rmrMessengerMock, readerMock, writerMock, ranReconnectionManager
61 func TestRanReconnectionGetNodebFailure(t *testing.T) {
62 _,_, readerMock, writerMock, ranReconnectionManager := initRanLostConnectionTest(t)
64 var nodebInfo *entities.NodebInfo
65 readerMock.On("GetNodeb", ranName).Return(nodebInfo, common.NewInternalError(errors.New("Error")))
66 err := ranReconnectionManager.ReconnectRan(ranName)
68 readerMock.AssertCalled(t, "GetNodeb", ranName)
69 writerMock.AssertNotCalled(t, "UpdateNodebInfo")
72 func TestShutdownRanReconnection(t *testing.T) {
73 _,_, readerMock, writerMock, ranReconnectionManager := initRanLostConnectionTest(t)
75 origNodebInfo := &entities.NodebInfo{RanName: ranName, GlobalNbId: &entities.GlobalNbId{PlmnId: "xxx", NbId: "yyy"}, ConnectionStatus: entities.ConnectionStatus_SHUT_DOWN}
76 var rnibErr common.IRNibError
77 readerMock.On("GetNodeb", ranName).Return(origNodebInfo, rnibErr)
78 err := ranReconnectionManager.ReconnectRan(ranName)
80 readerMock.AssertCalled(t, "GetNodeb", ranName)
81 writerMock.AssertNotCalled(t, "UpdateNodebInfo")
84 func TestShuttingdownRanReconnection(t *testing.T) {
85 _,_, readerMock, writerMock, ranReconnectionManager := initRanLostConnectionTest(t)
87 origNodebInfo := &entities.NodebInfo{RanName: ranName, GlobalNbId: &entities.GlobalNbId{PlmnId: "xxx", NbId: "yyy"}, ConnectionStatus: entities.ConnectionStatus_SHUTTING_DOWN}
88 var rnibErr common.IRNibError
89 readerMock.On("GetNodeb", ranName).Return(origNodebInfo, rnibErr)
90 updatedNodebInfo := *origNodebInfo
91 updatedNodebInfo.ConnectionStatus = entities.ConnectionStatus_SHUT_DOWN
92 writerMock.On("UpdateNodebInfo", &updatedNodebInfo).Return(rnibErr)
93 err := ranReconnectionManager.ReconnectRan(ranName)
95 readerMock.AssertCalled(t, "GetNodeb", ranName)
96 writerMock.AssertNumberOfCalls(t, "UpdateNodebInfo", 1)
99 func TestConnectingRanWithMaxAttemptsReconnection(t *testing.T) {
100 _,_, readerMock, writerMock, ranReconnectionManager := initRanLostConnectionTest(t)
102 origNodebInfo := &entities.NodebInfo{RanName: ranName, GlobalNbId: &entities.GlobalNbId{PlmnId: "xxx", NbId: "yyy"}, ConnectionStatus: entities.ConnectionStatus_CONNECTING, ConnectionAttempts: 20}
103 var rnibErr common.IRNibError
104 readerMock.On("GetNodeb", ranName).Return(origNodebInfo, rnibErr)
105 updatedNodebInfo := *origNodebInfo
106 updatedNodebInfo.ConnectionStatus = entities.ConnectionStatus_DISCONNECTED
107 writerMock.On("UpdateNodebInfo", &updatedNodebInfo).Return(rnibErr)
108 err := ranReconnectionManager.ReconnectRan(ranName)
110 readerMock.AssertCalled(t, "GetNodeb", ranName)
111 writerMock.AssertNumberOfCalls(t, "UpdateNodebInfo", 1)
114 func TestUnconnectableRanUpdateNodebInfoFailure(t *testing.T) {
115 _,_, readerMock, writerMock, ranReconnectionManager := initRanLostConnectionTest(t)
117 origNodebInfo := &entities.NodebInfo{RanName: ranName, GlobalNbId: &entities.GlobalNbId{PlmnId: "xxx", NbId: "yyy"}, ConnectionStatus: entities.ConnectionStatus_SHUTTING_DOWN}
118 var rnibErr common.IRNibError
119 readerMock.On("GetNodeb", ranName).Return(origNodebInfo, rnibErr)
120 updatedNodebInfo := *origNodebInfo
121 updatedNodebInfo.ConnectionStatus = entities.ConnectionStatus_SHUT_DOWN
122 writerMock.On("UpdateNodebInfo", &updatedNodebInfo).Return(common.NewInternalError(errors.New("Error")))
123 err := ranReconnectionManager.ReconnectRan(ranName)
124 assert.NotNil(t, err)
125 readerMock.AssertCalled(t, "GetNodeb", ranName)
126 writerMock.AssertNumberOfCalls(t, "UpdateNodebInfo", 1)
129 func TestConnectedRanExecuteSetupSuccess(t *testing.T) {
130 _,rmrMessengerMock, readerMock, writerMock, ranReconnectionManager := initRanLostConnectionTest(t)
132 origNodebInfo := &entities.NodebInfo{RanName: ranName, GlobalNbId: &entities.GlobalNbId{PlmnId: "xxx", NbId: "yyy"}, ConnectionStatus: entities.ConnectionStatus_CONNECTED, E2ApplicationProtocol:entities.E2ApplicationProtocol_ENDC_X2_SETUP_REQUEST}
133 var rnibErr common.IRNibError
134 readerMock.On("GetNodeb", ranName).Return(origNodebInfo, rnibErr)
135 updatedNodebInfo := *origNodebInfo
136 updatedNodebInfo.ConnectionStatus = entities.ConnectionStatus_CONNECTING
137 updatedNodebInfo.ConnectionAttempts++
138 writerMock.On("UpdateNodebInfo", &updatedNodebInfo).Return(nil)
139 rmrMessengerMock.On("SendMsg",mock.Anything, mock.AnythingOfType("int")).Return(&rmrCgo.MBuf{},nil)
140 err := ranReconnectionManager.ReconnectRan(ranName)
142 readerMock.AssertCalled(t, "GetNodeb", ranName)
143 writerMock.AssertNumberOfCalls(t, "UpdateNodebInfo", 1)
144 rmrMessengerMock.AssertNumberOfCalls(t, "SendMsg", 1)
147 func TestConnectedRanExecuteSetupFailure(t *testing.T) {
148 _,_, readerMock, writerMock, ranReconnectionManager := initRanLostConnectionTest(t)
150 origNodebInfo := &entities.NodebInfo{RanName: ranName, GlobalNbId: &entities.GlobalNbId{PlmnId: "xxx", NbId: "yyy"}, ConnectionStatus: entities.ConnectionStatus_CONNECTED}
151 var rnibErr common.IRNibError
152 readerMock.On("GetNodeb", ranName).Return(origNodebInfo, rnibErr)
153 updatedNodebInfo := *origNodebInfo
154 updatedNodebInfo.ConnectionStatus = entities.ConnectionStatus_CONNECTING
155 updatedNodebInfo.ConnectionAttempts++
156 writerMock.On("UpdateNodebInfo", &updatedNodebInfo).Return(common.NewInternalError(errors.New("Error")))
157 err := ranReconnectionManager.ReconnectRan(ranName)
158 assert.NotNil(t, err)
159 readerMock.AssertCalled(t, "GetNodeb", ranName)
160 writerMock.AssertNumberOfCalls(t, "UpdateNodebInfo", 1)
163 // TODO: should extract to test_utils
164 func getRmrService(rmrMessengerMock *mocks.RmrMessengerMock, log *logger.Logger) *services.RmrService {
165 rmrMessenger := rmrCgo.RmrMessenger(rmrMessengerMock)
166 messageChannel := make(chan *models.NotificationResponse)
167 rmrMessengerMock.On("Init", tests.GetPort(), tests.MaxMsgSize, tests.Flags, log).Return(&rmrMessenger)
168 return services.NewRmrService(services.NewRmrConfig(tests.Port, tests.MaxMsgSize, tests.Flags, log), rmrMessenger, make(sessions.E2Sessions), messageChannel)