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
Empty file added tests/clock/__init__.py
Empty file.
123 changes: 123 additions & 0 deletions tests/clock/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
import re
import time
import pytest
import logging

from tests.clock.test_clock import ClockConsts, ClockUtils


def pytest_addoption(parser):
parser.addoption("--ntp_server", action="store", default=None, required=True, help="IP of NTP server to use")


@pytest.fixture(scope='session', autouse=True)
def ntp_server(request):
"""
@summary: Return NTP server's ip if given, otherwise skip the test
"""
ntp_server_ip = request.config.getoption("ntp_server")
logging.info(f'NTP server ip from execution parameter: {ntp_server_ip}')
if ntp_server_ip is None:
pytest.fail("IP of NTP server was not given")
return ntp_server_ip


@pytest.fixture(scope="function")
def init_timezone(duthosts):
"""
@summary: fixture to init timezone before and after each test
"""

logging.info(f'Set timezone to {ClockConsts.TEST_TIMEZONE} before test')
ClockUtils.run_cmd(duthosts, ClockConsts.CMD_CONFIG_CLOCK_TIMEZONE, ClockConsts.TEST_TIMEZONE)

yield

logging.info(f'Set timezone to {ClockConsts.TEST_TIMEZONE} after test')
ClockUtils.run_cmd(duthosts, ClockConsts.CMD_CONFIG_CLOCK_TIMEZONE, ClockConsts.TEST_TIMEZONE)


@pytest.fixture(scope="function")
def restore_time(duthosts, ntp_server):
"""
@summary: fixture to restore time after test (using ntp)
"""
logging.info('Check if there is ntp configured before test')
show_ntp_output = ClockUtils.run_cmd(duthosts, ClockConsts.CMD_SHOW_NTP)
if 'unsynchronised' in show_ntp_output:
logging.info('There is no NTP server configured before test')
orig_ntp_server = None
else:
synchronized_str = 'synchronised to NTP server'
logging.info('There is NTP server configured before test')
assert synchronized_str in show_ntp_output, f'There is NTP configured but output do not contain ' \
f'"{synchronized_str}"'
orig_ntp_server = re.findall(r'\d+.\d+.\d+.\d+',
re.findall(r'synchronised to NTP server \(\d+.\d+.\d+.\d+\)',
show_ntp_output)[0])[0]
logging.info(f'Original NTP: {orig_ntp_server}')

if orig_ntp_server:
logging.info('Disable original NTP before test')
output = ClockUtils.run_cmd(duthosts, ClockConsts.CMD_CONFIG_NTP_DEL, orig_ntp_server)
assert ClockConsts.OUTPUT_CMD_NTP_DEL_SUCCESS.format(orig_ntp_server) in output, \
f'Error: The given string does not contain the expected substring.\n' \
f'Expected substring: "{ClockConsts.OUTPUT_CMD_NTP_DEL_SUCCESS.format(orig_ntp_server)}"\n' \
f'Given (whole) string: "{output}"'

yield

logging.info(f'Reset time after test. Sync with NTP server: {ntp_server}')

logging.info(f'Sync with NTP server: {ntp_server}')
output = ClockUtils.run_cmd(duthosts, ClockConsts.CMD_CONFIG_NTP_ADD, ntp_server)
assert ClockConsts.OUTPUT_CMD_NTP_ADD_SUCCESS.format(ntp_server) in output, \
f'Error: The given string does not contain the expected substring.\n' \
f'Expected substring: "{ClockConsts.OUTPUT_CMD_NTP_ADD_SUCCESS.format(ntp_server)}"\n' \
f'Given (whole) string: "{output}"'

logging.info('Check polling time')
show_ntp_output = ClockUtils.run_cmd(duthosts, ClockConsts.CMD_SHOW_NTP)
match = re.search(ClockConsts.REGEX_NTP_POLLING_TIME, show_ntp_output)
if match:
polling_time_seconds = int(match.group(1))
else:
logging.info('Could not match the regex.\nPattern: "{}"\nShow ntp output string: "{}"'
.format(ClockConsts.REGEX_NTP_POLLING_TIME, show_ntp_output))
polling_time_seconds = ClockConsts.RANDOM_NUM
logging.info(f'Polling time (in seconds): {polling_time_seconds + 1}')

logging.info('Wait for the sync')
time.sleep(polling_time_seconds)

logging.info(f'Delete NTP server: {ntp_server}')
output = ClockUtils.run_cmd(duthosts, ClockConsts.CMD_CONFIG_NTP_DEL, ntp_server)
assert ClockConsts.OUTPUT_CMD_NTP_DEL_SUCCESS.format(ntp_server) in output, \
f'Error: The given string does not contain the expected substring.\n' \
f'Expected substring: "{ClockConsts.OUTPUT_CMD_NTP_DEL_SUCCESS.format(ntp_server)}"\n' \
f'Given (whole) string: "{output}"'

logging.info('Wait for the sync')
time.sleep(polling_time_seconds)

if orig_ntp_server:
logging.info('Restore original NTP server after test')
output = ClockUtils.run_cmd(duthosts, ClockConsts.CMD_CONFIG_NTP_ADD, orig_ntp_server)
assert ClockConsts.OUTPUT_CMD_NTP_ADD_SUCCESS.format(orig_ntp_server) in output, \
f'Error: The given string does not contain the expected substring.\n' \
f'Expected substring: "{ClockConsts.OUTPUT_CMD_NTP_ADD_SUCCESS.format(orig_ntp_server)}"\n' \
f'Given (whole) string: "{output}"'

logging.info('Check polling time')
show_ntp_output = ClockUtils.run_cmd(duthosts, ClockConsts.CMD_SHOW_NTP)
match = re.search(ClockConsts.REGEX_NTP_POLLING_TIME, show_ntp_output)
if match:
polling_time_seconds = int(match.group(1))
else:
logging.info('Could not match the regex.\nPattern: "{}"\nShow ntp output string: "{}"'
.format(ClockConsts.REGEX_NTP_POLLING_TIME, show_ntp_output))
polling_time_seconds = ClockConsts.RANDOM_NUM
logging.info(f'Polling time (in seconds): {polling_time_seconds + 1}')

logging.info('Wait for the sync')
time.sleep(polling_time_seconds)
Loading