From 0986f9fa1872f478764478a12f4d57803d94f201 Mon Sep 17 00:00:00 2001 From: Michael Boulton Date: Sat, 6 Jan 2024 15:24:48 +0000 Subject: [PATCH 1/8] mqtt as optional extra --- pyproject.toml | 4 +++- tavern/_core/plugins.py | 8 +++++++- tests/conftest.py | 11 ++++++----- tox-integration.ini | 2 ++ tox.ini | 1 + 5 files changed, 19 insertions(+), 7 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 5488b6e01..f426ce8bc 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -27,7 +27,6 @@ dependencies = [ "PyYAML>=6.0.1,<7", "jmespath>=1,<2", "jsonschema>=4,<5", - "paho-mqtt>=1.3.1,<=1.6.1", "pyjwt>=2.5.0,<3", "pykwalify>=1.8.0,<2", "pytest>=7,<7.3", @@ -90,6 +89,9 @@ dev = [ # "tbump@https://github.com/michaelboulton/tbump/archive/714ba8957a3c84b625608ceca39811ebe56229dc.zip", ] +mqtt = [ + "paho-mqtt>=1.3.1,<=1.6.1", +] [project.scripts] diff --git a/tavern/_core/plugins.py b/tavern/_core/plugins.py index 5120de482..29e8fe261 100644 --- a/tavern/_core/plugins.py +++ b/tavern/_core/plugins.py @@ -4,6 +4,7 @@ significantly if/when a proper plugin system is implemented! """ import dataclasses +import importlib.util import logging from functools import partial from typing import Any, List, Mapping, Optional @@ -105,7 +106,12 @@ def enabled(current_backend, ext): ext.name == test_block_config.tavern_internal.backends[current_backend] ) - for backend in ["http", "mqtt"]: + backends = ["http"] + + if importlib.util.find_spec("paho.mqtt") is not None: + backends.append("mqtt") + + for backend in backends: namespace = "tavern_{}".format(backend) manager = stevedore.EnabledExtensionManager( diff --git a/tests/conftest.py b/tests/conftest.py index a770e583b..a0c6ea2a0 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -6,7 +6,8 @@ import yaml import tavern -import tavern._plugins.mqtt.tavernhook as mqtt_plugin + +# import tavern._plugins.mqtt.tavernhook as mqtt_plugin from tavern._plugins.rest.tavernhook import TavernRestPlugin as rest_plugin @@ -28,8 +29,8 @@ def extension(name, point): "requests", rest_plugin, ), - extension( - "paho-mqtt", - mqtt_plugin, - ), + # extension( + # "paho-mqtt", + # mqtt_plugin, + # ), ] diff --git a/tox-integration.ini b/tox-integration.ini index e4eeea814..d06eae8d7 100644 --- a/tox-integration.ini +++ b/tox-integration.ini @@ -6,6 +6,8 @@ isolated_build = True [testenv] allowlist_externals = docker +extras = + mqtt: mqtt basepython = python3.11 passenv = DOCKER_TLS_VERIFY,DOCKER_HOST,DOCKER_CERT_PATH,DOCKER_BUILDKIT setenv = diff --git a/tox.ini b/tox.ini index 17e0c0bdd..e5c328511 100644 --- a/tox.ini +++ b/tox.ini @@ -11,6 +11,7 @@ allowlist_externals = install_command = python -m pip install {opts} {packages} -c constraints.txt extras = dev + mqtt commands = {envbindir}/python -m pytest --cov-report term-missing --cov tavern {posargs} From 74c5b73d5213960dbf7be6856436e0e6e990252d Mon Sep 17 00:00:00 2001 From: Michael Boulton Date: Sat, 6 Jan 2024 15:29:02 +0000 Subject: [PATCH 2/8] Clean error messages --- tavern/_core/plugins.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tavern/_core/plugins.py b/tavern/_core/plugins.py index 29e8fe261..5b76e973e 100644 --- a/tavern/_core/plugins.py +++ b/tavern/_core/plugins.py @@ -199,11 +199,11 @@ def get_request_type( keys[p.plugin.request_block_name] = p.plugin.request_type if len(set(keys) & set(stage)) > 1: - logger.error("Can only specify 1 request type") - raise exceptions.DuplicateKeysError + raise exceptions.DuplicateKeysError( + f"Can only specify 1 request type but got {set(keys)}" + ) elif not list(set(keys) & set(stage)): - logger.error("Need to specify one of '%s'", keys.keys()) - raise exceptions.MissingKeysError + raise exceptions.MissingKeysError(f"Need to specify one of '{keys.keys()}'") # We've validated that 1 and only 1 is there, so just loop until the first # one is found From c92a8ec216083b19791163c51a8f375497c6bdbb Mon Sep 17 00:00:00 2001 From: Michael Boulton Date: Sat, 6 Jan 2024 15:31:32 +0000 Subject: [PATCH 3/8] Clean error messages --- tavern/_core/plugins.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tavern/_core/plugins.py b/tavern/_core/plugins.py index 5b76e973e..4b1d34c75 100644 --- a/tavern/_core/plugins.py +++ b/tavern/_core/plugins.py @@ -203,7 +203,9 @@ def get_request_type( f"Can only specify 1 request type but got {set(keys)}" ) elif not list(set(keys) & set(stage)): - raise exceptions.MissingKeysError(f"Need to specify one of '{keys.keys()}'") + raise exceptions.MissingKeysError( + f"Need to specify one of '{set(keys.keys())}'" + ) # We've validated that 1 and only 1 is there, so just loop until the first # one is found From b656ceccaa525d1b105d65f677bfba0c9e832d2d Mon Sep 17 00:00:00 2001 From: Michael Boulton Date: Sat, 6 Jan 2024 15:32:12 +0000 Subject: [PATCH 4/8] Re-add mqtt plugin to unit tests --- tests/conftest.py | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/tests/conftest.py b/tests/conftest.py index a0c6ea2a0..a770e583b 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -6,8 +6,7 @@ import yaml import tavern - -# import tavern._plugins.mqtt.tavernhook as mqtt_plugin +import tavern._plugins.mqtt.tavernhook as mqtt_plugin from tavern._plugins.rest.tavernhook import TavernRestPlugin as rest_plugin @@ -29,8 +28,8 @@ def extension(name, point): "requests", rest_plugin, ), - # extension( - # "paho-mqtt", - # mqtt_plugin, - # ), + extension( + "paho-mqtt", + mqtt_plugin, + ), ] From 680247080836c2a122a0dc1304e81366167437d9 Mon Sep 17 00:00:00 2001 From: Michael Boulton Date: Sat, 6 Jan 2024 15:32:47 +0000 Subject: [PATCH 5/8] Give error message a better message --- tavern/_core/plugins.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tavern/_core/plugins.py b/tavern/_core/plugins.py index 4b1d34c75..27d0f18b8 100644 --- a/tavern/_core/plugins.py +++ b/tavern/_core/plugins.py @@ -204,7 +204,7 @@ def get_request_type( ) elif not list(set(keys) & set(stage)): raise exceptions.MissingKeysError( - f"Need to specify one of '{set(keys.keys())}'" + f"Need to specify one of valid request types: '{set(keys.keys())}'" ) # We've validated that 1 and only 1 is there, so just loop until the first From 84a3ca3fe7f64c67956366a31cfeae204967a60f Mon Sep 17 00:00:00 2001 From: Michael Boulton Date: Sat, 6 Jan 2024 15:39:03 +0000 Subject: [PATCH 6/8] Only add mqtt plugin if imported --- tests/conftest.py | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/tests/conftest.py b/tests/conftest.py index a770e583b..e9a02cc37 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -6,7 +6,6 @@ import yaml import tavern -import tavern._plugins.mqtt.tavernhook as mqtt_plugin from tavern._plugins.rest.tavernhook import TavernRestPlugin as rest_plugin @@ -23,13 +22,23 @@ def set_plugins(): def extension(name, point): return stevedore.extension.Extension(name, point, point, point) - tavern._core.plugins.load_plugins.plugins = [ + plugins = [ extension( "requests", rest_plugin, ), - extension( - "paho-mqtt", - mqtt_plugin, - ), ] + + try: + import tavern._plugins.mqtt.tavernhook as mqtt_plugin + except ImportError: + pass + else: + plugins.append( + extension( + "paho-mqtt", + mqtt_plugin, + ) + ) + + tavern._core.plugins.load_plugins.plugins = plugins From 1b613417c6269c3f0bcf75b6ea72a1398dfe8437 Mon Sep 17 00:00:00 2001 From: Michael Boulton Date: Sat, 6 Jan 2024 15:44:04 +0000 Subject: [PATCH 7/8] catch error --- tavern/_core/plugins.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/tavern/_core/plugins.py b/tavern/_core/plugins.py index 27d0f18b8..680385508 100644 --- a/tavern/_core/plugins.py +++ b/tavern/_core/plugins.py @@ -108,7 +108,11 @@ def enabled(current_backend, ext): backends = ["http"] - if importlib.util.find_spec("paho.mqtt") is not None: + try: + importlib.util.find_spec("paho.mqtt") + except ModuleNotFoundError: + pass + else: backends.append("mqtt") for backend in backends: From 6b96eeadb37bcdbced0401047f4c48d81b147106 Mon Sep 17 00:00:00 2001 From: Michael Boulton Date: Sat, 20 Jan 2024 13:11:20 +0000 Subject: [PATCH 8/8] Fix duplicate section --- tox-integration.ini | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tox-integration.ini b/tox-integration.ini index fc9b493fc..4d5c18577 100644 --- a/tox-integration.ini +++ b/tox-integration.ini @@ -6,8 +6,6 @@ isolated_build = True [testenv] allowlist_externals = docker -extras = - mqtt: mqtt basepython = python3.11 passenv = DOCKER_TLS_VERIFY,DOCKER_HOST,DOCKER_CERT_PATH,DOCKER_BUILDKIT setenv = @@ -32,6 +30,7 @@ deps = colorlog mqtt: fluent-logger extras = + mqtt: mqtt grpc: grpc commands = ; docker compose stop