From ca43009b53e2c755cc3cbf70ba5fa2ac6b9b6b77 Mon Sep 17 00:00:00 2001 From: Sunil Thaha Date: Thu, 27 Jun 2024 21:36:25 +1000 Subject: [PATCH] chore(validator): use hatch fmt for formatting and linting (#1576) * chore(validator): fix ruff lints Signed-off-by: Sunil Thaha * chore(validator): use hatch fmt for formatting Signed-off-by: Sunil Thaha * chore(validator): fix ruff ARG002 lint Signed-off-by: Sunil Thaha --------- Signed-off-by: Sunil Thaha --- e2e/tools/validator/pyproject.toml | 4 ++++ e2e/tools/validator/ruff.toml | 1 - .../validator/src/validator/cli/__init__.py | 19 ++++++++----------- .../validator/src/validator/cli/options.py | 1 + .../src/validator/config/__init__.py | 5 +++-- .../src/validator/prometheus/__init__.py | 19 ++++++++++--------- .../validator/src/validator/specs/__init__.py | 7 ++++--- .../src/validator/stresser/__init__.py | 8 ++++---- .../src/validator/validations/__init__.py | 13 +++++++------ .../tests/validator/config/test_config.py | 4 +++- .../validator/prometheus/test_prometheus.py | 10 ++++++---- .../tests/validator/stresser/test_stresser.py | 4 ++-- 12 files changed, 52 insertions(+), 43 deletions(-) delete mode 100644 e2e/tools/validator/ruff.toml diff --git a/e2e/tools/validator/pyproject.toml b/e2e/tools/validator/pyproject.toml index 639446f4aa..1089daf4d6 100644 --- a/e2e/tools/validator/pyproject.toml +++ b/e2e/tools/validator/pyproject.toml @@ -93,3 +93,7 @@ exclude_lines = [ markers = [ "focus", # used in development to mark focused tests ] + +[tool.ruff] +line-length = 120 + diff --git a/e2e/tools/validator/ruff.toml b/e2e/tools/validator/ruff.toml deleted file mode 100644 index f11cf6352d..0000000000 --- a/e2e/tools/validator/ruff.toml +++ /dev/null @@ -1 +0,0 @@ -line-length = 120 diff --git a/e2e/tools/validator/src/validator/cli/__init__.py b/e2e/tools/validator/src/validator/cli/__init__.py index 0b39d7da92..da85595c99 100644 --- a/e2e/tools/validator/src/validator/cli/__init__.py +++ b/e2e/tools/validator/src/validator/cli/__init__.py @@ -2,25 +2,22 @@ # # SPDX-License-Identifier: APACHE-2.0 -import subprocess -import os +import datetime +import json import logging +import os +import subprocess import typing -import json -import datetime import click -from validator.__about__ import __version__ from validator import config +from validator.__about__ import __version__ from validator.cli import options - +from validator.prometheus import Comparator, PrometheusClient, Series from validator.specs import Reporter as SpecReporter -from validator.stresser import Remote - -from validator.stresser import ScriptResult +from validator.stresser import Remote, ScriptResult from validator.validations import Loader, QueryTemplate -from validator.prometheus import Comparator, Series, PrometheusClient logger = logging.getLogger(__name__) pass_config = click.make_pass_decorator(config.Validator) @@ -55,7 +52,7 @@ def h4(self, text: str) -> None: def new_report(dir: str) -> Report: # run git describe command and get the output as the report name - git_describe = subprocess.run(["git", "describe", "--tag"], stdout=subprocess.PIPE) + git_describe = subprocess.run(["git", "describe", "--tag"], stdout=subprocess.PIPE, check=False) tag = git_describe.stdout.decode().strip() results_dir = os.path.join(dir, f"validator-{tag}") diff --git a/e2e/tools/validator/src/validator/cli/options.py b/e2e/tools/validator/src/validator/cli/options.py index 390a0cd506..2ef28b638e 100644 --- a/e2e/tools/validator/src/validator/cli/options.py +++ b/e2e/tools/validator/src/validator/cli/options.py @@ -1,4 +1,5 @@ import datetime + import click from prometheus_api_client.utils import parse_datetime diff --git a/e2e/tools/validator/src/validator/config/__init__.py b/e2e/tools/validator/src/validator/config/__init__.py index d01e8db401..f4f49bc606 100644 --- a/e2e/tools/validator/src/validator/config/__init__.py +++ b/e2e/tools/validator/src/validator/config/__init__.py @@ -3,9 +3,10 @@ # SPDX-License-Identifier: APACHE-2.0 import os -import yaml from typing import NamedTuple +import yaml + class Remote(NamedTuple): host: str @@ -60,7 +61,7 @@ def load(config_file: str) -> Validator: Returns: Config: A named tuple containing the configuration values. """ - with open(config_file, "r") as file: + with open(config_file) as file: config = yaml.safe_load(file) remote_config = config["remote"] diff --git a/e2e/tools/validator/src/validator/prometheus/__init__.py b/e2e/tools/validator/src/validator/prometheus/__init__.py index 04828ba134..06c9fe3220 100644 --- a/e2e/tools/validator/src/validator/prometheus/__init__.py +++ b/e2e/tools/validator/src/validator/prometheus/__init__.py @@ -1,14 +1,14 @@ -import re import logging -from typing import Tuple, List, NamedTuple, Protocol +import re from datetime import datetime +from typing import List, NamedTuple, Protocol, Tuple + +import numpy as np import numpy.typing as npt from prometheus_api_client import PrometheusConnect -import numpy as np from validator.config import Prometheus as PromConfig - logger = logging.getLogger(__name__) @@ -37,22 +37,22 @@ class Series: query: str - def __init__(self, query: str, samples: List[Tuple[int, str]]): + def __init__(self, query: str, samples: list[tuple[int, str]]): self.query = query self.samples = [Sample(int(s[0]), float(s[1])) for s in samples] @classmethod - def from_samples(cls, query: str, samples: List[Sample]) -> "Series": + def from_samples(cls, query: str, samples: list[Sample]) -> "Series": s = Series(query, []) s.samples = samples[:] return s @property - def timestamps(self) -> List[float]: + def timestamps(self) -> list[float]: return [s.timestamp for s in self.samples] @property - def values(self) -> List[float]: + def values(self) -> list[float]: return [s.value for s in self.samples] def __str__(self) -> str: @@ -98,7 +98,7 @@ def mape(actual: npt.ArrayLike, expected: npt.ArrayLike) -> float: return 100 * np.mean(np.abs(np.divide(np.subtract(actual, expected), actual))) -def filter_by_equal_timestamps(a: Series, b: Series) -> Tuple[Series, Series]: +def filter_by_equal_timestamps(a: Series, b: Series) -> tuple[Series, Series]: """ filter_by_equal_timestamps will filter out samples from a and b that have the same timestamp. @@ -146,6 +146,7 @@ def strip_query(query: str) -> str: class Queryable(Protocol): + # ruff: noqa: ARG002 (we don't care about the arguments here) def range_query(self, query: str, start: datetime, end: datetime) -> list[Series]: return [] diff --git a/e2e/tools/validator/src/validator/specs/__init__.py b/e2e/tools/validator/src/validator/specs/__init__.py index 803c03d2ee..7b0609e6dd 100644 --- a/e2e/tools/validator/src/validator/specs/__init__.py +++ b/e2e/tools/validator/src/validator/specs/__init__.py @@ -4,8 +4,9 @@ # a python program to get host and VM cpu spec, dram size, number of cpu cores, and return a json output import subprocess -import validator.config as config import typing + +from validator import config from validator.stresser import Remote @@ -41,7 +42,7 @@ def parse_lscpu_output(output: str): def get_host_cpu_spec(): # get host cpu spec host_cpu_spec = {} - lscpu = subprocess.run(["lscpu"], stdout=subprocess.PIPE) + lscpu = subprocess.run(["lscpu"], stdout=subprocess.PIPE, check=False) if lscpu.stdout: host_cpu_spec = parse_lscpu_output(lscpu.stdout.decode()) return host_cpu_spec @@ -58,7 +59,7 @@ def get_vm_cpu_spec(vm: Remote): def get_host_dram_size(): # get host dram size dram_size = "" - meminfo = open("/proc/meminfo", "r") + meminfo = open("/proc/meminfo") for line in meminfo: if "MemTotal" in line: dram_size = line.split(":")[1].strip() diff --git a/e2e/tools/validator/src/validator/stresser/__init__.py b/e2e/tools/validator/src/validator/stresser/__init__.py index 578070802b..5eedb9d06b 100644 --- a/e2e/tools/validator/src/validator/stresser/__init__.py +++ b/e2e/tools/validator/src/validator/stresser/__init__.py @@ -1,10 +1,10 @@ import logging +from datetime import datetime +from typing import NamedTuple import paramiko -from validator import config -from typing import NamedTuple -from datetime import datetime +from validator import config logger = logging.getLogger(__name__) @@ -88,7 +88,7 @@ def run_script(self, script_path: str) -> ScriptResult: print("‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾\n\n") if exit_status != 0: - logger.warn("script execution failed") + logger.warning("script execution failed") else: logger.info("script execution successful") diff --git a/e2e/tools/validator/src/validator/validations/__init__.py b/e2e/tools/validator/src/validator/validations/__init__.py index f73bd9150d..4acb08c99c 100644 --- a/e2e/tools/validator/src/validator/validations/__init__.py +++ b/e2e/tools/validator/src/validator/validations/__init__.py @@ -1,15 +1,16 @@ import logging -from typing import Dict, NamedTuple, List -from validator import config import re +from typing import Dict, List, NamedTuple + import yaml +from validator import config logger = logging.getLogger(__name__) class QueryTemplate: - def __init__(self, query: str, vars: Dict[str, str]) -> None: + def __init__(self, query: str, vars: dict[str, str]) -> None: self._original = query self._vars = vars self._promql = query.format(**vars) @@ -50,8 +51,8 @@ class Validation(NamedTuple): actual: QueryTemplate -def read_validations(file_path: str, vars: Dict[str, str]) -> list[Validation]: - with open(file_path, "r") as file: +def read_validations(file_path: str, vars: dict[str, str]) -> list[Validation]: + with open(file_path) as file: yml = yaml.safe_load(file) return [ Validation( @@ -67,7 +68,7 @@ class Loader: def __init__(self, cfg: config.Validator): self.cfg = cfg - def load(self) -> List[Validation]: + def load(self) -> list[Validation]: vars = {} vm = self.cfg.metal.vm diff --git a/e2e/tools/validator/tests/validator/config/test_config.py b/e2e/tools/validator/tests/validator/config/test_config.py index d9972019cc..4327764829 100644 --- a/e2e/tools/validator/tests/validator/config/test_config.py +++ b/e2e/tools/validator/tests/validator/config/test_config.py @@ -1,5 +1,7 @@ import os + import pytest + from validator.config import load @@ -12,7 +14,7 @@ def write(content): file.write(content) return file_path - yield write + return write @pytest.fixture diff --git a/e2e/tools/validator/tests/validator/prometheus/test_prometheus.py b/e2e/tools/validator/tests/validator/prometheus/test_prometheus.py index 1bae9c828e..1a46825868 100644 --- a/e2e/tools/validator/tests/validator/prometheus/test_prometheus.py +++ b/e2e/tools/validator/tests/validator/prometheus/test_prometheus.py @@ -1,18 +1,20 @@ import datetime -import pytest + import numpy as np +import pytest from validator.config import ( Prometheus as PromConfig, +) +from validator.config import ( PrometheusJob as Job, ) - from validator.prometheus import ( Comparator, Series, filter_by_equal_timestamps, - mse, mape, + mse, ) @@ -209,7 +211,7 @@ class MockPromClient: def __init__(self, responses): self.responses = responses - def range_query(self, query: str, *args, **kwargs) -> list[Series]: + def range_query(self, query: str, _start: datetime.datetime, _end: datetime.datetime) -> list[Series]: return [Series(query, r["values"]) for r in self.responses] diff --git a/e2e/tools/validator/tests/validator/stresser/test_stresser.py b/e2e/tools/validator/tests/validator/stresser/test_stresser.py index 921ab79de4..116cd43ee0 100644 --- a/e2e/tools/validator/tests/validator/stresser/test_stresser.py +++ b/e2e/tools/validator/tests/validator/stresser/test_stresser.py @@ -1,6 +1,6 @@ import pytest -from validator import stresser -from validator import config + +from validator import config, stresser @pytest.fixture