|
| 1 | +import unittest |
| 2 | + |
| 3 | +from flexmock import flexmock, flexmock_teardown |
| 4 | +from hamcrest import assert_that, has_length, equal_to, is_ |
| 5 | +from pyeapi.api.vlans import Vlans |
| 6 | +from pyeapi.eapilib import CommandError |
| 7 | + |
| 8 | +from netman.adapters.switches.arista import Arista |
| 9 | +from netman.core.objects.exceptions import BadVlanNumber, VlanAlreadyExist, BadVlanName, UnknownVlan, \ |
| 10 | + OperationNotCompleted |
| 11 | +from netman.core.objects.switch_descriptor import SwitchDescriptor |
| 12 | +from tests.fixtures.arista import vlans_payload, vlan_data |
| 13 | + |
| 14 | + |
| 15 | +class AristaTest(unittest.TestCase): |
| 16 | + |
| 17 | + def setUp(self): |
| 18 | + self.switch = Arista(SwitchDescriptor(model='arista', hostname="my.hostname")) |
| 19 | + self.switch.conn = flexmock() |
| 20 | + self.switch.node = flexmock() |
| 21 | + |
| 22 | + def tearDown(self): |
| 23 | + flexmock_teardown() |
| 24 | + |
| 25 | + def test_get_vlans(self): |
| 26 | + four_vlans_payload = vlans_payload(result=[{'vlans': {'1': vlan_data(name='default'), |
| 27 | + '123': vlan_data(name='VLAN0123'), |
| 28 | + '456': vlan_data(name='Patate'), |
| 29 | + '4444': vlan_data(name='VLAN4444')}}]) |
| 30 | + |
| 31 | + self.switch.conn.should_receive("execute").with_args("show vlan").once().and_return(four_vlans_payload) |
| 32 | + |
| 33 | + vlan_list = self.switch.get_vlans() |
| 34 | + vlan_list = sorted(vlan_list, key=lambda x: x.number) |
| 35 | + |
| 36 | + assert_that(vlan_list, has_length(4)) |
| 37 | + assert_that(vlan_list[0].number, equal_to(1)) |
| 38 | + assert_that(vlan_list[0].name, equal_to("default")) |
| 39 | + |
| 40 | + def test_get_vlans_with_default_name_returns_no_name(self): |
| 41 | + my_vlan = {u'status': u'active', u'interfaces': {}, u'dynamic': False, u'name': u'VLAN0123'} |
| 42 | + |
| 43 | + self.switch.conn.should_receive("execute").with_args("show vlan").once() \ |
| 44 | + .and_return(vlans_payload(result=[{'vlans': {'123': my_vlan}}])) |
| 45 | + |
| 46 | + vlan_list = self.switch.get_vlans() |
| 47 | + vlan_list = sorted(vlan_list, key=lambda x: x.number) |
| 48 | + |
| 49 | + assert_that(vlan_list[0].number, equal_to(123)) |
| 50 | + assert_that(vlan_list[0].name, equal_to(None)) |
| 51 | + |
| 52 | + def test_get_vlan_doesnt_exist(self): |
| 53 | + self.switch.conn.should_receive("execute").with_args("show vlan 111").and_raise(CommandError(1000, 'msg')) |
| 54 | + |
| 55 | + with self.assertRaises(UnknownVlan): |
| 56 | + self.switch.get_vlan(111) |
| 57 | + |
| 58 | + def test_get_vlan(self): |
| 59 | + self.switch.conn.should_receive("execute").with_args("show vlan 123").once() \ |
| 60 | + .and_return(vlans_payload(result=[{'vlans': {'123': vlan_data(name='My-Vlan-Name')}}])) |
| 61 | + |
| 62 | + vlan = self.switch.get_vlan(123) |
| 63 | + |
| 64 | + assert_that(vlan.number, is_(123)) |
| 65 | + assert_that(vlan.name, is_('My-Vlan-Name')) |
| 66 | + |
| 67 | + def test_add_vlan(self): |
| 68 | + vlan = flexmock(spec=Vlans) |
| 69 | + |
| 70 | + self.switch.conn.should_receive("execute").with_args("show vlan 123").once().and_raise( |
| 71 | + CommandError(1000, 'msg')) |
| 72 | + vlan.should_receive("configure_vlan").with_args(123, []).once().and_return(True) |
| 73 | + |
| 74 | + self.switch.add_vlan(123) |
| 75 | + |
| 76 | + def test_add_vlan_with_name(self): |
| 77 | + vlan = flexmock(spec=Vlans) |
| 78 | + |
| 79 | + self.switch.conn.should_receive("execute").with_args("show vlan 123").once().and_raise( |
| 80 | + CommandError(1000, 'msg')) |
| 81 | + vlan.should_receive("configure_vlan").with_args(123, ["name gertrude"]).once().and_return(True) |
| 82 | + |
| 83 | + self.switch.add_vlan(123, "gertrude") |
| 84 | + |
| 85 | + def test_add_vlan_bad_vlan_number(self): |
| 86 | + with self.assertRaises(BadVlanNumber): |
| 87 | + self.switch.add_vlan(12334) |
| 88 | + |
| 89 | + def test_add_vlan_already_exits(self): |
| 90 | + self.switch.conn.should_receive("execute").with_args("show vlan 123").once()\ |
| 91 | + .and_return(vlans_payload(result=[{'vlans': {'123': vlan_data(name='VLAN0123')}}])) |
| 92 | + |
| 93 | + with self.assertRaises(VlanAlreadyExist): |
| 94 | + self.switch.add_vlan(123) |
| 95 | + |
| 96 | + def test_add_vlan_bad_vlan_name(self): |
| 97 | + vlan = flexmock(spec=Vlans) |
| 98 | + |
| 99 | + self.switch.conn.should_receive("execute").with_args("show vlan 123").once().and_raise( |
| 100 | + CommandError(1000, 'msg')) |
| 101 | + vlan.should_receive("configure_vlan").with_args(123, ["name gertrude_invalid_name"]).once().and_return(False) |
| 102 | + |
| 103 | + with self.assertRaises(BadVlanName): |
| 104 | + self.switch.add_vlan(123, "gertrude_invalid_name") |
| 105 | + |
| 106 | + def test_remove_vlan(self): |
| 107 | + vlan = flexmock(spec=Vlans) |
| 108 | + |
| 109 | + self.switch.conn.should_receive("execute").with_args("show vlan 123").once() |
| 110 | + vlan.should_receive("delete").with_args(123).once().and_return(True) |
| 111 | + |
| 112 | + self.switch.remove_vlan(123) |
| 113 | + |
| 114 | + def test_remove_vlan_unknown_vlan(self): |
| 115 | + self.switch.conn.should_receive("execute").with_args("show vlan 123").once().and_raise( |
| 116 | + CommandError(1000, 'msg')) |
| 117 | + |
| 118 | + with self.assertRaises(UnknownVlan): |
| 119 | + self.switch.remove_vlan(123) |
| 120 | + |
| 121 | + def test_remove_vlan_unable_to_remove(self): |
| 122 | + vlan = flexmock(spec=Vlans) |
| 123 | + |
| 124 | + self.switch.conn.should_receive("execute").with_args("show vlan 123").once() |
| 125 | + vlan.should_receive("delete").with_args(123).once().and_return(False) |
| 126 | + |
| 127 | + with self.assertRaises(OperationNotCompleted): |
| 128 | + self.switch.remove_vlan(123) |
0 commit comments