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.
 
 
 

171 lines
5.5 KiB

  1. #!/usr/bin/env python
  2. ############################################################################
  3. # Joshua R. Boverhof, David W. Robertson, LBNL
  4. # See LBNLCopyright for copyright notice!
  5. ###########################################################################
  6. import sys
  7. import unittest
  8. import ConfigParser
  9. import os
  10. from wstools.Utility import DOM
  11. from wstools.WSDLTools import WSDLReader
  12. from wstools.TimeoutSocket import TimeoutError
  13. cwd = 'tests'
  14. # that's for tox/pytest
  15. nameGenerator = None
  16. def makeTestSuite(section='services_by_file'):
  17. global nameGenerator
  18. cp, numTests = setUpOptions(section)
  19. nameGenerator = getOption(cp, section)
  20. suite = unittest.TestSuite()
  21. for i in range(0, numTests):
  22. case = WSDLToolsAbstractCase()
  23. case.__test__ = True
  24. suite.addTest(unittest.makeSuite(case, 'test_'))
  25. return suite
  26. class WSDLToolsAbstractCase(unittest.TestCase):
  27. __test__ = False
  28. def __init__(self, methodName='runTest'):
  29. unittest.TestCase.__init__(self, methodName)
  30. def setUp(self):
  31. makeTestSuite()
  32. self.path = nameGenerator.next()
  33. print self.path
  34. sys.stdout.flush()
  35. def __str__(self):
  36. teststr = unittest.TestCase.__str__(self)
  37. if hasattr(self, "path"):
  38. return "%s: %s" % (teststr, self.path)
  39. else:
  40. return "%s" % (teststr)
  41. def checkWSDLCollection(self, tag_name, component, key='name'):
  42. if self.wsdl is None:
  43. return
  44. definition = self.wsdl.document.documentElement
  45. version = DOM.WSDLUriToVersion(definition.namespaceURI)
  46. nspname = DOM.GetWSDLUri(version)
  47. for node in DOM.getElements(definition, tag_name, nspname):
  48. name = DOM.getAttr(node, key)
  49. comp = component[name]
  50. self.failUnlessEqual(eval('comp.%s' % key), name)
  51. def checkXSDCollection(self, tag_name, component, node, key='name'):
  52. for cnode in DOM.getElements(node, tag_name):
  53. name = DOM.getAttr(cnode, key)
  54. component[name]
  55. def test_all(self):
  56. try:
  57. if self.path[:7] == 'http://':
  58. self.wsdl = WSDLReader().loadFromURL(self.path)
  59. else:
  60. self.wsdl = WSDLReader().loadFromFile('tests/' + self.path)
  61. except TimeoutError:
  62. print "connection timed out"
  63. sys.stdout.flush()
  64. return
  65. except:
  66. self.path = self.path + ": load failed, unable to start"
  67. raise
  68. try:
  69. self.checkWSDLCollection('service', self.wsdl.services)
  70. except:
  71. self.path = self.path + ": wsdl.services"
  72. raise
  73. try:
  74. self.checkWSDLCollection('message', self.wsdl.messages)
  75. except:
  76. self.path = self.path + ": wsdl.messages"
  77. raise
  78. try:
  79. self.checkWSDLCollection('portType', self.wsdl.portTypes)
  80. except:
  81. self.path = self.path + ": wsdl.portTypes"
  82. raise
  83. try:
  84. self.checkWSDLCollection('binding', self.wsdl.bindings)
  85. except:
  86. self.path = self.path + ": wsdl.bindings"
  87. raise
  88. try:
  89. self.checkWSDLCollection('import', self.wsdl.imports, \
  90. key='namespace')
  91. except:
  92. self.path = self.path + ": wsdl.imports"
  93. raise
  94. try:
  95. for key in self.wsdl.types.keys():
  96. schema = self.wsdl.types[key]
  97. self.failUnlessEqual(key, schema.getTargetNamespace())
  98. definition = self.wsdl.document.documentElement
  99. version = DOM.WSDLUriToVersion(definition.namespaceURI)
  100. nspname = DOM.GetWSDLUri(version)
  101. for node in DOM.getElements(definition, 'types', nspname):
  102. for snode in DOM.getElements(node, 'schema'):
  103. tns = DOM.findTargetNS(snode)
  104. schema = self.wsdl.types[tns]
  105. self.schemaAttributesDeclarations(schema, snode)
  106. self.schemaAttributeGroupDeclarations(schema, snode)
  107. self.schemaElementDeclarations(schema, snode)
  108. self.schemaTypeDefinitions(schema, snode)
  109. except:
  110. self.path = self.path + ": wsdl.types"
  111. raise
  112. if self.wsdl.extensions:
  113. print 'No check for WSDLTools(%s) Extensions:' % (self.wsdl.name)
  114. for ext in self.wsdl.extensions:
  115. print '\t', ext
  116. def schemaAttributesDeclarations(self, schema, node):
  117. self.checkXSDCollection('attribute', schema.attr_decl, node)
  118. def schemaAttributeGroupDeclarations(self, schema, node):
  119. self.checkXSDCollection('group', schema.attr_groups, node)
  120. def schemaElementDeclarations(self, schema, node):
  121. self.checkXSDCollection('element', schema.elements, node)
  122. def schemaTypeDefinitions(self, schema, node):
  123. self.checkXSDCollection('complexType', schema.types, node)
  124. self.checkXSDCollection('simpleType', schema.types, node)
  125. def setUpOptions(section):
  126. cp = ConfigParser.ConfigParser()
  127. cp.read(cwd + '/config.txt')
  128. if not cp.sections():
  129. print 'fatal error: configuration file config.txt not present'
  130. sys.exit(0)
  131. if not cp.has_section(section):
  132. print '%s section not present in configuration file, exiting' % section
  133. sys.exit(0)
  134. return cp, len(cp.options(section))
  135. def getOption(cp, section):
  136. for name, value in cp.items(section):
  137. yield value