Parcourir la source

oh no more unittests

git-svn-id: svn://bind10.isc.org/svn/bind10/trunk@970 e5f2f494-b856-4b98-b285-d166d9295462
Jelte Jansen il y a 15 ans
Parent
commit
91847ce76d

+ 2 - 0
src/lib/config/python/isc/config/ccsession.py

@@ -46,6 +46,8 @@ def parse_answer(msg):
     """Returns a tuple (rcode, value), where value depends on the
        command that was called. If rcode != 0, value is a string
        containing an error message"""
+    if type(msg) != dict:
+        raise ModuleCCSessionError("Answer message is not a dict: " + str(msg))
     if 'result' not in msg:
         raise ModuleCCSessionError("answer message does not contain 'result' element")
     elif type(msg['result']) != list:

+ 24 - 11
src/lib/config/python/isc/config/cfgmgr.py

@@ -91,9 +91,16 @@ class ConfigManagerData:
             file.write(s)
             file.write("\n")
             file.close()
-            os.rename(tmp_filename, self.db_filename)
+            if output_file_name:
+                os.rename(tmp_filename, output_file_name)
+            else:
+                os.rename(tmp_filename, self.db_filename)
         except IOError as ioe:
-            print("Unable to write config file; configuration not stored")
+            # TODO: log this (level critical)
+            print("[b10-cfgmgr] Unable to write config file; configuration not stored: " + str(ioe))
+        except OSError as ose:
+            # TODO: log this (level critical)
+            print("[b10-cfgmgr] Unable to write config file; configuration not stored: " + str(ose))
 
     def __eq__(self, other):
         """Returns True if the data contained is equal. data_path and
@@ -153,7 +160,7 @@ class ConfigManager:
         config_data = {}
         if name:
             if name in self.module_specs:
-                config_data[name] = self.module_specs[name].get_data
+                config_data[name] = self.module_specs[name].get_config_spec()
         else:
             for module_name in self.module_specs.keys():
                 config_data[module_name] = self.module_specs[module_name].get_config_spec()
@@ -166,7 +173,7 @@ class ConfigManager:
         commands = {}
         if name:
             if name in self.module_specs:
-                commands[name] = self.module_specs[name].get_commands_spec
+                commands[name] = self.module_specs[name].get_commands_spec()
         else:
             for module_name in self.module_specs.keys():
                 commands[module_name] = self.module_specs[module_name].get_commands_spec()
@@ -243,9 +250,10 @@ class ConfigManager:
                 self.cc.group_sendmsg({ "config_update": conf_part[module_name] }, module_name)
                 # replace 'our' answer with that of the module
                 answer, env = self.cc.group_recvmsg(False)
-            rcode, val = isc.config.ccsession.parse_answer(answer)
-            if rcode == 0:
-                self.write_config()
+            if answer:
+                rcode, val = isc.config.ccsession.parse_answer(answer)
+                if rcode == 0:
+                    self.write_config()
         elif len(cmd) == 1:
             # todo: use api (and check the data against the definition?)
             old_data = self.config.data.copy()
@@ -257,10 +265,14 @@ class ConfigManager:
                 if module != "version":
                     self.cc.group_sendmsg({ "config_update": self.config.data[module] }, module)
                     answer, env = self.cc.group_recvmsg(False)
-                    rcode, val = isc.config.ccsession.parse_answer(answer)
-                    if rcode != 0:
+                    if answer == None:
                         got_error = True
-                        err_list.append(val)
+                        err_list.append("No answer message from " + module)
+                    else:
+                        rcode, val = isc.config.ccsession.parse_answer(answer)
+                        if rcode != 0:
+                            got_error = True
+                            err_list.append(val)
             if not got_error:
                 self.write_config()
                 answer = isc.config.ccsession.create_answer(0)
@@ -272,7 +284,7 @@ class ConfigManager:
             print(cmd)
             answer = isc.config.ccsession.create_answer(1, "Wrong number of arguments")
         if not answer:
-            answer = isc.config.ccsession.create_answer(1, "Error handling set_config command")
+            answer = isc.config.ccsession.create_answer(1, "No answer message from " + cmd[0])
             
         return answer
 
@@ -312,6 +324,7 @@ class ConfigManager:
             elif cmd == isc.config.ccsession.COMMAND_SET_CONFIG:
                 answer = self._handle_set_config(arg)
             elif cmd == "shutdown":
+                # TODO: logging
                 print("[b10-cfgmgr] Received shutdown command")
                 self.running = False
                 answer = isc.config.ccsession.create_answer(0)

+ 82 - 8
src/lib/config/python/isc/config/cfgmgr_test.py

@@ -99,6 +99,9 @@ class FakeModuleCCSession:
     def group_sendmsg(self, msg, channel, target = None):
         self.message_queue.append([ channel, target, msg ])
 
+    def group_reply(self, env, msg):
+        pass
+
     def group_recvmsg(self, blocking):
         for qm in self.message_queue:
             if qm[0] in self.subscriptions and (qm[1] == None or qm[1] in self.subscriptions[qm[0]]):
@@ -176,6 +179,9 @@ class TestConfigManager(unittest.TestCase):
         self.assert_(module_spec.get_module_name() in self.cm.module_specs)
         config_spec = self.cm.get_config_spec()
         self.assertEqual(config_spec['Spec2'], module_spec.get_config_spec())
+        config_spec = self.cm.get_config_spec('Spec2')
+        self.assertEqual(config_spec['Spec2'], module_spec.get_config_spec())
+        
     
     def test_get_commands_spec(self):
         commands_spec = self.cm.get_commands_spec()
@@ -193,11 +199,17 @@ class TestConfigManager(unittest.TestCase):
         self.assert_(module_spec.get_module_name() in self.cm.module_specs)
         commands_spec = self.cm.get_commands_spec()
         self.assertEqual(commands_spec['Spec2'], module_spec.get_commands_spec())
+        commands_spec = self.cm.get_commands_spec('Spec2')
+        self.assertEqual(commands_spec['Spec2'], module_spec.get_commands_spec())
 
     def test_read_config(self):
         self.assertEqual(self.cm.config.data, {'version': 1})
         self.cm.read_config()
-        self.assertEqual(self.cm.config.data, {'TestModule': {'test': 124}, 'version': 1})
+        # due to what get written, the value here is what the last set_config command in test_handle_msg does
+        self.assertEqual(self.cm.config.data, {'TestModule': {'test': 125}, 'version': 1})
+        self.cm.data_path = "/no_such_path"
+        self.cm.read_config()
+        self.assertEqual(self.cm.config.data, {'version': 1})
 
     def test_write_config(self):
         # tested in ConfigManagerData tests
@@ -213,6 +225,7 @@ class TestConfigManager(unittest.TestCase):
         self._handle_msg_helper({ "command": [ "badcommand" ] }, { 'result': [ 1, "Unknown command: badcommand"]})
         self._handle_msg_helper({ "command": [ "get_commands_spec" ] }, { 'result': [ 0, {} ]})
         self._handle_msg_helper({ "command": [ "get_module_spec" ] }, { 'result': [ 0, {} ]})
+        self._handle_msg_helper({ "command": [ "get_module_spec", { "module_name": "Spec2" } ] }, { 'result': [ 0, {} ]})
         #self._handle_msg_helper({ "command": [ "get_module_spec", { "module_name": "nosuchmodule" } ] },
         #                        {'result': [1, 'No specification for module nosuchmodule']})
         self._handle_msg_helper({ "command": [ "get_module_spec", 1 ] },
@@ -236,21 +249,77 @@ class TestConfigManager(unittest.TestCase):
         # those in our fake msgq first.
         my_ok_answer = { 'result': [ 0 ] }
 
+
+        # Send the 'ok' that cfgmgr expects back to the fake queue first
         self.fake_session.group_sendmsg(my_ok_answer, "ConfigManager")
+        # then send the command
         self._handle_msg_helper({ "command": [ "set_config", [self.name, { "test": 123 }] ] },
                                 my_ok_answer)
+        # The cfgmgr should have eaten the ok message, and sent out an update again
         self.assertEqual(len(self.fake_session.message_queue), 1)
-        self.fake_session.group_sendmsg(my_ok_answer, "ConfigManager")
         self.assertEqual({'config_update': {'test': 123}},
                          self.fake_session.get_message(self.name, None))
-        self._handle_msg_helper({ "command": [ "set_config", [self.name, { "test": 124 }] ] },
-                                {'result': [0]})
+        # and the queue should now be empty again
+        self.assertEqual(len(self.fake_session.message_queue), 0)
 
-        #print(self.fake_session.message_queue)
+        # below are variations of the theme above
+        self.fake_session.group_sendmsg(my_ok_answer, "ConfigManager")
+        self._handle_msg_helper({ "command": [ "set_config", [self.name, { "test": 124 }] ] },
+                                my_ok_answer)
         self.assertEqual(len(self.fake_session.message_queue), 1)
         self.assertEqual({'config_update': {'test': 124}},
                          self.fake_session.get_message(self.name, None))
-        self.assertEqual({'version': 1, 'TestModule': {'test': 124}}, self.cm.config.data)
+        self.assertEqual(len(self.fake_session.message_queue), 0)
+
+
+        # This is the last 'succes' one, the value set here is what test_read_config expects
+        self.fake_session.group_sendmsg(my_ok_answer, "ConfigManager")
+        self._handle_msg_helper({ "command": [ "set_config", [ { self.name: { "test": 125 } }] ] },
+                                my_ok_answer )
+        self.assertEqual(len(self.fake_session.message_queue), 1)
+        self.assertEqual({'config_update': {'test': 125}},
+                         self.fake_session.get_message(self.name, None))
+        self.assertEqual(len(self.fake_session.message_queue), 0)
+
+        self.fake_session.group_sendmsg({ 'result': "bad_answer" }, "ConfigManager")
+        self.assertRaises(isc.config.ccsession.ModuleCCSessionError,
+                          self.cm.handle_msg,
+                          { "command": [ "set_config", [ { self.name: { "test": 125 } }] ] } )
+        self.assertEqual(len(self.fake_session.message_queue), 1)
+        self.assertEqual({'config_update': {'test': 125}},
+                         self.fake_session.get_message(self.name, None))
+        self.assertEqual(len(self.fake_session.message_queue), 0)
+
+        my_bad_answer = { 'result': [1, "bad_answer"] }
+        self.fake_session.group_sendmsg(my_bad_answer, "ConfigManager")
+        self._handle_msg_helper({ "command": [ "set_config", [ { self.name: { "test": 125 } }] ] },
+                                my_bad_answer )
+        self.assertEqual(len(self.fake_session.message_queue), 1)
+        self.assertEqual({'config_update': {'test': 125}},
+                         self.fake_session.get_message(self.name, None))
+        self.assertEqual(len(self.fake_session.message_queue), 0)
+
+        my_bad_answer = { 'result': [1, "bad_answer"] }
+        self.fake_session.group_sendmsg(my_bad_answer, "ConfigManager")
+        self._handle_msg_helper({ "command": [ "set_config", [ self.name, { "test": 125 }] ] },
+                                my_bad_answer )
+        self.assertEqual(len(self.fake_session.message_queue), 1)
+        self.assertEqual({'config_update': {'test': 125}},
+                         self.fake_session.get_message(self.name, None))
+        self.assertEqual(len(self.fake_session.message_queue), 0)
+
+        self._handle_msg_helper({ "command": [ "set_config", [ ] ] },
+                                {'result': [1, 'Wrong number of arguments']} )
+        self._handle_msg_helper({ "command": [ "set_config", [ { self.name: { "test": 125 } }] ] },
+                                { 'result': [1, 'No answer message from TestModule']} )
+        self._handle_msg_helper({ "command": [ "set_config", [ self.name, { "test": 125 }] ] },
+                                { 'result': [1, 'No answer message from TestModule']} )
+
+        #self.assertEqual(len(self.fake_session.message_queue), 1)
+        #self.assertEqual({'config_update': {'test': 124}},
+        #                 self.fake_session.get_message(self.name, None))
+        #self.assertEqual({'version': 1, 'TestModule': {'test': 124}}, self.cm.config.data)
+        #
         self._handle_msg_helper({ "command": 
                                   ["module_spec", self.spec.get_full_spec()]
                                 },
@@ -267,10 +336,15 @@ class TestConfigManager(unittest.TestCase):
         #                 self.fake_session.get_message("Cmd-Ctrld", None))
         #self.assertEqual({'commands_update': [ self.name, self.commands ] },
         #                 self.fake_session.get_message("Cmd-Ctrld", None))
-        
-        
+
+        self._handle_msg_helper({ "command": 
+                                  ["shutdown"]
+                                },
+                                {'result': [0]})
 
     def test_run(self):
+        self.fake_session.group_sendmsg({ "command": [ "get_commands_spec" ] }, "ConfigManager")
+        self.cm.run()
         pass
 
 

+ 1 - 1
src/lib/config/testdata/b10-config.db

@@ -1 +1 @@
-{'version': 1}
+{'TestModule': {'test': 125}, 'version': 1}