Skip to content

Commit 28f9152

Browse files
committed
Handle guest logs
1 parent e4ac9e7 commit 28f9152

File tree

8 files changed

+57
-29
lines changed

8 files changed

+57
-29
lines changed

tmt/steps/finish/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ def go(self, force: bool = False) -> None:
189189

190190
# Stop and remove provisioned guests
191191
for guest in self.plan.provision.guests():
192-
guest.handle_guest_logs(logs=guest.logs)
192+
guest.handle_guest_logs(log_names=guest.log_names)
193193
guest.stop()
194194
guest.remove()
195195

tmt/steps/provision/__init__.py

+39-21
Original file line numberDiff line numberDiff line change
@@ -626,9 +626,10 @@ def is_ready(self) -> bool:
626626
raise NotImplementedError
627627

628628
@property
629-
def logs(self) -> list[str]:
629+
def log_names(self) -> list[str]:
630+
"""Return name list of logs the guest could provide."""
630631

631-
raise NotImplementedError
632+
return []
632633

633634
@classmethod
634635
def options(cls, how: Optional[str] = None) -> list[tmt.options.ClickOptionDecoratorType]:
@@ -1109,38 +1110,55 @@ def requires(cls) -> list['tmt.base.Dependency']:
11091110
return []
11101111

11111112
def acquire_log(self, log_name: str) -> Optional[str]:
1112-
"""fetch and return content of a requested log"""
1113+
"""
1114+
Fetch and return content of a log.
1115+
:param log_name: name of the log.
1116+
:returns: content of the log.
1117+
"""
11131118
raise NotImplementedError
11141119

11151120
def store_log(
11161121
self,
11171122
log_path: Path,
11181123
log_content: str,
11191124
log_name: Optional[str] = None) -> None:
1120-
"""save log content and return the path"""
1121-
if log_name:
1122-
# if log_path contains log file name
1123-
if log_path.is_dir():
1124-
log_path.write_text(log_content)
1125-
else:
1126-
(log_path / log_name).write_text(log_content)
1127-
else:
1125+
"""
1126+
Save log content to a file.
1127+
:param log_path: a path to save into,could be a directory
1128+
or a file path.
1129+
:param log_content: content of the log.
1130+
:param log_name: name of the log, if not set, log_path
1131+
is supposed to be '/dev/null' or a file path.
1132+
"""
1133+
# if log_path is file path or /dev/null
1134+
if not log_path.is_dir() or str(log_path) == '/dev/null':
11281135
log_path.write_text(log_content)
1136+
# log_path is a directory
1137+
else:
1138+
if log_name:
1139+
name_str = log_name
1140+
else:
1141+
name_str = 'tmt-guestlog-' + \
1142+
datetime.datetime.now(datetime.timezone.utc).strftime("%Y%m%d%H%M%S")
1143+
self.warn(f'log_name is not set, using file name:{name_str}')
1144+
(log_path / name_str).write_text(log_content)
11291145

11301146
def handle_guest_logs(self,
11311147
log_path: Optional[Path] = None,
1132-
logs: Optional[list[str]] = None) -> None:
1133-
"""get log content and save it to the workdir/provision/,
1134-
list the log dir in guests.yaml"""
1135-
logs = logs or []
1136-
log_path = log_path or self.workdir
1137-
for log_name in logs:
1148+
log_names: Optional[list[str]] = None) -> None:
1149+
"""
1150+
Get log content and save it to a directory.
1151+
:param log_path: a directory to save into.If not set,self.workdir
1152+
or Path.cwd() will be used.
1153+
:param log_names: name list of logs need to be handled.If not set,
1154+
self.log_names will be used.
1155+
"""
1156+
log_names = log_names or self.log_names
1157+
log_path = log_path or self.workdir or Path.cwd()
1158+
for log_name in log_names:
11381159
log_content = self.acquire_log(log_name)
11391160
if log_content:
1140-
if log_path:
1141-
self.store_log(log_path, log_content, log_name)
1142-
else:
1143-
self.store_log(Path.cwd(), log_content, log_name)
1161+
self.store_log(log_path, log_content, log_name)
11441162
else:
11451163
self.debug(f'no content in {log_name}')
11461164

tmt/steps/provision/artemis.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -497,7 +497,8 @@ def is_ready(self) -> bool:
497497
return self.guest is not None
498498

499499
@property
500-
def logs(self) -> list[str]:
500+
def log_names(self) -> list[str]:
501+
"""Return name list of logs the guest could provide."""
501502
return []
502503

503504
def _create(self) -> None:

tmt/steps/provision/connect.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,8 @@ class GuestConnect(tmt.steps.provision.GuestSsh):
7070
hard_reboot: Optional[ShellScript]
7171

7272
@property
73-
def logs(self) -> list[str]:
73+
def log_names(self) -> list[str]:
74+
"""Return name list of logs the guest could provide."""
7475
return []
7576

7677
def reboot(

tmt/steps/provision/local.py

+7-2
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ def is_ready(self) -> bool:
2727
return True
2828

2929
@property
30-
def logs(self) -> list[str]:
30+
def log_names(self) -> list[str]:
31+
"""Return name list of logs the guest could provide."""
3132
return ['dmesg']
3233

3334
def _run_ansible(
@@ -138,7 +139,11 @@ def pull(
138139
""" Nothing to be done to pull workdir """
139140

140141
def acquire_log(self, log_name: str) -> Optional[str]:
141-
"""fetch and return content of a requested log"""
142+
"""
143+
Fetch and return content of a log.
144+
:param log_name: name of the log.
145+
:returns: content of the log.
146+
"""
142147
if log_name == 'dmesg':
143148
return self.execute(Command('dmesg')).stdout
144149
return None

tmt/steps/provision/mrack.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -605,7 +605,8 @@ def is_ready(self) -> bool:
605605
return False
606606

607607
@property
608-
def logs(self) -> list[str]:
608+
def log_names(self) -> list[str]:
609+
"""Return name list of logs the guest could provide."""
609610
return []
610611

611612
def _create(self, tmt_name: str) -> None:

tmt/steps/provision/podman.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,8 @@ def is_ready(self) -> bool:
114114
return str(cmd_output.stdout).strip() == 'true'
115115

116116
@property
117-
def logs(self) -> list[str]:
117+
def log_names(self) -> list[str]:
118+
"""Return name list of logs the guest could provide."""
118119
return []
119120

120121
def wake(self) -> None:

tmt/steps/provision/testcloud.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,8 @@ def is_ready(self) -> bool:
362362
return False
363363

364364
@property
365-
def logs(self) -> list[str]:
365+
def log_names(self) -> list[str]:
366+
"""Return name list of logs the guest could provide."""
366367
return []
367368

368369
def _get_url(self, url: str, message: str) -> requests.Response:

0 commit comments

Comments
 (0)