7679473764324980e00c52f985036e0ac55c9427
[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/models"
25         "e2mgr/rNibWriter"
26         "e2mgr/rmrCgo"
27         "e2mgr/services"
28         "e2mgr/tests"
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"
35         "testing"
36 )
37
38 func initRanLostConnectionTest(t *testing.T) (*logger.Logger, *mocks.RmrMessengerMock, *mocks.RnibReaderMock, *mocks.RnibWriterMock, *RanReconnectionManager) {
39         logger, err := logger.InitLogger(logger.DebugLevel)
40         if err != nil {
41                 t.Errorf("#... - failed to initialize logger, error: %s", err)
42         }
43
44         rmrMessengerMock := &mocks.RmrMessengerMock{}
45         rmrService := getRmrService(rmrMessengerMock, logger)
46
47         readerMock := &mocks.RnibReaderMock{}
48         rnibReaderProvider := func() reader.RNibReader {
49                 return readerMock
50         }
51         writerMock := &mocks.RnibWriterMock{}
52         rnibWriterProvider := func() rNibWriter.RNibWriter {
53                 return writerMock
54         }
55         ranSetupManager := NewRanSetupManager(logger, rmrService, rnibWriterProvider)
56         ranReconnectionManager := NewRanReconnectionManager(logger, configuration.ParseConfiguration(), rnibReaderProvider, rnibWriterProvider, ranSetupManager)
57         return logger, rmrMessengerMock, readerMock, writerMock, ranReconnectionManager
58 }
59
60 func TestRanReconnectionGetNodebFailure(t *testing.T) {
61         _, _, readerMock, writerMock, ranReconnectionManager := initRanLostConnectionTest(t)
62         ranName := "test"
63         var nodebInfo *entities.NodebInfo
64         readerMock.On("GetNodeb", ranName).Return(nodebInfo, common.NewInternalError(errors.New("Error")))
65         err := ranReconnectionManager.ReconnectRan(ranName)
66         assert.NotNil(t, err)
67         readerMock.AssertCalled(t, "GetNodeb", ranName)
68         writerMock.AssertNotCalled(t, "UpdateNodebInfo")
69 }
70
71 func TestShutdownRanReconnection(t *testing.T) {
72         _, _, readerMock, writerMock, ranReconnectionManager := initRanLostConnectionTest(t)
73         ranName := "test"
74         origNodebInfo := &entities.NodebInfo{RanName: ranName, GlobalNbId: &entities.GlobalNbId{PlmnId: "xxx", NbId: "yyy"}, ConnectionStatus: entities.ConnectionStatus_SHUT_DOWN}
75         var rnibErr error
76         readerMock.On("GetNodeb", ranName).Return(origNodebInfo, rnibErr)
77         err := ranReconnectionManager.ReconnectRan(ranName)
78         assert.Nil(t, err)
79         readerMock.AssertCalled(t, "GetNodeb", ranName)
80         writerMock.AssertNotCalled(t, "UpdateNodebInfo")
81 }
82
83 func TestShuttingdownRanReconnection(t *testing.T) {
84         _, _, readerMock, writerMock, ranReconnectionManager := initRanLostConnectionTest(t)
85         ranName := "test"
86         origNodebInfo := &entities.NodebInfo{RanName: ranName, GlobalNbId: &entities.GlobalNbId{PlmnId: "xxx", NbId: "yyy"}, ConnectionStatus: entities.ConnectionStatus_SHUTTING_DOWN}
87         var rnibErr error
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)
93         assert.Nil(t, err)
94         readerMock.AssertCalled(t, "GetNodeb", ranName)
95         writerMock.AssertNumberOfCalls(t, "UpdateNodebInfo", 1)
96 }
97
98 func TestConnectingRanWithMaxAttemptsReconnection(t *testing.T) {
99         _, _, readerMock, writerMock, ranReconnectionManager := initRanLostConnectionTest(t)
100         ranName := "test"
101         origNodebInfo := &entities.NodebInfo{RanName: ranName, GlobalNbId: &entities.GlobalNbId{PlmnId: "xxx", NbId: "yyy"}, ConnectionStatus: entities.ConnectionStatus_CONNECTING, ConnectionAttempts: 20}
102         var rnibErr error
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)
108         assert.Nil(t, err)
109         readerMock.AssertCalled(t, "GetNodeb", ranName)
110         writerMock.AssertNumberOfCalls(t, "UpdateNodebInfo", 1)
111 }
112
113 func TestUnconnectableRanUpdateNodebInfoFailure(t *testing.T) {
114         _, _, readerMock, writerMock, ranReconnectionManager := initRanLostConnectionTest(t)
115         ranName := "test"
116         origNodebInfo := &entities.NodebInfo{RanName: ranName, GlobalNbId: &entities.GlobalNbId{PlmnId: "xxx", NbId: "yyy"}, ConnectionStatus: entities.ConnectionStatus_SHUTTING_DOWN}
117         var rnibErr error
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)
126 }
127
128 func TestConnectedRanExecuteSetupSuccess(t *testing.T) {
129         _, rmrMessengerMock, readerMock, writerMock, ranReconnectionManager := initRanLostConnectionTest(t)
130         ranName := "test"
131         origNodebInfo := &entities.NodebInfo{RanName: ranName, GlobalNbId: &entities.GlobalNbId{PlmnId: "xxx", NbId: "yyy"}, ConnectionStatus: entities.ConnectionStatus_CONNECTED, E2ApplicationProtocol: entities.E2ApplicationProtocol_ENDC_X2_SETUP_REQUEST}
132         var rnibErr error
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)
140         assert.Nil(t, err)
141         readerMock.AssertCalled(t, "GetNodeb", ranName)
142         writerMock.AssertNumberOfCalls(t, "UpdateNodebInfo", 1)
143         rmrMessengerMock.AssertNumberOfCalls(t, "SendMsg", 1)
144 }
145
146 func TestConnectedRanExecuteSetupFailure(t *testing.T) {
147         _, _, readerMock, writerMock, ranReconnectionManager := initRanLostConnectionTest(t)
148         ranName := "test"
149         origNodebInfo := &entities.NodebInfo{RanName: ranName, GlobalNbId: &entities.GlobalNbId{PlmnId: "xxx", NbId: "yyy"}, ConnectionStatus: entities.ConnectionStatus_CONNECTED}
150         var rnibErr error
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)
160 }
161
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)
168 }