Skip to content

Commit

Permalink
chore(validator): use hatch fmt for formatting and linting (#1576)
Browse files Browse the repository at this point in the history
* chore(validator): fix ruff lints

Signed-off-by: Sunil Thaha <[email protected]>

* chore(validator): use hatch fmt for formatting

Signed-off-by: Sunil Thaha <[email protected]>

* chore(validator): fix ruff ARG002 lint

Signed-off-by: Sunil Thaha <[email protected]>

---------

Signed-off-by: Sunil Thaha <[email protected]>
  • Loading branch information
sthaha authored Jun 27, 2024
1 parent 96ae88b commit ca43009
Show file tree
Hide file tree
Showing 12 changed files with 52 additions and 43 deletions.
4 changes: 4 additions & 0 deletions e2e/tools/validator/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -93,3 +93,7 @@ exclude_lines = [
markers = [
"focus", # used in development to mark focused tests
]

[tool.ruff]
line-length = 120

1 change: 0 additions & 1 deletion e2e/tools/validator/ruff.toml

This file was deleted.

19 changes: 8 additions & 11 deletions e2e/tools/validator/src/validator/cli/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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}")
Expand Down
1 change: 1 addition & 0 deletions e2e/tools/validator/src/validator/cli/options.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import datetime

import click
from prometheus_api_client.utils import parse_datetime

Expand Down
5 changes: 3 additions & 2 deletions e2e/tools/validator/src/validator/config/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"]
Expand Down
19 changes: 10 additions & 9 deletions e2e/tools/validator/src/validator/prometheus/__init__.py
Original file line number Diff line number Diff line change
@@ -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__)


Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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 []

Expand Down
7 changes: 4 additions & 3 deletions e2e/tools/validator/src/validator/specs/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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
Expand All @@ -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()
Expand Down
8 changes: 4 additions & 4 deletions e2e/tools/validator/src/validator/stresser/__init__.py
Original file line number Diff line number Diff line change
@@ -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__)

Expand Down Expand Up @@ -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")

Expand Down
13 changes: 7 additions & 6 deletions e2e/tools/validator/src/validator/validations/__init__.py
Original file line number Diff line number Diff line change
@@ -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)
Expand Down Expand Up @@ -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(
Expand All @@ -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
Expand Down
4 changes: 3 additions & 1 deletion e2e/tools/validator/tests/validator/config/test_config.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import os

import pytest

from validator.config import load


Expand All @@ -12,7 +14,7 @@ def write(content):
file.write(content)
return file_path

yield write
return write


@pytest.fixture
Expand Down
Original file line number Diff line number Diff line change
@@ -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,
)


Expand Down Expand Up @@ -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]


Expand Down
4 changes: 2 additions & 2 deletions e2e/tools/validator/tests/validator/stresser/test_stresser.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import pytest
from validator import stresser
from validator import config

from validator import config, stresser


@pytest.fixture
Expand Down

0 comments on commit ca43009

Please sign in to comment.