|
7 | 7 | from tests.common.utilities import wait_until |
8 | 8 | from tests.common.config_reload import config_reload |
9 | 9 | from tests.common.helpers.assertions import pytest_assert |
10 | | -from tests.common.helpers.ntp_helper import NtpDaemon, ntp_daemon_in_use # noqa: F401 |
| 10 | +from tests.common.helpers.ntp_helper import NtpDaemon, ntp_daemon_in_use, setup_ntp_context # noqa: F401 |
11 | 11 | from tests.common.helpers.snmp_helpers import get_snmp_facts |
12 | 12 | from tests.common.devices.ptf import PTFHost |
13 | 13 |
|
@@ -156,20 +156,6 @@ def execute_dut_command(duthost, command, mvrf=True, ignore_errors=False): |
156 | 156 | return result |
157 | 157 |
|
158 | 158 |
|
159 | | -def setup_ntp(ptfhost, duthost, ntp_servers): |
160 | | - """setup ntp client and server""" |
161 | | - ptfhost.lineinfile(path="/etc/ntp.conf", line="server 127.127.1.0 prefer") |
162 | | - # restart ntp server |
163 | | - ntp_en_res = ptfhost.service(name="ntp", state="restarted") |
164 | | - pytest_assert(wait_until(120, 5, 0, check_ntp_status, ptfhost, NtpDaemon.NTP), |
165 | | - "NTP server was not started in PTF container {}; NTP service start result {}" |
166 | | - .format(ptfhost.hostname, ntp_en_res)) |
167 | | - # setup ntp on dut to sync with ntp server |
168 | | - for ntp_server in ntp_servers: |
169 | | - duthost.command("config ntp del %s" % ntp_server) |
170 | | - duthost.command("config ntp add %s" % ptfhost.mgmt_ip) |
171 | | - |
172 | | - |
173 | 159 | class TestMvrfInbound(): |
174 | 160 | def test_ping(self, duthost): |
175 | 161 | duthost.ping() |
@@ -227,22 +213,10 @@ def test_ntp(self, duthosts, rand_one_dut_hostname, ptfhost, check_ntp_sync, |
227 | 213 | duthost = duthosts[rand_one_dut_hostname] |
228 | 214 | # Check if ntp was not in sync with ntp server before enabling mvrf, if yes then setup ntp server on ptf |
229 | 215 | if check_ntp_sync: |
230 | | - setup_ntp(ptfhost, duthost, ntp_servers) |
231 | | - |
232 | | - # There is no entry ntp in `/etc/passwd` on kvm testbed. |
233 | | - cmd = "getent passwd ntp" |
234 | | - ntp_uid_output = duthost.command(cmd, module_ignore_errors=True) |
235 | | - if duthost.facts["asic_type"] == "vs" and ntp_uid_output['rc'] == 2: |
236 | | - return |
237 | | - assert ntp_uid_output['rc'] == 0, "Run command '{}' failed".format(cmd) |
238 | | - ntp_uid = ":".join(ntp_uid_output['stdout'].split(':')[2:4]) |
239 | | - |
240 | | - force_ntp = "timeout 20 ntpd -gq -u {}".format(ntp_uid) |
241 | | - duthost.service(name="ntp", state="stopped") |
242 | | - logger.info("Ntp restart in mgmt vrf") |
243 | | - execute_dut_command(duthost, force_ntp) |
244 | | - duthost.service(name="ntp", state="restarted") |
245 | | - pytest_assert(wait_until(400, 10, 0, check_ntp_status, duthost, ntp_daemon_in_use), "Ntp not started") |
| 216 | + with setup_ntp_context(ptfhost, duthost, False): |
| 217 | + pytest_assert(wait_until(400, 10, 0, check_ntp_status, duthost, ntp_daemon_in_use), "Ntp not started") |
| 218 | + else: |
| 219 | + pytest_assert(wait_until(400, 10, 0, check_ntp_status, duthost, ntp_daemon_in_use), "Ntp not started") |
246 | 220 |
|
247 | 221 | def test_service_acl(self, duthosts, rand_one_dut_hostname, localhost): |
248 | 222 | duthost = duthosts[rand_one_dut_hostname] |
|
0 commit comments