Fix reading of nested cnfvars from string
authorAdrian Müller <adrian.mueller@intra2net.com>
Wed, 21 Aug 2024 13:35:35 +0000 (15:35 +0200)
committerChristian Herdtweck <christian.herdtweck@intra2net.com>
Mon, 30 Sep 2024 08:49:02 +0000 (10:49 +0200)
The function parse_cnf_children previously disregarded the children
received from a recursive parse_cnf_children call if the state was None
after the recursive function call. Hence, the leftover children would
be omitted instead of attached to the corresponding parent cnfvar. This
lead to the following configuration omitting the _IP and _NETMASK
entries:

1 FIREWALL_NETGROUP,99: "QA host IP"
2   (1) FIREWALL_NETGROUP_NETWORK,0: ""
3     (2) FIREWALL_NETGROUP_NETWORK_IP,0: "192.168.1.254"
4     (2) FIREWALL_NETGROUP_NETWORK_NETMASK,0: "255.255.255.255"

src/cnfvar/string.py
test/cnfvar/test_store.py

index 5a8ba24..b59058c 100644 (file)
@@ -553,9 +553,9 @@ def parse_cnf_children(state, parent):
                 # parent is further down in hierarchy -> new level
                 (state, children, new_parent) = \
                     parse_cnf_children (state, new_parent)
+                cnf_line["children"] = children
                 if state is None:
                     break
-                cnf_line["children"] = children
                 current = get(state)
                 new_parent = get_parent(current)
                 if new_parent is None:
index 1b48732..0da57a8 100644 (file)
@@ -84,6 +84,19 @@ class TestStore(unittest.TestCase):
 
         self.assertEqual(str(cnf_input).splitlines(), expected_input.splitlines())
 
+    def test_correct_parsing_of_nested_cnf_from_string(self):
+        """Test that grandchildren cnfvars aren't disregarded when no other child cnfvar follows them."""
+        input = dedent("""\
+            1 FIREWALL_NETGROUP,99: "QA host IP"
+            2   (1) FIREWALL_NETGROUP_NETWORK,0: ""
+            3     (2) FIREWALL_NETGROUP_NETWORK_IP,0: "192.168.1.254"
+            4     (2) FIREWALL_NETGROUP_NETWORK_NETMASK,0: "255.255.255.255"
+            """)
+
+        cnf = CnfList.from_cnf_string(input)
+
+        self.assertEqual(str(cnf).splitlines(), input.splitlines())
+
     def test_deleting_cnfvars(self):
         """Test that we can delete top-level cnfvars."""
         # To avoid creating long dummy objects, we can convert our test file