Browse Source

Refactor sending netmiko config commands

Extracts the sending of netmiko configuration commands into a separate
method.  This allows subclasses to implement as necessary.

Change-Id: I8eea36c5ae93230df6c626a0ac9e2225d8f2da0e
Related-Bug: #1740587
changes/36/530536/2
Mark Goddard 4 years ago
parent
commit
6c2e671d3d
2 changed files with 25 additions and 6 deletions
  1. +11
    -3
      networking_generic_switch/devices/netmiko_devices/__init__.py
  2. +14
    -3
      networking_generic_switch/tests/unit/netmiko/test_netmiko_base.py

+ 11
- 3
networking_generic_switch/devices/netmiko_devices/__init__.py View File

@ -136,9 +136,7 @@ class NetmikoSwitch(devices.GenericSwitchDevice):
try:
with ngs_lock.PoolLock(self.locker, **self.lock_kwargs):
with self._get_connection() as net_connect:
net_connect.enable()
output = net_connect.send_config_set(
config_commands=cmd_set)
output = self.send_config_set(net_connect, cmd_set)
# NOTE (vsaienko) always save configuration
# when configuration is applied successfully.
self.save_configuration(net_connect)
@ -188,6 +186,16 @@ class NetmikoSwitch(devices.GenericSwitchDevice):
port=port,
segmentation_id=segmentation_id))
def send_config_set(self, net_connect, cmd_set):
"""Send a set of configuration lines to the device.
:param net_connect: a netmiko connection object.
:param cmd_set: a list of configuration lines to send.
:returns: The output of the configuration commands.
"""
net_connect.enable()
return net_connect.send_config_set(config_commands=cmd_set)
def save_configuration(self, net_connect):
"""Save the device's configuration.


+ 14
- 3
networking_generic_switch/tests/unit/netmiko/test_netmiko_base.py View File

@ -144,14 +144,25 @@ class TestNetmikoSwitch(NetmikoSwitchTestBase):
self.assertFalse(connect_mock.send_command.called)
@mock.patch.object(netmiko_devices.NetmikoSwitch, '_get_connection')
@mock.patch.object(netmiko_devices.NetmikoSwitch, 'send_config_set')
@mock.patch.object(netmiko_devices.NetmikoSwitch, 'save_configuration')
def test_send_commands_to_device(self, save_mock, gc_mock):
def test_send_commands_to_device(self, save_mock, send_mock, gc_mock):
connect_mock = mock.MagicMock(netmiko.base_connection.BaseConnection)
gc_mock.return_value.__enter__.return_value = connect_mock
self.switch.send_commands_to_device(['spam ham aaaa'])
send_mock.return_value = 'fake output'
result = self.switch.send_commands_to_device(['spam ham aaaa'])
send_mock.assert_called_once_with(connect_mock, ['spam ham aaaa'])
self.assertEqual('fake output', result)
save_mock.assert_called_once_with(connect_mock)
def test_send_config_set(self):
connect_mock = mock.MagicMock(netmiko.base_connection.BaseConnection)
connect_mock.send_config_set.return_value = 'fake output'
result = self.switch.send_config_set(connect_mock, ['spam ham aaaa'])
connect_mock.enable.assert_called_once_with()
connect_mock.send_config_set.assert_called_once_with(
config_commands=['spam ham aaaa'])
save_mock.assert_called_once_with(connect_mock)
self.assertEqual('fake output', result)
def test_save_configuration(self):
connect_mock = mock.MagicMock(netmiko.base_connection.BaseConnection)


Loading…
Cancel
Save