Exemplo n.º 1
0
    def setUp(self):
        self.fl = FactoryLoader()
        self.fl.catalog = {
            'RouteTable': {
                'item': 'route-table/rt',
                'rpc': 'get-route-information',
                'args_key': 'destination',
                'key': 'rt-destination',
                'view': 'RouteTableView'
            }
        }

        self.fl._catalog_dict = {
            'RouteTableView': {
                'groups': {
                    'entry': 'rt-entry'
                },
                'fields_entry': {
                    'via': 'nh/via | nh/nh-local-interface',
                    'age': {
                        'age/@seconds': 'int'
                    },
                    'nexthop': 'nh/to',
                    'protocol': 'protocol-name'
                },
                'extends': 'test'
            }
        }
Exemplo n.º 2
0
class TestFactoryLoader(unittest.TestCase):
    def setUp(self):
        self.fl = FactoryLoader()
        self.fl.catalog = {
            'RouteTable': {
                'item': 'route-table/rt',
                'rpc': 'get-route-information',
                'args_key': 'destination',
                'key': 'rt-destination',
                'view': 'RouteTableView'
            }
        }

        self.fl._catalog_dict = {
            'RouteTableView': {
                'groups': {
                    'entry': 'rt-entry'
                },
                'fields_entry': {
                    'via': 'nh/via | nh/nh-local-interface',
                    'age': {
                        'age/@seconds': 'int'
                    },
                    'nexthop': 'nh/to',
                    'protocol': 'protocol-name'
                },
                'extends': 'test'
            }
        }

    def test_FactoryLoader__build_optable(self):
        self.assertEqual(self.fl._build_optable('RouteTable'),
                         self.fl.catalog['RouteTable'])

    def test_FactoryLoader__build_cfgtable(self):
        self.assertEqual(self.fl._build_cfgtable('RouteTable'),
                         self.fl.catalog['RouteTable'])

    @patch('jnpr.junos.factory.factory_loader._VIEW')
    def test_FactoryLoader__build_view(self, mock_view):
        mock_view.return_value = type('test', (object, ), {})
        self.assertEqual(self.fl._build_view('RouteTableView'),
                         self.fl.catalog['RouteTableView'])

    def test_FactoryLoader__fieldfunc_False(self):
        fn = self.fl._fieldfunc_False('test')
        self.assertFalse(fn('test'))

    def test_FactoryLoader__add_dictfield_RuntimeError(self):
        self.assertRaises(RuntimeError, self.fl._add_dictfield, 'testing',
                          'age', {'age/@seconds': 'test'}, {})

    def test_FactoryLoader__add_dictfield_ValueError(self):
        self.assertRaises(ValueError, self.fl._add_dictfield, 'testing', 'age',
                          {'age/@seconds': 'test=test'}, {})
Exemplo n.º 3
0
 def load_junos_view(self, view_path):
     try:
         with open(view_path) as f:
             tmp_yaml = f.read()
         yaml_str = re.sub(r"unicode", "str", tmp_yaml)
         globals().update(FactoryLoader().load(yaml.safe_load(yaml_str)))
     except:
         pass
Exemplo n.º 4
0
class TestFactoryLoader(unittest.TestCase):

    def setUp(self):
        self.fl = FactoryLoader()
        self.fl.catalog = {'RouteTable':
                           {'item': 'route-table/rt',
                            'rpc': 'get-route-information',
                            'args_key': 'destination',
                            'key': 'rt-destination',
                            'view': 'RouteTableView'}}

        self.fl._catalog_dict = {'RouteTableView': {'groups': {'entry': 'rt-entry'},
                                                    'fields_entry': {'via': 'nh/via | nh/nh-local-interface',
                                                                     'age': {'age/@seconds': 'int'},
                                                                     'nexthop': 'nh/to',
                                                                     'protocol': 'protocol-name'},
                                                    'extends': 'test'}}

    def test_FactoryLoader__build_optable(self):
        self.assertEqual(self.fl._build_optable('RouteTable'),
                         self.fl.catalog['RouteTable'])

    def test_FactoryLoader__build_cfgtable(self):
        self.assertEqual(self.fl._build_cfgtable('RouteTable'),
                         self.fl.catalog['RouteTable'])

    @patch('jnpr.junos.factory.factory_loader._VIEW')
    def test_FactoryLoader__build_view(self, mock_view):
        mock_view.return_value = type('test', (object,), {})
        self.assertEqual(self.fl._build_view('RouteTableView'),
                         self.fl.catalog['RouteTableView'])

    def test_FactoryLoader__fieldfunc_False(self):
        fn = self.fl._fieldfunc_False('test')
        self.assertFalse(fn('test'))

    def test_FactoryLoader__add_dictfield_RuntimeError(self):
        self.assertRaises(
            RuntimeError, self.fl._add_dictfield, 'testing', 'age', {
                'age/@seconds': 'test'}, {})

    def test_FactoryLoader__add_dictfield_ValueError(self):
        self.assertRaises(
            ValueError, self.fl._add_dictfield, 'testing', 'age', {
                'age/@seconds': 'test=test'}, {})
Exemplo n.º 5
0
    def setUp(self):
        self.fl = FactoryLoader()
        self.fl.catalog = {'RouteTable':
                           {'item': 'route-table/rt',
                            'rpc': 'get-route-information',
                            'args_key': 'destination',
                            'key': 'rt-destination',
                            'view': 'RouteTableView'}}

        self.fl._catalog_dict = {'RouteTableView': {'groups': {'entry': 'rt-entry'},
                                                    'fields_entry': {'via': 'nh/via | nh/nh-local-interface',
                                                                     'age': {'age/@seconds': 'int'},
                                                                     'nexthop': 'nh/to',
                                                                     'protocol': 'protocol-name'},
                                                    'extends': 'test'}}
