Browse Source

[2113] Validate against the spec

Michal 'vorner' Vaner 12 years ago
parent
commit
d3892b7807

+ 4 - 0
src/bin/cfgmgr/plugins/datasrc_config_plugin.py

@@ -22,6 +22,10 @@ def check(config):
     """
     Check the configuration.
     """
+    # Check the data layout first
+    errors=[]
+    if not spec.validate_config(False, config, errors):
+        return ' '.join(errors)
     # TODO: Once we have solved ticket #2051, create the list and
     # fill it with the configuration. We probably want to have some way
     # to not load the data sources, just the configuration. It could

+ 31 - 0
src/bin/cfgmgr/plugins/tests/datasrc_test.py

@@ -22,6 +22,20 @@ import datasrc_config_plugin
 import unittest
 
 class DatasrcTest(unittest.TestCase):
+    def reject(self, config):
+        """
+        Just a shortcut to check the config is rejected.
+        """
+        self.assertIsNotNone(datasrc_config_plugin.check({"datasources":
+                                                         config}))
+
+    def accept(self, config):
+        """
+        Just a shortcut to check the config is accepted.
+        """
+        self.assertIsNone(datasrc_config_plugin.check({"datasources":
+                                                      config}))
+
     def test_load(self):
         """
         Checks the entry point returns the correct values.
@@ -32,5 +46,22 @@ class DatasrcTest(unittest.TestCase):
         # The plugin stores it's spec
         self.assertEqual(spec, datasrc_config_plugin.spec)
 
+    def test_empty(self):
+        """
+        Check an empty input is OK.
+        """
+        self.assertIsNone(datasrc_config_plugin.check({}))
+
+    def test_invalid_spec(self):
+        """
+        Check it rejects stuff that is not well-formed according
+        to the spec.
+        """
+        self.reject("test")
+        self.reject(13)
+        self.reject([])
+        self.reject({"IN": {}})
+        self.reject({"IN": [{"bad-name": True}]})
+
 if __name__ == '__main__':
         unittest.main()