diff --git a/tests/common/helpers/ntp_helper.py b/tests/common/helpers/ntp_helper.py index 16bf2f4b959..027935fbd36 100644 --- a/tests/common/helpers/ntp_helper.py +++ b/tests/common/helpers/ntp_helper.py @@ -60,20 +60,24 @@ def setup_ntp_func(ptfhost, duthosts, rand_one_dut_hostname, ptf_use_ipv6): yield result -@pytest.fixture(scope="module") -def ntp_daemon_in_use(duthost): - ntpsec_conf_stat = duthost.stat(path="/etc/ntpsec/ntp.conf") +def get_ntp_daemon_in_use(host): + ntpsec_conf_stat = host.stat(path="/etc/ntpsec/ntp.conf") if ntpsec_conf_stat["stat"]["exists"]: return NtpDaemon.NTPSEC - chrony_conf_stat = duthost.stat(path="/etc/chrony/chrony.conf") + chrony_conf_stat = host.stat(path="/etc/chrony/chrony.conf") if chrony_conf_stat["stat"]["exists"]: return NtpDaemon.CHRONY - ntp_conf_stat = duthost.stat(path="/etc/ntp.conf") + ntp_conf_stat = host.stat(path="/etc/ntp.conf") if ntp_conf_stat["stat"]["exists"]: return NtpDaemon.NTP pytest.fail("Unable to determine NTP daemon in use") +@pytest.fixture(scope="module") +def ntp_daemon_in_use(duthost): + return get_ntp_daemon_in_use(duthost) + + def check_ntp_status(host, ntp_daemon_in_use): if ntp_daemon_in_use == NtpDaemon.CHRONY: res = host.command("timedatectl show -p NTPSynchronized --value") diff --git a/tests/gnmi/helper.py b/tests/gnmi/helper.py index 5ad730cdc78..b2d8d42f7a2 100644 --- a/tests/gnmi/helper.py +++ b/tests/gnmi/helper.py @@ -3,6 +3,7 @@ import pytest from tests.common.utilities import wait_until from tests.common.helpers.gnmi_utils import GNMIEnvironment +from tests.common.helpers.ntp_helper import NtpDaemon, get_ntp_daemon_in_use # noqa: F401 logger = logging.getLogger(__name__) @@ -112,8 +113,9 @@ def apply_cert_config(duthost): time.sleep(GNMI_SERVER_START_WAIT_TIME) dut_command = "sudo netstat -nap | grep %d" % env.gnmi_port output = duthost.shell(dut_command, module_ignore_errors=True) - is_time_synced = wait_until(60, 3, 0, check_system_time_sync, duthost) - assert is_time_synced, "Failed to synchronize DUT system time with NTP Server" + if duthost.facts['platform'] != 'x86_64-kvm_x86_64-r0': + is_time_synced = wait_until(60, 3, 0, check_system_time_sync, duthost) + assert is_time_synced, "Failed to synchronize DUT system time with NTP Server" if env.gnmi_process not in output['stdout']: # Dump tcp port status and gnmi log logger.info("TCP port status: " + output['stdout']) @@ -149,21 +151,28 @@ def check_system_time_sync(duthost): If not synchronized, it attempts to restart the NTP service. """ - ntp_status_cmd = "ntpstat" - restart_ntp_cmd = "sudo systemctl restart ntp" + ntp_daemon = get_ntp_daemon_in_use(duthost) - ntp_status = duthost.shell(ntp_status_cmd, module_ignore_errors=True) - if "synchronised" in ntp_status["stdout"]: + if ntp_daemon == NtpDaemon.CHRONY: + ntp_status_cmd = "chronyc -c tracking" + restart_ntp_cmd = "sudo systemctl restart chrony" + else: + ntp_status_cmd = "ntpstat" + restart_ntp_cmd = "sudo systemctl restart ntp" + + ntp_status = duthost.command(ntp_status_cmd, module_ignore_errors=True) + if (ntp_daemon == NtpDaemon.CHRONY and "Not synchronised" not in ntp_status["stdout"]) or \ + (ntp_daemon != NtpDaemon.CHRONY and "unsynchronised" not in ntp_status["stdout"]): logger.info("DUT %s is synchronized with NTP server.", duthost) return True - else: logger.info("DUT %s is NOT synchronized. Restarting NTP service...", duthost) - duthost.shell(restart_ntp_cmd) + duthost.command(restart_ntp_cmd) time.sleep(5) # Rechecking status after restarting NTP - ntp_status = duthost.shell(ntp_status_cmd, module_ignore_errors=True) - if "synchronised" in ntp_status["stdout"]: + ntp_status = duthost.command(ntp_status_cmd, module_ignore_errors=True) + if (ntp_daemon == NtpDaemon.CHRONY and "Not synchronised" not in ntp_status["stdout"]) or \ + (ntp_daemon != NtpDaemon.CHRONY and "synchronized" in ntp_status["stdout"]): logger.info("DUT %s is now synchronized with NTP server.", duthost) return True else: diff --git a/tests/mvrf/test_mgmtvrf.py b/tests/mvrf/test_mgmtvrf.py index 772e1755b0a..08edb1cc04e 100644 --- a/tests/mvrf/test_mgmtvrf.py +++ b/tests/mvrf/test_mgmtvrf.py @@ -7,8 +7,8 @@ from tests.common.utilities import wait_until from tests.common.config_reload import config_reload from tests.common.helpers.assertions import pytest_assert +from tests.common.helpers.ntp_helper import NtpDaemon, ntp_daemon_in_use # noqa: F401 from tests.common.helpers.snmp_helpers import get_snmp_facts -from pkg_resources import parse_version from tests.common.devices.ptf import PTFHost pytestmark = [ @@ -117,8 +117,11 @@ def change_critical_services(duthosts, rand_one_dut_hostname): duthost.reset_critical_services_tracking_list(backup) -def check_ntp_status(host): - ntpstat_cmd = 'ntpstat' +def check_ntp_status(host, ntp_daemon_in_use): # noqa: F811 + if ntp_daemon_in_use == NtpDaemon.CHRONY: + ntpstat_cmd = "chronyc -c tracking" + else: + ntpstat_cmd = "ntpstat" if isinstance(host, PTFHost): res = host.command(ntpstat_cmd, module_ignore_errors=True) else: @@ -148,11 +151,7 @@ def execute_dut_command(duthost, command, mvrf=True, ignore_errors=False): result = {} prefix = "" if mvrf: - dut_kernel = duthost.shell("cat /proc/version | awk '{ print $3 }' | cut -d '-' -f 1")["stdout"] - if parse_version(dut_kernel) > parse_version("4.9.0"): - prefix = "sudo ip vrf exec mgmt " - else: - prefix = "sudo cgexec -g l3mdev:mgmt " + prefix = "sudo ip vrf exec mgmt " result = duthost.command(prefix + command, module_ignore_errors=ignore_errors) return result @@ -162,7 +161,7 @@ def setup_ntp(ptfhost, duthost, ntp_servers): ptfhost.lineinfile(path="/etc/ntp.conf", line="server 127.127.1.0 prefer") # restart ntp server ntp_en_res = ptfhost.service(name="ntp", state="restarted") - pytest_assert(wait_until(120, 5, 0, check_ntp_status, ptfhost), + pytest_assert(wait_until(120, 5, 0, check_ntp_status, ptfhost, NtpDaemon.NTP), "NTP server was not started in PTF container {}; NTP service start result {}" .format(ptfhost.hostname, ntp_en_res)) # setup ntp on dut to sync with ntp server @@ -223,7 +222,8 @@ def test_curl(self, duthosts, rand_one_dut_hostname, setup_http_server): class TestServices(): @pytest.mark.usefixtures("ntp_teardown") - def test_ntp(self, duthosts, rand_one_dut_hostname, ptfhost, check_ntp_sync, ntp_servers): + def test_ntp(self, duthosts, rand_one_dut_hostname, ptfhost, check_ntp_sync, + ntp_servers, ntp_daemon_in_use): # noqa: F811 duthost = duthosts[rand_one_dut_hostname] # Check if ntp was not in sync with ntp server before enabling mvrf, if yes then setup ntp server on ptf if check_ntp_sync: @@ -242,7 +242,7 @@ def test_ntp(self, duthosts, rand_one_dut_hostname, ptfhost, check_ntp_sync, ntp logger.info("Ntp restart in mgmt vrf") execute_dut_command(duthost, force_ntp) duthost.service(name="ntp", state="restarted") - pytest_assert(wait_until(400, 10, 0, check_ntp_status, duthost), "Ntp not started") + pytest_assert(wait_until(400, 10, 0, check_ntp_status, duthost, ntp_daemon_in_use), "Ntp not started") def test_service_acl(self, duthosts, rand_one_dut_hostname, localhost): duthost = duthosts[rand_one_dut_hostname] diff --git a/tests/ntp/test_ntp.py b/tests/ntp/test_ntp.py index 4f439b19413..d142d508be2 100644 --- a/tests/ntp/test_ntp.py +++ b/tests/ntp/test_ntp.py @@ -1,6 +1,6 @@ from tests.common.utilities import wait_until from tests.common.helpers.assertions import pytest_assert -from tests.common.helpers.ntp_helper import check_ntp_status, run_ntp, setup_ntp_context, NtpDaemon, ntp_daemon_in_use # noqa F401 +from tests.common.helpers.ntp_helper import check_ntp_status, run_ntp, setup_ntp_context, NtpDaemon, ntp_daemon_in_use # noqa: F401, E501 import logging import time import pytest