diff --git a/CHANGELOG.md b/CHANGELOG.md index c0f3111..9370ab3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/check_patroni/types.py b/check_patroni/types.py index 096f31b..3032547 100644 --- a/check_patroni/types.py +++ b/check_patroni/types.py @@ -1,3 +1,4 @@ +import json from typing import Any, Callable, List, Optional, Tuple, Union from urllib.parse import urlparse @@ -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") diff --git a/setup.py b/setup.py index 1df70a2..31825ed 100644 --- a/setup.py +++ b/setup.py @@ -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={ diff --git a/tests/conftest.py b/tests/conftest.py index f98d47a..3a071c0 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -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], @@ -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.