Exemplo n.º 6
0
    def setUp(self):
        self.fl = FactoryLoader()
        self.fl.catalog = {
            "RouteTable": {
                "item": "route-table/rt",
                "rpc": "get-route-information",
                "args_key": "destination",
                "key": "rt-destination",
                "view": "RouteTableView",
            }
        }

        self.fl._catalog_dict = {
            "RouteTableView": {
                "groups": {"entry": "rt-entry"},
                "fields_entry": {
                    "via": "nh/via | nh/nh-local-interface",
                    "age": {"age/@seconds": "int"},
                    "nexthop": "nh/to",
                    "protocol": "protocol-name",
                },
                "extends": "test",
            }
        }
Exemplo n.º 7
0
  view: RouteTableView

RouteTableView:
  groups:
    entry: rt-entry
  fields_entry:
    # fields taken from the group 'entry'
    protocol: protocol-name
    via: nh/via | nh/nh-local-interface
    age: { age/@seconds : int }
    nexthop: nh/to
    preference: preference
    metric: metric | med
"""

globals().update(FactoryLoader().load(yaml.load(rt_yml,
                                                Loader=yaml.FullLoader)))


class PyEZNode(GNS3Node):
    def __init__(self, node_id, port, name, config=None):

        super().__init__(node_id, port, name)
        self.pyez_connection = None
        self.initial_config = os.path.abspath(config)
        self.detailed_ospf = False

        self.netmiko_node['device_type'] = 'juniper_junos_telnet'
        self.netmiko_node['username'] = '******'
        self.netmiko_node['password'] = '******'
        self.netmiko_node['default_enter'] = '\r\n'
 def __init_(self):
     FactoryLoader.__init__(self)
     self.__catalog_dict = {}
Exemplo n.º 9
0
def _loadyaml_bypass(yaml_str):
    """Bypass Juniper's loadyaml and directly call FactoryLoader"""
    return FactoryLoader().load(yaml.load(yaml_str))
Exemplo n.º 10
0
def _loadyaml_bypass(yaml_file):
    """Bypass Juniper's loadyaml to utilize yaml.safe_load, thereby avoiding potential
    unintended code execution.
    """
    with open(yaml_file) as handle:
        return FactoryLoader().load(yaml.safe_load(handle.read()))
Exemplo n.º 11
0
class TestFactoryLoader(unittest.TestCase):
    def setUp(self):
        self.fl = FactoryLoader()
        self.fl.catalog = {
            "RouteTable": {
                "item": "route-table/rt",
                "rpc": "get-route-information",
                "args_key": "destination",
                "key": "rt-destination",
                "view": "RouteTableView",
            }
        }

        self.fl._catalog_dict = {
            "RouteTableView": {
                "groups": {"entry": "rt-entry"},
                "fields_entry": {
                    "via": "nh/via | nh/nh-local-interface",
                    "age": {"age/@seconds": "int"},
                    "nexthop": "nh/to",
                    "protocol": "protocol-name",
                },
                "extends": "test",
            }
        }

    def test_FactoryLoader__build_optable(self):
        self.assertEqual(
            self.fl._build_optable("RouteTable"), self.fl.catalog["RouteTable"]
        )

    def test_FactoryLoader__build_cfgtable(self):
        self.assertEqual(
            self.fl._build_cfgtable("RouteTable"), self.fl.catalog["RouteTable"]
        )

    @patch("jnpr.junos.factory.factory_loader._VIEW")
    def test_FactoryLoader__build_view(self, mock_view):
        mock_view.return_value = type("test", (object,), {})
        self.assertEqual(
            self.fl._build_view("RouteTableView"), self.fl.catalog["RouteTableView"]
        )

    def test_FactoryLoader__fieldfunc_True(self):
        fn = self.fl._fieldfunc_True("test")
        self.assertTrue(fn("test"))

    def test_FactoryLoader__fieldfunc_True_fail(self):
        # Assert that false is returned if value not found
        fn = self.fl._fieldfunc_True("test")
        self.assertFalse(fn("nope"))

    def test_FactoryLoader__fieldfunc_True_regex(self):
        fn = self.fl._fieldfunc_True("regex(this|test)")
        self.assertTrue(fn("test"))

    def test_FactoryLoader__fieldfunc_True_regex_fail(self):
        # Assert that false is returned if value not found
        fn = self.fl._fieldfunc_True("regex(this|test)")
        self.assertFalse(fn("nope"))

    def test_FactoryLoader__fieldfunc_False(self):
        fn = self.fl._fieldfunc_False("test")
        self.assertFalse(fn("test"))

    def test_FactoryLoader__fieldfunc_False_fail(self):
        # Assert that true is returned if value not found
        fn = self.fl._fieldfunc_False("test")
        self.assertTrue(fn("nope"))

    def test_FactoryLoader__fieldfunc_False_regex(self):
        fn = self.fl._fieldfunc_False("regex(this|test)")
        self.assertFalse(fn("test"))

    def test_FactoryLoader__fieldfunc_False_regex_fail(self):
        # Assert that true is returned if value not found
        fn = self.fl._fieldfunc_False("regex(this|test)")
        self.assertTrue(fn("nope"))

    def test_FactoryLoader__add_dictfield_RuntimeError(self):
        self.assertRaises(
            RuntimeError,
            self.fl._add_dictfield,
            "testing",
            "age",
            {"age/@seconds": "test"},
            {},
        )

    def test_FactoryLoader__add_dictfield_ValueError(self):
        self.assertRaises(
            ValueError,
            self.fl._add_dictfield,
            "testing",
            "age",
            {"age/@seconds": "test=test"},
            {},
        )