From 5043793336f1e0ac266709b8b3f2c161ff563a11 Mon Sep 17 00:00:00 2001 From: Chi Song <27178119+squirrelsc@users.noreply.github.com> Date: Wed, 9 Dec 2020 20:12:50 +0800 Subject: [PATCH] fix who -b issue on Ubuntu 14.04 who -b doesn't return value in first boot on Ubuntu 14.04. Use uptime -s to get boot time. uptime -s doesn't exists on some centos version, so it's not default command. --- lisa/tools/__init__.py | 2 ++ lisa/tools/reboot.py | 10 +++++++++- lisa/tools/uptime.py | 23 +++++++++++++++++++++++ 3 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 lisa/tools/uptime.py diff --git a/lisa/tools/__init__.py b/lisa/tools/__init__.py index cb285f1a0..a4572495c 100644 --- a/lisa/tools/__init__.py +++ b/lisa/tools/__init__.py @@ -10,6 +10,7 @@ from .modinfo import Modinfo from .ntttcp import Ntttcp from .reboot import Reboot from .uname import Uname +from .uptime import Uptime from .who import Who __all__ = [ @@ -25,5 +26,6 @@ __all__ = [ "Ntttcp", "Reboot", "Uname", + "Uptime", "Who", ] diff --git a/lisa/tools/reboot.py b/lisa/tools/reboot.py index c4e3b604e..8455e6d46 100644 --- a/lisa/tools/reboot.py +++ b/lisa/tools/reboot.py @@ -8,6 +8,7 @@ from lisa.util.perf_timer import create_timer from lisa.util.process import ExecutableResult from .date import Date +from .uptime import Uptime from .who import Who @@ -26,7 +27,14 @@ class Reboot(Tool): def reboot(self) -> None: who = self.node.tools[Who] timer = create_timer() - last_boot_time = who.last_boot() + + # who -b doesn't return correct content in Ubuntu 14.04, but uptime works. + # uptime has no -s parameter in some distros, so not use is as default. + try: + last_boot_time = who.last_boot() + except Exception: + uptime = self.node.tools[Uptime] + last_boot_time = uptime.since_time() current_boot_time = last_boot_time # who -b returns time without seconds. diff --git a/lisa/tools/uptime.py b/lisa/tools/uptime.py new file mode 100644 index 000000000..2ad1d8125 --- /dev/null +++ b/lisa/tools/uptime.py @@ -0,0 +1,23 @@ +from datetime import datetime + +from dateutil.parser import parser + +from lisa.executable import Tool +from lisa.util import LisaException + + +class Uptime(Tool): + @property + def command(self) -> str: + return "uptime" + + def _check_exists(self) -> bool: + return True + + def since_time(self, no_error_log: bool = True) -> datetime: + command_result = self.run("-s", no_error_log=no_error_log) + if command_result.exit_code != 0: + raise LisaException( + f"get unexpected non-zero exit code {command_result.exit_code}" + ) + return parser().parse(command_result.stdout)