Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pass ssh_config only if file exists #538

Merged
merged 2 commits into from
May 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions nornir/plugins/connections/netconf.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from pathlib import Path
from typing import Any, Dict, Optional

from ncclient import manager
Expand Down Expand Up @@ -108,9 +109,10 @@ def open(

if "ssh_config" not in extras:
try:
parameters["ssh_config"] = configuration.ssh.config_file # type: ignore
ssh_config_file = Path(configuration.ssh.config_file) # type: ignore
if ssh_config_file.exists():
parameters["ssh_config"] = ssh_config_file
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this can perhaps become a bit confusing if a user has made a typo in the name of the config file, that way the supplied file wouldn't exist and the user wouldn't be notified about that problem. But I'm also fine with this small fix for Nornir 2.0 and then I can change it for 3.0.

except AttributeError:
parameters["ssh_config"] = None
pass

parameters.update(extras)
Expand Down
6 changes: 6 additions & 0 deletions tests/inventory_data/netconf_hosts.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,9 @@ netconf1.no_group:
hostkey_verify: False
look_for_keys: False
ssh_config: null

netconf2.no_group:
hostname: localhost
username: netconf
password: netconf
port: 65025
3 changes: 3 additions & 0 deletions tests/plugins/tasks/networking/test_netconf_capabilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@


def test_netconf_capabilities(netconf):
netconf = netconf.filter(name="netconf1.no_group")
assert netconf.inventory.hosts

result = netconf.run(networking.netconf_capabilities)

for _, v in result.items():
Expand Down
25 changes: 25 additions & 0 deletions tests/plugins/tasks/networking/test_netconf_connection.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from nornir.core.task import Result, Task
from nornir.plugins.tasks import networking


def get_task(task: Task, path: str = "", filter_type: str = "xpath") -> Result:
manager = task.host.get_connection("netconf", task.nornir.config)
params = {}
if path:
params["filter"] = (filter_type, path)
result = manager.get(**params)

return Result(host=task.host, result=result.data_xml)


class Test:
def test_netconf_connection_non_existent_ssh_config(self, netconf):
netconf = netconf.filter(name="netconf2.no_group")
assert netconf.inventory.hosts

netconf.config.ssh.config_file = "i dont exist"
result = netconf.run(networking.netconf_capabilities)
assert result

for _, v in result.items():
assert not isinstance(v.exception, FileNotFoundError)
3 changes: 3 additions & 0 deletions tests/plugins/tasks/networking/test_netconf_edit_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@


def test_netconf_edit_config(netconf):
netconf = netconf.filter(name="netconf1.no_group")
assert netconf.inventory.hosts

result = netconf.run(networking.netconf_get_config)

for _, v in result.items():
Expand Down
6 changes: 6 additions & 0 deletions tests/plugins/tasks/networking/test_netconf_get.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,19 @@


def test_netconf_get(netconf):
netconf = netconf.filter(name="netconf1.no_group")
assert netconf.inventory.hosts

result = netconf.run(networking.netconf_get)

for _, v in result.items():
assert "<turing-machine" in v.result


def test_netconf_get_subtree(netconf):
netconf = netconf.filter(name="netconf1.no_group")
assert netconf.inventory.hosts

result = netconf.run(
networking.netconf_get,
path="<turing-machine></turing-machine>",
Expand Down
6 changes: 6 additions & 0 deletions tests/plugins/tasks/networking/test_netconf_get_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,19 @@


def test_netconf_get_config(netconf):
netconf = netconf.filter(name="netconf1.no_group")
assert netconf.inventory.hosts

result = netconf.run(networking.netconf_get_config, source="startup")

for _, v in result.items():
assert "<turing-machine" in v.result


def test_netconf_get_config_subtree(netconf):
netconf = netconf.filter(name="netconf1.no_group")
assert netconf.inventory.hosts

result = netconf.run(
networking.netconf_get_config,
source="startup",
Expand Down