From c48d67fa358857adac982d7c11a1fe01e1b3ea97 Mon Sep 17 00:00:00 2001
From: Erik Reid <erik.reid@geant.org>
Date: Sun, 3 Feb 2019 14:02:02 +0100
Subject: [PATCH] pull out netconf validation for testability

---
 inventory_provider/juniper.py | 31 ++++++++++++++++---------------
 1 file changed, 16 insertions(+), 15 deletions(-)

diff --git a/inventory_provider/juniper.py b/inventory_provider/juniper.py
index 9cd737b4..7d8f52d5 100644
--- a/inventory_provider/juniper.py
+++ b/inventory_provider/juniper.py
@@ -101,19 +101,8 @@ def _rpc(hostname, ssh):
     return dev.rpc
 
 
-def load_config(hostname, ssh_params):
-    """
-    loads netconf data from the router, validates and
-    returns as an lxml etree doc
-
-    :param hostname: router hostname
-    :param ssh_params: 'ssh' config element(cf. config.py:CONFIG_SCHEMA)
-    :return:
-    """
+def validate_netconf_config(config_doc):
     juniper_logger = logging.getLogger(JUNIPER_LOGGER_NAME)
-
-    juniper_logger.info("capturing netconf data for '%s'" % hostname)
-
     def _validate(schema, doc):
         if schema.validate(doc):
             return
@@ -124,16 +113,28 @@ def load_config(hostname, ssh_params):
     schema_doc = etree.XML(CONFIG_SCHEMA.encode('utf-8'))
     config_schema = etree.XMLSchema(schema_doc)
 
-    config = _rpc(hostname, ssh_params).get_config()
-    _validate(config_schema, config)
+    _validate(config_schema, config_doc)
 
     # validate interfaces/interface/unit elements ...
     schema_doc = etree.XML(UNIT_SCHEMA.encode('utf-8'))
     unit_schema = etree.XMLSchema(schema_doc)
-    for i in config.xpath('//configuration/interfaces/interface'):
+    for i in config_doc.xpath('//configuration/interfaces/interface'):
         for u in i.xpath('./unit'):
             _validate(unit_schema, u)
 
+def load_config(hostname, ssh_params):
+    """
+    loads netconf data from the router, validates and
+    returns as an lxml etree doc
+
+    :param hostname: router hostname
+    :param ssh_params: 'ssh' config element(cf. config.py:CONFIG_SCHEMA)
+    :return:
+    """
+    juniper_logger = logging.getLogger(JUNIPER_LOGGER_NAME)
+    juniper_logger.info("capturing netconf data for '%s'" % hostname)
+    config = _rpc(hostname, ssh_params).get_config()
+    validate_netconf_config(config)
     return config
 
 
-- 
GitLab