Skip to content

Commit

Permalink
rMerge branch 'martin-volf-validation'
Browse files Browse the repository at this point in the history
  • Loading branch information
Leonidas Poulopoulos committed Jul 5, 2016
2 parents a4d3b47 + 2207127 commit 4ae3ecf
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 17 deletions.
16 changes: 7 additions & 9 deletions ncclient/operations/edit.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,17 +109,15 @@ def request(self, source="candidate"):
*source* is the name of the configuration datastore being validated or `config` element containing the configuration subtree to be validated
** modified to only accept valid string as source argument. Elements no longer accepted - earies - 04/22/2013
:seealso: :ref:`srctarget_params`"""
node = new_ele("validate")
# rfc6241 sec 8.6.4 states valid source can be <candidate> or <config> elements
tags = ("config", "candidate")
if source not in tags:
raise XMLError("Invalid source type: [%s], must be one of %s" % (source, tags))
src_ele = sub_ele(node, "source")
sub_ele(src_ele, source)
if type(source) is str:
src = util.datastore_or_url("source", source, self._assert)
else:
validated_element(source, ("config", qualify("config")))
src = new_ele("source")
src.append(source)
node.append(src)
return self._request(node)


Expand Down
25 changes: 17 additions & 8 deletions test/unit/operations/test_edit.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,26 +98,35 @@ def test_copy_config(self, mock_request):
self.assertEqual(call, xml)

@patch('ncclient.operations.RPC._request')
def test_validate(self, mock_request):
def test_validate_config(self, mock_request):
session = ncclient.transport.SSHSession(self.device_handler)
session._server_capabilities = [':validate']
obj = Validate(session, self.device_handler, raise_mode=RaiseMode.ALL)
obj.request("config")
cfg_data = new_ele("config")
sub_ele(cfg_data, "data")
obj.request(cfg_data)
node = new_ele("validate")
node = sub_ele(node, "source")
sub_ele(node, "config")
src = sub_ele(node, "source")
cfg = sub_ele(src, "config")
sub_ele(cfg, "data")
xml = ElementTree.tostring(node, method='xml')
call = mock_request.call_args_list[0][0][0]
call = ElementTree.tostring(call, method='xml')
#self.assertEqual(call, xml)
self.assertEqual(call, xml)

@patch('ncclient.operations.RPC._request')
def test_validate_exception(self, mock_request):
def test_validate_datastore(self, mock_request):
session = ncclient.transport.SSHSession(self.device_handler)
session._server_capabilities = [':validate']
obj = Validate(session, self.device_handler, raise_mode=RaiseMode.ALL)
with self.assertRaises(XMLError):
obj.request("running")
obj.request("candidate")
node = new_ele("validate")
src = sub_ele(node, "source")
sub_ele(src, "candidate")
xml = ElementTree.tostring(node, method='xml')
call = mock_request.call_args_list[0][0][0]
call = ElementTree.tostring(call, method='xml')
self.assertEqual(call, xml)

@patch('ncclient.operations.RPC._request')
def test_commit(self, mock_request):
Expand Down

0 comments on commit 4ae3ecf

Please sign in to comment.