From af541ab85f1bea2293b2e261e5fe9643ca776d25 Mon Sep 17 00:00:00 2001 From: bingwang Date: Tue, 23 Jun 2020 07:07:11 +0000 Subject: [PATCH] Add check and waiting for exabgp http server --- tests/bgp/test_bgp_speaker.py | 19 ++++++++++++++----- tests/common/plugins/fib.py | 31 +++++++++++++++++++++++++------ tests/common/utilities.py | 18 ++++++++++++++++++ 3 files changed, 57 insertions(+), 11 deletions(-) diff --git a/tests/bgp/test_bgp_speaker.py b/tests/bgp/test_bgp_speaker.py index 10da8642d4c..e76ba430e59 100644 --- a/tests/bgp/test_bgp_speaker.py +++ b/tests/bgp/test_bgp_speaker.py @@ -7,7 +7,7 @@ from common.fixtures.ptfhost_utils import copy_ptftests_directory # lgtm[py/unused-import] from common.fixtures.ptfhost_utils import change_mac_addresses # lgtm[py/unused-import] from ptf_runner import ptf_runner - +from common.utilities import wait_tcp_connection def generate_ips(num, prefix, exclude_ips): """ @@ -39,7 +39,7 @@ def announce_route(ptfip, neighbor, route, nexthop, port): @pytest.fixture(scope="module") -def common_setup_teardown(duthost, ptfhost): +def common_setup_teardown(duthost, ptfhost, localhost): logging.info("########### Setup for bgp speaker testing ###########") @@ -102,9 +102,16 @@ def common_setup_teardown(duthost, ptfhost): peer_asn=mg_facts['minigraph_bgp_asn'], port=str(port_num[i])) + # check exabgp http_api port is ready + http_ready = True + for i in range(0, 3): + http_ready = wait_tcp_connection(localhost, ptfip, port_num[i]) + if not http_ready: + break + logging.info("########### Done setup for bgp speaker testing ###########") - yield ptfip, mg_facts, interface_facts, vlan_ips, speaker_ips, port_num + yield ptfip, mg_facts, interface_facts, vlan_ips, speaker_ips, port_num, http_ready logging.info("########### Teardown for bgp speaker testing ###########") @@ -122,7 +129,8 @@ def common_setup_teardown(duthost, ptfhost): def test_bgp_speaker_bgp_sessions(common_setup_teardown, duthost, ptfhost, collect_techsupport): """Setup bgp speaker on T0 topology and verify bgp sessions are established """ - ptfip, mg_facts, interface_facts, vlan_ips, speaker_ips, port_num = common_setup_teardown + ptfip, mg_facts, interface_facts, vlan_ips, speaker_ips, port_num, http_ready = common_setup_teardown + assert http_ready logging.info("Wait some time to verify that bgp sessions are established") time.sleep(20) @@ -137,7 +145,8 @@ def test_bgp_speaker_announce_routes(common_setup_teardown, testbed, duthost, pt """Setup bgp speaker on T0 topology and verify routes advertised by bgp speaker is received by T0 TOR """ - ptfip, mg_facts, interface_facts, vlan_ips, speaker_ips, port_num = common_setup_teardown + ptfip, mg_facts, interface_facts, vlan_ips, speaker_ips, port_num, http_ready = common_setup_teardown + assert http_ready logging.info("announce route") peer_range = mg_facts['minigraph_bgp_peers_with_range'][0]['ip_range'][0] diff --git a/tests/common/plugins/fib.py b/tests/common/plugins/fib.py index 8d839ebe410..1798cc4ebce 100644 --- a/tests/common/plugins/fib.py +++ b/tests/common/plugins/fib.py @@ -5,6 +5,8 @@ import pytest import logging import ipaddr as ipaddress +from common.utilities import wait_tcp_connection + logger = logging.getLogger(__name__) @@ -19,7 +21,6 @@ def announce_routes(ptfip, port, routes): url = "http://%s:%d" % (ptfip, port) data = { "commands": ";".join(messages) } r = requests.post(url, data=data) - print r assert r.status_code == 200 @@ -93,7 +94,7 @@ def generate_routes(family, podset_number, tor_number, tor_subnet_number, return routes -def fib_t0(ptfhost, testbed): +def fib_t0(ptfhost, testbed, localhost): logger.info("use fib_t0 to setup routes for topo {}".format(testbed['topo']['name'])) podset_number = 200 @@ -137,6 +138,15 @@ def fib_t0(ptfhost, testbed): local_asn = asn, \ peer_asn = asn, \ port = port6) + # check if bgp http_api is ready + for k, v in testbed['topo']['properties']['configuration'].items(): + vm_offset = testbed['topo']['properties']['topology']['VMs'][k]['vm_offset'] + + port = 5000 + vm_offset + assert wait_tcp_connection(localhost, ptfip, port) + + port6 = 6000 + vm_offset + assert wait_tcp_connection(localhost, ptfip, port6) for k, v in testbed['topo']['properties']['configuration'].items(): vm_offset = testbed['topo']['properties']['topology']['VMs'][k]['vm_offset'] @@ -154,7 +164,7 @@ def fib_t0(ptfhost, testbed): announce_routes(ptfip, port6, routes_v6) -def fib_t1_lag(ptfhost, testbed): +def fib_t1_lag(ptfhost, testbed, localhost): logger.info("use fib_t1_lag to setup routes for topo {}".format(testbed['topo']['name'])) podset_number = 200 @@ -198,6 +208,15 @@ def fib_t1_lag(ptfhost, testbed): local_asn = asn, \ peer_asn = asn, \ port = port6) + # Check if bgp http_api port is ready + for k, v in testbed['topo']['properties']['configuration'].items(): + vm_offset = testbed['topo']['properties']['topology']['VMs'][k]['vm_offset'] + + port = 5000 + vm_offset + assert wait_tcp_connection(localhost, ptfip, port) + + port6 = 6000 + vm_offset + assert wait_tcp_connection(localhost, ptfip, port6) for k, v in testbed['topo']['properties']['configuration'].items(): @@ -229,11 +248,11 @@ def fib_t1_lag(ptfhost, testbed): @pytest.fixture(scope='module') -def fib(ptfhost, testbed): +def fib(ptfhost, testbed, localhost): logger.info("setup fib to topo {}".format(testbed['topo']['name'])) if testbed['topo']['type'] == "t0": - fib_t0(ptfhost, testbed) + fib_t0(ptfhost, testbed, localhost) elif testbed['topo']['type'] == "t1": - fib_t1_lag(ptfhost, testbed) + fib_t1_lag(ptfhost, testbed, localhost) else: logger.error("unknonw topology {}".format(testbed['topo']['name'])) diff --git a/tests/common/utilities.py b/tests/common/utilities.py index ecf1a86ee59..7f6d6edca7c 100644 --- a/tests/common/utilities.py +++ b/tests/common/utilities.py @@ -50,3 +50,21 @@ def wait_until(timeout, interval, condition, *args, **kwargs): if elapsed_time >= timeout: logging.debug("%s is still False after %d seconds, exit with False" % (condition.__name__, timeout)) return False + +def wait_tcp_connection(client, server_hostname, listening_port, timeout_s = 30): + """ + @summary: Wait until tcp connection is ready or timeout + @param client: The tcp client host instance + @param server_hostname: The tcp server hostname + @param listening_port: Port server is listening on + @param timeout: Maximum time to wait (30s in default) + """ + res = client.wait_for(host=server_hostname, + port=listening_port, + state='started', + timeout=timeout_s, + module_ignore_errors=True) + if 'exception' in res: + logging.warn("Failed to establish TCP connection to %s:%d, timeout=%d" % (str(server_hostname), listening_port, timeout_s)) + return False + return True