def test_delete(self): self.ncclient.command.side_effect = [ # list dhcp bindings to clear FakeNcClientResponse(fixtures.show_dhcp(1)), # run remove dhcp binding commands FakeNcClientResponse(fixtures.ok()), # run clear commands FakeNcClientResponse(fixtures.ok()), # save commands FakeNcClientResponse(fixtures.ok()) ] port = base_driver.PortInfo( switch_host='switch1.host.com', switch_username='******', switch_password='******', interface='eth1/1', hardware_id='hardware1', vlan_id=1, ip='10.0.0.2', mac_address='ff:ff:ff:ff:ff:ff', trunked=True) eventlet.spawn(self.driver.delete, port).wait() self.assertEqual(self.ncclient.command.call_count, 4) show_bindings_cmd = self._get_called_commands(0) remove_bindings_cmd = self._get_called_commands(1) clear_port_cmd = self._get_called_commands(2) save_cmd = self._get_called_commands(3) show_bindings_expected = ['show running dhcp | egrep port-channel1$'] remove_bindings_expected = [ 'configure terminal', 'interface port-channel 1', ('no ip source binding 10.0.0.1 FFFF.FFFF.FFFF.FFFF ' 'vlan 1 interface port-channel1') ] clear_port_expected = [ 'configure terminal', 'interface port-channel 1', 'no ip verify source dhcp-snooping-vlan', 'no interface port-channel 1', 'configure terminal', 'default interface ethernet 1/1', 'configure terminal', 'interface ethernet 1/1', 'shutdown' ] save_expected = ['copy running-config startup-config'] self.assertEqual(show_bindings_cmd, show_bindings_expected) self.assertEqual(remove_bindings_cmd, remove_bindings_expected) self.assertEqual(clear_port_cmd, clear_port_expected) self.assertEqual(save_cmd, save_expected)
def test_attach(self): self.ncclient.command.side_effect = [ # run attach commands FakeNcClientResponse(fixtures.ok()) ] port = base_driver.PortInfo(switch_host='switch1.host.com', switch_username='******', switch_password='******', interface='eth1/1', hardware_id='hardware1', vlan_id=1, ip='10.0.0.2', mac_address='ff:ff:ff:ff:ff:ff', trunked=True) eventlet.spawn(self.driver.attach, port).wait() self.assertEqual(self.ncclient.command.call_count, 2) attach_cmd = self._get_called_commands(0) save_cmd = self._get_called_commands(1) attach_expected = [ 'configure terminal', 'interface port-channel 1', 'switchport trunk allowed vlan add 1', 'configure terminal', ('ip source binding 10.0.0.2 ff:ff:ff:ff:ff:ff ' 'vlan 1 interface port-channel1') ] save_expected = ['copy running-config startup-config'] self.assertEqual(attach_cmd, attach_expected) self.assertEqual(save_cmd, save_expected)
def test_save_combines_same_host(self): self.ncclient.command.side_effect = [ # save commands FakeNcClientResponse(fixtures.ok()) ] self.driver = driver.CiscoDriver(save_queue_max_age=.01, save_queue_get_wait=0) port = base_driver.PortInfo( switch_host='switch1.host.com', switch_username='******', switch_password='******', interface='eth1/1', hardware_id='hardware1', vlan_id=1, ip='10.0.0.2', mac_address='ff:ff:ff:ff:ff:ff', trunked=True) self.driver.save(port) self.driver.save(port) eventlet.sleep(.02) self.assertEqual(self.ncclient.command.call_count, 1) save_cmd = self._get_called_commands(0) save_expected = ['copy running-config startup-config'] self.assertEqual(save_cmd, save_expected)
def test_save_retries(self): self.ncclient.command.side_effect = [ # first save exception Exception("failed save"), # second save works FakeNcClientResponse(fixtures.ok()) ] port = base_driver.PortInfo( switch_host='switch1.host.com', switch_username='******', switch_password='******', interface='eth1/1', hardware_id='hardware1', vlan_id=1, ip='10.0.0.2', mac_address='ff:ff:ff:ff:ff:ff', trunked=True) self.driver.save(port) eventlet.sleep(.02) self.assertEqual(self.ncclient.command.call_count, 2) save_cmd = self._get_called_commands(0) save_cmd2 = self._get_called_commands(1) save_expected = ['copy running-config startup-config'] self.assertEqual(save_cmd, save_expected) self.assertEqual(save_cmd2, save_expected)
def test_detach(self): self.ncclient.command.side_effect = [ # run detach commands FakeNcClientResponse(fixtures.ok()), # run remove ip binding commands FakeNcClientResponse(fixtures.ok()), # save commands FakeNcClientResponse(fixtures.ok()) ] port = base_driver.PortInfo( switch_host='switch1.host.com', switch_username='******', switch_password='******', interface='eth1/1', hardware_id='hardware1', vlan_id=1, ip='10.0.0.2', mac_address='ff:ff:ff:ff:ff:ff', trunked=True) eventlet.spawn(self.driver.detach, port).wait() self.assertEqual(self.ncclient.command.call_count, 3) detach_cmd = self._get_called_commands(0) remove_binding_cmd = self._get_called_commands(1) save_cmd = self._get_called_commands(2) detach_expected = [ 'configure terminal', 'interface port-channel 1', 'switchport trunk allowed vlan remove 1' ] remove_binding_cmd = [ 'configure terminal', 'interface port-channel 1', 'configure terminal', ('no ip source binding 10.0.0.2 ff:ff:ff:ff:ff:ff ' 'vlan 1 interface port-channel1') ] save_expected = ['copy running-config startup-config'] self.assertEqual(detach_cmd, detach_expected) self.assertEqual(remove_binding_cmd, remove_binding_cmd) self.assertEqual(save_cmd, save_expected)
def test_delete(self): self.ncclient.command.side_effect = [ # list dhcp bindings to clear FakeNcClientResponse(fixtures.show_dhcp(1)), # run clear commands FakeNcClientResponse(fixtures.ok()) ] port = base_driver.PortInfo(switch_host='switch1.host.com', switch_username='******', switch_password='******', interface='eth1/1', hardware_id='hardware1', vlan_id=1, ip='10.0.0.2', mac_address='ff:ff:ff:ff:ff:ff', trunked=True) eventlet.spawn(self.driver.delete, port).wait() self.assertEqual(self.ncclient.command.call_count, 3) remove_bindings_cmd = self._get_called_commands(0) clear_port_cmd = self._get_called_commands(1) save_cmd = self._get_called_commands(2) remove_bindings_expected = ['show running dhcp | egrep port-channel1$'] clear_port_expected = [ 'configure terminal', 'interface port-channel 1', ('no ip source binding 10.0.0.1 FFFF.FFFF.FFFF.FFFF ' 'vlan 1 interface port-channel1'), 'configure terminal', 'interface port-channel 1', 'no ip verify source dhcp-snooping-vlan', 'no interface port-channel 1', 'configure terminal', 'default interface ethernet 1/1', 'configure terminal', 'interface ethernet 1/1', 'shutdown' ] save_expected = ['copy running-config startup-config'] self.assertEqual(remove_bindings_cmd, remove_bindings_expected) self.assertEqual(clear_port_cmd, clear_port_expected) self.assertEqual(save_cmd, save_expected)
def test_create(self): self.ncclient.command.side_effect = [ # list dhcp bindings to clear FakeNcClientResponse(fixtures.show_dhcp(1)), # run clear commands FakeNcClientResponse(fixtures.ok()), # run create commands FakeNcClientResponse(fixtures.ok()) ] port = base_driver.PortInfo(switch_host='switch1.host.com', switch_username='******', switch_password='******', interface='eth1/1', hardware_id='hardware1', vlan_id=1, ip='10.0.0.2', mac_address='ff:ff:ff:ff:ff:ff', trunked=True) eventlet.spawn(self.driver.create, port).wait() self.assertEqual(self.ncclient.command.call_count, 4) remove_bindings_cmd = self._get_called_commands(0) clear_port_cmd = self._get_called_commands(1) create_port_cmd = self._get_called_commands(2) save_cmd = self._get_called_commands(3) remove_bindings_expected = ['show running dhcp | egrep port-channel1$'] clear_port_expected = [ 'configure terminal', 'interface port-channel 1', ('no ip source binding 10.0.0.1 FFFF.FFFF.FFFF.FFFF ' 'vlan 1 interface port-channel1'), 'configure terminal', 'interface port-channel 1', 'no ip verify source dhcp-snooping-vlan', 'no interface port-channel 1', 'configure terminal', 'default interface ethernet 1/1', 'configure terminal', 'interface ethernet 1/1', 'shutdown' ] create_port_expected = [ 'configure terminal', 'interface port-channel 1', 'description CUSThardware1-host', 'switchport mode trunk', 'switchport trunk allowed vlan 1', 'spanning-tree port type edge trunk', 'vpc 1', 'ip verify source dhcp-snooping-vlan', 'no shutdown', 'configure terminal', ('ip source binding 10.0.0.2 ff:ff:ff:ff:ff:ff ' 'vlan 1 interface port-channel1'), 'configure terminal', 'interface ethernet 1/1', 'description CUSThardware1-host', 'switchport mode trunk', 'switchport trunk allowed vlan 1', 'spanning-tree port type edge trunk', 'spanning-tree bpduguard enable', 'channel-group 1 mode active', 'no lldp transmit', 'no cdp enable', 'no shutdown' ] save_expected = ['copy running-config startup-config'] self.assertEqual(remove_bindings_cmd, remove_bindings_expected) self.assertEqual(clear_port_cmd, clear_port_expected) self.assertEqual(create_port_cmd, create_port_expected) self.assertEqual(save_cmd, save_expected)
def test_create_access(self): self.ncclient.command.side_effect = [ # list dhcp bindings to clear FakeNcClientResponse(fixtures.show_dhcp(1)), # run dhcp binding delete commands FakeNcClientResponse(fixtures.ok()), # run clear commands FakeNcClientResponse(fixtures.ok()), # run create commands FakeNcClientResponse(fixtures.ok()), # run save commands FakeNcClientResponse(fixtures.ok()), ] port = base_driver.PortInfo( switch_host='switch1.host.com', switch_username='******', switch_password='******', interface='eth1/1', hardware_id='hardware1', vlan_id=1, ip='10.0.0.2', mac_address='ff:ff:ff:ff:ff:ff', trunked=False) eventlet.spawn(self.driver.create, port).wait() self.assertEqual(self.ncclient.command.call_count, 5) show_bindings_cmd = self._get_called_commands(0) remove_bindings_cmd = self._get_called_commands(1) clear_port_cmd = self._get_called_commands(2) create_port_cmd = self._get_called_commands(3) save_cmd = self._get_called_commands(4) show_bindings_expected = ['show running dhcp | egrep port-channel1$'] remove_bindings_expected = [ 'configure terminal', 'interface port-channel 1', ('no ip source binding 10.0.0.1 FFFF.FFFF.FFFF.FFFF ' 'vlan 1 interface port-channel1'), ] clear_port_expected = [ 'configure terminal', 'interface port-channel 1', 'no ip verify source dhcp-snooping-vlan', 'no interface port-channel 1', 'configure terminal', 'default interface ethernet 1/1', 'configure terminal', 'interface ethernet 1/1', 'shutdown' ] create_port_expected = [ 'configure terminal', 'interface ethernet 1/1', 'description CUSThardware1-host', 'switchport mode access', 'switchport access vlan 1', 'spanning-tree port type edge', 'spanning-tree bpduguard enable', 'lldp transmit', 'cdp enable', 'ip verify source dhcp-snooping-vlan', 'no ip verify source dhcp-snooping-vlan', 'no shutdown' ] save_expected = ['copy running-config startup-config'] self.assertEqual(show_bindings_cmd, show_bindings_expected) self.assertEqual(remove_bindings_cmd, remove_bindings_expected) self.assertEqual(clear_port_cmd, clear_port_expected) self.assertEqual(create_port_cmd, create_port_expected) self.assertEqual(save_cmd, save_expected)