A fork of https://github.com/Synerty/SOAPpy-py3 This is a working tree till fixes get imported upstream.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

292 lines
8.8 KiB

  1. #!/usr/bin/env python
  2. import getopt
  3. import sys
  4. import string
  5. import re
  6. import time
  7. sys.path.insert(1,"..")
  8. from SOAPpy import SOAP
  9. import traceback
  10. DEFAULT_SERVERS_FILE = './inventory.servers'
  11. DEFAULT_METHODS = ('SimpleBuy', 'RequestForQuote','Buy','Ping')
  12. def usage (error = None):
  13. sys.stdout = sys.stderr
  14. if error != None:
  15. print error
  16. print """usage: %s [options] [server ...]
  17. If a long option shows an argument is mandatory, it's mandatory for the
  18. equivalent short option also.
  19. -?, --help display this usage
  20. -d, --debug turn on debugging in the SOAP library
  21. -i, --invert test servers *not* in the list of servers given
  22. -m, --method=METHOD#[,METHOD#...]
  23. call only the given methods, specify a METHOD# of ?
  24. for the list of method numbers
  25. -o, --output=TYPE turn on output, TYPE is one or more of s(uccess),
  26. f(ailure), n(ot implemented), F(ailed (as expected)),
  27. a(ll)
  28. [f]
  29. -s, --servers=FILE use FILE as list of servers to test [%s]
  30. -t, --stacktrace print a stack trace on each unexpected failure
  31. -T, --always-stacktrace
  32. print a stack trace on any failure
  33. """ % (sys.argv[0], DEFAULT_SERVERS_FILE),
  34. sys.exit (0)
  35. def methodUsage ():
  36. sys.stdout = sys.stderr
  37. print "Methods are specified by number. Multiple methods can be " \
  38. "specified using a\ncomma-separated list of numbers or ranges. " \
  39. "For example 1,4-6,8 specifies\nmethods 1, 4, 5, 6, and 8.\n"
  40. print "The available methods are:\n"
  41. half = (len (DEFAULT_METHODS) + 1) / 2
  42. for i in range (half):
  43. print "%4d. %-25s" % (i + 1, DEFAULT_METHODS[i]),
  44. if i + half < len (DEFAULT_METHODS):
  45. print "%4d. %-25s" % (i + 1 + half, DEFAULT_METHODS[i + half]),
  46. print
  47. sys.exit (0)
  48. def readServers (file):
  49. servers = []
  50. f = open (file, 'r')
  51. while 1:
  52. line = f.readline ()
  53. if line == '':
  54. break
  55. if line[0] in ('#', '\n') or line[0] in string.whitespace:
  56. continue
  57. cur = {'nonfunctional': {}}
  58. tag = None
  59. servers.append (cur)
  60. while 1:
  61. if line[0] in string.whitespace:
  62. if tag == 'nonfunctional':
  63. value = method + ' ' + cur[tag][method]
  64. else:
  65. value = cur[tag]
  66. value += ' ' + line.strip ()
  67. else:
  68. tag, value = line.split (':', 1)
  69. tag = tag.strip ().lower ()
  70. value = value.strip ()
  71. if value[0] == '"' and value[-1] == '"':
  72. value = value[1:-1]
  73. if tag == 'nonfunctional':
  74. value = value.split (' ', 1) + ['']
  75. method = value[0]
  76. cur[tag][method] = value[1]
  77. else:
  78. cur[tag] = value
  79. line = f.readline ()
  80. if line == '' or line[0] == '\n':
  81. break
  82. return servers
  83. def str2list (s):
  84. l = {}
  85. for i in s.split (','):
  86. if i.find ('-') != -1:
  87. i = i.split ('-')
  88. for i in range (int (i[0]),int (i[1]) + 1):
  89. l[i] = 1
  90. else:
  91. l[int (i)] = 1
  92. l = l.keys ()
  93. l.sort ()
  94. return l
  95. def SimpleBuy(serv, sa, epname):
  96. serv = serv._sa (sa % {'methodname':'SimpleBuy'})
  97. return serv.SimpleBuy(ProductName="widget", Quantity = 50, Address = "this is my address") #JHawk, Phalanx require this order of params
  98. def RequestForQuote(serv, sa, epname):
  99. serv = serv._sa (sa % {'methodname':'RequestForQuote'})
  100. return serv.RequestForQuote(Quantity=3, ProductName = "thing") # for Phalanx, JHawk
  101. def Buy(serv, sa, epname):
  102. import copy
  103. serv = serv._sa (sa % {'methodname':'Buy'})
  104. billTo_d = {"name":"Buyer One", "address":"1 1st Street",
  105. "city":"New York", "state":"NY", "zipCode":"10000"}
  106. shipTo_d = {"name":"Buyer One ", "address":"1 1st Street ",
  107. "city":"New York ", "state":"NY ", "zipCode":"10000 "}
  108. for k,v in shipTo_d.items():
  109. shipTo_d[k] = v[:-1]
  110. itemd1 = SOAP.structType( {"name":"widg1","quantity":200,"price":SOAP.decimalType(45.99), "_typename":"LineItem"})
  111. itemd2 = SOAP.structType( {"name":"widg2","quantity":400,"price":SOAP.decimalType(33.45), "_typename":"LineItem"})
  112. items_d = SOAP.arrayType( [itemd1, itemd2] )
  113. items_d._ns = "http://www.soapinterop.org/Bid"
  114. po_d = SOAP.structType( data = {"poID":"myord","createDate":SOAP.dateTimeType(),"shipTo":shipTo_d, "billTo":billTo_d, "items":items_d})
  115. try:
  116. # it's called PO by MST (MS SOAP Toolkit), JHawk (.NET Remoting),
  117. # Idoox WASP, Paul (SOAP::Lite), PranishK (ATL), GLUE, Aumsoft,
  118. # HP, EasySoap, and Jake (Frontier). [Actzero accepts either]
  119. return serv.Buy(PO=po_d)
  120. except:
  121. # called PurchaseOrder by KeithBa
  122. return serv.Buy(PurchaseOrder=po_d)
  123. def Ping(serv, sa, epname):
  124. serv = serv._sa (sa % {'methodname':'Ping'})
  125. return serv.Ping()
  126. def main():
  127. servers = DEFAULT_SERVERS_FILE
  128. methodnums = None
  129. output = 'f'
  130. invert = 0
  131. succeed = 0
  132. printtrace = 0
  133. stats = 1
  134. total = 0
  135. fail = 0
  136. failok = 0
  137. notimp = 0
  138. try:
  139. opts,args = getopt.getopt (sys.argv[1:], '?dm:io:s:t',
  140. ['help', 'method', 'debug', 'invert',
  141. 'output', 'servers='])
  142. for opt, arg in opts:
  143. if opt in ('-?', '--help'):
  144. usage ()
  145. elif opt in ('-d', '--debug'):
  146. SOAP.Config.debug = 1
  147. elif opt in ('-i', '--invert'):
  148. invert = 1
  149. elif opt in ('-m', '--method'):
  150. if arg == '?':
  151. methodUsage ()
  152. methodnums = str2list (arg)
  153. elif opt in ('-o', '--output'):
  154. output = arg
  155. elif opt in ('-s', '--servers'):
  156. servers = arg
  157. else:
  158. raise AttributeError, \
  159. "Recognized but unimplemented option `%s'" % opt
  160. except SystemExit:
  161. raise
  162. except:
  163. usage (sys.exc_info ()[1])
  164. if 'a' in output:
  165. output = 'fFns'
  166. servers = readServers(servers)
  167. if methodnums == None:
  168. methodnums = range (1, len (DEFAULT_METHODS) + 1)
  169. limitre = re.compile ('|'.join (args), re.IGNORECASE)
  170. for s in servers:
  171. if (not not limitre.match (s['name'])) == invert:
  172. continue
  173. serv = SOAP.SOAPProxy(s['endpoint'], namespace = s['namespace'])
  174. for num in (methodnums):
  175. if num > len(DEFAULT_METHODS):
  176. break
  177. total += 1
  178. name = DEFAULT_METHODS[num - 1]
  179. title = '%s: %s (#%d)' % (s['name'], name, num)
  180. try:
  181. fn = globals ()[name]
  182. except KeyboardInterrupt:
  183. raise
  184. except:
  185. if 'n' in output:
  186. print title, "test not yet implemented"
  187. notimp += 1
  188. continue
  189. try:
  190. res = fn (serv, s['soapaction'], s['name'])
  191. if s['nonfunctional'].has_key (name):
  192. print title, "succeeded despite marked nonfunctional"
  193. elif 's' in output:
  194. print title, "succeeded "
  195. succeed += 1
  196. except KeyboardInterrupt:
  197. print "fail"
  198. raise
  199. except:
  200. if s['nonfunctional'].has_key (name):
  201. if 'F' in output:
  202. t = 'as expected'
  203. if s['nonfunctional'][name] != '':
  204. t += ', ' + s['nonfunctional'][name]
  205. print title, "failed (%s) -" %t, sys.exc_info()[1]
  206. failok += 1
  207. else:
  208. if 'f' in output:
  209. print title, "failed -", str (sys.exc_info()[1])
  210. fail += 1
  211. if stats:
  212. print " Tests ended at:", time.ctime (time.time())
  213. if stats > 0:
  214. print " Total tests: %d" % total
  215. print " Successes: %d (%3.2f%%)" % \
  216. (succeed, 100.0 * succeed / total)
  217. if stats > 0 or fail > 0:
  218. print "Failed unexpectedly: %d (%3.2f%%)" % \
  219. (fail, 100.0 * fail / total)
  220. if stats > 0:
  221. print " Failed as expected: %d (%3.2f%%)" % \
  222. (failok, 100.0 * failok / total)
  223. if stats > 0 or notimp > 0:
  224. print " Not implemented: %d (%3.2f%%)" % \
  225. (notimp, 100.0 * notimp / total)
  226. return fail + notimp
  227. if __name__ == "__main__":
  228. main()