def test_set_handler_no_community_data_is_already_presented():
    from bgpcfgd.managers_allow_list import BGPAllowListMgr
    cfg_mgr = MagicMock()
    cfg_mgr.update.return_value = None
    cfg_mgr.get_text.return_value = [
        'ip prefix-list PL_ALLOW_LIST_DEPLOYMENT_ID_5_COMMUNITY_empty_V4 seq 10 deny 0.0.0.0/0 le 17',
        'ip prefix-list PL_ALLOW_LIST_DEPLOYMENT_ID_5_COMMUNITY_empty_V4 seq 20 permit 20.20.30.0/24 le 32',
        'ip prefix-list PL_ALLOW_LIST_DEPLOYMENT_ID_5_COMMUNITY_empty_V4 seq 30 permit 40.50.0.0/16 le 32',
        'ipv6 prefix-list PL_ALLOW_LIST_DEPLOYMENT_ID_5_COMMUNITY_empty_V6 seq 10 deny 0::/0 le 59',
        'ipv6 prefix-list PL_ALLOW_LIST_DEPLOYMENT_ID_5_COMMUNITY_empty_V6 seq 20 deny 0::/0 ge 65',
        'ipv6 prefix-list PL_ALLOW_LIST_DEPLOYMENT_ID_5_COMMUNITY_empty_V6 seq 30 permit fc01:20::/64 le 128',
        'ipv6 prefix-list PL_ALLOW_LIST_DEPLOYMENT_ID_5_COMMUNITY_empty_V6 seq 40 permit fc01:30::/64 le 128',
        'route-map ALLOW_LIST_DEPLOYMENT_ID_5_V4 permit 30000',
        ' match ip address prefix-list PL_ALLOW_LIST_DEPLOYMENT_ID_5_COMMUNITY_empty_V4',
        'route-map ALLOW_LIST_DEPLOYMENT_ID_5_V6 permit 30000',
        ' match ipv6 address prefix-list PL_ALLOW_LIST_DEPLOYMENT_ID_5_COMMUNITY_empty_V6',
        'route-map ALLOW_LIST_DEPLOYMENT_ID_5_V4 permit 65535',
        ' set community 123:123 additive',
        'route-map ALLOW_LIST_DEPLOYMENT_ID_5_V6 permit 65535',
        ' set community 123:123 additive',
        ""
    ]
    common_objs = {
            'directory': Directory(),
            'cfg_mgr': cfg_mgr,
            'tf': TemplateFabric(),
            'constants': global_constants,
    }
    mgr = BGPAllowListMgr(common_objs, "CONFIG_DB", "BGP_ALLOWED_PREFIXES")
    mgr.set_handler("DEPLOYMENT_ID|5", {
        "prefixes_v4": "20.20.30.0/24,40.50.0.0/16",
        "prefixes_v6": "fc01:20::/64,fc01:30::/64",
    })
    assert not cfg_mgr.push_list.called, "cfg_mgr.push_list was called, but it shouldn't have been"
Esempio n. 2
0
def __set_prepare_config_common(status,
                                bbr_enabled_pgs,
                                available_pgs,
                                mapping_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=available_pgs)
    m._BBRMgr__get_available_peers_per_peer_group = MagicMock(
        return_value=mapping_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)
Esempio n. 3
0
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 set_del_test(op, args, currect_config, expected_config):
    from bgpcfgd.managers_allow_list import BGPAllowListMgr
    set_del_test.push_list_called = False
    def push_list(args):
        set_del_test.push_list_called = True
        assert args == expected_config
        return True
    #
    bgpcfgd.frr.run_command = lambda cmd: (0, "", "")
    #
    cfg_mgr = MagicMock()
    cfg_mgr.update.return_value = None
    cfg_mgr.push_list = push_list
    cfg_mgr.get_text.return_value = currect_config
    common_objs = {
        'directory': Directory(),
        'cfg_mgr':   cfg_mgr,
        'tf':        TemplateFabric(),
        'constants': global_constants,
    }

    mgr = BGPAllowListMgr(common_objs, "CONFIG_DB", "BGP_ALLOWED_PREFIXES")
    if op == "SET":
        mgr.set_handler(*args)
    elif op == "DEL":
        mgr.del_handler(*args)
    else:
        assert False, "Wrong operation"
    if expected_config:
        assert set_del_test.push_list_called, "cfg_mgr.push_list wasn't called"
    else:
        assert not set_del_test.push_list_called, "cfg_mgr.push_list was called"
