utils.py 5.7 KB

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