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.
This commit is contained in:
Chi Song 2020-12-09 20:12:50 +08:00
Родитель b96cb62789
Коммит 5043793336
3 изменённых файлов: 34 добавлений и 1 удалений

Просмотреть файл

@ -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",
]

Просмотреть файл

@ -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()
# 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.

23
lisa/tools/uptime.py Normal file
Просмотреть файл

@ -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)