Skip to content

Commit 383066c

Browse files
monkutaYour NameKarolinaPomian
authored
Test: Mtl manager fixture (#1219)
This class adds a class to manage the lifecycle of the MtlManager process on a remote host, as well as a connected fixture, that turns the MtlManager on at the beginning of the test and off at the end of it. --------- Co-authored-by: Your Name <[email protected]> Co-authored-by: KarolinaPomian <[email protected]> Co-authored-by: KarolinaPomian <[email protected]>
1 parent 1972a3c commit 383066c

File tree

3 files changed

+107
-0
lines changed

3 files changed

+107
-0
lines changed

tests/validation/common/mtl_manager/__init__.py

Whitespace-only changes.
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
import logging
2+
3+
from mfd_connect.exceptions import ConnectionCalledProcessError
4+
5+
logger = logging.getLogger(__name__)
6+
7+
8+
class MtlManager:
9+
"""
10+
Class to manage the lifecycle of the MtlManager process on a remote host.
11+
12+
Attributes:
13+
host: Host object containing a .connection attribute for remote command execution.
14+
mtl_manager_process: The running MtlManager process object (if started).
15+
"""
16+
17+
def __init__(self, host):
18+
"""
19+
Initialize the MtlManager with a host object.
20+
:param host: Host object with a .connection attribute.
21+
"""
22+
self.host = host
23+
self.cmd = "sudo MtlManager"
24+
self.mtl_manager_process = None
25+
26+
def start(self):
27+
"""
28+
Starts the MtlManager process on the remote host using sudo.
29+
Returns True if started successfully, False otherwise.
30+
"""
31+
if not self.mtl_manager_process or not self.mtl_manager_process.running:
32+
connection = self.host.connection
33+
try:
34+
logger.info(f"Running command on host {self.host.name}: {self.cmd}")
35+
self.mtl_manager_process = connection.start_process(
36+
self.cmd, stderr_to_stdout=True
37+
)
38+
39+
if not self.mtl_manager_process.running:
40+
err = self.mtl_manager_process.stderr_text
41+
logger.error(f"MtlManager failed to start. Error output:\n{err}")
42+
return False
43+
logger.info(
44+
f"MtlManager started with PID {self.mtl_manager_process.pid}."
45+
)
46+
return True
47+
except ConnectionCalledProcessError as e:
48+
logger.error(f"Failed to start MtlManager: {e}")
49+
return False
50+
else:
51+
logger.info("MtlManager is already running.")
52+
return True
53+
54+
def stop(self):
55+
"""
56+
Stops the MtlManager process on the remote host.
57+
First attempts to gracefully stop the process if it's tracked,
58+
then falls back to pkill if needed.
59+
"""
60+
if self.mtl_manager_process and self.mtl_manager_process.running:
61+
try:
62+
logger.info("Stopping MtlManager using process object methods...")
63+
# Try graceful termination first
64+
self.mtl_manager_process.stop()
65+
66+
# Check if the process stopped gracefully
67+
if self.mtl_manager_process.running:
68+
logger.info("MtlManager still running, trying kill...")
69+
self.mtl_manager_process.kill()
70+
71+
# Check logs for errors
72+
log_output = self.mtl_manager_process.stdout_text
73+
if log_output:
74+
if "error" in log_output.lower() or "fail" in log_output.lower():
75+
logger.error(f"Errors found in MtlManager logs: {log_output}")
76+
77+
logger.info("MtlManager stopped successfully.")
78+
return
79+
except Exception as e:
80+
logger.error(f"Error while stopping MtlManager process: {e}")
81+
82+
# Fallback to pkill if the process object is not available or the above failed
83+
connection = self.host.connection
84+
try:
85+
logger.info("Stopping MtlManager using sudo pkill MtlManager (fallback)...")
86+
connection.execute_command("sudo pkill MtlManager")
87+
logger.info("MtlManager stopped (via pkill).")
88+
except ConnectionCalledProcessError as e:
89+
logger.error(f"Failed to stop MtlManager via pkill: {e}")

tests/validation/conftest.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from typing import Dict
1111

1212
import pytest
13+
from common.mtl_manager.mtlManager import MtlManager
1314
from common.nicctl import Nicctl
1415
from mfd_common_libs.custom_logger import add_logging_level
1516
from mfd_common_libs.log_levels import TEST_FAIL, TEST_INFO, TEST_PASS
@@ -193,6 +194,23 @@ def media_file(media_ramdisk, request, hosts, test_config):
193194
)
194195

195196

197+
@pytest.fixture(scope="session", autouse=True)
198+
def mtl_manager(hosts):
199+
"""
200+
Automatically start MtlManager on all hosts at the beginning of the test session,
201+
and stop it at the end.
202+
"""
203+
managers = {}
204+
for host in hosts.values():
205+
mgr = MtlManager(host)
206+
if not mgr.start():
207+
raise RuntimeError(f"Failed to start MtlManager on host {host.name}")
208+
managers[host.name] = mgr
209+
yield managers
210+
for mgr in managers.values():
211+
mgr.stop()
212+
213+
196214
def pytest_addoption(parser):
197215
parser.addoption(
198216
"--keep", help="keep result media files: all, failed, none (default)"

0 commit comments

Comments
 (0)