Skip to content

Commit

Permalink
Add log messages to containers logs on test start,end
Browse files Browse the repository at this point in the history
  • Loading branch information
Shay Arbov committed Oct 9, 2017
1 parent dd0d370 commit 7dd23fb
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 17 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ test:

coverage: test
# Create a coverage report and validate the given threshold
coverage html --fail-under=87 -d build/coverage
coverage html --fail-under=85 -d build/coverage

nose2:
mkdir -p build/
Expand Down
34 changes: 26 additions & 8 deletions docker_test_tools/environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from docker_test_tools.api_version import get_server_api_version

SEPARATOR = '|'
COMMON_LOG_PREFIX = '>>>'

log = logging.getLogger(__name__)

Expand Down Expand Up @@ -139,14 +140,27 @@ def split_logs(self):
try:
with open(self.log_path, 'r') as combined_log_file:
for log_line in combined_log_file.readlines():
separator_location = log_line.find(SEPARATOR)
if separator_location != -1:
service_name = log_line[:separator_location].strip()
message = log_line[separator_location + 1:]
if service_name not in services_log_files:
services_log_files[service_name] = open(os.path.join(log_dir, service_name + '.log'), 'w')

services_log_files[service_name].write(message)

# Write common log lines to all log files
if log_line.startswith(COMMON_LOG_PREFIX):
for services_log_file in services_log_files.values():
services_log_file.write("\n{log_line}\n".format(log_line=log_line))

else:
# Write each log message to the appropriate log file (by prefix)
separator_location = log_line.find(SEPARATOR)
if separator_location != -1:

# split service name from log message
service_name = log_line[:separator_location].strip()
message = log_line[separator_location + 1:]

# Create a log file if one doesn't exists
if service_name not in services_log_files:
services_log_files[service_name] = \
open(os.path.join(log_dir, service_name + '.log'), 'w')

services_log_files[service_name].write(message)
finally:
for services_log_file in services_log_files.values():
services_log_file.close()
Expand Down Expand Up @@ -437,6 +451,10 @@ def _get_environment_variables():
env['COMPOSE_API_VERSION'] = env['DOCKER_API_VERSION'] = server_api_version
return env

def write_common_log_message(self, message):
self.logs_file.write('\n{prefix} {message}\n\n'.format(prefix=COMMON_LOG_PREFIX, message=message))
self.logs_file.flush()

@staticmethod
def _to_str(value):
"""
Expand Down
14 changes: 13 additions & 1 deletion docker_test_tools/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,20 @@ def startTestRun(self, event):
self.controller.setup()

def startTest(self, event):
"""Assign the controller object to the test."""
"""Run on test start.
- Assign the controller object to the test.
- Write a test started log message to the main log file.
"""
event.test.controller = self.controller
self.controller.write_common_log_message("TEST STARTED: {test_id}".format(test_id=event.test.id()))

def stopTest(self, event):
""""Run on test stop.
- Write a test ended log message to the main log file.
"""
self.controller.write_common_log_message("TEST ENDED: {test_id}".format(test_id=event.test.id()))

def stopTestRun(self, event):
"""Tears down the environment using docker commands."""
Expand Down
27 changes: 20 additions & 7 deletions tests/integration/conftest.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,32 @@
"""utilized by in pytest configuration."""
import pytest

from docker_test_tools.environment import EnvironmentController

controller = EnvironmentController.from_file(config_path='tests/integration/pytest.cfg')


@pytest.fixture(scope="session", autouse=True)
def global_setup_teardown():
"""This function will be executed once per testing session."""
def pytest_configure(config):
"""Run prior to any test - setup the environment."""
controller.setup()
yield


def pytest_unconfigure(config):
"""Run post all tests - tear down the environment."""
controller.teardown()


def pytest_runtest_setup(item):
"""Assign the controller as a test class member."""
"""Run on test start.
- Assign the controller object to the test.
- Write a test started log message to the main log file.
"""
item.parent.obj.controller = controller
controller.write_common_log_message("TEST STARTED: {test_id}".format(test_id=item.nodeid))


def pytest_runtest_teardown(item):
""""Run on test stop.
- Write a test ended log message to the main log file.
"""
controller.write_common_log_message("TEST ENDED: {test_id}".format(test_id=item.nodeid))

0 comments on commit 7dd23fb

Please sign in to comment.