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.
 
 
 

175 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. suite.addTest(unittest.makeSuite(WSDLToolsTestCase, 'test_'))
  23. return suite
  24. class WSDLToolsTestCase(unittest.TestCase):
  25. def __init__(self, methodName='runTest'):
  26. unittest.TestCase.__init__(self, methodName)
  27. def setUp(self):
  28. makeTestSuite()
  29. self.path = nameGenerator.next()
  30. print self.path
  31. sys.stdout.flush()
  32. def __str__(self):
  33. teststr = unittest.TestCase.__str__(self)
  34. if hasattr(self, "path"):
  35. return "%s: %s" % (teststr, self.path)
  36. else:
  37. return "%s" % (teststr)
  38. def checkWSDLCollection(self, tag_name, component, key='name'):
  39. if self.wsdl is None:
  40. return
  41. definition = self.wsdl.document.documentElement
  42. version = DOM.WSDLUriToVersion(definition.namespaceURI)
  43. nspname = DOM.GetWSDLUri(version)
  44. for node in DOM.getElements(definition, tag_name, nspname):
  45. name = DOM.getAttr(node, key)
  46. comp = component[name]
  47. self.failUnlessEqual(eval('comp.%s' % key), name)
  48. def checkXSDCollection(self, tag_name, component, node, key='name'):
  49. for cnode in DOM.getElements(node, tag_name):
  50. name = DOM.getAttr(cnode, key)
  51. component[name]
  52. def test_all(self):
  53. try:
  54. if self.path[:7] == 'http://':
  55. self.wsdl = WSDLReader().loadFromURL(self.path)
  56. else:
  57. self.wsdl = WSDLReader().loadFromFile('tests/' + self.path)
  58. except TimeoutError:
  59. print "connection timed out"
  60. sys.stdout.flush()
  61. return
  62. except:
  63. self.path = self.path + ": load failed, unable to start"
  64. raise
  65. try:
  66. self.checkWSDLCollection('service', self.wsdl.services)
  67. except:
  68. self.path = self.path + ": wsdl.services"
  69. raise
  70. try:
  71. self.checkWSDLCollection('message', self.wsdl.messages)
  72. except:
  73. self.path = self.path + ": wsdl.messages"
  74. raise
  75. try:
  76. self.checkWSDLCollection('portType', self.wsdl.portTypes)
  77. except:
  78. self.path = self.path + ": wsdl.portTypes"
  79. raise
  80. try:
  81. self.checkWSDLCollection('binding', self.wsdl.bindings)
  82. except:
  83. self.path = self.path + ": wsdl.bindings"
  84. raise
  85. try:
  86. self.checkWSDLCollection('import', self.wsdl.imports, \
  87. key='namespace')
  88. except:
  89. self.path = self.path + ": wsdl.imports"
  90. raise
  91. try:
  92. for key in self.wsdl.types.keys():
  93. schema = self.wsdl.types[key]
  94. self.failUnlessEqual(key, schema.getTargetNamespace())
  95. definition = self.wsdl.document.documentElement
  96. version = DOM.WSDLUriToVersion(definition.namespaceURI)
  97. nspname = DOM.GetWSDLUri(version)
  98. for node in DOM.getElements(definition, 'types', nspname):
  99. for snode in DOM.getElements(node, 'schema'):
  100. tns = DOM.findTargetNS(snode)
  101. schema = self.wsdl.types[tns]
  102. self.schemaAttributesDeclarations(schema, snode)
  103. self.schemaAttributeGroupDeclarations(schema, snode)
  104. self.schemaElementDeclarations(schema, snode)
  105. self.schemaTypeDefinitions(schema, snode)
  106. except:
  107. self.path = self.path + ": wsdl.types"
  108. raise
  109. if self.wsdl.extensions:
  110. print 'No check for WSDLTools(%s) Extensions:' % (self.wsdl.name)
  111. for ext in self.wsdl.extensions:
  112. print '\t', ext
  113. def schemaAttributesDeclarations(self, schema, node):
  114. self.checkXSDCollection('attribute', schema.attr_decl, node)
  115. def schemaAttributeGroupDeclarations(self, schema, node):
  116. self.checkXSDCollection('group', schema.attr_groups, node)
  117. def schemaElementDeclarations(self, schema, node):
  118. self.checkXSDCollection('element', schema.elements, node)
  119. def schemaTypeDefinitions(self, schema, node):
  120. self.checkXSDCollection('complexType', schema.types, node)
  121. self.checkXSDCollection('simpleType', schema.types, node)
  122. def setUpOptions(section):
  123. cp = ConfigParser.ConfigParser()
  124. cp.read(cwd + '/config.txt')
  125. if not cp.sections():
  126. print 'fatal error: configuration file config.txt not present'
  127. sys.exit(0)
  128. if not cp.has_section(section):
  129. print '%s section not present in configuration file, exiting' % section
  130. sys.exit(0)
  131. return cp, len(cp.options(section))
  132. def getOption(cp, section):
  133. for name, value in cp.items(section):
  134. yield value
  135. def main():
  136. unittest.main(defaultTest="makeTestSuite")
  137. if __name__ == "__main__":
  138. main()