framework_test.py 7.2 KB

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