<commit_before>"""
Test base helpers.
"""

# Python3 support
from __future__ import print_function
from __future__ import unicode_literals

# Python std lib
import os
import sys
import unittest

# third party libs
try:
    import jinja2  # noqa
    HAS_JINJA = True
except ImportError:
    HAS_JINJA = False

try:
    import jtextfsm as textfsm  # noqa
    HAS_TEXTFSM = True
except ImportError:
    HAS_TEXTFSM = False

try:
    from lxml import etree as ET
    HAS_LXML = True
except ImportError:
    HAS_LXML = False

try:
    from netaddr.core import AddrFormatError
    HAS_NETADDR = True
except ImportError:
    HAS_NETADDR = False

# NAPALM base
import napalm_base.helpers
import napalm_base.exceptions
from napalm_base.base import NetworkDriver
from napalm_base.utils.string_parsers import convert_uptime_string_seconds


class TestBaseHelpers(unittest.TestCase):
    """Test helpers functions."""

    @classmethod
    def setUpClass(cls):
        cls.network_driver = FakeNetworkDriver()
        # neded when calling helpers

    def test_load_template(self):
        """
        Tests the helper function ```load_template```:

            * check if raises TemplateNotImplemented when trying to use inexisting template
            * check if can load empty template
            * check if raises TemplateRenderException when template is not correctly formatted
            * check if can load correct template
            * check if raises IOError if invalid path is specified
            * check if raises TemplateNotImplemented when trying to use inexisting template in
              custom path
            * check if can load correct template from custom path
            * check if template passed as string can be loaded
        """
        self.assertTrue(HAS_JINJA)  # firstly check if jinja2 is installed
        _NTP_PEERS_LIST = [
            '172.17.17.1',
            '172.17.17.2'
        ]
        _TEMPLATE_VARS = {
            'peers': _NTP_PEERS_LIST
        }

        self.assertRaises(napalm_base.exceptions.TemplateNotImplemented,
                          napalm_base.helpers.load_template,
                          self.network_driver,
                          '__this_template_does_not_exist__',
                          **_TEMPLATE_VARS)

        self.assertTrue(napalm_base.helpers.load_template(self.network_driver,
                                                          '__empty_template__',
                                                          **_TEMPLATE_VARS))

        self.assertRaises(napalm_base.exceptions.TemplateRenderException,
                          napalm_base.helpers.load_template,
                          self.network_driver,
                          '__completely_wrong_template__',
                          **_TEMPLATE_VARS)

        self.assertTrue(napalm_base.helpers.load_template(self.network_driver,
                                                          '__a_very_nice_template__',
                                                          **_TEMPLATE_VARS))

        self.assertRaises(IOError,
                          napalm_base.helpers.load_template,
                          self.network_driver,
                          '__a_very_nice_template__',
                          template_path='/this/path/does/not/exist',
                          **_TEMPLATE_VARS)

        install_dir = os.path.dirname(
            os.path.abspath(sys.modules[self.network_driver.__module__].__file__))
        custom_path = os.path.join(install_dir, '../custom/path/base')

        self.assertRaises(napalm_base.exceptions.TemplateNotImplemented,
                          napalm_base.helpers.load_template,
                          self.network_driver,
                          '__this_template_does_not_exist__',
                          template_path=custom_path,
                          **_TEMPLATE_VARS)

        self.assertTrue(napalm_base.helpers.load_template(self.network_driver,
                                                          '__a_very_nice_template__',
                                                          template_path=custom_path,
                                                          **_TEMPLATE_VARS))

        template_source = '{% for peer in peers %}ntp peer {{peer}}\n{% endfor %}'

        self.assertTrue(napalm_base.helpers.load_template(self.network_driver,
                                                          '_this_still_needs_a_name',
                                                          template_source=template_source,
                                                          **_TEMPLATE_VARS))
<commit_msg>Test search path building<commit_after>"""
Test base helpers.
"""

# Python3 support
from __future__ import print_function
from __future__ import unicode_literals

# Python std lib
import os
import sys
import unittest

# third party libs
try:
    import jinja2  # noqa
    HAS_JINJA = True
except ImportError:
    HAS_JINJA = False

try:
    import jtextfsm as textfsm  # noqa
    HAS_TEXTFSM = True
