blob: 87d01774d1648bc6f2ddc6b75d1a3827644db26e [file] [log] [blame]
Wei-Yu Chen49950b92021-11-08 19:19:18 +08001"""
2Copyright 2020 The Magma Authors.
3
4This source code is licensed under the BSD-style license found in the
5LICENSE file in the root directory of this source tree.
6
7Unless required by applicable law or agreed to in writing, software
8distributed under the License is distributed on an "AS IS" BASIS,
9WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10See the License for the specific language governing permissions and
11limitations under the License.
12"""
13
14from unittest import TestCase, mock
15from xml.etree import ElementTree
16
17import pkg_resources
18from enodebd import metrics
19from data_models.data_model_parameters import ParameterName
20from devices.device_utils import EnodebDeviceName
21from state_machines.enb_acs_manager import StateMachineManager
22from stats_manager import StatsManager
23from tests.test_utils.config_builder import EnodebConfigBuilder
24from tests.test_utils.enb_acs_builder import (
25 EnodebAcsStateMachineBuilder,
26)
27
28
29class StatsManagerTest(TestCase):
30 """
31 Tests for eNodeB statistics manager
32 """
33
34 def setUp(self) -> None:
35 service = EnodebConfigBuilder.get_service_config()
36 self.enb_acs_manager = StateMachineManager(service)
37 self.mgr = StatsManager(self.enb_acs_manager)
38 self.is_clear_stats_called = False
39
40 def tearDown(self):
41 self.mgr = None
42
43 def test_check_rf_tx(self):
44 """ Check that stats are cleared when transmit is disabled on eNB """
45 handler = EnodebAcsStateMachineBuilder \
46 .build_acs_state_machine(EnodebDeviceName.BAICELLS)
47 with mock.patch(
48 'magma.enodebd.devices.baicells.BaicellsHandler.is_enodeb_connected',
49 return_value=True,
50 ):
51 handler.device_cfg.set_parameter(ParameterName.RF_TX_STATUS, True)
52 handler.device_cfg.set_parameter(
53 ParameterName.SERIAL_NUMBER,
54 '123454',
55 )
56 with mock.patch(
57 'magma.enodebd.stats_manager.StatsManager'
58 '._clear_stats',
59 ) as func:
60 self.mgr._check_rf_tx_for_handler(handler)
61 func.assert_not_called()
62 handler.device_cfg.set_parameter(
63 ParameterName.RF_TX_STATUS,
64 False,
65 )
66 self.mgr._check_rf_tx_for_handler(handler)
67 func.assert_any_call()
68
69 def test_parse_stats(self):
70 """ Test that example statistics from eNodeB can be parsed, and metrics
71 updated """
72 # Example performance metrics structure, sent by eNodeB
73 pm_file_example = pkg_resources.resource_string(
74 __name__,
75 'pm_file_example.xml',
76 )
77
78 root = ElementTree.fromstring(pm_file_example)
79 self.mgr._parse_pm_xml('1234', root)
80
81 # Check that metrics were correctly populated
82 # See '<V i="5">123</V>' in pm_file_example
83 rrc_estab_attempts = metrics.STAT_RRC_ESTAB_ATT.collect()
84 self.assertEqual(rrc_estab_attempts[0].samples[0][2], 123)
85 # See '<V i="7">99</V>' in pm_file_example
86 rrc_estab_successes = metrics.STAT_RRC_ESTAB_SUCC.collect()
87 self.assertEqual(rrc_estab_successes[0].samples[0][2], 99)
88 # See '<SV>654</SV>' in pm_file_example
89 rrc_reestab_att_reconf_fail = \
90 metrics.STAT_RRC_REESTAB_ATT_RECONF_FAIL.collect()
91 self.assertEqual(rrc_reestab_att_reconf_fail[0].samples[0][2], 654)
92 # See '<SV>65537</SV>' in pm_file_example
93 erab_rel_req_radio_conn_lost = \
94 metrics.STAT_ERAB_REL_REQ_RADIO_CONN_LOST.collect()
95 self.assertEqual(erab_rel_req_radio_conn_lost[0].samples[0][2], 65537)
96
97 pdcp_user_plane_bytes_ul = \
98 metrics.STAT_PDCP_USER_PLANE_BYTES_UL.collect()
99 pdcp_user_plane_bytes_dl = \
100 metrics.STAT_PDCP_USER_PLANE_BYTES_DL.collect()
101 self.assertEqual(pdcp_user_plane_bytes_ul[0].samples[0][1], {'enodeb': '1234'})
102 self.assertEqual(pdcp_user_plane_bytes_dl[0].samples[0][1], {'enodeb': '1234'})
103 self.assertEqual(pdcp_user_plane_bytes_ul[0].samples[0][2], 1000)
104 self.assertEqual(pdcp_user_plane_bytes_dl[0].samples[0][2], 500)
105
106 def test_clear_stats(self):
107 """
108 Check that stats of PMPM_FILE_TO_METRIC_MAP is cleared successfully
109 """
110 # Example performance metrics structure, sent by eNodeB
111 pm_file_example = pkg_resources.resource_string(
112 __name__,
113 'pm_file_example.xml',
114 )
115
116 root = ElementTree.fromstring(pm_file_example)
117 self.mgr._parse_pm_xml('1234', root)
118
119 # Check that metrics were correctly populated
120 rrc_estab_attempts = metrics.STAT_RRC_ESTAB_ATT.collect()
121 self.assertEqual(rrc_estab_attempts[0].samples[0][2], 123)
122
123 self.mgr._clear_stats()
124 rrc_estab_attempts = metrics.STAT_RRC_ESTAB_ATT.collect()
125 # After clearing stats collection of metric should report 0
126 self.assertEqual(rrc_estab_attempts[0].samples[0][2], 0)