Skip to content

Commit 50ec24b

Browse files
committed
address review comments
1 parent 82ca808 commit 50ec24b

File tree

4 files changed

+47
-17
lines changed

4 files changed

+47
-17
lines changed

conftest.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,7 @@ def _add_upgrade_test(_item: Item, _upgrade_deployment_modes: list[str]) -> bool
226226
def pytest_sessionstart(session: Session) -> None:
227227
log_file = session.config.getoption("log_file") or "pytest-tests.log"
228228
tests_log_file = os.path.join(get_base_dir(), log_file)
229+
LOGGER.info(f"Writing tests log to {tests_log_file}")
229230
if os.path.exists(tests_log_file):
230231
pathlib.Path(tests_log_file).unlink()
231232

@@ -264,7 +265,7 @@ def pytest_runtest_setup(item: Item) -> None:
264265
db = Database()
265266
db.insert_test_start_time(
266267
test_name=f"{item.fspath}::{item.name}",
267-
start_time=int(datetime.datetime.now().strftime("%s")),
268+
start_time=int(datetime.datetime.now().timestamp()),
268269
)
269270
except Exception as db_exception:
270271
LOGGER.error(f"Database error: {db_exception}. Must-gather collection may not be accurate")
@@ -323,7 +324,8 @@ def pytest_sessionfinish(session: Session, exitstatus: int) -> None:
323324
db = Database()
324325
file_path = db.database_file_path
325326
LOGGER.info(f"Removing database file path {file_path}")
326-
os.remove(file_path)
327+
if os.path.exists(file_path):
328+
os.remove(file_path)
327329
# clean up the empty folders
328330
collector_directory = py_config["must_gather_collector"]["must_gather_base_directory"]
329331
if os.path.exists(collector_directory):

utilities/database.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import logging
2+
import os
23

34
from sqlalchemy import Integer, String, create_engine
45
from sqlalchemy.orm import Mapped, Session, mapped_column
@@ -24,7 +25,7 @@ class OpenDataHubTestTable(Base):
2425

2526
class Database:
2627
def __init__(self, database_file_name: str = TEST_DB, verbose: bool = True) -> None:
27-
self.database_file_path = f"{get_base_dir()}{database_file_name}"
28+
self.database_file_path = os.path.join(get_base_dir(), database_file_name)
2829
self.connection_string = f"sqlite:///{self.database_file_path}"
2930
self.verbose = verbose
3031
self.engine = create_engine(url=self.connection_string, echo=self.verbose)
@@ -38,9 +39,15 @@ def insert_test_start_time(self, test_name: str, start_time: int) -> None:
3839

3940
def get_test_start_time(self, test_name: str) -> int:
4041
with Session(bind=self.engine) as db_session:
41-
return (
42+
result_row = (
4243
db_session.query(OpenDataHubTestTable)
4344
.with_entities(OpenDataHubTestTable.start_time)
4445
.filter_by(test_name=test_name)
45-
.one()[0]
46+
.first()
4647
)
48+
if result_row:
49+
start_time_value = result_row[0]
50+
else:
51+
start_time_value = 0
52+
LOGGER.warning(f"No test found with name: {test_name}")
53+
return start_time_value

utilities/exceptions.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,9 @@ def __str__(self) -> str:
9696
return f"Failed to log in as user {self.user}."
9797

9898

99-
class InvalidArguments(Exception):
99+
class InvalidArgumentsError(Exception):
100+
"""Raised when mutually exclusive or invalid argument combinations are passed."""
101+
100102
pass
101103

102104

utilities/must_gather_collector.py

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,25 @@
55
from pytest import Item
66
from pyhelper_utils.shell import run_command
77

8-
from utilities.exceptions import InvalidArguments
8+
from utilities.exceptions import InvalidArgumentsError
99
from utilities.infra import get_rhods_csv_version, get_oc_image_info, generate_openshift_pull_secret_file
1010

1111
BASE_DIRECTORY_NAME = "must-gather-collected"
12+
RESULTS_DIR = "/home/odh/opendatahub-tests/"
1213

1314

1415
def get_base_dir() -> str:
15-
if os.path.exists("/home/odh/opendatahub-tests/"):
16+
if os.path.exists(RESULTS_DIR):
1617
# we are running from jenkins.
17-
return "/home/odh/opendatahub-tests/results"
18+
return RESULTS_DIR
1819
else:
1920
# this is local run
2021
return ""
2122

2223

2324
def set_must_gather_collector_values() -> dict[str, str]:
2425
py_config["must_gather_collector"] = {
25-
"must_gather_base_directory": f"{get_base_dir()}{BASE_DIRECTORY_NAME}",
26+
"must_gather_base_directory": os.path.join(get_base_dir(), BASE_DIRECTORY_NAME),
2627
}
2728
return py_config["must_gather_collector"]
2829

@@ -81,8 +82,22 @@ def run_must_gather(
8182
component_name: str = "",
8283
namespaces_dict: dict[str, str] | None = None,
8384
) -> str:
85+
"""
86+
Process the arguments to build must-gather command and run the same
87+
88+
Args:
89+
image_url (str): must-gather image url
90+
target_dir (str): must-gather target directory
91+
since (str): duration in seconds for must-gather log collection
92+
component_name (str): must-gather component name
93+
namespaces_dict (dict[str, str] | None): namespaces dict for extra data collection from different component
94+
namespaces
95+
96+
Returns:
97+
str: must-gather output
98+
"""
8499
if component_name and namespaces_dict:
85-
raise InvalidArguments("component name and namespaces can't be passed together")
100+
raise InvalidArgumentsError("component name and namespaces can't be passed together")
86101

87102
must_gather_command = "oc adm must-gather"
88103
if target_dir:
@@ -117,12 +132,16 @@ def run_must_gather(
117132

118133

119134
def get_must_gather_image_info(architecture: str = "linux/amd64") -> str:
120-
csv_version = get_rhods_csv_version()
121-
must_gather_image_manifest = f"quay.io/modh/must-gather:rhoai-{csv_version.major}.{csv_version.minor}"
122-
image_info = get_oc_image_info(
123-
image=must_gather_image_manifest, architecture=architecture, pull_secret=generate_openshift_pull_secret_file()
124-
)
125-
return f"quay.io/modh/must-gather@{image_info['digest']}"
135+
try:
136+
csv_version = get_rhods_csv_version()
137+
must_gather_image_manifest = f"quay.io/modh/must-gather:rhoai-{csv_version.major}.{csv_version.minor}"
138+
pull_secret = generate_openshift_pull_secret_file()
139+
image_info = get_oc_image_info(
140+
image=must_gather_image_manifest, architecture=architecture, pull_secret=pull_secret
141+
)
142+
return f"quay.io/modh/must-gather@{image_info['digest']}"
143+
except Exception as exec:
144+
raise RuntimeError(f"Failed to retrieve must-gather image info: {str(exec)}") from exec
126145

127146

128147
def collect_rhoai_must_gather(

0 commit comments

Comments
 (0)