-
Notifications
You must be signed in to change notification settings - Fork 1k
Clock tests #8056
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Clock tests #8056
Changes from all commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
a8bd27f
clock tests
alonn6 16268f4
pre commit fixes
alonn6 e2e36ca
pre commit fixes
alonn6 71dd779
change to new string format
alonn6 9411083
comment typo fix
alonn6 e55f7d9
wrap allure step context in a context manager function
alonn6 8d0ffda
compare dates and times as date-time objects to handle midnight corne…
alonn6 4d46f3d
pre-commit fixes
alonn6 c8c5ac0
use library for time and date parsing
alonn6 96b457e
replace verify methods with straight forward assert
alonn6 ad7f696
fix conftest
alonn6 b88eed7
log fix in conftest
alonn6 d698c88
fix
alonn6 e09ed3a
replace strings to the old format
alonn6 4b7d286
pre commit fixes
alonn6 1fac0d7
revert string format changes
alonn6 c938229
remove log message
alonn6 93277d8
update
alonn6 1263499
replace strings format
alonn6 e039ae8
use existing allure step util
alonn6 f1d13ee
remove unused import
alonn6 a4ceb37
remove and restore ntp that configured before test
alonn6 6069def
remove unnecessary constants
alonn6 0c4ceaa
make ntp server parameter be required
alonn6 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.