Browse Source

Extract netmiko device configuration save

The configuration save operation of netmiko devices is extracted into a
separate function. This allows devices to implement the function as
required, or to use the base class's implementation.

Change-Id: I135ccb842a1120b099afe1107da7b549bad943ef
Related-Bug: #1740587
changes/35/530535/2
Mark Goddard 4 years ago
parent
commit
a4631e15ba
2 changed files with 23 additions and 12 deletions
  1. +9
    -2
      networking_generic_switch/devices/netmiko_devices/__init__.py
  2. +14
    -10
      networking_generic_switch/tests/unit/netmiko/test_netmiko_base.py

+ 9
- 2
networking_generic_switch/devices/netmiko_devices/__init__.py View File

@ -141,8 +141,7 @@ class NetmikoSwitch(devices.GenericSwitchDevice):
config_commands=cmd_set)
# NOTE (vsaienko) always save configuration
# when configuration is applied successfully.
if self.SAVE_CONFIGURATION:
net_connect.send_command(self.SAVE_CONFIGURATION)
self.save_configuration(net_connect)
except Exception as e:
raise exc.GenericSwitchNetmikoConnectError(
config=device_utils.sanitise_config(self.config), error=e)
@ -188,3 +187,11 @@ class NetmikoSwitch(devices.GenericSwitchDevice):
self._format_commands(self.DELETE_PORT,
port=port,
segmentation_id=segmentation_id))
def save_configuration(self, net_connect):
"""Save the device's configuration.
:param net_connect: a netmiko connection object.
"""
if self.SAVE_CONFIGURATION:
net_connect.send_command(self.SAVE_CONFIGURATION)

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

@ -15,6 +15,7 @@
import fixtures
import mock
import netmiko
import netmiko.base_connection
from oslo_config import fixture as config_fixture
import paramiko
import tenacity
@ -143,22 +144,25 @@ class TestNetmikoSwitch(NetmikoSwitchTestBase):
self.assertFalse(connect_mock.send_command.called)
@mock.patch.object(netmiko_devices.NetmikoSwitch, '_get_connection')
def test_send_commands_to_device(self, gc_mock):
connect_mock = mock.MagicMock(SAVE_CONFIGURATION=None)
@mock.patch.object(netmiko_devices.NetmikoSwitch, 'save_configuration')
def test_send_commands_to_device(self, save_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'])
gc_mock.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)
def test_save_configuration(self):
connect_mock = mock.MagicMock(netmiko.base_connection.BaseConnection)
self.switch.save_configuration(connect_mock)
self.assertFalse(connect_mock.send_command.called)
@mock.patch.object(netmiko_devices.NetmikoSwitch, '_get_connection')
def test_send_commands_to_device_save_configuration(self, gc_mock):
connect_mock = mock.MagicMock(SAVE_CONFIGURAION='save me')
gc_mock.return_value.__enter__.return_value = connect_mock
self.switch.send_commands_to_device(['spam ham aaaa'])
connect_mock.send_config_set.assert_called_once_with(
config_commands=['spam ham aaaa'])
@mock.patch.object(netmiko_devices.NetmikoSwitch, 'SAVE_CONFIGURATION',
'save me')
def test_save_configuration_required(self):
connect_mock = mock.MagicMock(netmiko.base_connection.BaseConnection)
self.switch.save_configuration(connect_mock)
connect_mock.send_command.called_once_with('save me')
@mock.patch.object(netmiko_devices.ngs_lock, 'PoolLock', autospec=True)


Loading…
Cancel
Save