Skip to content

Commit 80d9310

Browse files
committed
address review comments
1 parent 82ca808 commit 80d9310

4 files changed

Lines changed: 33 additions & 16 deletions

File tree

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: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
import logging
2+
import os
3+
from typing import Any
24

3-
from sqlalchemy import Integer, String, create_engine
5+
from sqlalchemy import Integer, String, create_engine, ColumnElement
46
from sqlalchemy.orm import Mapped, Session, mapped_column
57
from sqlalchemy.orm import DeclarativeBase
8+
from sqlalchemy.exc import NoResultFound
69
from utilities.must_gather_collector import get_base_dir
710

811
LOGGER = logging.getLogger(__name__)
@@ -24,7 +27,7 @@ class OpenDataHubTestTable(Base):
2427

2528
class Database:
2629
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}"
30+
self.database_file_path = os.path.join(get_base_dir(), database_file_name)
2831
self.connection_string = f"sqlite:///{self.database_file_path}"
2932
self.verbose = verbose
3033
self.engine = create_engine(url=self.connection_string, echo=self.verbose)
@@ -38,9 +41,16 @@ def insert_test_start_time(self, test_name: str, start_time: int) -> None:
3841

3942
def get_test_start_time(self, test_name: str) -> int:
4043
with Session(bind=self.engine) as db_session:
41-
return (
44+
result_row = (
4245
db_session.query(OpenDataHubTestTable)
4346
.with_entities(OpenDataHubTestTable.start_time)
4447
.filter_by(test_name=test_name)
45-
.one()[0]
48+
.first()
4649
)
50+
if result_row:
51+
start_time_value = result_row[0]
52+
else:
53+
start_time_value = 0
54+
LOGGER.warning(f"No test found with name: {test_name}")
55+
return start_time_value
56+

utilities/exceptions.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,8 @@ 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."""
100101
pass
101102

102103

utilities/must_gather_collector.py

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
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"
@@ -22,7 +22,7 @@ def get_base_dir() -> str:
2222

2323
def set_must_gather_collector_values() -> dict[str, str]:
2424
py_config["must_gather_collector"] = {
25-
"must_gather_base_directory": f"{get_base_dir()}{BASE_DIRECTORY_NAME}",
25+
"must_gather_base_directory": os.path.join(get_base_dir(), BASE_DIRECTORY_NAME),
2626
}
2727
return py_config["must_gather_collector"]
2828

@@ -82,7 +82,7 @@ def run_must_gather(
8282
namespaces_dict: dict[str, str] | None = None,
8383
) -> str:
8484
if component_name and namespaces_dict:
85-
raise InvalidArguments("component name and namespaces can't be passed together")
85+
raise InvalidArgumentsError("component name and namespaces can't be passed together")
8686

8787
must_gather_command = "oc adm must-gather"
8888
if target_dir:
@@ -117,12 +117,16 @@ def run_must_gather(
117117

118118

119119
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']}"
120+
try:
121+
csv_version = get_rhods_csv_version()
122+
must_gather_image_manifest = f"quay.io/modh/must-gather:rhoai-{csv_version.major}.{csv_version.minor}"
123+
pull_secret = generate_openshift_pull_secret_file()
124+
image_info = get_oc_image_info(
125+
image=must_gather_image_manifest, architecture=architecture, pull_secret=pull_secret
126+
)
127+
return f"quay.io/modh/must-gather@{image_info['digest']}"
128+
except Exception as exec:
129+
raise RuntimeError(f"Failed to retrieve must-gather image info: {str(exec)}") from exec
126130

127131

128132
def collect_rhoai_must_gather(

0 commit comments

Comments
 (0)