Skip to content

Commit

Permalink
pass ssh_config only if file exists (#538)
Browse files Browse the repository at this point in the history
* pass ssh_config only if file exists

* added test that verifies non-existent files don't break
  • Loading branch information
dbarrosop authored May 13, 2020
1 parent 62fdd37 commit 30cd14c
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 2 deletions.
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
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

0 comments on commit 30cd14c

Please sign in to comment.