-
Notifications
You must be signed in to change notification settings - Fork 0
add ntp test #19
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
Draft
Yoda-Yothada
wants to merge
1
commit into
main
Choose a base branch
from
yoda_ntp_test
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+78
−0
Draft
add ntp test #19
Changes from all commits
Commits
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
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,78 @@ | ||
| import os | ||
| import re | ||
| import subprocess | ||
|
|
||
| MAX_OFFSET_SECONDS = 0.5 # 500 ms limit | ||
|
|
||
|
|
||
| def test_ntp_configuration_and_sync(): | ||
| raw_ntp_sources = os.environ.get("NTP_SOURCES") | ||
|
|
||
| # 1. Verify NTP_SOURCES environment variable and parse into a list | ||
| assert ( | ||
| raw_ntp_sources is not None and raw_ntp_sources.strip() != "" | ||
| ), "NTP_SOURCES environment variable is not set or empty." | ||
|
|
||
| # Split comma-separated string into a clean list of individual source hosts/IPs | ||
| ntp_sources = [s.strip() for s in raw_ntp_sources.split(",") if s.strip()] | ||
| assert ntp_sources, "NTP_SOURCES contains no valid source entries." | ||
|
|
||
| # 2. Basic service check via timedatectl | ||
| timedate_res = subprocess.run( | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The existing tests (like Docker and SELinux) use the host fixture from |
||
| ["timedatectl", "status"], | ||
| capture_output=True, | ||
| text=True, | ||
| check=True, | ||
| ) | ||
| assert ( | ||
| "NTP service: active" in timedate_res.stdout | ||
| or "System clock synchronized: yes" in timedate_res.stdout | ||
| ), f"System clock is not synchronized according to timedatectl:\n{timedate_res.stdout}" | ||
|
|
||
| # 3. Check that one of the NTP_SOURCES values is actively used by chrony (* = current synchronized source) | ||
| sources_res = subprocess.run( | ||
| ["chronyc", "-n", "sources"], | ||
| capture_output=True, | ||
| text=True, | ||
| check=True, | ||
| ) | ||
|
|
||
| # In 'chronyc sources', the line starting with '*' or '^*' indicates the active reference source. | ||
| active_source_match = re.search( | ||
| r"^\^?\*\s+([^\s]+)", sources_res.stdout, re.MULTILINE | ||
| ) | ||
| assert active_source_match is not None, ( | ||
| f"Chrony has no active reference source (no source marked with '*').\n" | ||
| f"Output:\n{sources_res.stdout}" | ||
| ) | ||
|
|
||
| active_source = active_source_match.group(1) | ||
|
|
||
| # Validate that active_source matches ANY server in your ntp_sources list | ||
| is_valid_source = any( | ||
| src in active_source or active_source in src for src in ntp_sources | ||
| ) | ||
| assert is_valid_source, ( | ||
| f"Active chrony source '{active_source}' does not match any expected NTP_SOURCES in {ntp_sources}.\n" | ||
| f"Chronyc Sources Output:\n{sources_res.stdout}" | ||
| ) | ||
|
|
||
| # 4. Check exact time offset using chronyc tracking | ||
| chrony_res = subprocess.run( | ||
| ["chronyc", "tracking"], | ||
| capture_output=True, | ||
| text=True, | ||
| check=True, | ||
| ) | ||
|
|
||
| # Output line example: "System time : 0.000012345 seconds slow of NTP time" | ||
| match = re.search(r"System time\s+:\s+([0-9.]+)\s+seconds", chrony_res.stdout) | ||
| assert ( | ||
| match is not None | ||
| ), f"Could not parse system time offset from chronyc output:\n{chrony_res.stdout}" | ||
|
|
||
| offset = float(match.group(1)) | ||
| assert offset <= MAX_OFFSET_SECONDS, ( | ||
| f"NTP time offset {offset:.4f}s exceeds maximum threshold of {MAX_OFFSET_SECONDS}s (500ms).\n" | ||
| f"Chronyc Tracking:\n{chrony_res.stdout}" | ||
| ) | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
At the moment, everything here is in one test function. Could you refactor it a bit so each test is a different function? See the docker tests for an example of what that looks like.
You could divide it into four functions: