framework_test.py 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. import os
  2. import subprocess
  3. import sys
  4. import traceback
  5. import logging
  6. from requests import ConnectionError
  7. from toolset.utils.output_helper import header
  8. from toolset.utils import docker_helper
  9. # Cross-platform colored text
  10. from colorama import Fore, Style
  11. class FrameworkTest:
  12. def __init__(self, name, directory, benchmarker_config, results, runTests,
  13. args):
  14. '''
  15. Constructor
  16. '''
  17. self.name = name
  18. self.directory = directory
  19. self.benchmarker_config = benchmarker_config
  20. self.results = results
  21. self.runTests = runTests
  22. self.fwroot = benchmarker_config.fwroot
  23. self.approach = ""
  24. self.classification = ""
  25. self.database = ""
  26. self.framework = ""
  27. self.language = ""
  28. self.orm = ""
  29. self.platform = ""
  30. self.webserver = ""
  31. self.os = ""
  32. self.database_os = ""
  33. self.display_name = ""
  34. self.notes = ""
  35. self.port = ""
  36. self.versus = ""
  37. self.docker_files = None
  38. # setup logging
  39. logging.basicConfig(stream=sys.stderr, level=logging.INFO)
  40. # Used in setup.sh scripts for consistency with
  41. # the bash environment variables
  42. self.troot = self.directory
  43. self.__dict__.update(args)
  44. ##########################################################################################
  45. # Public Methods
  46. ##########################################################################################
  47. def start(self, out):
  48. '''
  49. Start the test implementation
  50. '''
  51. test_docker_files = ["%s.dockerfile" % self.name]
  52. if self.docker_files is not None:
  53. if type(self.docker_files) is list:
  54. test_docker_files.extend(self.docker_files)
  55. else:
  56. raise Exception(
  57. "docker_files in benchmark_config.json must be an array")
  58. docker_helper.build(self.benchmarker_config, [self.name], out)
  59. docker_helper.run(self.benchmarker_config, test_docker_files, out)
  60. return 0
  61. def verify_urls(self, logPath):
  62. '''
  63. Verifys each of the URLs for this test. THis will sinply curl the URL and
  64. check for it's return status. For each url, a flag will be set on this
  65. object for whether or not it passed.
  66. Returns True if all verifications succeeded
  67. '''
  68. result = True
  69. def verify_type(test_type):
  70. verificationPath = os.path.join(logPath, test_type)
  71. try:
  72. os.makedirs(verificationPath)
  73. except OSError:
  74. pass
  75. with open(os.path.join(verificationPath, 'verification.txt'),
  76. 'w') as verification:
  77. test = self.runTests[test_type]
  78. test.setup_out(verification)
  79. verification.write(header("VERIFYING %s" % test_type.upper()))
  80. base_url = "http://%s:%s" % (
  81. self.benchmarker_config.server_host, self.port)
  82. try:
  83. # Verifies headers from the server. This check is made from the
  84. # App Server using Pythons requests module. Will do a second check from
  85. # the client to make sure the server isn't only accepting connections
  86. # from localhost on a multi-machine setup.
  87. results = test.verify(base_url)
  88. # Now verify that the url is reachable from the client machine, unless
  89. # we're already failing
  90. if not any(result == 'fail'
  91. for (result, reason, url) in results):
  92. p = subprocess.call(
  93. [
  94. "ssh", self.benchmarker_config.client_host,
  95. "curl -sSf %s" % base_url + test.get_url()
  96. ],
  97. shell=False,
  98. stdout=subprocess.PIPE,
  99. stderr=subprocess.PIPE)
  100. if p is not 0:
  101. results = [(
  102. 'fail',
  103. "Server did not respond to request from client machine.",
  104. base_url)]
  105. logging.warning(
  106. """This error usually means your server is only accepting
  107. requests from localhost.""")
  108. except ConnectionError as e:
  109. results = [('fail', "Server did not respond to request",
  110. base_url)]
  111. logging.warning(
  112. "Verifying test %s for %s caused an exception: %s",
  113. test_type, self.name, e)
  114. except Exception as e:
  115. results = [('fail', """Caused Exception in TFB
  116. This almost certainly means your return value is incorrect,
  117. but also that you have found a bug. Please submit an issue
  118. including this message: %s\n%s""" % (e, traceback.format_exc()),
  119. base_url)]
  120. logging.warning(
  121. "Verifying test %s for %s caused an exception: %s",
  122. test_type, self.name, e)
  123. traceback.format_exc()
  124. test.failed = any(
  125. result == 'fail' for (result, reason, url) in results)
  126. test.warned = any(
  127. result == 'warn' for (result, reason, url) in results)
  128. test.passed = all(
  129. result == 'pass' for (result, reason, url) in results)
  130. def output_result(result, reason, url):
  131. specific_rules_url = "http://frameworkbenchmarks.readthedocs.org/en/latest/Project-Information/Framework-Tests/#specific-test-requirements"
  132. color = Fore.GREEN
  133. if result.upper() == "WARN":
  134. color = Fore.YELLOW
  135. elif result.upper() == "FAIL":
  136. color = Fore.RED
  137. verification.write((
  138. " " + color + "%s" + Style.RESET_ALL + " for %s\n") %
  139. (result.upper(), url))
  140. print(" {!s}{!s}{!s} for {!s}\n".format(
  141. color, result.upper(), Style.RESET_ALL, url))
  142. if reason is not None and len(reason) != 0:
  143. for line in reason.splitlines():
  144. verification.write(" " + line + '\n')
  145. print(" " + line)
  146. if not test.passed:
  147. verification.write(
  148. " See %s\n" % specific_rules_url)
  149. print(" See {!s}\n".format(specific_rules_url))
  150. [output_result(r1, r2, url) for (r1, r2, url) in results]
  151. if test.failed:
  152. self.results.report_verify_results(self, test_type, 'fail')
  153. elif test.warned:
  154. self.results.report_verify_results(self, test_type, 'warn')
  155. elif test.passed:
  156. self.results.report_verify_results(self, test_type, 'pass')
  157. else:
  158. raise Exception(
  159. "Unknown error - test did not pass,warn,or fail")
  160. verification.flush()
  161. result = True
  162. for test_type in self.runTests:
  163. verify_type(test_type)
  164. if self.runTests[test_type].failed:
  165. result = False
  166. return result