Parcourir la source

[trac967] added some more tests for nested lists/maps

and added a check for the right identifier
Jelte Jansen il y a 14 ans
Parent
commit
9b93c07786

+ 1 - 0
src/lib/config/tests/testdata/Makefile.am

@@ -52,3 +52,4 @@ EXTRA_DIST += spec27.spec
 EXTRA_DIST += spec28.spec
 EXTRA_DIST += spec28.spec
 EXTRA_DIST += spec29.spec
 EXTRA_DIST += spec29.spec
 EXTRA_DIST += spec30.spec
 EXTRA_DIST += spec30.spec
+EXTRA_DIST += spec31.spec

+ 63 - 0
src/lib/config/tests/testdata/spec31.spec

@@ -0,0 +1,63 @@
+{
+    "module_spec": {
+        "module_name": "lists",
+        "module_description": "Logging options",
+        "config_data": [
+            {
+                "item_name": "first_list_items",
+                "item_type": "list",
+                "item_optional": false,
+                "item_default": [],
+                "list_item_spec": {
+                  "item_name": "first_list_item",
+                  "item_type": "map",
+                  "item_optional": false,
+                  "item_default": {},
+                  "map_item_spec": [
+                  {  "item_name": "foo",
+                     "item_type": "string",
+                     "item_optional": false,
+                     "item_default": "foo"
+                  },
+                  { "item_name": "second_list_items",
+                    "item_type": "list",
+                    "item_optional": false,
+                    "item_default": [],
+                    "list_item_spec": {
+                      "item_name": "second_list_item",
+                      "item_type": "map",
+                      "item_optional": false,
+                      "item_default": {},
+                      "map_item_spec": [
+                      { "item_name": "map_element",
+                        "item_type": "map",
+                        "item_optional": false,
+                        "item_default": {},
+                        "map_item_spec": [
+                        { "item_name": "list1",
+                          "item_type": "list",
+                          "item_optional": false,
+                          "item_default": [],
+                          "list_item_spec":
+                          { "item_name": "list2",
+                            "item_type": "list",
+                            "item_optional": false,
+                            "item_default": [],
+                            "list_item_spec":
+                            { "item_name": "number",
+                              "item_type": "integer",
+                              "item_optional": false,
+                              "item_default": 1
+                            }
+                          }
+                        }]
+                      }
+                      ]
+                    }
+                  }
+                  ]
+                }
+            }
+        ]
+    }
+}

+ 5 - 2
src/lib/python/isc/config/config_data.py

@@ -133,7 +133,7 @@ def _find_spec_part_single(cur_spec, id_part):
     # The specification we want a sub-part for should be either a
     # The specification we want a sub-part for should be either a
     # list or a map, which is internally represented by a dict with
     # list or a map, which is internally represented by a dict with
     # an element 'map_item_spec', a dict with an element 'list_item_spec',
     # an element 'map_item_spec', a dict with an element 'list_item_spec',
-    # or a list.
+    # or a list (when it is the 'main' config_data element of a module).
     if type(cur_spec) == dict and 'map_item_spec' in cur_spec.keys():
     if type(cur_spec) == dict and 'map_item_spec' in cur_spec.keys():
         for cur_spec_item in cur_spec['map_item_spec']:
         for cur_spec_item in cur_spec['map_item_spec']:
             if cur_spec_item['item_name'] == id:
             if cur_spec_item['item_name'] == id:
@@ -141,7 +141,10 @@ def _find_spec_part_single(cur_spec, id_part):
         # not found
         # not found
         raise isc.cc.data.DataNotFoundError(id + " not found")
         raise isc.cc.data.DataNotFoundError(id + " not found")
     elif type(cur_spec) == dict and 'list_item_spec' in cur_spec.keys():
     elif type(cur_spec) == dict and 'list_item_spec' in cur_spec.keys():
-        return cur_spec['list_item_spec']
+        if cur_spec['item_name'] == id:
+            return cur_spec['list_item_spec']
+        # not found
+        raise isc.cc.data.DataNotFoundError(id + " not found")
     elif type(cur_spec) == list:
     elif type(cur_spec) == list:
         for cur_spec_item in cur_spec:
         for cur_spec_item in cur_spec:
             if cur_spec_item['item_name'] == id:
             if cur_spec_item['item_name'] == id:

+ 21 - 0
src/lib/python/isc/config/tests/config_data_test.py

@@ -337,6 +337,27 @@ class TestMultiConfigData(unittest.TestCase):
         spec_part = self.mcd.find_spec_part("/BAD_NAME/first_list_items[0]/second_list_items[1]/final_element")
         spec_part = self.mcd.find_spec_part("/BAD_NAME/first_list_items[0]/second_list_items[1]/final_element")
         self.assertEqual(None, spec_part)
         self.assertEqual(None, spec_part)
 
 
+    def test_find_spec_part_nested2(self):
+        module_spec = isc.config.module_spec_from_file(self.data_path + os.sep + "spec31.spec")
+        self.mcd.set_specification(module_spec)
+        spec_part = self.mcd.find_spec_part("/lists/first_list_items[0]/second_list_items[1]/map_element/list1[1]/list2[2]")
+        self.assertEqual({"item_name": "number", "item_type": "integer", "item_optional": False, "item_default": 1}, spec_part)
+
+        spec_part = self.mcd.find_spec_part("/DOESNOTEXIST")
+        self.assertEqual(None, spec_part)
+        spec_part = self.mcd.find_spec_part("/lists/DOESNOTEXIST")
+        self.assertEqual(None, spec_part)
+        spec_part = self.mcd.find_spec_part("/lists/first_list_items[0]/DOESNOTEXIST")
+        self.assertEqual(None, spec_part)
+        spec_part = self.mcd.find_spec_part("/lists/first_list_items[0]/second_list_items[1]/DOESNOTEXIST")
+        self.assertEqual(None, spec_part)
+        spec_part = self.mcd.find_spec_part("/lists/first_list_items[0]/second_list_items[1]/map_element/DOESNOTEXIST")
+        self.assertEqual(None, spec_part)
+        spec_part = self.mcd.find_spec_part("/lists/first_list_items[0]/second_list_items[1]/map_element/list1[1]/DOESNOTEXIST")
+        self.assertEqual(None, spec_part)
+        spec_part = self.mcd.find_spec_part("/lists/first_list_items[0]/second_list_items[1]/map_element/list1[1]/list2[1]/DOESNOTEXIST")
+        self.assertEqual(None, spec_part)
+
     def test_get_current_config(self):
     def test_get_current_config(self):
         cf = { 'module1': { 'item1': 2, 'item2': True } }
         cf = { 'module1': { 'item1': 2, 'item2': True } }
         self.mcd._set_current_config(cf);
         self.mcd._set_current_config(cf);