conftest.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. # Licensed to the Apache Software Foundation (ASF) under one or more
  2. # contributor license agreements. See the NOTICE file distributed with
  3. # this work for additional information regarding copyright ownership.
  4. # The ASF licenses this file to You under the Apache License, Version 2.0
  5. # (the "License"); you may not use this file except in compliance with
  6. # the License. You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. import logging
  16. import os
  17. import time
  18. import subprocess
  19. import pytest
  20. EPOCH_TIME = int(time.time())
  21. def pytest_addoption(parser):
  22. parser.addoption("--output-dir",
  23. action="store",
  24. default="/tmp/BlockadeTests",
  25. help="location of output directory where output log "
  26. "and plot files will be created")
  27. parser.addoption("--log-format",
  28. action="store",
  29. default="%(asctime)s|%(levelname)s|%(threadName)s|"
  30. "%(filename)s:%(lineno)s -"
  31. " %(funcName)s()|%(message)s",
  32. help="specify log format")
  33. parser.addoption("--log-level",
  34. action="store",
  35. default="info",
  36. help="specify log level")
  37. parser.addoption("--containerStatusSleep",
  38. action="store",
  39. default="900",
  40. help="sleep time before checking container status")
  41. parser.addoption("--runSecondPhase",
  42. action="store",
  43. default="false",
  44. help="run second phase of the tests")
  45. @pytest.fixture
  46. def run_second_phase(request):
  47. """
  48. :param request:
  49. This function returns if the user has opted for running second phase
  50. of the tests.
  51. """
  52. return request.config.getoption("--runSecondPhase")
  53. def pytest_configure(config):
  54. global OUTPUT_DIR
  55. os.environ["CONTAINER_STATUS_SLEEP"] = config.option.containerStatusSleep
  56. OUTPUT_DIR = "%s/%s" % (config.option.output_dir, EPOCH_TIME)
  57. try:
  58. os.makedirs(OUTPUT_DIR)
  59. except OSError, e:
  60. raise Exception(e.strerror + ": " + e.filename)
  61. log_file = os.path.join(OUTPUT_DIR, "output.log")
  62. if config.option.log_level == "trace":
  63. loglevel = eval("logging.DEBUG")
  64. else:
  65. loglevel = eval("logging." + config.option.log_level.upper())
  66. logformatter = logging.Formatter(config.option.log_format)
  67. logging.basicConfig(filename=log_file,
  68. filemode='w',
  69. level=loglevel,
  70. format=config.option.log_format)
  71. console = logging.StreamHandler()
  72. console.setLevel(loglevel)
  73. console.setFormatter(logformatter)
  74. logging.getLogger('').addHandler(console)
  75. def pytest_report_teststatus(report):
  76. logger = logging.getLogger('main')
  77. loc, line, name = report.location
  78. if report.outcome == 'skipped':
  79. pass
  80. elif report.when == 'setup':
  81. logger.info("RUNNING TEST \"%s\" at location \"%s\" at line number"
  82. " \"%s\"" % (name, loc, str(line)))
  83. elif report.when == 'call':
  84. logger.info("TEST \"%s\" %s in %3.2f seconds" %
  85. (name, report.outcome.upper(), report.duration))
  86. log_file_path = "%s/%s_all_docker.log" % \
  87. (OUTPUT_DIR, name)
  88. gather_docker_logs(log_file_path)
  89. def pytest_sessionfinish(session):
  90. logger = logging.getLogger('main')
  91. logger.info("ALL TESTS FINISHED")
  92. logger.info("ALL logs present in following directory: %s", OUTPUT_DIR)
  93. def gather_docker_logs(log_file_path):
  94. docker_compose_file = os.environ["DOCKER_COMPOSE_FILE"]
  95. output = subprocess.check_output(["docker-compose", "-f",
  96. docker_compose_file, "logs"])
  97. with open(log_file_path, "w") as text_file:
  98. text_file.write(output)