summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChristian Breunig <christian@breunig.cc>2023-03-29 21:12:22 +0200
committerGitHub <noreply@github.com>2023-03-29 21:12:22 +0200
commit623dfc9d87d513bd69e5e6eef9664056dc1a45f1 (patch)
tree912adf40656c8419c4833983f6a7da636d6c2d42
parentdb0fc992c0f1e3f1ab6f099b07b933bfca07d520 (diff)
parentdccc689e0e666787834f45e6ab6bd972fddabf95 (diff)
downloadvyos-1x-623dfc9d87d513bd69e5e6eef9664056dc1a45f1.tar.gz
vyos-1x-623dfc9d87d513bd69e5e6eef9664056dc1a45f1.zip
Merge pull request #1900 from jestabro/diff-test
configdiff: T5089: add unit test of config_diff
-rw-r--r--python/vyos/configtree.py27
-rw-r--r--src/tests/test_config_diff.py70
-rw-r--r--tests/data/config.left36
-rw-r--r--tests/data/config.right25
4 files changed, 155 insertions, 3 deletions
diff --git a/python/vyos/configtree.py b/python/vyos/configtree.py
index c0b3ebd78..9308bdde4 100644
--- a/python/vyos/configtree.py
+++ b/python/vyos/configtree.py
@@ -60,7 +60,7 @@ class ConfigTree(object):
self.__get_error.restype = c_char_p
self.__to_string = self.__lib.to_string
- self.__to_string.argtypes = [c_void_p]
+ self.__to_string.argtypes = [c_void_p, c_bool]
self.__to_string.restype = c_char_p
self.__to_commands = self.__lib.to_commands
@@ -160,8 +160,8 @@ class ConfigTree(object):
def _get_config(self):
return self.__config
- def to_string(self):
- config_string = self.__to_string(self.__config).decode()
+ def to_string(self, ordered_values=False):
+ config_string = self.__to_string(self.__config, ordered_values).decode()
config_string = "{0}\n{1}".format(config_string, self.__version)
return config_string
@@ -352,6 +352,27 @@ def show_diff(left, right, path=[], commands=False, libpath=LIBPATH):
return res
+def union(left, right, libpath=LIBPATH):
+ if left is None:
+ left = ConfigTree(config_string='\n')
+ if right is None:
+ right = ConfigTree(config_string='\n')
+ if not (isinstance(left, ConfigTree) and isinstance(right, ConfigTree)):
+ raise TypeError("Arguments must be instances of ConfigTree")
+
+ __lib = cdll.LoadLibrary(libpath)
+ __tree_union = __lib.tree_union
+ __tree_union.argtypes = [c_void_p, c_void_p]
+ __tree_union.restype = c_void_p
+ __get_error = __lib.get_error
+ __get_error.argtypes = []
+ __get_error.restype = c_char_p
+
+ res = __tree_union( left._get_config(), right._get_config())
+ tree = ConfigTree(address=res)
+
+ return tree
+
class DiffTree:
def __init__(self, left, right, path=[], libpath=LIBPATH):
if left is None:
diff --git a/src/tests/test_config_diff.py b/src/tests/test_config_diff.py
new file mode 100644
index 000000000..f61cbc4a2
--- /dev/null
+++ b/src/tests/test_config_diff.py
@@ -0,0 +1,70 @@
+#!/usr/bin/env python3
+#
+# Copyright (C) 2023 VyOS maintainers and contributors
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License version 2 or later as
+# published by the Free Software Foundation.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+import os
+import vyos.configtree
+
+from unittest import TestCase
+
+class TestConfigDiff(TestCase):
+ def setUp(self):
+ with open('tests/data/config.left', 'r') as f:
+ config_string = f.read()
+ self.config_left = vyos.configtree.ConfigTree(config_string)
+
+ with open('tests/data/config.right', 'r') as f:
+ config_string = f.read()
+ self.config_right = vyos.configtree.ConfigTree(config_string)
+
+ self.config_null = vyos.configtree.ConfigTree('')
+
+ def test_unit(self):
+ diff = vyos.configtree.DiffTree(self.config_left, self.config_null)
+ sub = diff.sub
+ self.assertEqual(sub.to_string(), self.config_left.to_string())
+
+ diff = vyos.configtree.DiffTree(self.config_null, self.config_left)
+ add = diff.add
+ self.assertEqual(add.to_string(), self.config_left.to_string())
+
+ def test_symmetry(self):
+ lr_diff = vyos.configtree.DiffTree(self.config_left,
+ self.config_right)
+ rl_diff = vyos.configtree.DiffTree(self.config_right,
+ self.config_left)
+
+ sub = lr_diff.sub
+ add = rl_diff.add
+ self.assertEqual(sub.to_string(), add.to_string())
+ add = lr_diff.add
+ sub = rl_diff.sub
+ self.assertEqual(add.to_string(), sub.to_string())
+
+ def test_identity(self):
+ lr_diff = vyos.configtree.DiffTree(self.config_left,
+ self.config_right)
+
+ sub = lr_diff.sub
+ inter = lr_diff.inter
+ add = lr_diff.add
+
+ r_union = vyos.configtree.union(add, inter)
+ l_union = vyos.configtree.union(sub, inter)
+
+ self.assertEqual(r_union.to_string(),
+ self.config_right.to_string(ordered_values=True))
+ self.assertEqual(l_union.to_string(),
+ self.config_left.to_string(ordered_values=True))
diff --git a/tests/data/config.left b/tests/data/config.left
new file mode 100644
index 000000000..e57c40396
--- /dev/null
+++ b/tests/data/config.left
@@ -0,0 +1,36 @@
+node1 {
+ tag_node foo {
+ valueless
+ multi_node 'v2'
+ multi_node 'v1'
+ single 'left_val'
+ }
+ tag_node bar {
+ node {
+ single 'v0'
+ }
+ }
+ tag_node other {
+ leaf 'leaf_l'
+ }
+}
+
+node3 {
+}
+
+node2 {
+ sub_node_other {
+ single 'val'
+ }
+ sub_node {
+ tag_node other {
+ single 'val'
+ }
+ tag_node bob {
+ valued 'baz'
+ }
+ tag_node duff {
+ valued 'buz'
+ }
+ }
+}
diff --git a/tests/data/config.right b/tests/data/config.right
new file mode 100644
index 000000000..48defeb89
--- /dev/null
+++ b/tests/data/config.right
@@ -0,0 +1,25 @@
+node1 {
+ tag_node baz {
+ other_node {
+ multi_node 'some_val'
+ multe_node 'other_val'
+ }
+ }
+ tag_node foo {
+ valueless
+ multi_node 'v3'
+ multi_node 'v1'
+ single 'right_val'
+ }
+ tag_node other {
+ leaf 'leaf_r'
+ }
+}
+
+node2 {
+ sub_node {
+ tag_node other {
+ multi 'mv'
+ }
+ }
+}