smtesting.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. # This is a module for Server Manager testing using Python.
  2. # This provides several utility functions useful for testing
  3. import os
  4. import re
  5. import sys
  6. from paraview.modules.vtkRemotingServerManager import *
  7. from paraview.modules.vtkRemotingApplication import *
  8. from paraview.modules.vtkRemotingMisc import *
  9. # we get different behavior based on how we import servermanager
  10. # so we want to import servermanager the same way in this module
  11. # as we do in any module that is importing this
  12. SMModuleName = 'paraview.servermanager'
  13. if 'paraview.simple' in sys.modules:
  14. SMModuleName = 'paraview.simple'
  15. sm = __import__(SMModuleName)
  16. servermanager = sm.servermanager
  17. class TestError(Exception):
  18. pass
  19. __ProcessedCommandLineArguments__ = False
  20. DataDir = ""
  21. TempDir = ""
  22. BaselineImage = ""
  23. Threshold = 10.0
  24. SMStatesDir = ""
  25. StateXMLFileName = ""
  26. UseSavedStateForRegressionTests = False
  27. def Error(message):
  28. print ("ERROR: %s" % message)
  29. return False
  30. def ProcessCommandLineArguments():
  31. """Processes the command line areguments."""
  32. global DataDir
  33. global TempDir
  34. global BaselineImage
  35. global Threshold
  36. global StateXMLFileName
  37. global UseSavedStateForRegressionTests
  38. global SMStatesDir
  39. global __ProcessedCommandLineArguments__
  40. if __ProcessedCommandLineArguments__:
  41. return
  42. __ProcessedCommandLineArguments__ = True
  43. length = len(sys.argv)
  44. index = 1
  45. while index < length:
  46. key = sys.argv[index-1]
  47. value = sys.argv[index]
  48. index += 2
  49. if key == "-D":
  50. DataDir = value
  51. elif key == "-V":
  52. BaselineImage = value
  53. elif key == "-T":
  54. TempDir = value
  55. elif key == "-S":
  56. SMStatesDir = value
  57. elif key == "--threshold":
  58. Threshold = float(value)
  59. elif key == "--state":
  60. StateXMLFileName = value
  61. elif key == "--use_saved_state":
  62. UseSavedStateForRegressionTests = True
  63. index -= 1
  64. else:
  65. index -=1
  66. return
  67. def LoadServerManagerState(filename):
  68. """This function loads the servermanager state xml/pvsm.
  69. Returns the status of the load."""
  70. global DataDir
  71. ProcessCommandLineArguments()
  72. parser = servermanager.vtkPVXMLParser()
  73. try:
  74. fp = open(filename, "r")
  75. data = fp.read()
  76. fp.close()
  77. except:
  78. return Error("Failed to open state file %s" % filename)
  79. regExp = re.compile("\${DataDir}")
  80. data = regExp.sub(DataDir, data)
  81. if not parser.Parse(data):
  82. return Error("Failed to parse")
  83. loader = servermanager.vtkSMStateLoader()
  84. loader.SetSession(servermanager.ActiveConnection.Session)
  85. root = parser.GetRootElement()
  86. if loader.LoadState(root):
  87. pxm = servermanager.vtkSMProxyManager.GetProxyManager().GetActiveSessionProxyManager()
  88. pxm.UpdateRegisteredProxiesInOrder(0);
  89. pxm.UpdateRegisteredProxies(0)
  90. return True
  91. return Error("Failed to load state file %s" % filename)
  92. def DoRegressionTesting(rmProxy=None):
  93. """Perform regression testing."""
  94. global TempDir
  95. global BaselineImage
  96. global Threshold
  97. ProcessCommandLineArguments()
  98. testing = vtkSMTesting()
  99. testing.AddArgument("-T")
  100. testing.AddArgument(TempDir)
  101. testing.AddArgument("-V")
  102. testing.AddArgument(BaselineImage)
  103. if not rmProxy:
  104. rmProxy = servermanager.GetRenderView()
  105. if rmProxy:
  106. rmProxy = rmProxy.SMProxy
  107. if not rmProxy:
  108. raise "Failed to locate view to perform regression testing."
  109. #pyProxy(rmProxy).SetRenderWindowSize(300, 300);
  110. #rmProxy.GetProperty("RenderWindowSize").SetElement(0, 300)
  111. #rmProxy.GetProperty("RenderWindowSize").SetElement(1, 300)
  112. #rmProxy.UpdateVTKObjects()
  113. rmProxy.StillRender()
  114. testing.SetRenderViewProxy(rmProxy)
  115. if testing.RegressionTest(Threshold) == 1:
  116. return True
  117. return Error("Regression Test Failed!")
  118. def GetUniqueTempDirectory(prefix):
  119. """A convenience method to generate a temporary directory unique to the
  120. current ParaView process (or MPI group of processes)"""
  121. global TempDir
  122. pm = servermanager.vtkProcessModule.GetProcessModule()
  123. if pm.GetNumberOfLocalPartitions() > 1 and pm.GetSymmetricMPIMode():
  124. if pm.GetPartitionId() == 0:
  125. import os
  126. pid = os.getpid()
  127. else:
  128. pid = None
  129. from mpi4py import MPI
  130. comm = MPI.COMM_WORLD
  131. pid = comm.bcast(pid, root=0)
  132. else:
  133. import os
  134. pid = os.getpid()
  135. import os.path
  136. tempdir = os.path.join(TempDir, "%s%d" % (prefix,pid))
  137. if pm.GetPartitionId() == 0:
  138. try:
  139. os.makedirs(tempdir)
  140. except OSError:
  141. pass
  142. if pm.GetNumberOfLocalPartitions() > 1 and pm.GetSymmetricMPIMode():
  143. from mpi4py import MPI
  144. comm = MPI.COMM_WORLD
  145. comm.Barrier()
  146. return tempdir
  147. if __name__ == "__main__":
  148. # This script loads the state, saves out a temp state and loads the saved state.
  149. # This saved state is used for testing -- this will ensure load/save SM state
  150. # is working fine.
  151. servermanager.Connect()
  152. ProcessCommandLineArguments()
  153. ret = 1
  154. if StateXMLFileName:
  155. if LoadServerManagerState(StateXMLFileName):
  156. pxm = servermanager.vtkSMProxyManager.GetProxyManager().GetActiveSessionProxyManager()
  157. if UseSavedStateForRegressionTests:
  158. saved_state = os.path.join(TempDir, "temp.pvsm")
  159. pxm.SaveState(saved_state)
  160. pxm.UnRegisterProxies();
  161. LoadServerManagerState(saved_state)
  162. try:
  163. os.remove(saved_state)
  164. except:
  165. pass
  166. if DoRegressionTesting():
  167. ret = 0
  168. else:
  169. Error("No ServerManager state file specified")
  170. if ret:
  171. # This leads to vtkDebugLeaks reporting leaks, hence we do this
  172. # only when the tests failed.
  173. sys.exit(ret)