-
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
Merged
Merged
add ntp test #19
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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,94 @@ | ||
| # Copyright (c) 2026 StackHPC Ltd. | ||
|
|
||
| # Licensed under the Apache License, Version 2.0 (the "License"); you may | ||
| # not use this file except in compliance with the License. You may obtain | ||
| # a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
| # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
| # License for the specific language governing permissions and limitations | ||
| # under the License. | ||
|
|
||
|
|
||
| import os | ||
| import re | ||
| import pytest | ||
|
|
||
| MAX_OFFSET_SECONDS = 0.5 # 500 ms limit | ||
|
|
||
|
|
||
| def _get_parsed_ntp_sources(): | ||
| # fetch and parse NTP_SOURCES | ||
| raw_ntp_sources = os.environ.get("NTP_SOURCES") | ||
| if not raw_ntp_sources or not raw_ntp_sources.strip(): | ||
| return [] | ||
| return [s.strip() for s in raw_ntp_sources.split(",") if s.strip()] | ||
|
|
||
|
|
||
| def test_ntp_sources_env_var(): | ||
| """Verify NTP_SOURCES is set.""" | ||
| raw_ntp_sources = os.environ.get("NTP_SOURCES") | ||
| assert ( | ||
| raw_ntp_sources is not None and raw_ntp_sources.strip() != "" | ||
| ), "NTP_SOURCES environment variable is not set or empty." | ||
|
|
||
| ntp_sources = _get_parsed_ntp_sources() | ||
| assert ntp_sources, "NTP_SOURCES contains no valid source entries." | ||
|
|
||
|
|
||
| def test_chrony_service_and_sync_status(host): | ||
| """Check that chrony is running and syncing properly""" | ||
| # Check timedatectl | ||
| timedate_stdout = host.check_output("timedatectl status") | ||
| assert ( | ||
| "NTP service: active" in timedate_stdout | ||
| or "System clock synchronized: yes" in timedate_stdout | ||
| ), f"System clock is not synchronized according to timedatectl:\n{timedate_stdout}" | ||
|
|
||
| # Check chrony has an active reference source marked with '*' | ||
| sources_stdout = host.check_output("chronyc -n sources") | ||
| active_source_match = re.search(r"^\^?\*\s+([^\s]+)", sources_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_stdout}" | ||
| ) | ||
|
|
||
|
|
||
| def test_active_source_matches_expected(host): | ||
| """Check that one of the NTP_SOURCES values is actively used by chrony (*=current source)""" | ||
| ntp_sources = _get_parsed_ntp_sources() | ||
| assert ( | ||
| ntp_sources | ||
| ), "Cannot verify active source because NTP_SOURCES is missing or invalid." | ||
|
|
||
| sources_stdout = host.check_output("chronyc -n sources") | ||
| active_source_match = re.search(r"^\^?\*\s+([^\s]+)", sources_stdout, re.MULTILINE) | ||
| assert active_source_match is not None, "No active chrony source found to validate." | ||
|
|
||
| active_source = active_source_match.group(1) | ||
| 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_stdout}" | ||
| ) | ||
|
|
||
|
|
||
| def test_ntp_time_offset(host): | ||
| """Check time offset""" | ||
| chrony_stdout = host.check_output("chronyc tracking") | ||
|
|
||
| match = re.search(r"System time\s+:\s+([0-9.]+)\s+seconds", chrony_stdout) | ||
| assert ( | ||
| match is not None | ||
| ), f"Could not parse system time offset from chronyc output:\n{chrony_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_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.
Uh oh!
There was an error while loading. Please reload this page.