Esempio n. 5
0
def test_intf(mocked_log_warn):
    cfg_mgr = MagicMock()
    common_objs = {
        'directory': Directory(),
        'cfg_mgr': cfg_mgr,
        'tf': TemplateFabric(),
        'constants': {},
    }
    m = InterfaceMgr(common_objs, "CONFIG_DB",
                     swsscommon.CFG_VLAN_INTF_TABLE_NAME)

    set_handler_test(m, "Vlan1000", {})
    set_handler_test(m, "Vlan1000|192.168.0.1/21", {})

    # test set handler with invalid ip network
    res = m.set_handler("Vlan1000|invalid_netowrk", {})
    assert res, "Returns always True"
    mocked_log_warn.assert_called_with(
        "Subnet 'invalid_netowrk' format is wrong for interface 'Vlan1000'")

    del_handler_test(m, "Vlan1000")
    del_handler_test(m, "Vlan1000|192.168.0.1/21")
    del_handler_test(m, "Vlan1000|invalid_netowrk")
    mocked_log_warn.assert_called_with(
        "Subnet 'invalid_netowrk' format is wrong for interface 'Vlan1000'")
Esempio n. 6
0
def test_set_del_handler():
    cfg_mgr = MagicMock()
    common_objs = {
        'directory': Directory(),
        'cfg_mgr': cfg_mgr,
        'tf': TemplateFabric(),
        'constants': {},
    }
    m = BGPDataBaseMgr(common_objs, "CONFIG_DB",
                       swsscommon.CFG_DEVICE_METADATA_TABLE_NAME)
    assert m.constants == {}

    # test set_handler
    res = m.set_handler("test_key1", {"test_value1"})
    assert res, "Returns always True"
    assert "test_key1" in m.directory.get_slot(m.db_name, m.table_name)
    assert m.directory.get(m.db_name, m.table_name,
                           "test_key1") == {"test_value1"}

    res = m.set_handler("test_key2", {})
    assert res, "Returns always True"
    assert "test_key2" in m.directory.get_slot(m.db_name, m.table_name)
    assert m.directory.get(m.db_name, m.table_name, "test_key2") == {}

    # test del_handler
    m.del_handler("test_key")
    assert "test_key" not in m.directory.get_slot(m.db_name, m.table_name)
    assert "test_key2" in m.directory.get_slot(m.db_name, m.table_name)
    assert m.directory.get(m.db_name, m.table_name, "test_key2") == {}

    m.del_handler("test_key2")
    assert "test_key2" not in m.directory.get_slot(m.db_name, m.table_name)
Esempio n. 7
0
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)
Esempio n. 8
0
def test___restart_peers_found_deployment_id():
    from bgpcfgd.managers_allow_list import BGPAllowListMgr
    test___restart_peers_found_deployment_id.run_command_counter = 0
    def run_command(cmd):
        output = [
            ['vtysh', '-c', 'clear bgp peer-group BGP_TEST_PEER_GROUP_1 soft in'],
            ['vtysh', '-c', 'clear bgp peer-group BGP_TEST_PEER_GROUP_2 soft in'],
        ]
        desired_value = output[test___restart_peers_found_deployment_id.run_command_counter]
        assert cmd == desired_value
        test___restart_peers_found_deployment_id.run_command_counter += 1
        return 0, "", ""
    cfg_mgr = MagicMock()
    common_objs = {
        'directory': Directory(),
        'cfg_mgr':   cfg_mgr,
        'tf':        TemplateFabric(),
        'constants': global_constants,
    }
    mgr = BGPAllowListMgr(common_objs, "CONFIG_DB", "BGP_ALLOWED_PREFIXES")
    mocked = MagicMock(name='_BGPAllowListMgr__find_peer_group_by_deployment_id')
    mocked.return_value = ["BGP_TEST_PEER_GROUP_1", "BGP_TEST_PEER_GROUP_2"]
    mgr._BGPAllowListMgr__find_peer_group_by_deployment_id = mocked
    bgpcfgd.managers_allow_list.run_command = run_command
    rc = mgr._BGPAllowListMgr__restart_peers(5)
    assert rc
