-
Notifications
You must be signed in to change notification settings - Fork 76
Automate wake-on-LAN tests (New) #1686
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
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
2b99119
Automated the wake-on-LAN tests (New)
eugene-yujinwu 6aef680
Fix the unit test issue
eugene-yujinwu 13a3bec
Remove wol_server.py and place it in a separate repository outside of…
eugene-yujinwu 54e561b
remove f-string syntax not be supported in Python 3.5
eugene-yujinwu 9cc4e75
Use socket and urllib to replace netifaces and urllib3 since some iss…
eugene-yujinwu 274c2b8
Add a has_wake_on_lan_server manifest per Stanley's comments
eugene-yujinwu 712fd9f
fix some of the new reviews from Stanley
eugene-yujinwu fd95403
remove unused parameters in wol_check.py
eugene-yujinwu 8146925
Use the /proc/stat to get the system boot time
eugene-yujinwu f259115
fix some errors in the Readme
eugene-yujinwu 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,165 @@ | ||
| #!/usr/bin/env python3 | ||
|
|
||
| # Copyright 2025 Canonical Ltd. | ||
| # Written by: | ||
| # Eugene Wu <eugene.wu@canonical.com> | ||
| # | ||
| # This program is free software: you can redistribute it and/or modify | ||
| # it under the terms of the GNU General Public License version 3, | ||
| # as published by the Free Software Foundation. | ||
| # | ||
| # This program is distributed in the hope that it will be useful, | ||
| # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| # GNU General Public License for more details. | ||
| # | ||
| # You should have received a copy of the GNU General Public License | ||
| # along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
|
|
||
|
|
||
| import subprocess | ||
| import datetime | ||
| import re | ||
| import argparse | ||
| import logging | ||
| import sys | ||
|
|
||
|
|
||
| def get_timestamp(file): | ||
| with open(file, "r") as f: | ||
| saved_timestamp = float(f.read()) | ||
| readable_start_time = datetime.datetime.fromtimestamp(saved_timestamp) | ||
| logging.debug("Test started at: {}".format(readable_start_time)) | ||
| return saved_timestamp | ||
|
|
||
|
|
||
| def extract_timestamp(log_line): | ||
| pattern = r"(\d+\.\d+)" | ||
| match = re.search(pattern, log_line) | ||
| return float(match.group(1)) if match else None | ||
|
|
||
|
|
||
| def get_wakeup_timestamp(): | ||
| # Get the time stamp of the system resume from suspend for s3 | ||
| command = ["journalctl", "-b", "0", "--output=short-unix"] | ||
| result = subprocess.check_output( | ||
| command, shell=False, universal_newlines=True | ||
| ) | ||
| logs = result.splitlines() | ||
|
|
||
| for log in reversed(logs): | ||
| if r"suspend exit" in log: | ||
| logging.debug(log) | ||
| latest_system_back_time = extract_timestamp(log) | ||
| readable_back_time = datetime.datetime.fromtimestamp( | ||
| latest_system_back_time | ||
| ) | ||
| logging.debug("System back time: {}".format(readable_back_time)) | ||
| return latest_system_back_time | ||
|
|
||
| return None | ||
|
|
||
|
|
||
| def get_system_boot_time(): | ||
| """ | ||
| Read btime from /proc/stat and | ||
| return the system boot timestamp (Unix timestamp, in seconds). | ||
| """ | ||
| try: | ||
| with open("/proc/stat", "r") as f: | ||
| for line in f: | ||
| if line.startswith("btime"): | ||
| btime = float(line.split()[1]) | ||
| back_time = datetime.datetime.fromtimestamp(btime) | ||
| logging.debug("System back time: {}".format(back_time)) | ||
| return btime | ||
| logging.error("cannot find btime") | ||
| return None | ||
| except FileNotFoundError: | ||
| logging.error("cannot open /proc/stat.") | ||
| return None | ||
| except Exception as e: | ||
| logging.error("error while read btime: {}".format(e)) | ||
| return None | ||
|
|
||
|
|
||
| def parse_args(args=sys.argv[1:]): | ||
| """ | ||
| command line arguments parsing | ||
|
|
||
| :param args: arguments from sys | ||
| :type args: sys.argv | ||
| """ | ||
| parser = argparse.ArgumentParser( | ||
| description="Parse command line arguments." | ||
| ) | ||
|
|
||
| parser.add_argument("--powertype", type=str, help="Waked from s3 or s5.") | ||
| parser.add_argument( | ||
| "--timestamp_file", | ||
| type=str, | ||
| help="The file to store the timestamp of test start.", | ||
| ) | ||
| parser.add_argument( | ||
| "--delay", | ||
| type=int, | ||
| default=60, | ||
| help="Delay between attempts (in seconds).", | ||
| ) | ||
| parser.add_argument( | ||
| "--retry", type=int, default=3, help="Number of retry attempts." | ||
| ) | ||
|
|
||
| return parser.parse_args(args) | ||
|
|
||
|
|
||
| def main(): | ||
| args = parse_args() | ||
|
|
||
| logging.basicConfig( | ||
| level=logging.DEBUG, | ||
| stream=sys.stdout, | ||
| format="%(levelname)s: %(message)s", | ||
| ) | ||
|
|
||
| logging.info("wake-on-LAN check test started.") | ||
|
|
||
| powertype = args.powertype | ||
| timestamp_file = args.timestamp_file | ||
| delay = args.delay | ||
| max_retries = args.retry | ||
|
|
||
| logging.info("PowerType: {}".format(powertype)) | ||
|
|
||
| test_start_time = get_timestamp(timestamp_file) | ||
| if test_start_time is None: | ||
| raise SystemExit( | ||
| "Couldn't get the test start time from timestamp file." | ||
| ) | ||
|
|
||
| system_back_time = ( | ||
| get_wakeup_timestamp() if powertype == "s3" else get_system_boot_time() | ||
| ) | ||
| if system_back_time is None: | ||
| raise SystemExit("Couldn't get system back time.") | ||
|
|
||
| time_difference = system_back_time - test_start_time | ||
| logging.debug("time difference: {} seconds".format(time_difference)) | ||
|
|
||
| # system_back_time - test_start_time > 1.5*max_retries*delay which meanse | ||
| # the system was bring up by rtc other than Wake-on-LAN | ||
| expect_time_range = 1.5 * max_retries * delay | ||
| if time_difference > expect_time_range: | ||
| raise SystemExit( | ||
| "The system took much longer than expected to wake up," | ||
| " and it wasn't awakened by wake-on-LAN." | ||
| ) | ||
| elif time_difference < 0: | ||
| raise SystemExit("System resumed earlier than expected.") | ||
| else: | ||
| logging.info("wake-on-LAN works well.") | ||
| return True | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| main() | ||
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.