Module EDTestCasePluginExecute
[hide private]
[frames] | no frames]

Source Code for Module EDTestCasePluginExecute

  1  # coding: utf8 
  2  # 
  3  #    Project: The EDNA Kernel 
  4  #             http://www.edna-site.org 
  5  # 
  6  #    File: "$Id$" 
  7  # 
  8  #    Copyright (C) 2008-2012 European Synchrotron Radiation Facility 
  9  #                            Grenoble, France 
 10  # 
 11  #    Principal authors: Marie-Francoise Incardona (incardon@esrf.fr) 
 12  #                       Olof Svensson (svensson@esrf.fr)  
 13  #                       Jérôme Kieffer (jerome.kieffer@esrf.fr) 
 14  # 
 15  #    This program is free software: you can redistribute it and/or modify 
 16  #    it under the terms of the GNU Lesser General Public License as published 
 17  #    by the Free Software Foundation, either version 3 of the License, or 
 18  #    (at your option) any later version. 
 19  # 
 20  #    This program is distributed in the hope that it will be useful, 
 21  #    but WITHOUT ANY WARRANTY; without even the implied warranty of 
 22  #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 23  #    GNU Lesser General Public License for more details. 
 24  # 
 25  #    You should have received a copy of the GNU General Public License 
 26  #    and the GNU Lesser General Public License  along with this program.   
 27  #    If not, see <http://www.gnu.org/licenses/>. 
 28  # 
 29   
 30  __authors__ = [ "Marie-Francoise Incardona", "Olof Svensson", "Jérôme Kieffer" ] 
 31  __contact__ = "svensson@esrf.fr" 
 32  __license__ = "LGPLv3+" 
 33  __copyright__ = "European Synchrotron Radiation Facility, Grenoble, France" 
 34  __date__ = "20120216" 
 35   
 36  import types 
 37   
 38  from EDVerbose                           import EDVerbose 
 39  from EDAssert                            import EDAssert 
 40  from EDUtilsPath                         import EDUtilsPath 
 41  from EDTestCasePlugin                    import EDTestCasePlugin 
 42  from EDFactoryPluginStatic               import EDFactoryPluginStatic 
 43  from EDUtilsFile                         import EDUtilsFile 
 44  from EDUtilsParallel                     import EDUtilsParallel 
 45   
