Skip to content

Commit

Permalink
grpc as optional
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelboulton committed Oct 29, 2023
1 parent bc37b31 commit d1496b7
Show file tree
Hide file tree
Showing 11 changed files with 39 additions and 18 deletions.
16 changes: 9 additions & 7 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,6 @@ dependencies = [
"python-box>=6,<7",
"requests>=2.22.0,<3",
"stevedore>=4,<5",

"grpcio",
"grpcio-reflection",
"grpcio-status",
"google-api-python-client",
"protobuf",
"proto-plus"
]

requires-python = ">=3.8"
Expand All @@ -61,6 +54,15 @@ Documentation = "https://tavern.readthedocs.io/en/latest/"
Source = "https://github.com/taverntesting/tavern"

[project.optional-dependencies]
grpc = [
"grpcio",
"grpcio-reflection",
"grpcio-status",
"google-api-python-client",
"protobuf",
"proto-plus",
]

dev = [
"Faker",
"allure-pytest",
Expand Down
2 changes: 2 additions & 0 deletions tavern/_core/plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@ def enabled(current_backend, ext):
)

for backend in test_block_config.backends():
logger.debug("loading backend for %s", backend)

namespace = "tavern_{}".format(backend)

manager = stevedore.EnabledExtensionManager(
Expand Down
12 changes: 9 additions & 3 deletions tavern/_core/pytest/config.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import copy
import dataclasses
import importlib
from typing import Any, List

from tavern._core.strict_util import StrictLevel
Expand Down Expand Up @@ -49,6 +50,11 @@ def with_strictness(self, new_strict: StrictLevel) -> "TestConfig":

@staticmethod
def backends() -> List[str]:
# TODO: This is here in case in future we want to be able to turn some of these
# on or off
return ["http", "mqtt", "grpc"]
available_backends = ["http"]

if importlib.util.find_spec("paho.mqtt"):
available_backends.append("mqtt")
if importlib.util.find_spec("grpc"):
available_backends.append("grpc")

return available_backends
2 changes: 1 addition & 1 deletion tavern/_core/pytest/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
import logging
from typing import Dict, Iterator, List, Mapping

import pytest
import yaml
from box import Box

import pytest
from tavern._core import exceptions
from tavern._core.dict_util import deep_dict_merge, format_keys, get_tavern_box
from tavern._core.extfunctions import get_wrapped_create_function, is_ext_function
Expand Down
2 changes: 1 addition & 1 deletion tavern/_core/pytest/hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
import re
from textwrap import dedent

import pytest
import yaml

import pytest
from tavern._core import exceptions

from .util import add_ini_options, add_parser_options, get_option_generic
Expand Down
2 changes: 1 addition & 1 deletion tavern/_core/pytest/item.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
from typing import Optional, Tuple

import attr
import pytest
import yaml
from _pytest._code.code import ExceptionInfo
from _pytest.nodes import Node

import pytest
from tavern._core import exceptions
from tavern._core.loader import error_on_empty_scalar
from tavern._core.plugins import load_plugins
Expand Down
1 change: 0 additions & 1 deletion tavern/_core/pytest/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
from typing import Any, Dict

import pytest

from tavern._core.dict_util import format_keys, get_tavern_box
from tavern._core.general import load_global_config
from tavern._core.pytest.config import TavernInternalConfig, TestConfig
Expand Down
4 changes: 3 additions & 1 deletion tavern/_core/schema/extensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import re
from typing import Union

from grpc import StatusCode
from pykwalify.types import is_bool, is_float, is_int

from tavern._core import exceptions
Expand Down Expand Up @@ -151,6 +150,9 @@ def validate_grpc_status_is_valid_or_list_of_names(value, rule_obj, path):

def is_grpc_status(value):
value = value.upper()

from grpc import StatusCode

for status in StatusCode:
if status.name == value:
return True
Expand Down
4 changes: 2 additions & 2 deletions tavern/_core/schema/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import logging
import os
import tempfile
from typing import Dict
from typing import Dict, Mapping

import pykwalify
import yaml
Expand Down Expand Up @@ -129,7 +129,7 @@ def wrapfile(to_wrap):
os.remove(wrapped_tmp.name)


def verify_tests(test_spec, with_plugins: bool = True) -> None:
def verify_tests(test_spec: Mapping, with_plugins: bool = True) -> None:
"""Verify that a specific test block is correct
Todo:
Expand Down
10 changes: 9 additions & 1 deletion tavern/_core/schema/jsonschema.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import importlib
import logging
import re
from typing import Mapping

import jsonschema
from jsonschema import Draft7Validator, ValidationError
from jsonschema.validators import extend

from tavern._core import exceptions
from tavern._core.dict_util import recurse_access_key
from tavern._core.exceptions import BadSchemaError
from tavern._core.loader import (
Expand Down Expand Up @@ -103,7 +106,7 @@ def oneOf(validator, oneOf, instance, schema):
)


def verify_jsonschema(to_verify, schema) -> None:
def verify_jsonschema(to_verify: Mapping, schema) -> None:
"""Verify a generic file against a given jsonschema
Args:
Expand All @@ -116,6 +119,11 @@ def verify_jsonschema(to_verify, schema) -> None:

validator = CustomValidator(schema)

if "grpc" in to_verify and not importlib.util.find_spec("grpc"):
raise exceptions.BadSchemaError(
"Tried to use grpc connection string, but grpc was not installed. Reinstall Tavern with the grpc extra like `pip install tavern[grpc]`"
)

try:
validator.validate(to_verify)
except jsonschema.ValidationError as e:
Expand Down
2 changes: 2 additions & 0 deletions tox-integration.ini
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ deps =
pytest-cov
colorlog
mqtt: fluent-logger
extras =
grpc: grpc
commands =
; docker compose stop
; docker compose build
Expand Down

0 comments on commit d1496b7

Please sign in to comment.