1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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
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):
73
74
91
92
106
107 @deprecated
121
122
123
124
126 """
127 Sets the configuration file
128 """
129 self._strConfigurationFile = _strConfigurationFile
130
131
133 """
134 Returns the configuration file
135 """
136 return self._strConfigurationFile
137
138
139
141 if _strPluginName is None:
142 self._listRequiredConfigurationPluginNames.append(self._strPluginName)
143 else:
144 self._listRequiredConfigurationPluginNames.append(_strPluginName)
145
146
163
164
166 """
167 Returns the plugin instance
168 """
169 return self._edPlugin
170 plugin = property(getPlugin, doc="read-only only property")
171
172
174 """
175 Returns the plugin name
176 """
177 return self._strPluginName
178 pluginName = property(getPluginName, doc="read-only property")
179
180
182 """
183 Returns the plugin home directory
184 """
185 return self._strPluginHome
186 pluginHome = property(getPluginHome, doc="read-only property")
187
188
190 """
191 Returns the plugin test data home directory
192 """
193 return self._strPluginTestsDataHome
194
195
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
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
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
253 dictReplace = property(getDictReplace, doc="Read-only property")
254
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
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