utils.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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.example"
  37. default_config = setup_util.get_fwroot() + "/benchmark.cfg.example"
  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 in both old and new directories
  51. fwroot = setup_util.get_fwroot()
  52. config_files = glob.glob("%s/*/benchmark_config" % fwroot)
  53. config_files.extend(glob.glob("%s/frameworks/*/*/benchmark_config" % fwroot))
  54. tests = []
  55. for config_file_name in config_files:
  56. config = None
  57. with open(config_file_name, 'r') as config_file:
  58. try:
  59. config = json.load(config_file)
  60. except ValueError:
  61. # User-friendly errors
  62. print("Error loading '%s'." % config_file_name)
  63. raise
  64. # Find all tests in the config file
  65. config_tests = framework_test.parse_config(config,
  66. os.path.dirname(config_file_name), benchmarker)
  67. # Filter
  68. for test in config_tests:
  69. if len(include) is 0 and len(exclude) is 0:
  70. # No filters, we are running everything
  71. tests.append(test)
  72. elif test.name in exclude:
  73. continue
  74. elif test.name in include:
  75. tests.append(test)
  76. else:
  77. # An include list exists, but this test is
  78. # not listed there, so we ignore it
  79. pass
  80. # Ensure we were able to locate everything that was
  81. # explicitly included
  82. if 0 != len(include):
  83. names = {test.name for test in tests}
  84. if 0 != len(set(include) - set(names)):
  85. missing = list(set(include) - set(names))
  86. raise Exception("Unable to locate tests %s" % missing)
  87. tests.sort(key=lambda x: x.name)
  88. return tests
  89. def gather_frameworks(include = [], exclude=[], benchmarker=None):
  90. '''Return a dictionary mapping frameworks->[test1,test2,test3]
  91. for quickly grabbing all tests in a grouped manner.
  92. Args have the same meaning as gather_tests'''
  93. tests = gather_tests(include, exclude, benchmarker)
  94. frameworks = dict()
  95. for test in tests:
  96. if test.framework not in frameworks:
  97. frameworks[test.framework] = []
  98. frameworks[test.framework].append(test)
  99. return frameworks
  100. def header(message, top='-', bottom='-'):
  101. '''
  102. Generates a clean header
  103. '''
  104. topheader = (top * 80)[:80]
  105. bottomheader = (bottom * 80)[:80]
  106. result = ""
  107. if topheader != "":
  108. result += "%s" % topheader
  109. if message != "":
  110. if result == "":
  111. result = " %s" % message
  112. else:
  113. result += "\n %s" % message
  114. if bottomheader != "":
  115. if result == "":
  116. result = "%s" % bottomheader
  117. else:
  118. result += "\n%s" % bottomheader
  119. return result + '\n'
  120. def check_services(services):
  121. def check_service(address, port):
  122. try:
  123. s = socket.socket()
  124. s.settimeout(20)
  125. s.connect((address, port))
  126. return (True, "")
  127. except Exception as ex:
  128. return (False, ex)
  129. finally:
  130. s.close
  131. res = []
  132. for s in services:
  133. r = check_service(s[1], s[2])
  134. res.append((s[0], r[0], str(r[1])))
  135. return res
  136. def verify_database_connections(services):
  137. allGo = True
  138. messages = []
  139. for r in check_services(services):
  140. if r[1]:
  141. messages.append(r[0] + ": is GO!")
  142. else:
  143. messages.append(r[0] + ": is _NO_ GO!: ERROR: " + r[2])
  144. allGo = False
  145. return (allGo, messages)