Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .flake8
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ fcn_exclude_functions =
add_to_assignees,
validate_inference_output, # TODO: function should be fixed to get rid of this
group
writelines
temp_file
sleep

enable-extensions =
FCN,
Expand Down
55 changes: 24 additions & 31 deletions .github/workflows/scripts/pr_workflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,9 @@
import re
import sys

from github.PullRequest import PullRequest
from github.Repository import Repository
from github.MainClass import Github
from github.GithubException import UnknownObjectException
from github.Organization import Organization
from github.Team import Team

from constants import (
ALL_LABELS_DICT,
APPROVED,
CANCEL_ACTION,
CHANGED_REQUESTED_BY_LABEL_PREFIX,
COMMENTED_BY_LABEL_PREFIX,
Expand All @@ -22,8 +16,13 @@
SUPPORTED_LABELS,
VERIFIED_LABEL_STR,
WELCOME_COMMENT,
APPROVED,
)
from github.GithubException import UnknownObjectException
from github.MainClass import Github
from github.Organization import Organization
from github.PullRequest import PullRequest
from github.Repository import Repository
from github.Team import Team
from simple_logger.logger import get_logger

LOGGER = get_logger(name="pr_labeler")
Expand All @@ -35,7 +34,7 @@ class SupportedActions:
pr_size_action_name: str = "add-pr-size-label"
welcome_comment_action_name: str = "add-welcome-comment-set-assignee"
build_push_pr_image_action_name: str = "push-container-on-comment"
supported_actions: set[str] = {
supported_actions: set[str] = { # noqa: RUF012
pr_size_action_name,
add_remove_labels_action_name,
welcome_comment_action_name,
Expand All @@ -48,7 +47,7 @@ def __init__(self) -> None:
self.gh_client: Github

self.repo_name = os.environ["GITHUB_REPOSITORY"]
self.pr_number = int(os.getenv("GITHUB_PR_NUMBER", 0))
self.pr_number = int(os.getenv("GITHUB_PR_NUMBER", "0"))
self.action = os.getenv("ACTION")
self.event_action = os.getenv("GITHUB_EVENT_ACTION")
self.event_name = os.getenv("GITHUB_EVENT_NAME")
Expand Down Expand Up @@ -110,7 +109,7 @@ def verify_allowed_user(self) -> bool:
# check if the user is a member of opendatahub-tests-contributors
membership = team.get_team_membership(member=self.user_login)
LOGGER.info(f"User {self.user_login} is a member of the test contributor team. {membership}")
return True
return True # noqa: TRY300
except UnknownObjectException:
LOGGER.error(f"User {self.user_login} is not allowed for this action. Exiting.")
return False
Expand All @@ -123,23 +122,19 @@ def verify_labeler_config(self) -> None:
if not self.user_login:
sys.exit("`GITHUB_USER_LOGIN` is not set")

if (
self.event_name == "issue_comment" or self.event_name == "pull_request_review"
) and not self.comment_body:
if (self.event_name in {"issue_comment", "pull_request_review"}) and not self.comment_body:
LOGGER.info("No comment, nothing to do. Exiting.")
sys.exit(0)

def run_pr_label_action(self) -> None:
if self.action == self.SupportedActions.pr_size_action_name:
self.set_pr_size()

if self.action == self.SupportedActions.build_push_pr_image_action_name:
if not self.verify_allowed_user():
sys.exit(1)
if self.action == self.SupportedActions.build_push_pr_image_action_name and not self.verify_allowed_user():
sys.exit(1)

if self.action == self.SupportedActions.add_remove_labels_action_name:
if self.verify_allowed_user():
self.add_remove_pr_labels()
if self.action == self.SupportedActions.add_remove_labels_action_name and self.verify_allowed_user():
self.add_remove_pr_labels()

if self.action == self.SupportedActions.welcome_comment_action_name:
self.add_welcome_comment_set_assignee()
Expand Down Expand Up @@ -187,10 +182,9 @@ def set_label_in_repository(self, label: str) -> None:
LOGGER.info(f"repo labels: {repo_labels}")

try:
if _repo_label := self.repo.get_label(name=label):
if _repo_label.color != label_color:
LOGGER.info(f"Edit repository label: {label}, color: {label_color}")
_repo_label.edit(name=_repo_label.name, color=label_color)
if (_repo_label := self.repo.get_label(name=label)) and _repo_label.color != label_color:
LOGGER.info(f"Edit repository label: {label}, color: {label_color}")
_repo_label.edit(name=_repo_label.name, color=label_color)

except UnknownObjectException:
LOGGER.info(f"Add repository label: {label}, color: {label_color}")
Expand Down Expand Up @@ -249,16 +243,15 @@ def add_remove_pr_labels(self) -> None:

return

elif self.event_name == "pull_request_review":
elif (
self.event_name == "pull_request_review"
or self.event_name == "workflow_run"
and self.event_action == "submitted"
):
self.pull_request_review_label_actions()

return

# We will only reach here if the PR was created from a fork
elif self.event_name == "workflow_run" and self.event_action == "submitted":
self.pull_request_review_label_actions()
return

LOGGER.warning("`add_remove_pr_label` called without a supported event")

def pull_request_review_label_actions(
Expand Down Expand Up @@ -317,7 +310,7 @@ def issue_comment_label_actions(
if not action[CANCEL_ACTION] or self.event_action == "deleted":
self.approve_pr()

label_in_pr = any([label == _label.lower() for _label in self.pr_labels])
label_in_pr = any(label == _label.lower() for _label in self.pr_labels)
LOGGER.info(f"Processing label: {label}, action: {action}")

if action[CANCEL_ACTION] or self.event_action == "deleted":
Expand Down
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ repos:
exclude: .*/__snapshots__/.*|.*-input\.json$

- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.15.1
rev: v0.15.2
hooks:
- id: ruff
- id: ruff-format
Expand Down
54 changes: 27 additions & 27 deletions conftest.py
Original file line number Diff line number Diff line change
@@ -1,43 +1,43 @@
import datetime
import logging
import os
import pathlib
import shutil
import datetime
import traceback
from typing import Any

import pytest
import shortuuid
from _pytest.runner import CallInfo
from _pytest.nodes import Node
from _pytest.reports import TestReport
from _pytest.runner import CallInfo
from _pytest.terminal import TerminalReporter
from kubernetes.dynamic import DynamicClient
from ocp_resources.cluster_service_version import ClusterServiceVersion
from ocp_resources.resource import get_client
from pytest import (
Parser,
Session,
FixtureRequest,
FixtureDef,
Item,
Collector,
Config,
CollectReport,
Config,
FixtureDef,
FixtureRequest,
Item,
Parser,
Session,
)
from _pytest.nodes import Node
from _pytest.terminal import TerminalReporter
from typing import Optional, Any
from pytest_testconfig import config as py_config

from utilities.constants import KServeDeploymentType, MODEL_REGISTRY_CUSTOM_NAMESPACE
from utilities.constants import MODEL_REGISTRY_CUSTOM_NAMESPACE, KServeDeploymentType
from utilities.database import Database
from utilities.infra import get_data_science_cluster, get_dsci_applications_namespace, get_operator_distribution
from utilities.logger import separator, setup_logging
from utilities.must_gather_collector import (
set_must_gather_collector_directory,
set_must_gather_collector_values,
get_must_gather_collector_dir,
collect_rhoai_must_gather,
get_base_dir,
get_must_gather_collector_dir,
set_must_gather_collector_directory,
set_must_gather_collector_values,
)
from kubernetes.dynamic import DynamicClient
from utilities.infra import get_operator_distribution, get_dsci_applications_namespace, get_data_science_cluster
from ocp_resources.resource import get_client
from ocp_resources.cluster_service_version import ClusterServiceVersion

LOGGER = logging.getLogger(name=__name__)
BASIC_LOGGER = logging.getLogger(name="basic")
Expand Down Expand Up @@ -229,7 +229,7 @@ def _add_upgrade_test(_item: Item, _upgrade_deployment_modes: list[str]) -> bool
if not _upgrade_deployment_modes:
return True

return any([keyword for keyword in _item.keywords if keyword in _upgrade_deployment_modes])
return any(keyword for keyword in _item.keywords if keyword in _upgrade_deployment_modes)

pre_upgrade_tests: list[Item] = []
post_upgrade_tests: list[Item] = []
Expand Down Expand Up @@ -310,7 +310,7 @@ def pytest_sessionstart(session: Session) -> None:
value = log_cli_override.split("=", 1)[1].lower()
enable_console_value = value not in ("false", "0", "no", "off")

except Exception as e:
except Exception as e: # noqa: BLE001
# If there's any issue with option detection, fall back to default behavior
LOGGER.error(f"Error detecting log_cli option: {e}")
enable_console_value = True
Expand Down Expand Up @@ -397,9 +397,9 @@ def pytest_runtest_setup(item: Item) -> None:
db = item.config.option.must_gather_db
db.insert_test_start_time(
test_name=f"{item.fspath}::{item.name}",
start_time=int(datetime.datetime.now().timestamp()),
start_time=int(datetime.datetime.now().timestamp()), # noqa: DTZ005
)
except Exception as db_exception:
except Exception as db_exception: # noqa: BLE001
LOGGER.error(f"Database error: {db_exception}. Must-gather collection may not be accurate")

if KServeDeploymentType.RAW_DEPLOYMENT.lower() in item.keywords:
Expand Down Expand Up @@ -460,15 +460,15 @@ def pytest_sessionfinish(session: Session, exitstatus: int) -> None:
LOGGER.info(f"Deleting pytest base dir {session.config.option.basetemp}")
shutil.rmtree(path=session.config.option.basetemp, ignore_errors=True)

reporter: Optional[TerminalReporter] = session.config.pluginmanager.get_plugin("terminalreporter")
reporter: TerminalReporter | None = session.config.pluginmanager.get_plugin("terminalreporter")
if reporter:
reporter.summary_stats()


def calculate_must_gather_timer(test_start_time: int) -> int:
default_duration = 300
if test_start_time > 0:
duration = int(datetime.datetime.now().timestamp()) - test_start_time
duration = int(datetime.datetime.now().timestamp()) - test_start_time # noqa: DTZ005
return duration if duration > 60 else default_duration
else:
LOGGER.warning(f"Could not get start time of test. Collecting must-gather for last {default_duration}s")
Expand All @@ -492,7 +492,7 @@ def pytest_exception_interact(node: Item | Collector, call: CallInfo[Any], repor
try:
db = node.config.option.must_gather_db
test_start_time = db.get_test_start_time(test_name=test_name)
except Exception as db_exception:
except Exception as db_exception: # noqa: BLE001
test_start_time = 0
LOGGER.warning(f"Error: {db_exception} in accessing database.")

Expand All @@ -503,7 +503,7 @@ def pytest_exception_interact(node: Item | Collector, call: CallInfo[Any], repor
target_dir=os.path.join(get_must_gather_collector_dir(), "pytest_exception_interact"),
)

except Exception as current_exception:
except Exception as current_exception: # noqa: BLE001
LOGGER.warning(f"Failed to collect logs: {test_name}: {current_exception} {traceback.format_exc()}")


Expand Down
3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ fix = true
output-format = "grouped"
extend-exclude = ["utilities/manifests"]

[tool.ruff.lint]
external = ["E501"]

[tool.ruff.format]
exclude = [".git", ".venv", ".mypy_cache", ".tox", "__pycache__", "utilities/manifests"]

Expand Down
5 changes: 3 additions & 2 deletions tests/cluster_health/test_operator_health.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@
from kubernetes.dynamic import DynamicClient
from ocp_resources.data_science_cluster import DataScienceCluster
from ocp_resources.dsc_initialization import DSCInitialization
from utilities.general import wait_for_pods_running
from utilities.infra import wait_for_dsci_status_ready, wait_for_dsc_status_ready
from pytest_testconfig import config as py_config
from simple_logger.logger import get_logger

from utilities.general import wait_for_pods_running
from utilities.infra import wait_for_dsc_status_ready, wait_for_dsci_status_ready

LOGGER = get_logger(name=__name__)


Expand Down
Loading
Loading