Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

compat. with old Python libs #59

Merged
merged 4 commits into from
Oct 13, 2023
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
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,17 @@

### Fixed

* Add compatibility with [requests](https://requests.readthedocs.io)
version 2.25 and higher.

### Misc

* Improve test coverage by running an HTTP server to fake the Patroni API (#55
by @dlax).
* Work around old pytest versions in type annotations in the test suite.
* Declare compatibility with click version 7.1 (or higher).
* In tests, work around nagiosplugin 1.3.2 not properly handling stdout
redirection.

## check_patroni 1.0.0 - 2023-08-28

Expand Down
3 changes: 2 additions & 1 deletion check_patroni/types.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import json
from typing import Any, Callable, List, Optional, Tuple, Union
from urllib.parse import urlparse

Expand Down Expand Up @@ -71,7 +72,7 @@ def rest_api(self, service: str) -> Any:

try:
return r.json()
except requests.exceptions.JSONDecodeError:
except (json.JSONDecodeError, ValueError):
return None
raise nagiosplugin.CheckError("Connection failed for all provided endpoints")

Expand Down
5 changes: 3 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,12 @@ def get_version() -> str:
"attrs >= 17, !=21.1",
"requests",
"nagiosplugin >= 1.3.2",
"click >= 8.0.1",
"click >= 7.1",
],
extras_require={
"test": [
"pytest",
"importlib_metadata; python_version < '3.8'",
"pytest >= 6.0.2",
],
},
entry_points={
Expand Down
38 changes: 36 additions & 2 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,46 @@
import logging
import sys
from pathlib import Path
from threading import Thread
from typing import Any, Iterator
from typing import Any, Iterator, Tuple
from unittest.mock import patch

if sys.version_info >= (3, 8):
from importlib.metadata import version as metadata_version
else:
from importlib_metadata import version as metadata_version

import pytest
from click.testing import CliRunner

from . import PatroniAPI

logger = logging.getLogger(__name__)


def numversion(pkgname: str) -> Tuple[int, ...]:
version = metadata_version(pkgname)
return tuple(int(v) for v in version.split(".", 3))


if numversion("pytest") >= (6, 2):
TempPathFactory = pytest.TempPathFactory
else:
from _pytest.tmpdir import TempPathFactory


@pytest.fixture(scope="session", autouse=True)
def nagioplugin_runtime_stdout() -> Iterator[None]:
# work around https://github.com/mpounsett/nagiosplugin/issues/24 when
# nagiosplugin is older than 1.3.3
if numversion("nagiosplugin") < (1, 3, 3):
target = "nagiosplugin.runtime.Runtime.stdout"
with patch(target, None):
logger.warning("patching %r", target)
yield None
else:
yield None


@pytest.fixture(
params=[False, True],
Expand All @@ -23,7 +57,7 @@ def datadir() -> Path:

@pytest.fixture(scope="session")
def patroni_api(
tmp_path_factory: pytest.TempPathFactory, datadir: Path
tmp_path_factory: TempPathFactory, datadir: Path
) -> Iterator[PatroniAPI]:
"""A fake HTTP server for the Patroni API serving files from a temporary
directory.
Expand Down
Loading