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

Source Code for Module EDTestCasePlugin

  1  # coding: utf8 
  2  # 
  3  #    Project: The EDNA Kernel 
  4  #             http://www.edna-site.org 
  5  # 
  6  #    Copyright (C) 2008-2012 European Synchrotron Radiation Facility 
  7  #                            Grenoble, France 
  8  # 
  9  #    Principal authors: Marie-Francoise Incardona (incardon@esrf.fr) 
 10  #                       Olof Svensson (svensson@esrf.fr)  
 11  # 
 12  #    This program is free software: you can redistribute it and/or modify 
 13  #    it under the terms of the GNU Lesser General Public License as published 
 14  #    by the Free Software Foundation, either version 3 of the License, or 
 15  #    (at your option) any later version. 
 16  # 
 17  #    This program is distributed in the hope that it will be useful, 
 18  #    but WITHOUT ANY WARRANTY; without even the implied warranty of 
 19  #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 20  #    GNU Lesser General Public License for more details. 
 21  # 
 22  #    You should have received a copy of the GNU General Public License 
 23  #    and the GNU Lesser General Public License  along with this program.   
 24  #    If not, see <http://www.gnu.org/licenses/>. 
 25  # 
 26   
 27  """ 
 28  Test case for both unit tests and execution tests for EDNA plugins  
 29   
 30  It is especially in charge of  
 31   - downloading large files not contained in the SVN repository for the tests (typically images coming from CCD, 8 or 16 Megabytes each) 
 32   - doing the replacement or variable like ${EDNA_HOME} in the XML strings 
 33  """ 
 34   
 35   
 36  __authors__ = [ "Marie-Francoise Incardona", "Olof Svensson", "Jérôme Kieffer" ] 
 37  __contact__ = "svensson@esrf.fr" 
 38  __license__ = "LGPLv3+" 
 39  __copyright__ = "European Synchrotron Radiation Facility, Grenoble, France" 
 40  __date__ = "20120131" 
 41   
 42  import sys, os, threading, urllib2 
 43   
 44  from EDVerbose             import EDVerbose 
 45  from EDUtilsPath           import EDUtilsPath 
 46  from EDTestCase            import EDTestCase 
 47  from EDUtilsTest           import EDUtilsTest 
 48  from EDUtilsFile           import EDUtilsFile 
 49  from EDConfigurationStatic import EDConfigurationStatic, EDConfiguration 
 50  from EDFactoryPlugin       import EDFactoryPlugin 
 51  from EDDecorator           import deprecated 
 52   
 53  iMAX_DOWNLOAD_TIME = 60 
