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_lock_context_exit(self, mock_request, mock_session):
session = ncclient.transport.SSHSession(self.device_handler)
obj = LockContext(session, self.device_handler, "running")
self.assertFalse(obj.__exit__())
node = new_ele("unlock")
sub_ele(sub_ele(node, "target"), "running")
xml = ElementTree.tostring(node)
call = mock_request.call_args_list[0][0][0]
call = ElementTree.tostring(call)
self.assertEqual(call, xml)
def _mock_manager_setup(self, *args, **kwargs):
if kwargs:
device_params = kwargs['device_params']
device_handler = make_device_handler(device_params)
session = SSHSession(device_handler)
return Manager(session, device_handler)
def test_get_schema(self, mock_request):
session = ncclient.transport.SSHSession(self.device_handler)
obj = GetSchema(session, self.device_handler, raise_mode=RaiseMode.ALL)
identifier = "foo"
version = "1.0"
reqformat = "xsd"
obj.request(identifier, version, reqformat)
node = etree.Element(qualify("get-schema", NETCONF_MONITORING_NS))
id = etree.SubElement(node,
qualify("identifier", NETCONF_MONITORING_NS))
id.text = identifier
ver = etree.SubElement(node, qualify("version", NETCONF_MONITORING_NS))
ver.text = version
formt = etree.SubElement(node,
qualify("format", NETCONF_MONITORING_NS))
formt.text = reqformat
xml = ElementTree.tostring(node)
call = mock_request.call_args_list[0][0][0]
def _mock_manager(self, *args, **kwargs):
if kwargs and 'ignore_warning' not in kwargs:
# Little hack for mocked execute
if 'dev_timeout' in kwargs:
if (args and args[0].findtext('package-name') ==
'/var/tmp/test_no_result.tgz'):
return self._read_file(args[0].tag + '.no_result.xml')
else:
return self._read_file(args[0].tag + '.xml')
if 'path' in kwargs:
if kwargs['path'] == '/packages':
return self._read_file('file-list_dir.xml')
device_params = kwargs['device_params']
device_handler = make_device_handler(device_params)
session = SSHSession(device_handler)
return Manager(session, device_handler)
elif args:
if args[0].find('at') is not None:
return self._read_file('request-reboot-at.xml')
elif self._testMethodName == 'test_sw_check_pending_install':
if args[0].text == 'request-package-check-pending-install':
return self._read_file(
'request-package-check-pending-install-error.xml')
elif self._testMethodName == 'test_sw_check_pending_install_RpcError_continue':
if args[0].text == 'request-package-check-pending-install':
xml = '''
protocol
operation-failed
error
syntax error
def test_loadconf_json(self, mock_request, mock_session):
device_handler = manager.make_device_handler({'name': 'junos'})
session = ncclient.transport.SSHSession(device_handler)
obj = LoadConfiguration(
session,
device_handler,
raise_mode=RaiseMode.ALL)
location = '{ "configuration": { "system": { "location": { "floor": "7" }}}}'
config_json = json.loads(location)
config = json.dumps(config_json)
obj.request(format='json', action='merge', config=config)
node = new_ele('load-configuration', {'format': 'json', 'action': 'merge'})
sub_ele(node, 'configuration-json').text = config
call = mock_request.call_args_list[0][0][0]
self.assertEqual(call.tag, node.tag)
self.assertEqual(call.attrib, node.attrib)
def _mock_manager_setup(self, *args, **kwargs):
if kwargs:
device_params = kwargs['device_params']
device_handler = make_device_handler(device_params)
session = SSHSession(device_handler)
return Manager(session, device_handler)
def test_unlock(self, mock_request, mock_session):
session = ncclient.transport.SSHSession(self.device_handler)
obj = Unlock(session, self.device_handler, raise_mode=RaiseMode.ALL)
obj.request(target="running")
node = new_ele("unlock")
sub_ele(sub_ele(node, "target"), "running")
xml = ElementTree.tostring(node)
call = mock_request.call_args_list[0][0][0]
call = ElementTree.tostring(call)
self.assertEqual(call, xml)
def test_edit_config_2(self, mock_request):
session = ncclient.transport.SSHSession(self.device_handler)
session._server_capabilities = [":rollback-on-error", ":validate"]
obj = EditConfig(
session,
self.device_handler,
raise_mode=RaiseMode.ALL)
root = """
system {
host-name foo-bar;
}
"""
obj.request(copy.deepcopy(root), format="text", target="running", error_option="rollback-on-error",
default_operation="default", test_option="test")
node = new_ele("edit-config")
node.append(util.datastore_or_url("target", "running"))
sub_ele(node, "default-operation").text = "default"
sub_ele(node, "test-option").text = "test"
def test_get_with_defaults_not_supported(self, mock_request):
session = ncclient.transport.SSHSession(self.device_handler)
session._server_capabilities = Capabilities([
"urn:ietf:params:netconf:capability:with-defaults:1.0"
"?basic-mode=explicit"
"&also-supported=report-all,trim"
])
obj = Get(session, self.device_handler, raise_mode=RaiseMode.ALL)
expected_error = (
"Invalid 'with-defaults' mode 'report-all-tagged'; the server "
"only supports the following: explicit, report-all, trim"
)
self.assertRaisesRegexp(
WithDefaultsError,
expected_error,
obj.request,
with_defaults='report-all-tagged'