def test___set_handler_validate():
    from bgpcfgd.managers_allow_list import BGPAllowListMgr
    cfg_mgr = MagicMock()
    common_objs = {
        'directory': Directory(),
        'cfg_mgr':   cfg_mgr,
        'tf':        TemplateFabric(),
        'constants': global_constants,
    }
    mgr = BGPAllowListMgr(common_objs, "CONFIG_DB", "BGP_ALLOWED_PREFIXES")
    data = {
        "prefixes_v4": "20.20.30.0/24,40.50.0.0/16",
        "prefixes_v6": "fc01:20::/64,fc01:30::/64",
    }
    assert not mgr._BGPAllowListMgr__set_handler_validate("DEPLOYMENT_ID|5|1010:2020", None)
    assert not mgr._BGPAllowListMgr__set_handler_validate("DEPLOYMENT_ID1|5|1010:2020", data)
    assert not mgr._BGPAllowListMgr__set_handler_validate("DEPLOYMENT_ID|z|1010:2020", data)
    assert not mgr._BGPAllowListMgr__set_handler_validate("DEPLOYMENT_ID|5|1010:2020", {
        "prefixes_v4": "20.20.30.0/24,40.50.0.0/16",
        "prefixes_v6": "20.20.30.0/24,40.50.0.0/16",
    })
    assert not mgr._BGPAllowListMgr__set_handler_validate("DEPLOYMENT_ID|5|1010:2020", {
        "prefixes_v4": "fc01:20::/64,fc01:30::/64",
        "prefixes_v6": "fc01:20::/64,fc01:30::/64",
    })
Esempio n. 10
0
def constructor():
    cfg_mgr = MagicMock()

    common_objs = {
        'directory': Directory(),
        'cfg_mgr': cfg_mgr,
        'constants': {},
    }

    mgr = RouteMapMgr(common_objs, "APPL_DB", "BGP_PROFILE_TABLE")
    return mgr
def construct_BGPAllowListMgr(constants):
    from bgpcfgd.managers_allow_list import BGPAllowListMgr
    cfg_mgr = MagicMock()
    common_objs = {
        'directory': Directory(),
        'cfg_mgr':   cfg_mgr,
        'tf':        TemplateFabric(),
        'constants': constants,
    }
    mgr = BGPAllowListMgr(common_objs, "CONFIG_DB", "BGP_ALLOWED_PREFIXES")
    return mgr
Esempio n. 12
0
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"
Esempio n. 13
0
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")
Esempio n. 14
0
def test_intf_ipv6():
    cfg_mgr = MagicMock()
    common_objs = {
        'directory': Directory(),
        'cfg_mgr': cfg_mgr,
        'tf': TemplateFabric(),
        'constants': {},
    }
    m = InterfaceMgr(common_objs, "CONFIG_DB",
                     swsscommon.CFG_VLAN_INTF_TABLE_NAME)

    set_handler_test(m, "Vlan1000|fc02:1000::1/64", {})
    del_handler_test(m, "Vlan1000|fc02:1000::1/64")
Esempio n. 15
0
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)
Esempio n. 16
0
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 constructor():
    cfg_mgr = MagicMock()

    common_objs = {
        'directory': Directory(),
        'cfg_mgr':   cfg_mgr,
        'tf':        TemplateFabric(),
        'constants': {},
    }

    mgr = StaticRouteMgr(common_objs, "CONFIG_DB", "STATIC_ROUTE")
    assert len(mgr.static_routes) == 0

    return mgr
Esempio n. 18
0
def constructor():
    cfg_mgr = MagicMock()
    common_objs = {
        'directory': Directory(),
        'cfg_mgr': cfg_mgr,
        'tf': TemplateFabric(TEMPLATE_PATH),
        'constants': {},
    }

    m = ZebraSetSrc(common_objs, "STATE_DB",
                    swsscommon.STATE_INTERFACE_TABLE_NAME)
    assert m.lo_ipv4 == None
    assert m.lo_ipv6 == None

    return m
def test___to_prefix_list():
    from bgpcfgd.managers_allow_list import BGPAllowListMgr
    cfg_mgr = MagicMock()
    common_objs = {
        'directory': Directory(),
        'cfg_mgr':   cfg_mgr,
        'tf':        TemplateFabric(),
        'constants': global_constants,
    }
    mgr = BGPAllowListMgr(common_objs, "CONFIG_DB", "BGP_ALLOWED_PREFIXES")

    res_v4 = mgr._BGPAllowListMgr__to_prefix_list(mgr.V4, ["1.2.3.4/32", "10.20.20.10/24"])
    assert res_v4 == ["permit 1.2.3.4/32", "permit 10.20.20.10/24 le 32"]
    res_v6 = mgr._BGPAllowListMgr__to_prefix_list(mgr.V6, ["fc00::1/128", "fc00::/64"])
    assert res_v6 == ["permit fc00::1/128", "permit fc00::/64 le 128"]
