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)