Browse Source

Merge #1715

Crash of bindctl when the config unset command was invoked. The code of
the command was missing, probably never implemented and obviously never
tested.
Michal 'vorner' Vaner 13 years ago
parent
commit
098da24ddd

+ 10 - 0
src/bin/bindctl/tests/bindctl_test.py

@@ -365,10 +365,20 @@ class TestConfigCommands(unittest.TestCase):
         self.assertEqual((5, MultiConfigData.LOCAL),
                          self.tool.config_data.get_value("/foo/an_int"))
 
+        cmd = cmdparse.BindCmdParse("config unset identifier=\"foo/an_int\"")
+        self.tool.apply_config_cmd(cmd)
+
+        self.assertEqual((1, MultiConfigData.DEFAULT),
+                         self.tool.config_data.get_value("/foo/an_int"))
+
         # this should raise a NotFoundError
         cmd = cmdparse.BindCmdParse("config set identifier=\"foo/bar\" value=\"[]\"")
         self.assertRaises(isc.cc.data.DataNotFoundError, self.tool.apply_config_cmd, cmd)
 
+        cmd = cmdparse.BindCmdParse("config unset identifier=\"foo/bar\"")
+        self.assertRaises(isc.cc.data.DataNotFoundError,
+                          self.tool.apply_config_cmd, cmd)
+
         # this should raise a TypeError
         cmd = cmdparse.BindCmdParse("config set identifier=\"foo/an_int\" value=\"[]\"")
         self.assertRaises(isc.cc.data.DataTypeError, self.tool.apply_config_cmd, cmd)

+ 10 - 0
src/lib/python/isc/config/config_data.py

@@ -672,6 +672,16 @@ class MultiConfigData:
                 self._append_value_item(result, spec_part, identifier, all, True)
         return result
 
+    def unset(self, identifier):
+        """
+        Reset the value to default.
+        """
+        spec_part = self.find_spec_part(identifier)
+        if spec_part is not None:
+            isc.cc.data.unset(self._local_changes, identifier)
+        else:
+            raise isc.cc.data.DataNotFoundError(identifier + "not found")
+
     def set_value(self, identifier, value):
         """Set the local value at the given identifier to value. If
            there is a specification for the given identifier, the type

+ 32 - 1
src/lib/python/isc/config/tests/config_data_test.py

@@ -657,7 +657,38 @@ class TestMultiConfigData(unittest.TestCase):
         self.assertEqual(MultiConfigData.LOCAL, status)
 
         self.assertRaises(isc.cc.data.DataTypeError, self.mcd.set_value, "Spec2/item5[a]", "asdf")
-        
+
+
+    def test_unset(self):
+        """
+        Test the unset command works.
+        """
+        module_spec = isc.config.module_spec_from_file(self.data_path + os.sep + "spec2.spec")
+        self.mcd.set_specification(module_spec)
+        self.mcd.set_specification(module_spec)
+        value, status = self.mcd.get_value("Spec2/item1")
+        # This is the default first
+        self.assertEqual(1, value)
+        self.assertEqual(MultiConfigData.DEFAULT, status)
+        # Unseting a default item does nothing.
+        self.mcd.unset("Spec2/item1")
+        value, status = self.mcd.get_value("Spec2/item1")
+        # This should be the default
+        self.assertEqual(1, value)
+        self.assertEqual(MultiConfigData.DEFAULT, status)
+        # Set it to something else
+        self.mcd.set_value("Spec2/item1", 42)
+        value, status = self.mcd.get_value("Spec2/item1")
+        self.assertEqual(42, value)
+        self.assertEqual(MultiConfigData.LOCAL, status)
+        # Try to unset it
+        self.mcd.unset("Spec2/item1")
+        value, status = self.mcd.get_value("Spec2/item1")
+        # This should be the default
+        self.assertEqual(1, value)
+        self.assertEqual(MultiConfigData.DEFAULT, status)
+        # Unset a nonexisting item. Should raise.
+        self.assertRaises(isc.cc.data.DataNotFoundError, self.mcd.unset, "Spec2/doesnotexist")
 
     def test_get_config_item_list(self):
         config_items = self.mcd.get_config_item_list()