def constructor(skip_bgp_asn=False):
    cfg_mgr = MagicMock()

    common_objs = {
        'directory': Directory(),
        'cfg_mgr':   cfg_mgr,
        'tf':        TemplateFabric(),
        'constants': {},
    }

    mgr = AdvertiseRouteMgr(common_objs, "STATE_DB", swsscommon.STATE_ADVERTISE_NETWORK_TABLE_NAME)
    if not skip_bgp_asn:
        mgr.directory.put("CONFIG_DB", swsscommon.CFG_DEVICE_METADATA_TABLE_NAME, "localhost", {"bgp_asn": "65100"})
    assert len(mgr.advertised_routes) == 0

    return mgr
Esempio n. 21
0
def constructor(skip_bgp_asn=False):
    cfg_mgr = MagicMock()

    common_objs = {
        'directory': Directory(),
        'cfg_mgr':   cfg_mgr,
        'tf':        TemplateFabric(),
        'constants': {},
    }

    mgr = StaticRouteMgr(common_objs, "CONFIG_DB", "STATIC_ROUTE")
    if not skip_bgp_asn:
        mgr.directory.put("CONFIG_DB", swsscommon.CFG_DEVICE_METADATA_TABLE_NAME, "localhost", {"bgp_asn": "65100"})
    assert len(mgr.static_routes) == 0

    return mgr
Esempio n. 22
0
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
Esempio n. 23
0
def test___restart_peers_not_found_deployment_id():
    from bgpcfgd.managers_allow_list import BGPAllowListMgr
    def run_command(cmd):
        assert cmd == ['vtysh', '-c', 'clear bgp * soft in']
        return 0, "", ""
    cfg_mgr = MagicMock()
    common_objs = {
        'directory': Directory(),
        'cfg_mgr':   cfg_mgr,
        'tf':        TemplateFabric(),
        'constants': global_constants,
    }
    mgr = BGPAllowListMgr(common_objs, "CONFIG_DB", "BGP_ALLOWED_PREFIXES")
    mocked = MagicMock(name='_BGPAllowListMgr__find_peer_group_by_deployment_id')
    mocked.return_value = []
    mgr._BGPAllowListMgr__find_peer_group_by_deployment_id = mocked
    bgpcfgd.managers_allow_list.run_command = run_command
    rc = mgr._BGPAllowListMgr__restart_peers(5)
    assert rc
Esempio n. 24
0
def constructor():
    cfg_mgr = MagicMock()
    constants = load_constants()['constants']
    common_objs = {
        'directory': Directory(),
        'cfg_mgr': cfg_mgr,
        'tf': TemplateFabric(TEMPLATE_PATH),
        'constants': constants
    }

    return_value_map = {
        "['vtysh', '-c', 'show bgp vrfs json']":
        (0, "{\"vrfs\": {\"default\": {}}}", ""),
        "['vtysh', '-c', 'show bgp vrf default neighbors json']":
        (0, "{\"10.10.10.1\": {}, \"20.20.20.1\": {}, \"fc00:10::1\": {}}", "")
    }

    bgpcfgd.managers_bgp.run_command = lambda cmd: return_value_map[str(cmd)]
    m = bgpcfgd.managers_bgp.BGPPeerMgrBase(
        common_objs, "CONFIG_DB", swsscommon.CFG_BGP_NEIGHBOR_TABLE_NAME,
        "general", True)
    assert m.peer_type == "general"
    assert m.check_neig_meta == ('bgp' in constants
                                 and 'use_neighbors_meta' in constants['bgp']
                                 and constants['bgp']['use_neighbors_meta'])

    m.directory.put("CONFIG_DB", swsscommon.CFG_DEVICE_METADATA_TABLE_NAME,
                    "localhost", {"bgp_asn": "65100"})
    m.directory.put("CONFIG_DB", swsscommon.CFG_LOOPBACK_INTERFACE_TABLE_NAME,
                    "Loopback0|11.11.11.11/32", {})
    m.directory.put("CONFIG_DB", swsscommon.CFG_LOOPBACK_INTERFACE_TABLE_NAME,
                    "Loopback0|FC00:1::32/128", {})
    m.directory.put("LOCAL", "local_addresses", "30.30.30.30",
                    {"interface": "Ethernet4|30.30.30.30/24"})
    m.directory.put("LOCAL", "local_addresses", "fc00:20::20",
                    {"interface": "Ethernet8|fc00:20::20/96"})
    m.directory.put("LOCAL", "interfaces", "Ethernet4|30.30.30.30/24",
                    {"anything": "anything"})
    m.directory.put("LOCAL", "interfaces", "Ethernet8|fc00:20::20/96",
                    {"anything": "anything"})

    return m
