Clean up exception handling in TestON core
- Catch more specific exceptions
- Clearer Style
- Allow CTRL-C to exit the test cleanly whether from the TestON
cli or ruinning as a script
Change-Id: I7f2e8eeba5229f97c87e06b49a585933a61fa4ad
diff --git a/TestON/core/teston.py b/TestON/core/teston.py
index 4e53ee2..17893a1 100644
--- a/TestON/core/teston.py
+++ b/TestON/core/teston.py
@@ -146,8 +146,10 @@
try :
self.configDict = xmldict.xml_to_dict(xml)
return self.configDict
- except Exception:
+ except IOError:
print "There is no such file to parse " + self.configFile
+ else:
+ print "There is no such file to parse " + self.configFile
def componentInit(self,component):
'''
@@ -400,7 +402,13 @@
print "Disconnecting from " + str(tempObject.name) + ": " + \
str(tempObject)
tempObject.disconnect()
- except Exception:
+ except KeyboardInterrupt:
+ pass
+ except KeyError:
+ # Component not created yet
+ self.log.warn( "Could not find the component " +
+ str( component ) )
+ except StandardError:
self.log.exception( "Exception while disconnecting from " +
str( component ) )
result = self.FALSE
@@ -408,7 +416,14 @@
for driver in self.componentDictionary.keys():
try:
vars(self)[driver].close_log_handles()
- except Exception:
+ except KeyboardInterrupt:
+ pass
+ except KeyError:
+ # Component not created yet
+ self.log.warn( "Could not find the component " +
+ str( driver ) + " while trying to" +
+ " close log file" )
+ except StandardError:
self.log.exception( "Exception while closing log files for " +
str( driver ) )
result = self.FALSE
@@ -440,7 +455,7 @@
for component in self.componentDictionary.keys():
tempObject = vars(self)[component]
result = tempObject.onfail()
- except(Exception),e:
+ except StandardError as e:
print str(e)
result = self.FALSE
else:
@@ -448,7 +463,7 @@
for component in components:
tempObject = vars(self)[component]
result = tempObject.onfail()
- except(Exception),e:
+ except StandardError as e:
print str(e)
result = self.FALSE
@@ -558,7 +573,7 @@
try :
import json
response_dict = json.loads(response)
- except Exception:
+ except StandardError:
self.log.exception( "Json Parser is unable to parse the string" )
return response_dict
elif ini_match :
@@ -573,8 +588,8 @@
self.log.info(" Response is in 'XML' format and Converting to '"+return_format+"' format")
try :
response_dict = xmldict.xml_to_dict("<response> "+str(response)+" </response>")
- except Exception, e:
- self.log.exception( e )
+ except StandardError:
+ self.log.exception()
return response_dict
def dict_to_return_format(self,response,return_format,response_dict):
@@ -635,7 +650,10 @@
try:
thread._Thread__stop()
except:
- print(str(thread.getName()) + ' could not be terminated' )
+ # NOTE: We should catch any exceptions while trying to
+ # close the thread so that we can try to close the other
+ # threads as well
+ print( str( thread.getName() ) + ' could not be terminated' )
sys.exit()
def verifyOptions(options):
@@ -760,7 +778,7 @@
main.exit()
try :
testModule = __import__(main.classPath, globals(), locals(), [main.TEST], -1)
- except(ImportError):
+ except ImportError:
print "There was an import error, it might mean that there is no test named "+main.TEST
main.exit()
@@ -773,12 +791,12 @@
def verifyParams():
try :
main.params = main.params['PARAMS']
- except(KeyError):
+ except KeyError:
print "Error with the params file: Either the file not specified or the format is not correct"
main.exit()
try :
main.topology = main.topology['TOPOLOGY']
- except(KeyError):
+ except KeyError:
print "Error with the Topology file: Either the file not specified or the format is not correct"
main.exit()