diff --git a/src/sonic-bgpcfgd/tests/data/constants/constants_use_neig.yml b/src/sonic-bgpcfgd/tests/data/constants/constants_use_neig.yml new file mode 100644 index 0000000000..12144a8783 --- /dev/null +++ b/src/sonic-bgpcfgd/tests/data/constants/constants_use_neig.yml @@ -0,0 +1,60 @@ +constants: + deployment_id_asn_map: + "1" : 65432 + "2" : 65433 + bgp: + traffic_shift_community: 12345:12345 + families: + - ipv4 + - ipv6 + use_deployment_id: false + use_neighbors_meta: true + graceful_restart: + enabled: true + restart_time: 240 + multipath_relax: + enabled: true + maximum_paths: + enabled: true + ipv4: 64 + ipv6: 64 + allow_list: + enabled: true + default_action: "permit" # or "deny" + drop_community: 5060:12345 # value of the community to identify a prefix to drop. Make sense only with allow_list_default_action equal to 'permit' + default_pl_rules: + v4: + - "deny 0.0.0.0/0 le 17" + - "permit 127.0.0.1/32" + v6: + - "deny 0::/0 le 59" + - "deny 0::/0 ge 65" + bbr: + enabled: true + default_state: "disabled" + peers: + general: # peer_type + db_table: "BGP_NEIGHBOR" + template_dir: "general" + bbr: + PEER_V4: + - ipv4 + PEER_V6: + - ipv6 + internal: # peer_type + db_table: "BGP_INTERNAL_NEIGHBOR" + template_dir: "internal" + monitors: # peer_type + enabled: true + db_table: "BGP_MONITORS" + peer_group: "BGPMON" + template_dir: "monitors" + dynamic: # peer_type + enabled: true + db_table: "BGP_PEER_RANGE" + peer_group: "BGP_SPEAKER" + template_dir: "dynamic" + voq_chassis: # peer_type + enabled: true + db_table: "BGP_VOQ_CHASSIS_NEIGHBOR" + template_dir: "voq_chassis" diff --git a/src/sonic-bgpcfgd/tests/test_bgp.py b/src/sonic-bgpcfgd/tests/test_bgp.py index a4c5ef9977..d332dc4348 100644 --- a/src/sonic-bgpcfgd/tests/test_bgp.py +++ b/src/sonic-bgpcfgd/tests/test_bgp.py @@ -10,9 +10,20 @@ import bgpcfgd.managers_bgp TEMPLATE_PATH = os.path.abspath('../../dockers/docker-fpm-frr/frr') -def constructor(): +def load_constant_files(): + paths = ["tests/data/constants", "../../files/image_config/constants"] + constant_files = [] + + for path in paths: + constant_files += [os.path.abspath(os.path.join(path, name)) for name in os.listdir(path) + if os.path.isfile(os.path.join(path, name)) and name.startswith("constants")] + + return constant_files + + +def constructor(constants_path): cfg_mgr = MagicMock() - constants = load_constants()['constants'] + constants = load_constants(constants_path)['constants'] common_objs = { 'directory': Directory(), 'cfg_mgr': cfg_mgr, @@ -38,75 +49,89 @@ def constructor(): 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"}) + if m.check_neig_meta: + m.directory.put("CONFIG_DB", swsscommon.CFG_DEVICE_NEIGHBOR_METADATA_TABLE_NAME, "TOR", {}) + return m @patch('bgpcfgd.managers_bgp.log_info') def test_update_peer_up(mocked_log_info): - m = constructor() - res = m.set_handler("10.10.10.1", {"admin_status": "up"}) - assert res, "Expect True return value for peer update" - mocked_log_info.assert_called_with("Peer 'default|10.10.10.1' admin state is set to 'up'") + for constant in load_constant_files(): + m = constructor(constant) + res = m.set_handler("10.10.10.1", {"admin_status": "up"}) + assert res, "Expect True return value for peer update" + mocked_log_info.assert_called_with("Peer 'default|10.10.10.1' admin state is set to 'up'") @patch('bgpcfgd.managers_bgp.log_info') def test_update_peer_up_ipv6(mocked_log_info): - m = constructor() - res = m.set_handler("fc00:10::1", {"admin_status": "up"}) - assert res, "Expect True return value for peer update" - mocked_log_info.assert_called_with("Peer 'default|fc00:10::1' admin state is set to 'up'") + for constant in load_constant_files(): + m = constructor(constant) + res = m.set_handler("fc00:10::1", {"admin_status": "up"}) + assert res, "Expect True return value for peer update" + mocked_log_info.assert_called_with("Peer 'default|fc00:10::1' admin state is set to 'up'") @patch('bgpcfgd.managers_bgp.log_info') def test_update_peer_down(mocked_log_info): - m = constructor() - res = m.set_handler("10.10.10.1", {"admin_status": "down"}) - assert res, "Expect True return value for peer update" - mocked_log_info.assert_called_with("Peer 'default|10.10.10.1' admin state is set to 'down'") + for constant in load_constant_files(): + m = constructor(constant) + res = m.set_handler("10.10.10.1", {"admin_status": "down"}) + assert res, "Expect True return value for peer update" + mocked_log_info.assert_called_with("Peer 'default|10.10.10.1' admin state is set to 'down'") @patch('bgpcfgd.managers_bgp.log_err') def test_update_peer_no_admin_status(mocked_log_err): - m = constructor() - res = m.set_handler("10.10.10.1", {"anything": "anything"}) - assert res, "Expect True return value for peer update" - mocked_log_err.assert_called_with("Peer '(default|10.10.10.1)': Can't update the peer. Only 'admin_status' attribute is supported") + for constant in load_constant_files(): + m = constructor(constant) + res = m.set_handler("10.10.10.1", {"anything": "anything"}) + assert res, "Expect True return value for peer update" + mocked_log_err.assert_called_with("Peer '(default|10.10.10.1)': Can't update the peer. Only 'admin_status' attribute is supported") @patch('bgpcfgd.managers_bgp.log_err') def test_update_peer_invalid_admin_status(mocked_log_err): - m = constructor() - res = m.set_handler("10.10.10.1", {"admin_status": "invalid"}) - assert res, "Expect True return value for peer update" - mocked_log_err.assert_called_with("Peer 'default|10.10.10.1': Can't update the peer. It has wrong attribute value attr['admin_status'] = 'invalid'") + for constant in load_constant_files(): + m = constructor(constant) + res = m.set_handler("10.10.10.1", {"admin_status": "invalid"}) + assert res, "Expect True return value for peer update" + mocked_log_err.assert_called_with("Peer 'default|10.10.10.1': Can't update the peer. It has wrong attribute value attr['admin_status'] = 'invalid'") def test_add_peer(): - m = constructor() - res = m.set_handler("30.30.30.1", {"local_addr": "30.30.30.30", "admin_status": "up"}) - assert res, "Expect True return value" + for constant in load_constant_files(): + m = constructor(constant) + res = m.set_handler("30.30.30.1", {'asn': '65200', 'holdtime': '180', 'keepalive': '60', 'local_addr': '30.30.30.30', 'name': 'TOR', 'nhopself': '0', 'rrclient': '0'}) + assert res, "Expect True return value" def test_add_peer_ipv6(): - m = constructor() - res = m.set_handler("fc00:20::1", {"local_addr": "fc00:20::20", "admin_status": "up"}) - assert res, "Expect True return value" + for constant in load_constant_files(): + m = constructor(constant) + res = m.set_handler("fc00:20::1", {'asn': '65200', 'holdtime': '180', 'keepalive': '60', 'local_addr': 'fc00:20::20', 'name': 'TOR', 'nhopself': '0', 'rrclient': '0'}) + assert res, "Expect True return value" @patch('bgpcfgd.managers_bgp.log_warn') def test_add_peer_no_local_addr(mocked_log_warn): - m = constructor() - res = m.set_handler("30.30.30.1", {"admin_status": "up"}) - assert res, "Expect True return value" - mocked_log_warn.assert_called_with("Peer 30.30.30.1. Missing attribute 'local_addr'") + for constant in load_constant_files(): + m = constructor(constant) + res = m.set_handler("30.30.30.1", {"admin_status": "up"}) + assert res, "Expect True return value" + mocked_log_warn.assert_called_with("Peer 30.30.30.1. Missing attribute 'local_addr'") @patch('bgpcfgd.managers_bgp.log_debug') def test_add_peer_invalid_local_addr(mocked_log_debug): - m = constructor() - res = m.set_handler("30.30.30.1", {"local_addr": "40.40.40.40", "admin_status": "up"}) - assert not res, "Expect False return value" - mocked_log_debug.assert_called_with("Peer '30.30.30.1' with local address '40.40.40.40' wait for the corresponding interface to be set") + for constant in load_constant_files(): + m = constructor(constant) + res = m.set_handler("30.30.30.1", {"local_addr": "40.40.40.40", "admin_status": "up"}) + assert not res, "Expect False return value" + mocked_log_debug.assert_called_with("Peer '30.30.30.1' with local address '40.40.40.40' wait for the corresponding interface to be set") @patch('bgpcfgd.managers_bgp.log_info') def test_del_handler(mocked_log_info): - m = constructor() - m.del_handler("10.10.10.1") - mocked_log_info.assert_called_with("Peer '(default|10.10.10.1)' has been removed") + for constant in load_constant_files(): + m = constructor(constant) + m.del_handler("10.10.10.1") + mocked_log_info.assert_called_with("Peer '(default|10.10.10.1)' has been removed") @patch('bgpcfgd.managers_bgp.log_warn') def test_del_handler_nonexist_peer(mocked_log_warn): - m = constructor() - m.del_handler("40.40.40.1") - mocked_log_warn.assert_called_with("Peer '(default|40.40.40.1)' has not been found") + for constant in load_constant_files(): + m = constructor(constant) + m.del_handler("40.40.40.1") + mocked_log_warn.assert_called_with("Peer '(default|40.40.40.1)' has not been found") diff --git a/src/sonic-bgpcfgd/tests/util.py b/src/sonic-bgpcfgd/tests/util.py index a328a272c8..d9b3273865 100644 --- a/src/sonic-bgpcfgd/tests/util.py +++ b/src/sonic-bgpcfgd/tests/util.py @@ -13,8 +13,8 @@ def load_constants_dir_mappings(): result[name] = value["template_dir"] return result -def load_constants(): - with open(CONSTANTS_PATH) as f: +def load_constants(constants = CONSTANTS_PATH): + with open(constants) as f: data = yaml.load(f) # FIXME" , Loader=yaml.FullLoader) assert "constants" in data, "'constants' key not found in constants.yml" return data