utils.py 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. import ConfigParser
  2. import os
  3. import glob
  4. import json
  5. import socket
  6. from ast import literal_eval
  7. def gather_langauges():
  8. '''
  9. Gathers all the known languages in the suite via the folder names
  10. beneath FWROOT.
  11. '''
  12. # Avoid setting up a circular import
  13. from setup.linux import setup_util
  14. lang_dir = os.path.join(setup_util.get_fwroot(), "frameworks")
  15. langs = []
  16. for dir in glob.glob(os.path.join(lang_dir, "*")):
  17. langs.append(dir.replace(lang_dir,"")[1:])
  18. return langs
  19. def gather_tests(include = [], exclude=[], benchmarker=None):
  20. '''
  21. Given test names as strings, returns a list of FrameworkTest objects.
  22. For example, 'aspnet-mysql-raw' turns into a FrameworkTest object with
  23. variables for checking the test directory, the test database os, and
  24. other useful items.
  25. With no arguments, every test in this framework will be returned.
  26. With include, only tests with this exact name will be returned.
  27. With exclude, all tests but those excluded will be returned.
  28. A benchmarker is needed to construct full FrameworkTest objects. If
  29. one is not provided, a default Benchmarker will be created.
  30. '''
  31. # Avoid setting up a circular import
  32. from benchmark import framework_test
  33. from benchmark.benchmarker import Benchmarker
  34. from setup.linux import setup_util
  35. # Help callers out a bit
  36. if include is None:
  37. include = []
  38. if exclude is None:
  39. exclude = []
  40. # Old, hacky method to exclude all tests was to
  41. # request a test known to not exist, such as ''.
  42. # If test '' was requested, short-circuit and return
  43. # nothing immediately
  44. if len(include) == 1 and '' in include:
  45. return []
  46. # Setup default Benchmarker using example configuration
  47. if benchmarker is None:
  48. default_config = setup_util.get_fwroot() + "/benchmark.cfg"
  49. config = ConfigParser.SafeConfigParser()
  50. config.readfp(open(default_config))
  51. defaults = dict(config.items("Defaults"))
  52. # Convert strings into proper python types
  53. for k,v in defaults.iteritems():
  54. try:
  55. defaults[k] = literal_eval(v)
  56. except Exception:
  57. pass
  58. # Ensure we only run the __init__ method of Benchmarker
  59. defaults['install'] = None
  60. defaults['results_name'] = "(unspecified, datetime = %Y-%m-%d %H:%M:%S)"
  61. defaults['results_environment'] = "My Server Environment"
  62. defaults['test_dir'] = None
  63. defaults['quiet'] = True
  64. benchmarker = Benchmarker(defaults)
  65. # Search for configuration files
  66. fwroot = setup_util.get_fwroot()
  67. config_files = []
  68. if benchmarker.test_dir:
  69. for test_dir in benchmarker.test_dir:
  70. dir_config_files = glob.glob("{!s}/frameworks/{!s}/benchmark_config.json".format(fwroot, test_dir))
  71. if len(dir_config_files):
  72. config_files.extend(dir_config_files)
  73. else:
  74. raise Exception("Unable to locate tests in test-dir: {!s}".format(test_dir))
  75. else:
  76. config_files.extend(glob.glob("{!s}/frameworks/*/*/benchmark_config.json".format(fwroot)))
  77. tests = []
  78. for config_file_name in config_files:
  79. config = None
  80. with open(config_file_name, 'r') as config_file:
  81. try:
  82. config = json.load(config_file)
  83. except ValueError:
  84. # User-friendly errors
  85. print("Error loading '%s'." % config_file_name)
  86. raise
  87. # Find all tests in the config file
  88. config_tests = framework_test.parse_config(config,
  89. os.path.dirname(config_file_name), benchmarker)
  90. # Filter
  91. for test in config_tests:
  92. if len(include) is 0 and len(exclude) is 0:
  93. # No filters, we are running everything
  94. tests.append(test)
  95. elif test.name in exclude:
  96. continue
  97. elif test.name in include:
  98. tests.append(test)
  99. else:
  100. # An include list exists, but this test is
  101. # not listed there, so we ignore it
  102. pass
  103. # Ensure we were able to locate everything that was
  104. # explicitly included
  105. if 0 != len(include):
  106. names = {test.name for test in tests}
  107. if 0 != len(set(include) - set(names)):
  108. missing = list(set(include) - set(names))
  109. raise Exception("Unable to locate tests %s" % missing)
  110. tests.sort(key=lambda x: x.name)
  111. return tests
  112. def gather_frameworks(include = [], exclude=[], benchmarker=None):
  113. '''Return a dictionary mapping frameworks->[test1,test2,test3]
  114. for quickly grabbing all tests in a grouped manner.
  115. Args have the same meaning as gather_tests'''
  116. tests = gather_tests(include, exclude, benchmarker)
  117. frameworks = dict()
  118. for test in tests:
  119. if test.framework not in frameworks:
  120. frameworks[test.framework] = []
  121. frameworks[test.framework].append(test)
  122. return frameworks
  123. def header(message, top='-', bottom='-'):
  124. '''
  125. Generates a clean header
  126. '''
  127. topheader = (top * 80)[:80]
  128. bottomheader = (bottom * 80)[:80]
  129. result = ""
  130. if topheader != "":
  131. result += "%s" % topheader
  132. if message != "":
  133. if result == "":
  134. result = " %s" % message
  135. else:
  136. result += "\n %s" % message
  137. if bottomheader != "":
  138. if result == "":
  139. result = "%s" % bottomheader
  140. else:
  141. result += "\n%s" % bottomheader
  142. return result + '\n'
  143. def check_services(services):
  144. def check_service(address, port):
  145. try:
  146. s = socket.socket()
  147. s.settimeout(20)
  148. s.connect((address, port))
  149. return (True, "")
  150. except Exception as ex:
  151. return (False, ex)
  152. finally:
  153. s.close
  154. res = []
  155. for s in services:
  156. r = check_service(s[1], s[2])
  157. res.append((s[0], r[0], str(r[1])))
  158. return res
  159. def verify_database_connections(services):
  160. allGo = True
  161. messages = []
  162. for r in check_services(services):
  163. if r[1]:
  164. messages.append(r[0] + ": is GO!")
  165. else:
  166. messages.append(r[0] + ": is _NO_ GO!: ERROR: " + r[2])
  167. allGo = False
  168. return (allGo, messages)