framework_test.py 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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, database_container_id):
  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. result = docker_helper.build(self.benchmarker_config, [self.name], out)
  59. if result != 0:
  60. return result
  61. return docker_helper.run(self.benchmarker_config, test_docker_files,
  62. database_container_id, out)
  63. def verify_urls(self, logPath):
  64. '''
  65. Verifys each of the URLs for this test. This will simply curl the URL and
  66. check for it's return status. For each url, a flag will be set on this
  67. object for whether or not it passed.
  68. Returns True if all verifications succeeded
  69. '''
  70. result = True
  71. def verify_type(test_type):
  72. verificationPath = os.path.join(logPath, test_type)
  73. try:
  74. os.makedirs(verificationPath)
  75. except OSError:
  76. pass
  77. with open(os.path.join(verificationPath, 'verification.txt'),
  78. 'w') as verification:
  79. test = self.runTests[test_type]
  80. test.setup_out(verification)
  81. verification.write(header("VERIFYING %s" % test_type.upper()))
  82. base_url = "http://%s:%s" % (
  83. self.benchmarker_config.server_host, self.port)
  84. try:
  85. # Verifies headers from the server. This check is made from the
  86. # App Server using Pythons requests module. Will do a second check from
  87. # the client to make sure the server isn't only accepting connections
  88. # from localhost on a multi-machine setup.
  89. results = test.verify(base_url)
  90. # Now verify that the url is reachable from the client machine, unless
  91. # we're already failing
  92. if not any(result == 'fail'
  93. for (result, reason, url) in results):
  94. p = subprocess.call(
  95. [
  96. "ssh", self.benchmarker_config.client_host,
  97. "curl -sSf %s" % base_url + test.get_url()
  98. ],
  99. shell=False,
  100. stdout=subprocess.PIPE,
  101. stderr=subprocess.PIPE)
  102. if p is not 0:
  103. results = [(
  104. 'fail',
  105. "Server did not respond to request from client machine.",
  106. base_url)]
  107. logging.warning(
  108. """This error usually means your server is only accepting
  109. requests from localhost.""")
  110. except ConnectionError as e:
  111. results = [('fail', "Server did not respond to request",
  112. base_url)]
  113. logging.warning(
  114. "Verifying test %s for %s caused an exception: %s",
  115. test_type, self.name, e)
  116. except Exception as e:
  117. results = [('fail', """Caused Exception in TFB
  118. This almost certainly means your return value is incorrect,
  119. but also that you have found a bug. Please submit an issue
  120. including this message: %s\n%s""" % (e, traceback.format_exc()),
  121. base_url)]
  122. logging.warning(
  123. "Verifying test %s for %s caused an exception: %s",
  124. test_type, self.name, e)
  125. traceback.format_exc()
  126. test.failed = any(
  127. result == 'fail' for (result, reason, url) in results)
  128. test.warned = any(
  129. result == 'warn' for (result, reason, url) in results)
  130. test.passed = all(
  131. result == 'pass' for (result, reason, url) in results)
  132. def output_result(result, reason, url):
  133. specific_rules_url = "http://frameworkbenchmarks.readthedocs.org/en/latest/Project-Information/Framework-Tests/#specific-test-requirements"
  134. color = Fore.GREEN
  135. if result.upper() == "WARN":
  136. color = Fore.YELLOW
  137. elif result.upper() == "FAIL":
  138. color = Fore.RED
  139. verification.write((
  140. " " + color + "%s" + Style.RESET_ALL + " for %s\n") %
  141. (result.upper(), url))
  142. print(" {!s}{!s}{!s} for {!s}\n".format(
  143. color, result.upper(), Style.RESET_ALL, url))
  144. if reason is not None and len(reason) != 0:
  145. for line in reason.splitlines():
  146. verification.write(" " + line + '\n')
  147. print(" " + line)
  148. if not test.passed:
  149. verification.write(
  150. " See %s\n" % specific_rules_url)
  151. print(" See {!s}\n".format(specific_rules_url))
  152. [output_result(r1, r2, url) for (r1, r2, url) in results]
  153. if test.failed:
  154. self.results.report_verify_results(self, test_type, 'fail')
  155. elif test.warned:
  156. self.results.report_verify_results(self, test_type, 'warn')
  157. elif test.passed:
  158. self.results.report_verify_results(self, test_type, 'pass')
  159. else:
  160. raise Exception(
  161. "Unknown error - test did not pass,warn,or fail")
  162. verification.flush()
  163. result = True
  164. for test_type in self.runTests:
  165. verify_type(test_type)
  166. if self.runTests[test_type].failed:
  167. result = False
  168. return result