54 55 56 -class EDTestCasePlugin(EDTestCase):
57 """ 58 This is the main test class to test a plugin (Unit and Execution) 59 """ 60 URL_EDNA_SITE = "http://www.edna-site.org/data/tests/images" 61
62 - def __init__(self, _strPluginName, _strPluginDir=None, _strTestName=None):
63 """ 64 Initialize the test case by determining the paths to the plugin home and plugin test directories. 65 """ 66 EDTestCase.__init__(self, _strTestName) 67 self._edPlugin = None 68 self._strPluginName = _strPluginName 69 self._strPluginHome = EDUtilsTest.getFactoryPluginTest().getModuleLocation(_strPluginName) 70 self._strPluginTestsDataHome = EDUtilsTest.getPluginTestDataDirectory(self.getClassName()) 71 self._listRequiredConfigurationPluginNames = [] 72 self._strConfigurationFile = None
73 74
75 - def preProcess(self):
76 # Check if the plugin to be tested requires configuration 77 edPlugin = self.createPlugin() 78 if edPlugin is None: 79 strErr = "Unable to create plugin %s: Check the corresponding module" % self.getPluginName() 80 EDVerbose.ERROR(strErr) 81 raise RuntimeError(strErr) 82 if edPlugin.isRequiredToHaveConfiguration(): 83 self._listRequiredConfigurationPluginNames.append(self.getPluginName()) 84 # Check if the required plugin parameters are available 85 for strPluginName in self._listRequiredConfigurationPluginNames: 86 if self.getPluginConfig(strPluginName) is None: 87 EDVerbose.DEBUG("EDTestCasePlugin.preProcess: plugin configuration NOT found for plugin %s" % strPluginName) 88 self.setReasonForNotBeingExectuted("Missing configuration for %s" % strPluginName) 89 else: 90 EDVerbose.DEBUG("EDTestCasePlugin.preProcess: plugin configuration found for plugin %s" % strPluginName)
91 92
93 - def getPluginConfig(self, _strPluginName=None):
94 # Load the configuration file if provided 95 dictConfig = None 96 if _strPluginName is None: 97 strPluginName = self.getPluginName() 98 else: 99 strPluginName = _strPluginName 100 if self._strConfigurationFile is not None: 101 edConfig = EDConfiguration(self._strConfigurationFile) 102 dictConfig = edConfig.get(strPluginName) 103 else: 104 dictConfig = EDConfigurationStatic.get(strPluginName) 105 return dictConfig
106 107 @deprecated
108 - def getPluginConfiguration(self, _strPluginName=None):
109 # Load the configuration file if provided 110 xsConfiguration = None 111 if _strPluginName is None: 112 strPluginName = self.getPluginName() 113 else: 114 strPluginName = _strPluginName 115 if self._strConfigurationFile is not None: 116 edConfig = EDConfiguration(self._strConfigurationFile) 117 xsConfiguration = edConfig.getXSConfigurationItem(strPluginName) 118 else: 119 xsConfiguration = EDConfigurationStatic.getXSConfigurationItem(strPluginName) 120 return xsConfiguration
121 122 123 124
125 - def setConfigurationFile(self, _strConfigurationFile):
126 """ 127 Sets the configuration file 128 """ 129 self._strConfigurationFile = _strConfigurationFile
130 131
132 - def getConfigurationFile(self):
133 """ 134 Returns the configuration file 135 """ 136 return self._strConfigurationFile
137 138 139
140 - def setRequiredPluginConfiguration(self, _strPluginName=None):
141 if _strPluginName is None: 142 self._listRequiredConfigurationPluginNames.append(self._strPluginName) 143 else: 144 self._listRequiredConfigurationPluginNames.append(_strPluginName)
145 146
147 - def createPlugin(self):
148 """ 149 Creates a plugin instance 150 """ 151 edPlugin = None 152 exceptionObject = None 153 try: 154 edFactoryPlugin = EDFactoryPlugin() 155 edPlugin = edFactoryPlugin.loadPlugin(self.getPluginName()) 156 except ImportError, exceptionObject: 157 strWarningMessage = "Could not create the plugin: %s, reason: %s" % (self.getPluginName(), exceptionObject) 158 EDVerbose.WARNING(strWarningMessage) 159 if edPlugin is None: 160 if exceptionObject is None: 161 EDVerbose.error("EDTestCasePlugin.createPlugin: Could not create plugin: " + self.getPluginName()) 162 return edPlugin
163 164
165 - def getPlugin(self):
166 """ 167 Returns the plugin instance 168 """ 169 return self._edPlugin
170 plugin = property(getPlugin, doc="read-only only property") 171 172
173 - def getPluginName(self):
174 """ 175 Returns the plugin name 176 """ 177 return self._strPluginName
178 pluginName = property(getPluginName, doc="read-only property") 179 180
181 - def getPluginHome(self):
182 """ 183 Returns the plugin home directory 184 """ 185 return self._strPluginHome
186 pluginHome = property(getPluginHome, doc="read-only property") 187 188
189 - def getPluginTestsDataHome(self):
190 """ 191 Returns the plugin test data home directory 192 """ 193 return self._strPluginTestsDataHome
194 195
196 - def setPluginTestsDataHome(self, _strPluginTestsDataHome):
197 """ 198 Sets the plugin test data home directory 199 """ 200 self._strPluginTestsDataHome = _strPluginTestsDataHome
201 pluginTestDataHome = property(getPluginTestsDataHome, setPluginTestsDataHome, "pluginTestDataHome is the data directory in the plugin's tests") 202 203
204 - def loadTestImage(self, _listImageFileName):
205 """ 206 This method checks the presence of all the images in the list of image file names 207 in the $EDNA_HOME/tests/data/images directory. If one image is not present 208 this method tries to download it from http://www.edna-site.org/data/tests/images 209 """ 210 if not os.path.isdir(EDUtilsPath.EDNA_TESTIMAGES): 211 os.makedirs(EDUtilsPath.EDNA_TESTIMAGES) 212 for strImageName in _listImageFileName: 213 strImagePath = os.path.join(EDUtilsPath.EDNA_TESTIMAGES, strImageName) 214 if(not os.path.exists(strImagePath)): 215 EDVerbose.unitTest("Trying to download image %s, timeout set to %d s" % (strImagePath, iMAX_DOWNLOAD_TIME)) 216 if os.environ.has_key("http_proxy"): 217 dictProxies = {'http': os.environ["http_proxy"]} 218 proxy_handler = urllib2.ProxyHandler(dictProxies) 219 opener = urllib2.build_opener(proxy_handler).open 220 else: 221 opener = urllib2.urlopen 222 223 # Nota: since python2.6 there is a timeout in the urllib2 224 timer = threading.Timer(iMAX_DOWNLOAD_TIME + 1, timeoutDuringDownload) 225 timer.start() 226 if sys.version > (2, 6): 227 data = opener("%s/%s" % (self.URL_EDNA_SITE, strImageName), data=None, timeout=iMAX_DOWNLOAD_TIME).read() 228 else: 229 data = opener("%s/%s" % (self.URL_EDNA_SITE, strImageName), data=None).read() 230 timer.cancel() 231 232 try: 233 open(strImagePath, "wb").write(data) 234 except IOError: 235 raise IOError, "unable to write downloaded data to disk at %s" % strImagePath 236 237 if os.path.exists(strImagePath): 238 EDVerbose.unitTest("Image %s successfully downloaded." % strImagePath) 239 else: 240 raise RuntimeError, "Could not automatically download test images %r! \n \ 241 If you are behind a firewall, please set the environment variable http_proxy. \n \ 242 Otherwise please try to download the images manually from \n \ 243 http://www.edna-site.org/data/tests/images" % _listImageFileName
244
245 - def getDictReplace(self):
246 dictReplace = EDUtilsPath.getDictOfPaths() 247 dictReplace["${EDNA_PLUGIN_TESTS_DATA_HOME}"] = self._strPluginTestsDataHome 248 if self._edPlugin is not None: 249 workDir = self._edPlugin.getWorkingDirectory() 250 if workDir is not None: 251 dictReplace["${EDNA_WORKING_DIR}"] = workDir 252 return dictReplace
253 dictReplace = property(getDictReplace, doc="Read-only property") 254
255 - def readAndParseFile(self, _strFileName):
256 """ 257 Reads a file and parses potential existing environment variables such as: 258 - EDNA_TESTS_DATA_HOME 259 - EDNA_PLUGIN_TESTS_DATA_HOME 260 - EDNA_HOME 261 - USER 262 - TMPDIR 263 264 All those key are defined in a class dictionary 265 266 Returns the content of this file as a string 267 """ 268 return str(EDUtilsFile.readFileAndParseVariables(_strFileName, self.dictReplace))
269
270 271 272 -def timeoutDuringDownload():
273 """ 274 Function called after a timeout in the download part ... just raise an Exception. 275 """ 276 raise RuntimeError, "Could not automatically download test images ! \n \ 277 If you are behind a firewall, please set the environment variable http_proxy. \n \ 278 Otherwise please try to download the images manually from \n \ 279 http://www.edna-site.org/data/tests/images"
280