def __set_prepare_config_common(status, bbr_enabled_pgs, available_pgs, expected_cmds, bbr_applied_pgs=None): cfg_mgr = MagicMock() common_objs = { 'directory': Directory(), 'cfg_mgr': cfg_mgr, 'tf': TemplateFabric(), 'constants': global_constants, } m = BBRMgr(common_objs, "CONFIG_DB", "BGP_BBR") m.directory.data = { "CONFIG_DB__DEVICE_METADATA": { "localhost": { "bgp_asn": "65500" } } } m.bbr_enabled_pgs = bbr_enabled_pgs m._BBRMgr__get_available_peer_groups = MagicMock( return_value=sorted(available_pgs)) cmds, peer_groups = m._BBRMgr__set_prepare_config(status) assert cmds == expected_cmds assert set(peer_groups) == (available_pgs if not bbr_applied_pgs else bbr_applied_pgs)
def __restart_peers_common(run_command_results, run_command_expects, last_log_crit_message, mocked_log_crit): cfg_mgr = MagicMock() common_objs = { 'directory': Directory(), 'cfg_mgr': cfg_mgr, 'tf': TemplateFabric(), 'constants': global_constants, } m = BBRMgr(common_objs, "CONFIG_DB", "BGP_BBR") m.bbr_enabled_pgs = { "PEER_V4": ["ipv4", "ipv6"], "PEER_V6": ["ipv6"], } def run_command_mock(cmd): assert cmd == run_command_expects[run_command_mock.run] res = run_command_results[run_command_mock.run] run_command_mock.run += 1 return res run_command_mock.run = 0 bgpcfgd.managers_bbr.run_command = run_command_mock #lambda cmd: (0, "", "") m._BBRMgr__restart_peers() if last_log_crit_message is not None: mocked_log_crit.assert_called_with(last_log_crit_message)
def test__get_available_peer_groups(): cfg_mgr = MagicMock() common_objs = { 'directory': Directory(), 'cfg_mgr': cfg_mgr, 'tf': TemplateFabric(), 'constants': global_constants, } m = BBRMgr(common_objs, "CONFIG_DB", "BGP_BBR") m.cfg_mgr.get_text = MagicMock(return_value=[ ' neighbor PEER_V4 peer-group', ' neighbor PEER_V6 peer-group', ' address-family ipv4', ' neighbor PEER_V4 allowas-in 1', ' neighbor PEER_V4 soft-reconfiguration inbound', ' neighbor PEER_V4 route-map FROM_BGP_PEER_V4 in', ' neighbor PEER_V4 route-map TO_BGP_PEER_V4 out', ' exit-address-family', ' address-family ipv6', ' neighbor PEER_V6 allowas-in 1', ' neighbor PEER_V6 soft-reconfiguration inbound', ' neighbor PEER_V6 route-map FROM_BGP_PEER_V6 in', ' neighbor PEER_V6 route-map TO_BGP_PEER_V6 out', ' exit-address-family', ' ', ]) res = m._BBRMgr__get_available_peer_groups() assert res == {"PEER_V4", "PEER_V6"}
def test_del_handler(mocked_log_err): cfg_mgr = MagicMock() common_objs = { 'directory': Directory(), 'cfg_mgr': cfg_mgr, 'tf': TemplateFabric(), 'constants': global_constants, } m = BBRMgr(common_objs, "CONFIG_DB", "BGP_BBR") m.del_handler("anything") mocked_log_err.assert_called_with( "The 'BGP_BBR' table shouldn't be removed from the db")
def __set_validation_common(key, data, expected_log_err, expected_result, mocked_log_err): cfg_mgr = MagicMock() common_objs = { 'directory': Directory(), 'cfg_mgr': cfg_mgr, 'tf': TemplateFabric(), 'constants': global_constants, } m = BBRMgr(common_objs, "CONFIG_DB", "BGP_BBR") res = m._BBRMgr__set_validation(key, data) assert res == expected_result if expected_log_err is not None: mocked_log_err.assert_called_with(expected_log_err)
def read_pgs_common(constants, expected_log_info, expected_bbr_enabled_pgs, mocked_log_info): cfg_mgr = MagicMock() common_objs = { 'directory': Directory(), 'cfg_mgr': cfg_mgr, 'tf': TemplateFabric(), 'constants': constants, } m = BBRMgr(common_objs, "CONFIG_DB", "BGP_BBR") res = m._BBRMgr__read_pgs() assert res == expected_bbr_enabled_pgs if expected_log_info is not None: mocked_log_info.assert_called_with(expected_log_info)
def __set_prepare_config_common(status, bbr_enabled_pgs, expected_cmds): cfg_mgr = MagicMock() common_objs = { 'directory': Directory(), 'cfg_mgr': cfg_mgr, 'tf': TemplateFabric(), 'constants': global_constants, } m = BBRMgr(common_objs, "CONFIG_DB", "BGP_BBR") m.directory.data = { "CONFIG_DB__DEVICE_METADATA": { "localhost": { "bgp_asn": "65500" } } } m.bbr_enabled_pgs = bbr_enabled_pgs cmds = m._BBRMgr__set_prepare_config(status) assert cmds == expected_cmds
def __init_common(constants, expected_log_info, expected_log_err, expected_bbr_enabled_pgs, expected_status, mocked_log_err, mocked_log_info): cfg_mgr = MagicMock() common_objs = { 'directory': Directory(), 'cfg_mgr': cfg_mgr, 'tf': TemplateFabric(), 'constants': constants, } m = BBRMgr(common_objs, "CONFIG_DB", "BGP_BBR") m._BBRMgr__init() assert m.bbr_enabled_pgs == expected_bbr_enabled_pgs assert m.directory.get("CONFIG_DB", "BGP_BBR", "status") == expected_status if expected_status == "enabled": assert m.enabled if expected_log_err is not None: mocked_log_err.assert_called_with(expected_log_err) if expected_log_info is not None: mocked_log_info.assert_called_with(expected_log_info)
def test_constructor(): #m1, m2, m3): cfg_mgr = MagicMock() common_objs = { 'directory': Directory(), 'cfg_mgr': cfg_mgr, 'tf': TemplateFabric(), 'constants': {}, } m = BBRMgr(common_objs, "CONFIG_DB", "BGP_BBR") assert not m.enabled assert len(m.bbr_enabled_pgs) == 0 assert m.directory.get("CONFIG_DB", "BGP_BBR", "status") == "disabled"
def test__get_available_peers_per_peer_group(): cfg_mgr = MagicMock() common_objs = { 'directory': Directory(), 'cfg_mgr': cfg_mgr, 'tf': TemplateFabric(), 'constants': global_constants, } m = BBRMgr(common_objs, "CONFIG_DB", "BGP_BBR") m.cfg_mgr.get_text = MagicMock(return_value=[ ' neighbor PEER_V4 peer-group', ' neighbor PEER_V6 peer-group', ' neighbor 10.0.0.1 peer-group PEER_V4', ' neighbor fc00::1 peer-group PEER_V6', ' neighbor 10.0.0.10 peer-group PEER_V4', ' neighbor fc00::2 peer-group PEER_V6', ' ', ]) res = m._BBRMgr__get_available_peers_per_peer_group(['PEER_V4', "PEER_V6"]) assert dict(res) == { "PEER_V4": ['10.0.0.1', '10.0.0.10'], "PEER_V6": ['fc00::1', 'fc00::2'], }
def set_handler_common(key, value, is_enabled, is_valid, mocked_log_info): cfg_mgr = MagicMock() common_objs = { 'directory': Directory(), 'cfg_mgr': cfg_mgr, 'tf': TemplateFabric(), 'constants': global_constants, } m = BBRMgr(common_objs, "CONFIG_DB", "BGP_BBR") m.enabled = is_enabled prepare_config_return_value = ([[ "vtysh", "-c", "clear bgp peer-group PEER_V4 soft in" ], ["vtysh", "-c", "clear bgp peer-group PEER_V6 soft in"]], ["PEER_V4", "PEER_V6"]) m._BBRMgr__set_prepare_config = MagicMock( return_value=prepare_config_return_value) m.cfg_mgr.push_list = MagicMock(return_value=None) m.cfg_mgr.restart_peer_groups = MagicMock( return_value=None) # FIXME: check for input res = m.set_handler(key, value) assert res, "Returns always True" if not is_enabled: mocked_log_info.assert_called_with( 'BBRMgr::BBR is disabled. Drop the request') else: if is_valid: m._BBRMgr__set_prepare_config.assert_called_once_with( value["status"]) m.cfg_mgr.push_list.assert_called_once_with( prepare_config_return_value[0]) m.cfg_mgr.restart_peer_groups.assert_called_once_with( prepare_config_return_value[1]) else: m._BBRMgr__set_prepare_config.assert_not_called() m.cfg_mgr.push_list.assert_not_called() m.cfg_mgr.restart_peer_groups.assert_not_called()
def set_handler_common(key, value, is_enabled, is_valid, has_no_push_cmd_errors, mocked_log_crit, mocked_log_info): cfg_mgr = MagicMock() common_objs = { 'directory': Directory(), 'cfg_mgr': cfg_mgr, 'tf': TemplateFabric(), 'constants': global_constants, } m = BBRMgr(common_objs, "CONFIG_DB", "BGP_BBR") m.enabled = is_enabled prepare_config_return_value = [[ "vtysh", "-c", "clear bgp peer-group PEER_V4 soft in" ], ["vtysh", "-c", "clear bgp peer-group PEER_V6 soft in"]] m._BBRMgr__set_prepare_config = MagicMock( return_value=prepare_config_return_value) m.cfg_mgr.push_list = MagicMock(return_value=has_no_push_cmd_errors) m._BBRMgr__restart_peers = MagicMock() res = m.set_handler(key, value) assert res, "Returns always True" if not is_enabled: mocked_log_info.assert_called_with( 'BBRMgr::BBR is disabled. Drop the request') else: if is_valid: m._BBRMgr__set_prepare_config.assert_called_once_with( value["status"]) m.cfg_mgr.push_list.assert_called_once_with( prepare_config_return_value) if has_no_push_cmd_errors: m._BBRMgr__restart_peers.assert_called_once() else: mocked_log_crit.assert_called_with( "BBRMgr::can't apply configuration") m._BBRMgr__restart_peers.assert_not_called() else: m._BBRMgr__set_prepare_config.assert_not_called() m.cfg_mgr.push_list.assert_not_called() m._BBRMgr__restart_peers.assert_not_called()