Parcourir la source

[trac588] update exception handling logic

chenzhengzhang il y a 14 ans
Parent
commit
cbc69359f3
2 fichiers modifiés avec 24 ajouts et 7 suppressions
  1. 6 3
      src/bin/bindctl/bindcmd.py
  2. 18 4
      src/bin/bindctl/tests/bindctl_test.py

+ 6 - 3
src/bin/bindctl/bindcmd.py

@@ -123,10 +123,13 @@ class BindCmdInterpreter(Cmd):
         except FailToLogin as err:
             # error already printed when this was raised, ignoring
             pass
-        except (KeyboardInterrupt, socket.error):
+        except KeyboardInterrupt:
             print('\nExit from bindctl')
-        except http.client.CannotSendRequest:
-            print('Failed to send request, the connection is busy')
+        except socket.error as err:
+            if sys.stdin.isatty():
+                print("Socket error while sending request:", err)
+            else:
+                print('Exit from bindctl')
 
     def _get_saved_user_info(self, dir, file_name):
         ''' Read all the available username and password pairs saved in 

+ 18 - 4
src/bin/bindctl/tests/bindctl_test.py

@@ -17,6 +17,8 @@
 import unittest
 import isc.cc.data
 import os
+import sys
+import socket
 from isc.config.config_data import ConfigData, MultiConfigData
 from isc.config.module_spec import ModuleSpec
 from bindctl import cmdparse
@@ -271,7 +273,7 @@ class FakeCCSession(MultiConfigData):
                  ]
                }
         self.set_specification(ModuleSpec(spec))
-    
+
 
 class TestConfigCommands(unittest.TestCase):
     def setUp(self):
@@ -279,7 +281,21 @@ class TestConfigCommands(unittest.TestCase):
         mod_info = ModuleInfo(name = "foo")
         self.tool.add_module_info(mod_info)
         self.tool.config_data = FakeCCSession()
-        
+
+    def test_run(self):
+        stdout_backup = sys.stdout
+        sys.stdout = open(os.devnull, 'w')
+        def login_to_cmdctl():
+            return True
+        def loop():
+            self.tool._send_message("/module_spec", None)
+        self.tool.login_to_cmdctl = login_to_cmdctl
+        self.tool.cmdloop = loop
+
+        self.tool.conn.close()
+        self.assertRaises(None, self.tool.run())
+        sys.stdout = stdout_backup
+
     def test_apply_cfg_command_int(self):
         self.tool.location = '/'
 
@@ -333,8 +349,6 @@ class TestConfigCommands(unittest.TestCase):
         self.assertRaises(isc.cc.data.DataTypeError, self.tool.apply_config_cmd, cmd)
 
 
-    
-
 class FakeBindCmdInterpreter(bindcmd.BindCmdInterpreter):
     def __init__(self):
         pass