Esempio n. 25
0
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)
Esempio n. 26
0
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()
Esempio n. 27
0
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'],
    }
Esempio n. 28
0
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 test_directory(mocked_log_err):
    test_values = {
        "key1": {
            "key1_1": {
                "key1_1_1": "value1_1_1",
                "key1_1_2": "value1_1_2",
                "key1_1_3": "value1_1_3"
            }
        },
        "key2": {"value2"}
    }

    directory = Directory()

    # Put test values
    directory.put("db_name", "table", "key1", test_values["key1"])
    directory.put("db_name", "table", "key2", test_values["key2"])

    # Test get_path()
    assert directory.get_path("db_name", "table", "") == test_values
    assert directory.get_path("db_name", "table",
                              "key1/key1_1/key1_1_1") == "value1_1_1"
    assert directory.get_path("db_name", "table", "key1/key_nonexist") == None

    # Test path_exist()
    assert directory.path_exist("db_name", "table", "key1/key1_1/key1_1_1")
    assert not directory.path_exist("db_name", "table_nonexist", "")
    assert not directory.path_exist("db_name", "table", "key1/key_nonexist")

    # Test get_slot()
    assert directory.get_slot("db_name", "table") == test_values

    # Test get()
    assert directory.get("db_name", "table", "key2") == test_values["key2"]

    # Test remove()
    directory.remove("db_name", "table", "key2")
    assert not directory.path_exist("db_name", "table", "key2")

    # Test remove() with invalid input
    directory.remove("db_name", "table_nonexist", "key2")
    mocked_log_err.assert_called_with(
        "Directory: Can't remove key 'key2' from slot 'db_name__table_nonexist'. The slot doesn't exist"
    )
    directory.remove("db_name", "table", "key_nonexist")
    mocked_log_err.assert_called_with(
        "Directory: Can't remove key 'key_nonexist' from slot 'db_name__table'. The key doesn't exist"
    )

    # Test remove_slot()
    directory.remove_slot("db_name", "table")
    assert not directory.available("db_name", "table")

    # Test remove_slot() with nonexist table
    directory.remove_slot("db_name", "table_nonexist")
    mocked_log_err.assert_called_with(
        "Directory: Can't remove slot 'db_name__table_nonexist'. The slot doesn't exist"
    )
