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"
38 func initRanLostConnectionTest(t *testing.T) (*logger.Logger, *mocks.RnibReaderMock, *mocks.RnibWriterMock, *RanReconnectionManager) {
39 logger, err := logger.InitLogger(logger.DebugLevel)
41 t.Errorf("#... - failed to initialize logger, error: %s", err)
44 rmrService := getRmrService(&mocks.RmrMessengerMock{}, logger)
46 readerMock := &mocks.RnibReaderMock{}
47 rnibReaderProvider := func() reader.RNibReader {
50 writerMock := &mocks.RnibWriterMock{}
51 rnibWriterProvider := func() rNibWriter.RNibWriter {
55 ranReconnectionManager := NewRanReconnectionManager(logger, configuration.ParseConfiguration(), rnibReaderProvider, rnibWriterProvider, rmrService)
56 return logger, readerMock, writerMock, ranReconnectionManager
59 func TestRanReconnectionGetNodebFailure(t *testing.T) {
60 _, readerMock, writerMock, ranReconnectionManager := initRanLostConnectionTest(t)
62 var nodebInfo *entities.NodebInfo
63 readerMock.On("GetNodeb", ranName).Return(nodebInfo, common.NewInternalError(errors.New("Error")))
64 err := ranReconnectionManager.ReconnectRan(ranName)
66 readerMock.AssertCalled(t, "GetNodeb", ranName)
67 writerMock.AssertNotCalled(t, "UpdateNodebInfo")
70 func TestShutdownRanReconnection(t *testing.T) {
71 _, readerMock, writerMock, ranReconnectionManager := initRanLostConnectionTest(t)
73 origNodebInfo := &entities.NodebInfo{RanName: ranName, GlobalNbId: &entities.GlobalNbId{PlmnId: "xxx", NbId: "yyy"}, ConnectionStatus: entities.ConnectionStatus_SHUT_DOWN}
74 var rnibErr common.IRNibError
75 readerMock.On("GetNodeb", ranName).Return(origNodebInfo, rnibErr)
76 err := ranReconnectionManager.ReconnectRan(ranName)
78 readerMock.AssertCalled(t, "GetNodeb", ranName)
79 writerMock.AssertNotCalled(t, "UpdateNodebInfo")
82 func TestShuttingdownRanReconnection(t *testing.T) {
83 _, readerMock, writerMock, ranReconnectionManager := initRanLostConnectionTest(t)
85 origNodebInfo := &entities.NodebInfo{RanName: ranName, GlobalNbId: &entities.GlobalNbId{PlmnId: "xxx", NbId: "yyy"}, ConnectionStatus: entities.ConnectionStatus_SHUTTING_DOWN}
86 var rnibErr common.IRNibError
87 readerMock.On("GetNodeb", ranName).Return(origNodebInfo, rnibErr)
88 updatedNodebInfo := *origNodebInfo
89 updatedNodebInfo.ConnectionStatus = entities.ConnectionStatus_SHUT_DOWN
90 writerMock.On("UpdateNodebInfo", &updatedNodebInfo).Return(rnibErr)
91 err := ranReconnectionManager.ReconnectRan(ranName)
93 readerMock.AssertCalled(t, "GetNodeb", ranName)
94 writerMock.AssertNumberOfCalls(t, "UpdateNodebInfo", 1)
97 func TestConnectingRanWithMaxAttemptsReconnection(t *testing.T) {
98 _, readerMock, writerMock, ranReconnectionManager := initRanLostConnectionTest(t)
100 origNodebInfo := &entities.NodebInfo{RanName: ranName, GlobalNbId: &entities.GlobalNbId{PlmnId: "xxx", NbId: "yyy"}, ConnectionStatus: entities.ConnectionStatus_CONNECTING, ConnectionAttempts: 20}
101 var rnibErr common.IRNibError
102 readerMock.On("GetNodeb", ranName).Return(origNodebInfo, rnibErr)
103 updatedNodebInfo := *origNodebInfo
104 updatedNodebInfo.ConnectionStatus = entities.ConnectionStatus_DISCONNECTED
105 writerMock.On("UpdateNodebInfo", &updatedNodebInfo).Return(rnibErr)
106 err := ranReconnectionManager.ReconnectRan(ranName)
108 readerMock.AssertCalled(t, "GetNodeb", ranName)
109 writerMock.AssertNumberOfCalls(t, "UpdateNodebInfo", 1)
112 func TestUnconnectableRanUpdateNodebInfoFailure(t *testing.T) {
113 _, readerMock, writerMock, ranReconnectionManager := initRanLostConnectionTest(t)
115 origNodebInfo := &entities.NodebInfo{RanName: ranName, GlobalNbId: &entities.GlobalNbId{PlmnId: "xxx", NbId: "yyy"}, ConnectionStatus: entities.ConnectionStatus_SHUTTING_DOWN}
116 var rnibErr common.IRNibError
117 readerMock.On("GetNodeb", ranName).Return(origNodebInfo, rnibErr)
118 updatedNodebInfo := *origNodebInfo
119 updatedNodebInfo.ConnectionStatus = entities.ConnectionStatus_SHUT_DOWN
120 writerMock.On("UpdateNodebInfo", &updatedNodebInfo).Return(common.NewInternalError(errors.New("Error")))
121 err := ranReconnectionManager.ReconnectRan(ranName)
122 assert.NotNil(t, err)
123 readerMock.AssertCalled(t, "GetNodeb", ranName)
124 writerMock.AssertNumberOfCalls(t, "UpdateNodebInfo", 1)
127 // TODO: should extract to test_utils
128 func getRmrService(rmrMessengerMock *mocks.RmrMessengerMock, log *logger.Logger) *services.RmrService {
129 rmrMessenger := rmrCgo.RmrMessenger(rmrMessengerMock)
130 messageChannel := make(chan *models.NotificationResponse)
131 rmrMessengerMock.On("Init", tests.GetPort(), tests.MaxMsgSize, tests.Flags, log).Return(&rmrMessenger)
132 return services.NewRmrService(services.NewRmrConfig(tests.Port, tests.MaxMsgSize, tests.Flags, log), rmrMessenger, make(sessions.E2Sessions), messageChannel)