except ImportError:
    HAS_TEXTFSM = False

try:
    from lxml import etree as ET
    HAS_LXML = True
except ImportError:
    HAS_LXML = False

try:
    from netaddr.core import AddrFormatError
    HAS_NETADDR = True
except ImportError:
    HAS_NETADDR = False

# NAPALM base
import napalm_base.helpers
import napalm_base.exceptions
from napalm_base.base import NetworkDriver
from napalm_base.mock import MockDriver
from napalm_base.utils.string_parsers import convert_uptime_string_seconds


class TestBaseHelpers(unittest.TestCase):
    """Test helpers functions."""

    @classmethod
    def setUpClass(cls):
        cls.network_driver = FakeNetworkDriver()
        # neded when calling helpers

    def test_load_template(self):
        """
        Tests the helper function ```load_template```:

            * check if raises TemplateNotImplemented when trying to use inexisting template
            * check if can load empty template
            * check if raises TemplateRenderException when template is not correctly formatted
            * check if can load correct template
            * check if raises IOError if invalid path is specified
            * check if raises TemplateNotImplemented when trying to use inexisting template in
              custom path
            * check if can load correct template from custom path
            * check if template passed as string can be loaded
            * check that the search path setup by MRO is correct when loading an incorrecet template
        """
        class TestDriver(MockDriver):
            pass


        self.assertTrue(HAS_JINJA)  # firstly check if jinja2 is installed
        _NTP_PEERS_LIST = [
            '172.17.17.1',
            '172.17.17.2'
        ]
        _TEMPLATE_VARS = {
            'peers': _NTP_PEERS_LIST
        }

        self.assertRaises(napalm_base.exceptions.TemplateNotImplemented,
                          napalm_base.helpers.load_template,
                          self.network_driver,
                          '__this_template_does_not_exist__',
                          **_TEMPLATE_VARS)

        self.assertTrue(napalm_base.helpers.load_template(self.network_driver,
                                                          '__empty_template__',
                                                          **_TEMPLATE_VARS))

        self.assertRaises(napalm_base.exceptions.TemplateRenderException,
                          napalm_base.helpers.load_template,
                          self.network_driver,
                          '__completely_wrong_template__',
                          **_TEMPLATE_VARS)

        self.assertTrue(napalm_base.helpers.load_template(self.network_driver,
                                                          '__a_very_nice_template__',
                                                          **_TEMPLATE_VARS))

        self.assertRaises(IOError,
                          napalm_base.helpers.load_template,
                          self.network_driver,
                          '__a_very_nice_template__',
                          template_path='/this/path/does/not/exist',
                          **_TEMPLATE_VARS)

        install_dir = os.path.dirname(
            os.path.abspath(sys.modules[self.network_driver.__module__].__file__))
        custom_path = os.path.join(install_dir, '../custom/path/base')

        self.assertRaises(napalm_base.exceptions.TemplateNotImplemented,
                          napalm_base.helpers.load_template,
                          self.network_driver,
                          '__this_template_does_not_exist__',
                          template_path=custom_path,
                          **_TEMPLATE_VARS)

        self.assertTrue(napalm_base.helpers.load_template(self.network_driver,
                                                          '__a_very_nice_template__',
                                                          template_path=custom_path,
                                                          **_TEMPLATE_VARS))

        template_source = '{% for peer in peers %}ntp peer {{peer}}\n{% endfor %}'

        self.assertTrue(napalm_base.helpers.load_template(self.network_driver,
                                                          '_this_still_needs_a_name',
                                                          template_source=template_source,
                                                          **_TEMPLATE_VARS))
        self.assertRaisesRegexp(napalm_base.exceptions.TemplateNotImplemented,
                                "search path.*napalm-base/test/unit/templates',.*napalm-base/napalm_base/templates']",
                                napalm_base.helpers.load_template,
                                self.network_driver,
                                '__this_template_does_not_exist__',
                                **_TEMPLATE_VARS)


{'fraction_non_alphanumeric': 0.06084682691340642, 'fraction_numerical': 0.0038217841697676756, 'mean_word_length': 1.5530166880616174, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 4, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 40, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5869030', 'n_tokens_mistral': 2565, 'n_tokens_neox': 2441, 'n_words': 518}