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.
29 "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/common"
30 "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/entities"
31 "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/reader"
32 "github.com/pkg/errors"
33 "github.com/stretchr/testify/assert"
34 "github.com/stretchr/testify/mock"
38 func initRanLostConnectionTest(t *testing.T) (*logger.Logger, *mocks.RmrMessengerMock, *mocks.RnibReaderMock, *mocks.RnibWriterMock, *RanReconnectionManager) {
39 logger, err := logger.InitLogger(logger.DebugLevel)
41 t.Errorf("#... - failed to initialize logger, error: %s", err)
44 rmrMessengerMock := &mocks.RmrMessengerMock{}
45 rmrService := getRmrService(rmrMessengerMock, logger)
47 readerMock := &mocks.RnibReaderMock{}
48 rnibReaderProvider := func() reader.RNibReader {
51 writerMock := &mocks.RnibWriterMock{}
52 rnibWriterProvider := func() rNibWriter.RNibWriter {
55 ranSetupManager := NewRanSetupManager(logger, rmrService, rnibWriterProvider)
56 ranReconnectionManager := NewRanReconnectionManager(logger, configuration.ParseConfiguration(), rnibReaderProvider, rnibWriterProvider, ranSetupManager)
57 return logger, rmrMessengerMock, readerMock, writerMock, ranReconnectionManager
60 func TestRanReconnectionGetNodebFailure(t *testing.T) {
61 _, _, readerMock, writerMock, ranReconnectionManager := initRanLostConnectionTest(t)
63 var nodebInfo *entities.NodebInfo
64 readerMock.On("GetNodeb", ranName).Return(nodebInfo, common.NewInternalError(errors.New("Error")))
65 err := ranReconnectionManager.ReconnectRan(ranName)
67 readerMock.AssertCalled(t, "GetNodeb", ranName)
68 writerMock.AssertNotCalled(t, "UpdateNodebInfo")
71 func TestShutdownRanReconnection(t *testing.T) {
72 _, _, readerMock, writerMock, ranReconnectionManager := initRanLostConnectionTest(t)
74 origNodebInfo := &entities.NodebInfo{RanName: ranName, GlobalNbId: &entities.GlobalNbId{PlmnId: "xxx", NbId: "yyy"}, ConnectionStatus: entities.ConnectionStatus_SHUT_DOWN}
76 readerMock.On("GetNodeb", ranName).Return(origNodebInfo, rnibErr)
77 err := ranReconnectionManager.ReconnectRan(ranName)
79 readerMock.AssertCalled(t, "GetNodeb", ranName)
80 writerMock.AssertNotCalled(t, "UpdateNodebInfo")
83 func TestShuttingdownRanReconnection(t *testing.T) {
84 _, _, readerMock, writerMock, ranReconnectionManager := initRanLostConnectionTest(t)
86 origNodebInfo := &entities.NodebInfo{RanName: ranName, GlobalNbId: &entities.GlobalNbId{PlmnId: "xxx", NbId: "yyy"}, ConnectionStatus: entities.ConnectionStatus_SHUTTING_DOWN}
88 readerMock.On("GetNodeb", ranName).Return(origNodebInfo, rnibErr)
89 updatedNodebInfo := *origNodebInfo
90 updatedNodebInfo.ConnectionStatus = entities.ConnectionStatus_SHUT_DOWN
91 writerMock.On("UpdateNodebInfo", &updatedNodebInfo).Return(rnibErr)
92 err := ranReconnectionManager.ReconnectRan(ranName)
94 readerMock.AssertCalled(t, "GetNodeb", ranName)
95 writerMock.AssertNumberOfCalls(t, "UpdateNodebInfo", 1)
98 func TestConnectingRanWithMaxAttemptsReconnection(t *testing.T) {
99 _, _, readerMock, writerMock, ranReconnectionManager := initRanLostConnectionTest(t)
101 origNodebInfo := &entities.NodebInfo{RanName: ranName, GlobalNbId: &entities.GlobalNbId{PlmnId: "xxx", NbId: "yyy"}, ConnectionStatus: entities.ConnectionStatus_CONNECTING, ConnectionAttempts: 20}
103 readerMock.On("GetNodeb", ranName).Return(origNodebInfo, rnibErr)
104 updatedNodebInfo := *origNodebInfo
105 updatedNodebInfo.ConnectionStatus = entities.ConnectionStatus_DISCONNECTED
106 writerMock.On("UpdateNodebInfo", &updatedNodebInfo).Return(rnibErr)
107 err := ranReconnectionManager.ReconnectRan(ranName)
109 readerMock.AssertCalled(t, "GetNodeb", ranName)
110 writerMock.AssertNumberOfCalls(t, "UpdateNodebInfo", 1)
113 func TestUnconnectableRanUpdateNodebInfoFailure(t *testing.T) {
114 _, _, readerMock, writerMock, ranReconnectionManager := initRanLostConnectionTest(t)
116 origNodebInfo := &entities.NodebInfo{RanName: ranName, GlobalNbId: &entities.GlobalNbId{PlmnId: "xxx", NbId: "yyy"}, ConnectionStatus: entities.ConnectionStatus_SHUTTING_DOWN}
118 readerMock.On("GetNodeb", ranName).Return(origNodebInfo, rnibErr)
119 updatedNodebInfo := *origNodebInfo
120 updatedNodebInfo.ConnectionStatus = entities.ConnectionStatus_SHUT_DOWN
121 writerMock.On("UpdateNodebInfo", &updatedNodebInfo).Return(common.NewInternalError(errors.New("Error")))
122 err := ranReconnectionManager.ReconnectRan(ranName)
123 assert.NotNil(t, err)
124 readerMock.AssertCalled(t, "GetNodeb", ranName)
125 writerMock.AssertNumberOfCalls(t, "UpdateNodebInfo", 1)
128 func TestConnectedRanExecuteSetupSuccess(t *testing.T) {
129 _, rmrMessengerMock, readerMock, writerMock, ranReconnectionManager := initRanLostConnectionTest(t)
131 origNodebInfo := &entities.NodebInfo{RanName: ranName, GlobalNbId: &entities.GlobalNbId{PlmnId: "xxx", NbId: "yyy"}, ConnectionStatus: entities.ConnectionStatus_CONNECTED, E2ApplicationProtocol: entities.E2ApplicationProtocol_ENDC_X2_SETUP_REQUEST}
133 readerMock.On("GetNodeb", ranName).Return(origNodebInfo, rnibErr)
134 updatedNodebInfo := *origNodebInfo
135 updatedNodebInfo.ConnectionStatus = entities.ConnectionStatus_CONNECTING
136 updatedNodebInfo.ConnectionAttempts++
137 writerMock.On("UpdateNodebInfo", &updatedNodebInfo).Return(nil)
138 rmrMessengerMock.On("SendMsg", mock.Anything, mock.AnythingOfType("int")).Return(&rmrCgo.MBuf{}, nil)
139 err := ranReconnectionManager.ReconnectRan(ranName)
141 readerMock.AssertCalled(t, "GetNodeb", ranName)
142 writerMock.AssertNumberOfCalls(t, "UpdateNodebInfo", 1)
143 rmrMessengerMock.AssertNumberOfCalls(t, "SendMsg", 1)
146 func TestConnectedRanExecuteSetupFailure(t *testing.T) {
147 _, _, readerMock, writerMock, ranReconnectionManager := initRanLostConnectionTest(t)
149 origNodebInfo := &entities.NodebInfo{RanName: ranName, GlobalNbId: &entities.GlobalNbId{PlmnId: "xxx", NbId: "yyy"}, ConnectionStatus: entities.ConnectionStatus_CONNECTED}
151 readerMock.On("GetNodeb", ranName).Return(origNodebInfo, rnibErr)
152 updatedNodebInfo := *origNodebInfo
153 updatedNodebInfo.ConnectionStatus = entities.ConnectionStatus_CONNECTING
154 updatedNodebInfo.ConnectionAttempts++
155 writerMock.On("UpdateNodebInfo", &updatedNodebInfo).Return(common.NewInternalError(errors.New("Error")))
156 err := ranReconnectionManager.ReconnectRan(ranName)
157 assert.NotNil(t, err)
158 readerMock.AssertCalled(t, "GetNodeb", ranName)
159 writerMock.AssertNumberOfCalls(t, "UpdateNodebInfo", 1)
162 // TODO: should extract to test_utils
163 func getRmrService(rmrMessengerMock *mocks.RmrMessengerMock, log *logger.Logger) *services.RmrService {
164 rmrMessenger := rmrCgo.RmrMessenger(rmrMessengerMock)
165 messageChannel := make(chan *models.NotificationResponse)
166 rmrMessengerMock.On("Init", tests.GetPort(), tests.MaxMsgSize, tests.Flags, log).Return(&rmrMessenger)
167 return services.NewRmrService(services.NewRmrConfig(tests.Port, tests.MaxMsgSize, tests.Flags, log), rmrMessenger, messageChannel)