1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
|
import sys
import os
import unittest
sys.path.append(os.path.join(os.path.dirname(__file__), '../lib'))
from testlib import get_fixture, function
from testlib import EapiConfigUnitTest
import pyeapi.api.ospf
class TestApiOspf(EapiConfigUnitTest):
def __init__(self, *args, **kwargs):
super(TestApiOspf, self).__init__(*args, **kwargs)
self.instance = pyeapi.api.ospf.instance(None)
self.config = open(get_fixture('running_config.ospf')).read()
def test_get_no_vrf(self):
result = self.instance.get()
keys = ['networks', 'ospf_process_id', 'vrf', 'redistributions',
'router_id', 'shutdown']
self.assertEqual(sorted(keys), sorted(result.keys()))
self.assertEqual(result['vrf'], 'default')
def test_get_with_vrf(self):
result = self.instance.get(vrf='test')
keys = ['networks', 'ospf_process_id', 'vrf', 'redistributions',
'router_id', 'shutdown']
self.assertEqual(sorted(keys), sorted(result.keys()))
self.assertEqual(result['vrf'], 'test')
def test_create(self):
for ospf_id in ['65000', 65000]:
func = function('create', ospf_id)
cmds = 'router ospf {}'.format(ospf_id)
self.eapi_positive_config_test(func, cmds)
def test_create_with_vrf(self):
for ospf_id in ['65000', 65000]:
vrf_name = 'test'
func = function('create', ospf_id, vrf_name)
cmds = 'router ospf {} vrf {}'.format(ospf_id, vrf_name)
self.eapi_positive_config_test(func, cmds)
def test_create_invalid_id(self):
for ospf_id in ['66000', 66000]:
with self.assertRaises(ValueError):
self.instance.create(ospf_id)
def test_delete(self):
func = function('delete')
cmds = 'no router ospf 65000'
self.eapi_positive_config_test(func, cmds)
def test_add_network(self):
func = function('add_network', '172.16.10.0', '24', '0')
cmds = ['router ospf 65000', 'network 172.16.10.0/24 area 0']
self.eapi_positive_config_test(func, cmds)
func = function('add_network', '', '24', '0')
self.eapi_exception_config_test(func, ValueError)
func = function('add_network', '172.16.10.0', '', '0')
self.eapi_exception_config_test(func, ValueError)
def test_remove_network(self):
func = function('remove_network', '172.16.10.0', '24', '0')
cmds = ['router ospf 65000', 'no network 172.16.10.0/24 area 0']
self.eapi_positive_config_test(func, cmds)
func = function('remove_network', '', '24', '0')
self.eapi_exception_config_test(func, ValueError)
func = function('remove_network', '172.16.10.0', '', '0')
self.eapi_exception_config_test(func, ValueError)
def test_set_router_id(self):
for state in ['config', 'negate', 'default']:
rid = '1.1.1.1'
if state == 'config':
cmds = ['router ospf 65000', 'router-id 1.1.1.1']
func = function('set_router_id', rid)
elif state == 'negate':
cmds = ['router ospf 65000', 'no router-id']
func = function('set_router_id')
elif state == 'default':
cmds = ['router ospf 65000', 'default router-id']
func = function('set_router_id', rid, True)
self.eapi_positive_config_test(func, cmds)
cmds = ['router ospf 65000', 'no router-id']
func = function('set_router_id')
self.eapi_positive_config_test(func, cmds)
def test_set_shutdown(self):
for state in ['config', 'negate', 'default']:
if state == 'config':
cmds = ['router ospf 65000', 'shutdown']
func = function('set_shutdown')
elif state == 'negate':
cmds = ['router ospf 65000', 'no shutdown']
func = function('set_no_shutdown')
self.eapi_positive_config_test(func, cmds)
def test_add_redistribution_no_route_map(self):
for protocol in ['bgp', 'rip', 'static', 'connected', 'no-proto']:
cmds = ['router ospf 65000', 'redistribute {}'.format(protocol)]
func = function('add_redistribution', protocol)
if protocol != 'no-proto':
self.eapi_positive_config_test(func, cmds)
else:
self.eapi_exception_config_test(func, ValueError)
def test_add_redistribution_with_route_map(self):
for protocol in ['bgp', 'rip', 'static', 'connected']:
cmds = ['router ospf 65000', 'redistribute {} route-map test'.format(protocol)]
func = function('add_redistribution', protocol, 'test')
if protocol != 'no-proto':
self.eapi_positive_config_test(func, cmds)
else:
self.eapi_exception_config_test(func, ValueError)
def test_delete_redistribution_no_route_map(self):
for protocol in ['bgp', 'rip', 'static', 'connected', 'no-proto']:
cmds = ['router ospf 65000', 'no redistribute {}'.format(protocol)]
func = function('remove_redistribution', protocol)
if protocol != 'no-proto':
self.eapi_positive_config_test(func, cmds)
else:
self.eapi_exception_config_test(func, ValueError)
class TestApiNegOspf(EapiConfigUnitTest):
def __init__(self, *args, **kwargs):
super(TestApiNegOspf, self).__init__(*args, **kwargs)
self.instance = pyeapi.api.ospf.instance(None)
self.config = open(get_fixture('running_config.bgp')).read()
def test_no_get(self):
result = self.instance.get()
self.assertEqual(None, result)
def test_no_delete(self):
result = self.instance.delete()
self.assertTrue(result)
if __name__ == '__main__':
unittest.main()
|