def test___find_peer_group_by_deployment_id():
    from bgpcfgd.managers_allow_list import BGPAllowListMgr
    cfg_mgr = MagicMock()
    cfg_mgr.update.return_value = None
    cfg_mgr.get_text.return_value = [
        'router bgp 64601',
        ' neighbor BGPSLBPassive peer-group',
        ' neighbor BGPSLBPassive remote-as 65432',
        ' neighbor BGPSLBPassive passive',
        ' neighbor BGPSLBPassive ebgp-multihop 255',
        ' neighbor BGPSLBPassive update-source 10.1.0.32',
        ' neighbor PEER_V4 peer-group',
        ' neighbor PEER_V4_INT peer-group',
        ' neighbor PEER_V6 peer-group',
        ' neighbor PEER_V6_INT peer-group',
        ' neighbor 10.0.0.1 remote-as 64802',
        ' neighbor 10.0.0.1 peer-group PEER_V4',
        ' neighbor 10.0.0.1 description ARISTA01T1',
        ' neighbor 10.0.0.1 timers 3 10',
        ' neighbor fc00::2 remote-as 64802',
        ' neighbor fc00::2 peer-group PEER_V6',
        ' neighbor fc00::2 description ARISTA01T1',
        ' neighbor fc00::2 timers 3 10',
        ' address-family ipv4 unicast',
        '  neighbor BGPSLBPassive activate',
        '  neighbor BGPSLBPassive soft-reconfiguration inbound',
        '  neighbor BGPSLBPassive route-map FROM_BGP_SPEAKER in',
        '  neighbor BGPSLBPassive route-map TO_BGP_SPEAKER out',
        '  neighbor PEER_V4 soft-reconfiguration inbound',
        '  neighbor PEER_V4 allowas-in 1',
        '  neighbor PEER_V4 route-map FROM_BGP_PEER_V4 in',
        '  neighbor PEER_V4 route-map TO_BGP_PEER_V4 out',
        '  neighbor PEER_V4_INT soft-reconfiguration inbound',
        '  neighbor PEER_V4_INT allowas-in 1',
        '  neighbor PEER_V4_INT route-map FROM_BGP_PEER_V4 in',
        '  neighbor PEER_V4_INT route-map TO_BGP_PEER_V4 out',
        '  neighbor 10.0.0.1 activate',
        ' exit-address-family',
        ' address-family ipv6 unicast',
        '  neighbor BGPSLBPassive activate',
        '  neighbor PEER_V6 soft-reconfiguration inbound',
        '  neighbor PEER_V6 allowas-in 1',
        '  neighbor PEER_V6 route-map FROM_BGP_PEER_V6 in',
        '  neighbor PEER_V6 route-map TO_BGP_PEER_V6 out',
        '  neighbor PEER_V6_INT soft-reconfiguration inbound',
        '  neighbor PEER_V6_INT allowas-in 1',
        '  neighbor PEER_V6_INT route-map FROM_BGP_PEER_V6 in',
        '  neighbor PEER_V6_INT route-map TO_BGP_PEER_V6 out',
        '  neighbor fc00::2 activate',
        ' exit-address-family',
        'route-map ALLOW_LIST_DEPLOYMENT_ID_0_V4 permit 10',
        ' match community COMMUNITY_ALLOW_LIST_DEPLOYMENT_ID_0_COMMUNITY_1010:1010',
        ' match ip address prefix-list PL_ALLOW_LIST_DEPLOYMENT_ID_0_COMMUNITY_1010:1010_V4',
        'route-map ALLOW_LIST_DEPLOYMENT_ID_0_V4 permit 30000',
        ' match ip address prefix-list PL_ALLOW_LIST_DEPLOYMENT_ID_0_COMMUNITY_empty_V4',
        'route-map ALLOW_LIST_DEPLOYMENT_ID_0_V4 permit 65535',
        ' set community 5060:12345 additive',
        'route-map ALLOW_LIST_DEPLOYMENT_ID_0_V6 permit 10',
        ' match community COMMUNITY_ALLOW_LIST_DEPLOYMENT_ID_0_COMMUNITY_1010:1010',
        ' match ipv6 address prefix-list PL_ALLOW_LIST_DEPLOYMENT_ID_0_COMMUNITY_1010:1010_V6',
        'route-map ALLOW_LIST_DEPLOYMENT_ID_0_V6 permit 30000',
        ' match ipv6 address prefix-list PL_ALLOW_LIST_DEPLOYMENT_ID_0_COMMUNITY_empty_V6',
        'route-map ALLOW_LIST_DEPLOYMENT_ID_0_V6 permit 65535',
        ' set community 5060:12345 additive',
        'route-map FROM_BGP_PEER_V4 permit 100',
        'route-map FROM_BGP_PEER_V4 permit 2',
        ' call ALLOW_LIST_DEPLOYMENT_ID_0_V4',
        ' on-match next',
        'route-map FROM_BGP_PEER_V6 permit 1',
        ' set ipv6 next-hop prefer-global ',
        'route-map FROM_BGP_PEER_V6 permit 100',
        'route-map FROM_BGP_PEER_V6 permit 2',
        ' call ALLOW_LIST_DEPLOYMENT_ID_0_V6',
        ' on-match next',
        'route-map FROM_BGP_SPEAKER permit 10',
        'route-map RM_SET_SRC permit 10',
        ' set src 10.1.0.32',
        'route-map RM_SET_SRC6 permit 10',
        ' set src FC00:1::32',
        'route-map TO_BGP_PEER_V4 permit 100',
        'route-map TO_BGP_PEER_V6 permit 100',
        'route-map TO_BGP_SPEAKER deny 1',
    ]
    common_objs = {
        'directory': Directory(),
        'cfg_mgr':   cfg_mgr,
        'tf':        TemplateFabric(),
        'constants': global_constants,
    }
    mgr = BGPAllowListMgr(common_objs, "CONFIG_DB", "BGP_ALLOWED_PREFIXES")
    values = mgr._BGPAllowListMgr__find_peer_group_by_deployment_id(0)
    assert set(values) == {'PEER_V4_INT', 'PEER_V6_INT', 'PEER_V6', 'PEER_V4'}