From b1389a059c7f7b66383867bb0a1f8ea5af2f6672 Mon Sep 17 00:00:00 2001 From: fredgan Date: Wed, 17 Jun 2020 10:21:42 +0800 Subject: [PATCH 1/2] Bugfix #373 the get() operation not accepting multiple subtree filters --- ncclient/operations/util.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/ncclient/operations/util.py b/ncclient/operations/util.py index bb035027..b760fd19 100755 --- a/ncclient/operations/util.py +++ b/ncclient/operations/util.py @@ -56,6 +56,10 @@ def build_filter(spec, capcheck=None): rep.append(to_ele(criteria)) else: raise OperationError("Invalid filter type") + elif isinstance(spec, list): + rep = new_ele("filter", type="subtree") + for cri in spec: + rep.append(to_ele(cri)) else: rep = validated_element(spec, ("filter", qualify("filter"), From ac2f4bfafa7c54d5ee08497271ea44f5d7ed2c2c Mon Sep 17 00:00:00 2001 From: fredgan Date: Sat, 20 Jun 2020 16:47:05 +0800 Subject: [PATCH 2/2] add a testcase about multi subtree filters --- test/unit/operations/test_retrieve.py | 40 +++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/test/unit/operations/test_retrieve.py b/test/unit/operations/test_retrieve.py index fdf5b231..d5166fe4 100644 --- a/test/unit/operations/test_retrieve.py +++ b/test/unit/operations/test_retrieve.py @@ -220,3 +220,43 @@ def test_dispatch_2(self, mock_request): call = mock_request.call_args_list[0][0][0] call = ElementTree.tostring(call) self.assertEqual(call, xml) + + @patch('ncclient.operations.retrieve.RPC._request') + def test_get_with_multi_subtree_filters(self, mock_request): + result = ''' + + + + test_mod1_001 + this is a test-one example + + + test_mod2_002 + this is a test-two example + + a list of mod2 + + + ''' + mock_request.return_value = result + session = ncclient.transport.SSHSession(self.device_handler) + obj = Get(session, self.device_handler, raise_mode=RaiseMode.ALL) + + multi_subtree_filters = [ + ' \ + \ + \ + ', + '' + ] + + ret = obj.request(copy.deepcopy(multi_subtree_filters)) + node = new_ele("get") + node.append(util.build_filter(multi_subtree_filters)) + xml = ElementTree.tostring(node) + call = mock_request.call_args_list[0][0][0] + call = ElementTree.tostring(call) + self.assertEqual(call, xml) + self.assertEqual(ret, result) +