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.
 
 
 
 

291 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), end=' ')
  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]), end=' ')
  44. if i + half < len (DEFAULT_METHODS):
  45. print("%4d. %-25s" % (i + 1 + half, DEFAULT_METHODS[i + half]), end=' ')
  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 = list(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 list(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("Recognized but unimplemented option `%s'" % opt)
  159. except SystemExit:
  160. raise
  161. except:
  162. usage (sys.exc_info ()[1])
  163. if 'a' in output:
  164. output = 'fFns'
  165. servers = readServers(servers)
  166. if methodnums == None:
  167. methodnums = list(range(1, len (DEFAULT_METHODS) + 1))
  168. limitre = re.compile ('|'.join (args), re.IGNORECASE)
  169. for s in servers:
  170. if (not not limitre.match (s['name'])) == invert:
  171. continue
  172. serv = SOAP.SOAPProxy(s['endpoint'], namespace = s['namespace'])
  173. for num in (methodnums):
  174. if num > len(DEFAULT_METHODS):
  175. break
  176. total += 1
  177. name = DEFAULT_METHODS[num - 1]
  178. title = '%s: %s (#%d)' % (s['name'], name, num)
  179. try:
  180. fn = globals ()[name]
  181. except KeyboardInterrupt:
  182. raise
  183. except:
  184. if 'n' in output:
  185. print(title, "test not yet implemented")
  186. notimp += 1
  187. continue
  188. try:
  189. res = fn (serv, s['soapaction'], s['name'])
  190. if name in s['nonfunctional']:
  191. print(title, "succeeded despite marked nonfunctional")
  192. elif 's' in output:
  193. print(title, "succeeded ")
  194. succeed += 1
  195. except KeyboardInterrupt:
  196. print("fail")
  197. raise
  198. except:
  199. if name in s['nonfunctional']:
  200. if 'F' in output:
  201. t = 'as expected'
  202. if s['nonfunctional'][name] != '':
  203. t += ', ' + s['nonfunctional'][name]
  204. print(title, "failed (%s) -" %t, sys.exc_info()[1])
  205. failok += 1
  206. else:
  207. if 'f' in output:
  208. print(title, "failed -", str (sys.exc_info()[1]))
  209. fail += 1
  210. if stats:
  211. print(" Tests ended at:", time.ctime (time.time()))
  212. if stats > 0:
  213. print(" Total tests: %d" % total)
  214. print(" Successes: %d (%3.2f%%)" % \
  215. (succeed, 100.0 * succeed / total))
  216. if stats > 0 or fail > 0:
  217. print("Failed unexpectedly: %d (%3.2f%%)" % \
  218. (fail, 100.0 * fail / total))
  219. if stats > 0:
  220. print(" Failed as expected: %d (%3.2f%%)" % \
  221. (failok, 100.0 * failok / total))
  222. if stats > 0 or notimp > 0:
  223. print(" Not implemented: %d (%3.2f%%)" % \
  224. (notimp, 100.0 * notimp / total))
  225. return fail + notimp
  226. if __name__ == "__main__":
  227. main()