PageRenderTime 45ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/components/tools/OmeroPy/test/scriptstest/ping.py

https://github.com/aherbert/openmicroscopy
Python | 249 lines | 235 code | 5 blank | 9 comment | 0 complexity | 76439dc931d953f9e2e15e5419716f51 MD5 | raw file
  1. #!/usr/bin/env python
  2. """
  3. Integration test testing distributed processing via
  4. ServiceFactoryI.acquireProcessor().
  5. Copyright 2008 Glencoe Software, Inc. All rights reserved.
  6. Use is subject to license terms supplied in LICENSE.txt
  7. """
  8. import integration.library as lib
  9. import unittest, os, sys
  10. import omero
  11. import omero.clients
  12. import omero.model
  13. import omero.api
  14. import omero_api_IScript_ice
  15. import omero_ext.uuid as uuid # see ticket:3774
  16. from omero.util.temp_files import create_path, remove_path
  17. from omero.rtypes import *
  18. from omero.scripts import wait
  19. PINGFILE = """
  20. #!/usr/bin/env python
  21. import os
  22. import omero, omero.scripts as s
  23. import omero_ext.uuid as uuid # see ticket:3774
  24. #
  25. # Unique name so that IScript does not reject us
  26. # based on duplicate file names.
  27. #
  28. uuid = str(uuid.uuid4())
  29. print "I am the script named %s" % uuid
  30. #
  31. # Creation
  32. #
  33. client = s.client(uuid, "simple ping script", s.Long("a", optional=True).inout(), s.String("b", optional=True).inout())
  34. print "Session", client.getSession()
  35. #
  36. # Various diagnostics
  37. #
  38. import sys
  39. from pprint import pprint
  40. print "PATH:"
  41. pprint(sys.path)
  42. print "CONFIG"
  43. f = open("config","r")
  44. print "".join(f.readlines())
  45. f.close()
  46. from omero.rtypes import *
  47. import Ice
  48. ic = Ice.initialize(["--Ice.Plugin.IceSSL=IceSSL:createIceSSL"])
  49. print ic.getProperties().getPropertiesForPrefix("Ice")
  50. print ic.getProperties().getPropertiesForPrefix("omero")
  51. #
  52. # Echo'ing input to output
  53. #
  54. keys = client.getInputKeys()
  55. print "Keys found:"
  56. print keys
  57. for key in keys:
  58. client.setOutput(key, client.getInput(key))
  59. #
  60. # Env
  61. #
  62. print "This was my environment:"
  63. for k,v in os.environ.items():
  64. print "%s => %s" %(k,v)
  65. sys.stderr.write("Oh, and this is stderr.");
  66. """
  67. class CallbackI(omero.grid.ProcessCallback):
  68. def __init__(self):
  69. self.finish = []
  70. self.cancel = []
  71. self.kill = []
  72. def processFinished(self, rv, current = True):
  73. self.finish.append(rv)
  74. def processCancelled(self, rv, current = True):
  75. self.cancel.append(rv)
  76. def processKilled(self, rv, current = True):
  77. self.kill.append(rv)
  78. class TestPing(lib.ITest):
  79. """
  80. Tests which use the trivial script defined by PINGFILE to
  81. test the scripts API.
  82. """
  83. #
  84. # Helper methods
  85. #
  86. def _getProcessor(self):
  87. scripts = self.root.getSession().getScriptService()
  88. id = scripts.uploadOfficialScript("/tests/ping_py/%s.py" % self.uuid(), PINGFILE)
  89. j = omero.model.ScriptJobI()
  90. j.linkOriginalFile(omero.model.OriginalFileI(rlong(id),False))
  91. p = self.client.sf.sharedResources().acquireProcessor(j, 100)
  92. return p
  93. def _checkstd(self, output, which):
  94. rfile = output.val[which]
  95. ofile = rfile.val
  96. self.assert_( ofile )
  97. tmppath = create_path("pingtest")
  98. try:
  99. self.client.download(ofile, str(tmppath))
  100. self.assert_( os.path.getsize(str(tmppath)))
  101. return tmppath.text()
  102. finally:
  103. remove_path(tmppath)
  104. def assertIO(self, output):
  105. stdout = self._checkstd(output, "stdout")
  106. stderr = self._checkstd(output, "stderr")
  107. return stdout, stderr
  108. def assertSuccess(self, processor, process):
  109. wait(self.client, process)
  110. rc = process.poll()
  111. output = processor.getResults(process)
  112. stdout, stderr = self.assertIO(output)
  113. if rc is None or rc.val != 0:
  114. self.fail("STDOUT:\n%s\nSTDERR:\n%s\n" % (stdout, stderr))
  115. return output
  116. #
  117. # Test methods
  118. #
  119. def testPingViaISCript(self):
  120. p = self._getProcessor()
  121. input = rmap({})
  122. input.val["a"] = rlong(2)
  123. input.val["b"] = rstring("d")
  124. process = p.execute(input)
  125. output = self.assertSuccess(p, process)
  126. self.assert_( 2 == output.val["a"].val )
  127. def testPingParametersViaISCript(self):
  128. p = self._getProcessor()
  129. params = p.params()
  130. self.assert_( params )
  131. self.assert_( params.inputs["a"] )
  132. self.assert_( params.inputs["b"] )
  133. self.assert_( params.outputs["a"] )
  134. self.assert_( params.outputs["b"] )
  135. def testPingStdout(self):
  136. p = self._getProcessor()
  137. params = p.params()
  138. self.assert_( params.stdoutFormat )
  139. process = p.execute(rmap({}))
  140. output = self.assertSuccess(p, process)
  141. def testProcessCallback(self):
  142. callback = CallbackI()
  143. id = self.client.getCommunicator().stringToIdentity(str(uuid.uuid4()))
  144. cb = self.client.getAdapter().add(callback, id)
  145. cb = omero.grid.ProcessCallbackPrx.uncheckedCast(cb)
  146. p = self._getProcessor()
  147. params = p.params()
  148. self.assert_( params.stdoutFormat )
  149. process = p.execute(rmap({}))
  150. process.registerCallback(cb)
  151. output = self.assertSuccess(p, process)
  152. self.assertTrue( len(callback.finish) > 0 )
  153. def testProcessShutdown(self):
  154. p = self._getProcessor()
  155. process = p.execute(rmap({}))
  156. process.shutdown()
  157. output = p.getResults(process)
  158. # Probably doesn't have IO since killed
  159. # self.assertIO(output)
  160. def testProcessShutdownOneway(self):
  161. p = self._getProcessor()
  162. process = p.execute(rmap({}))
  163. oneway = omero.grid.ProcessPrx.uncheckedCast( process.ice_oneway() )
  164. oneway.shutdown()
  165. # Depending on what's faster this may or may not throw
  166. try:
  167. p.getResults(process)
  168. self.assert_(process.poll())
  169. output = p.getResults(process)
  170. except omero.ServerError:
  171. pass
  172. # Probably doesn't have IO since killed
  173. # self.assertIO(output)
  174. def testProcessorGetResultsBeforeFinished(self):
  175. p = self._getProcessor()
  176. process = p.execute(None)
  177. self.assertRaises(omero.ServerError, p.getResults, process)
  178. output = self.assertSuccess(p, process)
  179. #
  180. # Execution-less tests
  181. #
  182. def testProcessorExpires(self):
  183. p = self._getProcessor()
  184. self.assertTrue( p.expires() > 0 )
  185. def testProcessorGetJob(self):
  186. p = self._getProcessor()
  187. self.assert_( p.getJob() )
  188. def testProcessorStop(self):
  189. p = self._getProcessor()
  190. process = p.execute(rmap({}))
  191. p.stop()
  192. def testProcessorDetach(self):
  193. p = self._getProcessor()
  194. process = p.execute(rmap({}))
  195. p.setDetach(True)
  196. p.stop()
  197. if __name__ == '__main__':
  198. unittest.main()