Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions tests/common/helpers/ntp_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
29 changes: 19 additions & 10 deletions tests/gnmi/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)
Expand Down Expand Up @@ -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'])
Expand Down Expand Up @@ -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:
Expand Down
22 changes: 11 additions & 11 deletions tests/mvrf/test_mgmtvrf.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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

Expand All @@ -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
Expand Down Expand Up @@ -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:
Expand All @@ -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]
Expand Down
2 changes: 1 addition & 1 deletion tests/ntp/test_ntp.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Loading