Skip to content

Commit

Permalink
Merge branch 'master' into 51-move_xxxNornirNutsContext.nornir_filter…
Browse files Browse the repository at this point in the history
…_up_to_base_class
  • Loading branch information
Lukas Murer-Jäckle committed Nov 29, 2021
2 parents 94ba6ca + a050444 commit 169bae8
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 3 deletions.
11 changes: 11 additions & 0 deletions nuts/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,19 @@ def general_result(self) -> AggregatedResult:

if nornir_filter:
selected_hosts = self.nornir.filter(nornir_filter)

else:
selected_hosts = self.nornir

if not selected_hosts.inventory.hosts:
if nornir_filter:
raise NutsSetupError(
f'Host(s) "{",".join(nornir_filter.filters["name__any"])}" '
f"not found in the inventory."
)
else:
raise NutsSetupError("No Hosts found, is the nornir inventory empty?")

overall_results = selected_hosts.run(
task=self.nuts_task(), **self.nuts_arguments()
)
Expand Down
8 changes: 6 additions & 2 deletions nuts/yamlloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,15 +80,19 @@ def find_module_path(module_path: Optional[str], class_name: str) -> str:
if not module_path:
raise NutsUsageError(
"A module that corresponds to the test_class "
f"called {class_name} could not be found."
f"called {class_name} could not be found. "
f"Did you forget to put it into the index (nuts/index.py)?"
)
return module_path


def load_module(module_path: str) -> types.ModuleType:
spec = util.find_spec(module_path)
if spec is None:
raise NutsUsageError(f"Module path called {module_path} not found.")
raise NutsUsageError(
f"Module path called {module_path} not found. "
f"Did you spell the module and class name correctly?"
)
module = util.module_from_spec(spec)
# https://github.com/python/typeshed/issues/2793
assert isinstance(spec.loader, importlib.abc.Loader)
Expand Down
68 changes: 67 additions & 1 deletion tests/test_class_loading.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
from nuts import index
from tests.utils import YAML_EXTENSION

test_index = {"TestFixture": "tests.base_tests.class_loading"}
test_index = {
"TestFixture": "tests.base_tests.class_loading",
"TestFixtureNotExistingClass": "tests.base_tests.class_loading_with_typo",
}


@pytest.fixture
Expand Down Expand Up @@ -137,3 +140,66 @@ def test_find_test_module_of_class(mock_index):
path = index.find_test_module_of_class("TestFixture")
expected = "tests.base_tests.class_loading"
assert path == expected


def test_load_nonexisting_module(pytester):
arguments = {
"test_class_loading": """
---
- test_module: tests.base_tests.class_loading_with_typo
test_class: TestClass
test_data: []
"""
}
pytester.makefile(YAML_EXTENSION, **arguments)

result = pytester.runpytest()
result.stdout.fnmatch_lines(
[
"*nuts.helpers.errors.NutsUsageError: Module path called "
"tests.base_tests.class_loading_with_typo not found.*",
]
)
result.assert_outcomes(errors=1)


def test_load_nonexisting_index_key(pytester, mock_index):
arguments = {
"test_index_loading_class_not_found": """
---
- test_class: TestFixtureWithTypo
test_data: ['test1', 'test2']
"""
}

pytester.makefile(YAML_EXTENSION, **arguments)

result = pytester.runpytest()
result.stdout.fnmatch_lines(
[
"*nuts.helpers.errors.NutsUsageError: A module that corresponds to the "
"test_class called TestFixtureWithTypo could not be found.*",
]
)
result.assert_outcomes(errors=1)


def test_load_nonexisting_class_from_index(pytester, mock_index):
arguments = {
"test_index_loading_class_not_found": """
---
- test_class: TestFixtureNotExistingClass
test_data: ['test1', 'test2']
"""
}

pytester.makefile(YAML_EXTENSION, **arguments)

result = pytester.runpytest()
result.stdout.fnmatch_lines(
[
"*nuts.helpers.errors.NutsUsageError: Module path called "
"tests.base_tests.class_loading_with_typo not found.*",
]
)
result.assert_outcomes(errors=1)
43 changes: 43 additions & 0 deletions tests/test_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,3 +235,46 @@ def test_has_removed_l2(self, nuts_ctx):
"test_class_loading.yaml", "--nornir-config", "other-nr-config.yaml"
)
result.assert_outcomes(passed=4)

def test_wrong_host_in_test(self, pytester):
pytester.makepyfile(
basic_task="""
from nuts.context import NornirNutsContext
from nuts.helpers.errors import NutsSetupError
from nornir.core.filter import F
from nuts.helpers.filters import filter_hosts
import pytest
class CustomNornirNutsContext(NornirNutsContext):
def nuts_task(self):
return lambda task: task.host.name
def nornir_filter(self) -> F:
return filter_hosts(self.nuts_parameters["test_data"])
CONTEXT = CustomNornirNutsContext
class TestBasicTask:
@pytest.mark.xfail(raises=NutsSetupError)
def test_basic_task(self, nuts_ctx):
pass
"""
)
arguments = {
"test_class_loading": """
---
- test_module: basic_task
test_class: TestBasicTask
test_data:
- host: S1
- host: S2
"""
}
pytester.makefile(YAML_EXTENSION, **arguments)
result = pytester.runpytest("test_class_loading.yaml")
result.assert_outcomes(xpassed=1)

0 comments on commit 169bae8

Please sign in to comment.