46 -class EDTestCasePluginExecute(EDTestCasePlugin):
47 """ 48 This is the main class for all Plugin Execution tests 49 """ 50
51 - def __init__(self, _strPluginName, _strPluginDir=None, _strTestName=None):
52 """ 53 Initialise the Plugin Execution test 54 - Edna site 55 - Configuration files 56 - Data input file 57 - Plugin launcher 58 """ 59 EDTestCasePlugin.__init__(self, _strPluginName, _strPluginDir, _strTestName) 60 self._strRefConfigFile = None 61 self._dictStrDataInputFiles = {} 62 self._strDefaultInputDataKey = "defaultInputData" 63 self._dictStrReferenceDataOutputFiles = {} 64 self._strDefaultOutputDataKey = "defaultOutputData" 65 self._iNoExpectedErrorMessages = 0 66 self._iNoExpectedWarningMessages = 0 67 self._bAcceptPluginFailure = False 68 # Deprecated! 69 self.m_edObtainedOutputDataFile = None 70 EDUtilsParallel.uninitializeNbThread()
71 72
73 - def preProcess(self):
74 """ 75 Initialize the plugin to be launched 76 """ 77 EDTestCasePlugin.preProcess(self) 78 if(EDUtilsPath.EDNA_SITE == None): 79 raise RuntimeError, "EDNA_SITE must be set" 80 # Load the plugin that should be executed 81 self._edPlugin = EDFactoryPluginStatic.loadPlugin(self.getPluginName()) 82 if(self._edPlugin is not None): 83 for strInputDataKey in self._dictStrDataInputFiles.keys(): 84 if (type(self._dictStrDataInputFiles[ strInputDataKey ]) == types.ListType): 85 for strDataInputFile in self._dictStrDataInputFiles[ strInputDataKey ]: 86 strXMLData = self.readAndParseFile(strDataInputFile) 87 if (strInputDataKey == self._strDefaultInputDataKey): 88 self._edPlugin.setDataInput(strXMLData) 89 else: 90 self._edPlugin.setDataInput(strXMLData, strInputDataKey) 91 else: 92 strXMLData = self.readAndParseFile(self._dictStrDataInputFiles[ strInputDataKey ]) 93 if (strInputDataKey == self._strDefaultInputDataKey): 94 self._edPlugin.setDataInput(strXMLData) 95 else: 96 self._edPlugin.setDataInput(strXMLData, strInputDataKey) 97 #self._edPlugin.setDataInput( self._strXMLData, "inputMXCuBE" ) 98 else: 99 EDVerbose.ERROR("Cannot load plugin: %s" % self.getPluginName()) 100 raise RuntimeError 101 self._edPlugin.setConfig(self.getPluginConfig())
102 103
104 - def testExecute(self):
105 self.run()
106 107
108 - def process(self):
109 self.addTestMethod(self.testExecute)
110 111
112 - def getErrorMessages(self):
113 """ 114 Returns the error messages for the plugin launcher 115 """ 116 return self._edPlugin.getErrorMessages()
117 errorMessages = property(getErrorMessages, doc="read-only only property") 118 119
120 - def getWarningMessages(self):
121 """ 122 Returns the warning messages for the plugin launcher 123 """ 124 return self._edPlugin.getWarningMessages()
125 warningMessages = property(getWarningMessages, doc="read-only only property") 126 127
128 - def getRefConfigFile(self):
129 """ 130 Returns the reference configuration file (from edna/conf directory) 131 """ 132 return self._strRefConfigFile
133 134
135 - def setDataInputFile(self, _strDataInputFile, _strDataInputKey=None):
136 """ 137 Sets the data input file 138 """ 139 strDataInputKey = _strDataInputKey 140 if (strDataInputKey is None): 141 strDataInputKey = self._strDefaultInputDataKey 142 if (strDataInputKey == self._strDefaultInputDataKey): 143 # Do not create a list for the default key, just replace the existing value 144 self._dictStrDataInputFiles[ strDataInputKey ] = _strDataInputFile 145 else: 146 if (not strDataInputKey in self._dictStrDataInputFiles.keys()): 147 self._dictStrDataInputFiles[ strDataInputKey ] = [] 148 self._dictStrDataInputFiles[ strDataInputKey ].append(_strDataInputFile)
149 150
151 - def getDataInputFile(self, _strDataInputKey=None):
152 """ 153 Returns the data input file 154 """ 155 strDataInputFile = None 156 strDataInputKey = _strDataInputKey 157 if (strDataInputKey is None): 158 strDataInputKey = self._strDefaultInputDataKey 159 if (strDataInputKey in self._dictStrDataInputFiles.keys()): 160 strDataInputFile = self._dictStrDataInputFiles[ strDataInputKey ] 161 else: 162 strErrorMessage = "ERROR: " + str(self.__class__) + ".setDataInputFile, no data input file defined for key: " + strDataInputKey 163 EDVerbose.error(strErrorMessage) 164 raise RuntimeError, strErrorMessage 165 return strDataInputFile
166 167 168
169 - def setReferenceDataOutputFile(self, _strReferenceDataOutputFile, _strDataOutputKey=None):
170 """ 171 Sets the data input file 172 """ 173 strDataOutputKey = _strDataOutputKey 174 if (strDataOutputKey is None): 175 strDataOutputKey = self._strDefaultOutputDataKey 176 if (strDataOutputKey == self._strDefaultOutputDataKey): 177 # Do not create a list for the default key, just replace the existing value 178 self._dictStrReferenceDataOutputFiles[ strDataOutputKey ] = _strReferenceDataOutputFile 179 else: 180 if (not strDataOutputKey in self._dictStrReferenceDataOutputFiles.keys()): 181 self._dictStrReferenceDataOutputFiles[ strDataOutputKey ] = [] 182 self._dictStrReferenceDataOutputFiles[ strDataOutputKey ].append(_strReferenceDataOutputFile)
183 184
185 - def getReferenceDataOutputFile(self, _strDataOutputKey=None):
186 """ 187 Returns the data input file 188 """ 189 strReferenceDataOutputFile = None 190 strDataOutputKey = _strDataOutputKey 191 if (strDataOutputKey is None): 192 strDataOutputKey = self._strDefaultOutputDataKey 193 if (strDataOutputKey in self._dictStrReferenceDataOutputFiles.keys()): 194 strReferenceDataOutputFile = self._dictStrReferenceDataOutputFiles[ strDataOutputKey ] 195 else: 196 strErrorMessage = "ERROR: " + str(self.__class__) + ".getReferenceDataOutputFile, no data output file defined for key: " + strDataOutputKey 197 EDVerbose.error(strErrorMessage) 198 raise RuntimeError, strErrorMessage 199 return strReferenceDataOutputFile
200 201
202 - def getEdnaSite(self):
203 """ 204 Returns the EDNA_SITE environment variable 205 """ 206 return EDUtilsPath.EDNA_SITE
207 208
209 - def run(self):
210 """ 211 Executes the plugin and checks that the data output is not None 212 """ 213 EDVerbose.DEBUG("EDTestCasePluginExecute: Executing " + self.getPluginName()) 214 self._edPlugin.executeSynchronous() 215 # Check that the plugin didn't end in failure 216 EDAssert.equal(self._bAcceptPluginFailure , self._edPlugin.isFailure(), \ 217 "Plugin failure assert: should be %r, was %r" % (self._bAcceptPluginFailure , self._edPlugin.isFailure())) 218 # Checks the number of error messages 219 EDAssert.equal(self._iNoExpectedErrorMessages, len(self._edPlugin.getListOfErrorMessages()), \ 220 "Number of error messages: expected %d, got %d" % (self._iNoExpectedErrorMessages, len(self._edPlugin.getListOfErrorMessages()))) 221 # Checks the number of warning messages 222 EDAssert.equal(self._iNoExpectedWarningMessages, len(self._edPlugin.getListOfWarningMessages()), \ 223 "Number of warning messages: expected %d, got %d" % (self._iNoExpectedWarningMessages, len(self._edPlugin.getListOfWarningMessages()))) 224 # Check the output data 225 listOfDataOutputKeys = self._edPlugin.getListOfDataOutputKeys() 226 for strReferenceOutputDataKey in self._dictStrReferenceDataOutputFiles.keys(): 227 # Only check the reference data keys 228 if (strReferenceOutputDataKey in listOfDataOutputKeys): 229 EDVerbose.unitTest("Testing data output for %s" % strReferenceOutputDataKey) 230 listReferenceFile = self._dictStrReferenceDataOutputFiles[ strReferenceOutputDataKey ] 231 if (type(listReferenceFile) != types.ListType): 232 listReferenceFile = [ listReferenceFile ] 233 listReferenceOutput = [] 234 for strReferenceFile in listReferenceFile: 235 listReferenceOutput.append(self.readAndParseFile(strReferenceFile)) 236 # Obtained output 237 listObtainedOutputXML = [] 238 pyObjectObtainedDataOutput = self._edPlugin.getDataOutput(strReferenceOutputDataKey) 239 listObtainedOutput = None 240 if (type(pyObjectObtainedDataOutput) == types.ListType): 241 listObtainedOutput = pyObjectObtainedDataOutput 242 else: 243 listObtainedOutput = [ pyObjectObtainedDataOutput ] 244 for xsDataOutput in listObtainedOutput: 245 listObtainedOutputXML.append(xsDataOutput.marshal()) 246 # Compare the lists, sort them first 247 listReferenceOutput.sort() 248 listObtainedOutputXML.sort() 249 for iIndex in range(len(listReferenceOutput)): 250 # Check of deprecated tests - if default data key only warn 251 if (strReferenceOutputDataKey == self._strDefaultOutputDataKey): 252 if (listReferenceOutput[ iIndex ] != listObtainedOutputXML[ iIndex ]): 253 EDVerbose.unitTest("WARNING! Expected output is not corresponding to obtained output.") 254 else: 255 EDAssert.equal(listReferenceOutput[ iIndex ], listObtainedOutputXML[ iIndex ]) 256 # Legacy - save output data 257 if (strReferenceOutputDataKey == self._strDefaultOutputDataKey): 258 if (self.m_edObtainedOutputDataFile is None): 259 self.m_edObtainedOutputDataFile = self.getPluginName() + "_output.xml" 260 EDUtilsFile.writeFile(self.m_edObtainedOutputDataFile, self._edPlugin.getDataOutput().marshal())
261 262
263 - def setNoExpectedWarningMessages(self, _iNoExpectedWarningMessages):
264 self._iNoExpectedWarningMessages = _iNoExpectedWarningMessages
265 266
267 - def setNoExpectedErrorMessages(self, _iNoExpectedErrorMessages):
268 self._iNoExpectedErrorMessages = _iNoExpectedErrorMessages
269 270
271 - def setAcceptPluginFailure(self, _bValue):
272 self._bAcceptPluginFailure = _bValue
273