Replies: 1 comment 1 reply
-
Not sure if I understand your specific use case. Inside a Custom bundle implementation import pytest
import requests
from typing import Dict, Callable, Any
from nornir.core.task import Task, MultiResult, Result
from nuts.context import NornirNutsContext
from nuts.helpers.result import (
AbstractHostResultExtractor,
NutsResult,
)
from nuts.helpers.errors import Error
EXAMPLE = """
- test_module: demo.nb
test_class: TestNBDevice
test_data:
- host: demo1
model: C9200-48P
"""
class NBTaskError(Error):
"""My Netbox task failed"""
class NBExtractor(AbstractHostResultExtractor):
def single_transform(self, single_result: MultiResult) -> Dict[str, Dict[str, Any]]:
cli_result = self._simple_extract(single_result)
return cli_result
class NBContext(NornirNutsContext):
"""
- test_module: demo.nb
test_class: TestNBDevice
test_data:
- host: <host>
model: <model>
"""
def nuts_task(self) -> Callable[..., Result]:
return self.query_nb
def query_nb(self, task: Task, **kwargs: Any) -> Result:
""" """
netbox_name = task.host.get("nb_name")
device_data = requests.get(
f"https://demo.netbox.dev/api/dcim/devices/?name={netbox_name}",
headers={"Authorization": "Token b4a...............45"},
)
device_data.raise_for_status()
return Result(host=task.host, result=device_data.json())
def nuts_extractor(self) -> NBExtractor:
return NBExtractor(self)
CONTEXT = NBContext
class TestNBDevice:
@pytest.mark.nuts("model")
def test_nb_device(self, single_result: NutsResult, model: str) -> None:
nb_model = (
single_result.result.get("results", [])[0]
.get("device_type", {})
.get("model")
)
assert (
model == nb_model
), f"Model was expected to be {model}, but it was {nb_model}!" Test yaml file - test_module: demo.nb
test_class: TestNBDevice
test_data:
- host: demo1
model: C9200-48P Inventory: demo1:
hostname: demo.netbox.dev
data:
nb_name: C9200-SW1 |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
First of all, thanks for putting this together. This is a great project with the right intentions for network engineers wanting to perform validations against their network.
What I was wondering was if there was any way to pass a host specific var into a test? For example for reusability, I was planning to have my inventory have generic hostnames, core_switch1, user_switch1, etc but with hostname that has the FQDN of the device.
For one of my tests, I am trying to lookup Netbox whether an entry exists for an interface IP and as hostname is not specific, I prefer to use the hostname or even any data/var that's specified under the host if that makes sense.
Example inventory file
Beta Was this translation helpful? Give feedback.
All reactions