From 80a96d620bdf47ba0b3e109f3789a9777a2eb6ba Mon Sep 17 00:00:00 2001 From: Daniil Baturin Date: Thu, 14 Feb 2019 23:57:59 +0100 Subject: [vyos.configtree] T1248: add a function for node copying Also improve sanity checks in the rename function and add unit tests for copy and rename. --- debian/control | 2 +- python/vyos/configtree.py | 31 +++++++++++++++++++++++++++---- src/tests/test_config_parser.py | 18 ++++++++++++++++++ 3 files changed, 46 insertions(+), 5 deletions(-) diff --git a/debian/control b/debian/control index 93608d888..859c36f14 100644 --- a/debian/control +++ b/debian/control @@ -11,7 +11,7 @@ Build-Depends: debhelper (>= 9), python3-nose, python3-coverage, whois, - libvyosconfig0 + libvyosconfig0 (>= 0.0.7) Standards-Version: 3.9.6 Package: vyos-1x diff --git a/python/vyos/configtree.py b/python/vyos/configtree.py index 39fe41669..1f71ddd7c 100644 --- a/python/vyos/configtree.py +++ b/python/vyos/configtree.py @@ -118,6 +118,10 @@ class ConfigTree(object): self.__rename.argtypes = [c_void_p, c_char_p, c_char_p] self.__rename.restype = c_int + self.__copy = self.__lib.copy_node + self.__copy.argtypes = [c_void_p, c_char_p, c_char_p] + self.__copy.restype = c_int + self.__set_replace_value = self.__lib.set_replace_value self.__set_replace_value.argtypes = [c_void_p, c_char_p, c_char_p] self.__set_replace_value.restype = c_int @@ -201,12 +205,31 @@ class ConfigTree(object): self.__delete_value(self.__config, path_str, value.encode()) - def rename(self, path, newname): + def rename(self, path, new_name): check_path(path) path_str = " ".join(map(str, path)).encode() - newname_str = newname.encode() - - self.__rename(self.__config, path_str, newname_str) + newname_str = new_name.encode() + + # Check if a node with intended new name already exists + new_path = path[:-1] + [new_name] + if self.exists(new_path): + raise ConfigTreeError() + res = self.__rename(self.__config, path_str, newname_str) + if (res != 0): + raise ConfigTreeError("Path [{}] doesn't exist".format(oldpath)) + + def copy(self, old_path, new_path): + check_path(old_path) + check_path(new_path) + oldpath_str = " ".join(map(str, old_path)).encode() + newpath_str = " ".join(map(str, new_path)).encode() + + # Check if a node with intended new name already exists + if self.exists(new_path): + raise ConfigTreeError() + res = self.__copy(self.__config, oldpath_str, newpath_str) + if (res != 0): + raise ConfigTreeError("Path [{}] doesn't exist".format(oldpath)) def exists(self, path): check_path(path) diff --git a/src/tests/test_config_parser.py b/src/tests/test_config_parser.py index f58ff23bf..e47770a7f 100644 --- a/src/tests/test_config_parser.py +++ b/src/tests/test_config_parser.py @@ -41,3 +41,21 @@ class TestConfigParser(TestCase): self.assertTrue(self.config.exists(["top-level-tag-node"])) # No sorting is intentional, child order must be preserved self.assertEqual(self.config.list_nodes(["top-level-tag-node"]), ["foo", "bar"]) + + def test_copy(self): + self.config.copy(["top-level-tag-node", "bar"], ["top-level-tag-node", "baz"]) + print(self.config.to_string()) + self.assertTrue(self.config.exists(["top-level-tag-node", "baz"])) + + def test_copy_duplicate(self): + with self.assertRaises(vyos.configtree.ConfigTreeError): + self.config.copy(["top-level-tag-node", "foo"], ["top-level-tag-node", "bar"]) + + def test_rename(self): + self.config.rename(["top-level-tag-node", "bar"], "quux") + print(self.config.to_string()) + self.assertTrue(self.config.exists(["top-level-tag-node", "quux"])) + + def test_rename_duplicate(self): + with self.assertRaises(vyos.configtree.ConfigTreeError): + self.config.rename(["top-level-tag-node", "foo"], "bar") -- cgit v1.2.3