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.
 
 
 

181 lines
6.0 KiB

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