Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_run_with_incorrect_config_parameter_using_replace_method(self, mock_method):
# set parameter to fail
self.default_kwargs['config_text'] = 'invalid command'
self.default_kwargs['method'] = 'replace'
# prepare mock processing
err_msg = 'syntax error'
mock_device = mock.MagicMock()
mock_device.load_replace_candidate.side_effect = exceptions.ReplaceConfigException(err_msg)
mock_device.__enter__.return_value = mock_device
mock_driver = mock.MagicMock()
mock_driver.return_value = mock_device
mock_method.return_value = mock_driver
action = self.get_action_instance(self.full_config)
with self.assertRaisesRegexp(exceptions.ReplaceConfigException, err_msg):
action.run(**self.default_kwargs)
def load_replace_candidate(self, filename=None, config=None):
"""
SCP file to device filesystem, defaults to candidate_config.
Return None or raise exception
"""
self.config_replace = True
return_status, msg = self._load_candidate_wrapper(source_file=filename,
source_config=config,
dest_file=self.candidate_cfg,
file_system=self.dest_file_system)
if not return_status:
raise ReplaceConfigException(msg)
try:
fmt = self._detect_config_format(configuration)
if fmt == "xml":
configuration = etree.XML(configuration)
self.device.cu.load(
configuration,
format=fmt,
overwrite=overwrite,
ignore_warning=self.ignore_warning,
)
except ConfigLoadError as e:
if self.config_replace:
raise ReplaceConfigException(e.errs)
else:
raise MergeConfigException(e.errs)
If merge operation, perform copy running-config.
"""
if message:
raise NotImplementedError(
"Commit message not implemented for this platform"
)
# Always generate a rollback config on commit
self._gen_rollback_cfg()
if self.config_replace:
# Replace operation
filename = self.candidate_cfg
cfg_file = self._gen_full_path(filename)
if not self._check_file_exists(cfg_file):
raise ReplaceConfigException("Candidate config file does not exist")
if self.auto_rollback_on_error:
cmd = "configure replace {} force revert trigger error".format(cfg_file)
else:
cmd = "configure replace {} force".format(cfg_file)
output = self._commit_handler(cmd)
if (
("original configuration has been successfully restored" in output)
or ("error" in output.lower())
or ("not a valid config file" in output.lower())
or ("failed" in output.lower())
):
msg = "Candidate config could not be applied\n{}".format(output)
raise ReplaceConfigException(msg)
elif "%Please turn config archive on" in output:
msg = "napalm-ios replace() requires Cisco 'archive' feature to be enabled."
raise ReplaceConfigException(msg)
"""
If replacement operation, perform 'configure replace' for the entire config.
If merge operation, perform copy running-config.
"""
if message:
raise NotImplementedError('Commit message not implemented for this platform')
# Always generate a rollback config on commit
self._gen_rollback_cfg()
if self.config_replace:
# Replace operation
filename = self.candidate_cfg
cfg_file = self._gen_full_path(filename)
if not self._check_file_exists(cfg_file):
raise ReplaceConfigException("Candidate config file does not exist")
if self.auto_rollback_on_error:
cmd = 'configure replace {} force revert trigger error'.format(cfg_file)
else:
cmd = 'configure replace {} force'.format(cfg_file)
output = self._commit_hostname_handler(cmd)
if ('original configuration has been successfully restored' in output) or \
('error' in output.lower()) or \
('not a valid config file' in output.lower()) or \
('failed' in output.lower()):
msg = "Candidate config could not be applied\n{}".format(output)
raise ReplaceConfigException(msg)
elif '%Please turn config archive on' in output:
msg = "napalm-ios replace() requires Cisco 'archive' feature to be enabled."
raise ReplaceConfigException(msg)
else:
# Merge operation
def rollback(self):
"""Rollback configuration to filename or to self.rollback_cfg file."""
filename = self.rollback_cfg
cfg_file = self._gen_full_path(filename)
if not self._check_file_exists(cfg_file):
raise ReplaceConfigException("Rollback config file does not exist")
cmd = 'configure replace {} force'.format(cfg_file)
self.device.send_command_expect(cmd)
# Save config to startup
self.device.send_command_expect("write mem")
(s, d) for (s, d) in self.HEREDOC_COMMANDS if s in commands
]:
commands = self._multiline_convert(commands, start=start, depth=depth)
commands = self._mode_comment_convert(commands)
try:
if self.eos_autoComplete is not None:
self.device.run_commands(commands, autoComplete=self.eos_autoComplete)
else:
self.device.run_commands(commands)
except pyeapi.eapilib.CommandError as e:
self.discard_config()
msg = str(e)
if replace:
raise ReplaceConfigException(msg)
else:
raise MergeConfigException(msg)
def load_replace_candidate(self, filename=None, config=None):
self.pending_changes = True
self.replace = True
if not self.lock_on_connect:
self.device.lock()
try:
self.device.load_candidate_config(filename=filename, config=config)
except InvalidInputError as e:
self.pending_changes = False
self.replace = False
raise ReplaceConfigException(e.args[0])