URL: https://github.com/freeipa/freeipa/pull/803
Author: tomaskrizek
 Title: #803: ipatests: add systemd journal collection for multihost tests
Action: synchronized

To pull the PR as Git branch:
git remote add ghfreeipa https://github.com/freeipa/freeipa
git fetch ghfreeipa pull/803/head:pr803
git checkout pr803
From 477fa691bf8f297102953d1c6eb324c256f92881 Mon Sep 17 00:00:00 2001
From: Tomas Krizek <tkri...@redhat.com>
Date: Mon, 22 May 2017 18:27:44 +0200
Subject: [PATCH 1/2] ipatests: change logdir naming pattern for multihost
 tests

Remove brackets from the paths in naming pattern of directories
for multihost logs. Brackets in filenames require special handling
in markdown URLs, bash paths etc.

Related: https://pagure.io/freeipa/issue/6971

Signed-off-by: Tomas Krizek <tkri...@redhat.com>
---
 ipatests/pytest_plugins/integration/__init__.py | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)

diff --git a/ipatests/pytest_plugins/integration/__init__.py b/ipatests/pytest_plugins/integration/__init__.py
index 8fb10422e5..86a335605a 100644
--- a/ipatests/pytest_plugins/integration/__init__.py
+++ b/ipatests/pytest_plugins/integration/__init__.py
@@ -24,6 +24,7 @@
 import os
 import tempfile
 import shutil
+import re
 
 import pytest
 from pytest_multihost import make_multihost_fixture
@@ -45,6 +46,14 @@ def pytest_addoption(parser):
         help="Directory to store integration test logs in.")
 
 
+def _get_logname_from_node(node):
+    name = node.nodeid
+    name = re.sub('\(\)/', '', name)      # remove ()/
+    name = re.sub('[()]', '', name)       # and standalone brackets
+    name = re.sub('(/|::)', '-', name)
+    return name
+
+
 def collect_test_logs(node, logs_dict, test_config):
     """Collect logs from a test
 
@@ -55,7 +64,7 @@ def collect_test_logs(node, logs_dict, test_config):
     :param test_config: Pytest configuration
     """
     collect_logs(
-        name=node.nodeid.replace('/', '-').replace('::', '-'),
+        name=_get_logname_from_node(node),
         logs_dict=logs_dict,
         logfile_dir=test_config.getoption('logfile_dir'),
         beakerlib_plugin=test_config.pluginmanager.getplugin('BeakerLibPlugin'),

From 7cad33d0b96c1faa49244eac80a81df4295b989b Mon Sep 17 00:00:00 2001
From: Tomas Krizek <tkri...@redhat.com>
Date: Mon, 22 May 2017 18:33:49 +0200
Subject: [PATCH 2/2] ipatests: add systemd journal collection for multihost
 tests

Some messages are only logged in journal. Collection of journal
makes debugging failed tests from logs easier.

Fixes: https://pagure.io/freeipa/issue/6971

Signed-off-by: Tomas Krizek <tkri...@redhat.com>
---
 ipatests/pytest_plugins/integration/__init__.py   | 32 +++++++++++++++++++++++
 ipatests/pytest_plugins/integration/config.py     |  2 ++
 ipatests/pytest_plugins/integration/env_config.py |  2 ++
 ipatests/test_integration/test_testconfig.py      |  4 ++-
 4 files changed, 39 insertions(+), 1 deletion(-)

diff --git a/ipatests/pytest_plugins/integration/__init__.py b/ipatests/pytest_plugins/integration/__init__.py
index 86a335605a..c7090c3a95 100644
--- a/ipatests/pytest_plugins/integration/__init__.py
+++ b/ipatests/pytest_plugins/integration/__init__.py
@@ -71,6 +71,36 @@ def collect_test_logs(node, logs_dict, test_config):
     )
 
 
+def collect_systemd_journal(node, hosts, test_config):
+    """Collect systemd journal from remote hosts
+
+    :param node: The pytest collection node (request.node)
+    :param hosts: List of hosts from which to collect journal
+    :param test_config: Pytest configuration
+    """
+    name = _get_logname_from_node(node)
+    logfile_dir = test_config.getoption('logfile_dir')
+
+    for host in hosts:
+        log.info("Collecting journal from: %s", host.hostname)
+
+        topdirname = os.path.join(logfile_dir, name, host.hostname)
+        if not os.path.exists(topdirname):
+            os.makedirs(topdirname)
+
+        # Get journal content
+        cmd = host.run_command(
+            ['journalctl', '--since', host.config.log_journal_since],
+            log_stdout=False, raiseonerr=False)
+        if cmd.returncode:
+            log.error('An error occurred while collecting journal')
+            continue
+
+        # Write journal to file
+        with open(os.path.join(topdirname, "journal"), 'w') as f:
+            f.write(cmd.stdout_text)
+
+
 def collect_logs(name, logs_dict, logfile_dir=None, beakerlib_plugin=None):
     """Collect logs from remote hosts
 
@@ -157,7 +187,9 @@ def integration_logs(class_integration_logs, request):
     """Provides access to test integration logs, and collects after each test
     """
     yield class_integration_logs
+    hosts = class_integration_logs.keys()
     collect_test_logs(request.node, class_integration_logs, request.config)
+    collect_systemd_journal(request.node, hosts, request.config)
 
 
 @pytest.yield_fixture(scope='class')
diff --git a/ipatests/pytest_plugins/integration/config.py b/ipatests/pytest_plugins/integration/config.py
index 3d026e3408..c5cd162a2d 100644
--- a/ipatests/pytest_plugins/integration/config.py
+++ b/ipatests/pytest_plugins/integration/config.py
@@ -41,6 +41,7 @@ class Config(pytest_multihost.config.Config):
         'ad_admin_password',
         'dns_forwarder',
         'domain_level',
+        'log_journal_since',
     }
 
     def __init__(self, **kwargs):
@@ -62,6 +63,7 @@ def __init__(self, **kwargs):
         # 8.8.8.8 is probably the best-known public DNS
         self.dns_forwarder = kwargs.get('dns_forwarder') or '8.8.8.8'
         self.debug = False
+        self.log_journal_since = kwargs.get('log_journal_since') or '-1h'
         if self.domain_level is None:
             self.domain_level = MAX_DOMAIN_LEVEL
 
diff --git a/ipatests/pytest_plugins/integration/env_config.py b/ipatests/pytest_plugins/integration/env_config.py
index bd8c8d2a92..d140aa9df6 100644
--- a/ipatests/pytest_plugins/integration/env_config.py
+++ b/ipatests/pytest_plugins/integration/env_config.py
@@ -63,6 +63,8 @@
     _SettingInfo('ipv6', 'IPv6SETUP', False),
     _SettingInfo('debug', 'IPADEBUG', False),
     _SettingInfo('domain_level', 'DOMAINLVL', MAX_DOMAIN_LEVEL),
+
+    _SettingInfo('log_journal_since', 'LOG_JOURNAL_SINCE', '-1h'),
 )
 
 
diff --git a/ipatests/test_integration/test_testconfig.py b/ipatests/test_integration/test_testconfig.py
index c66be8c33d..b495f368e4 100644
--- a/ipatests/test_integration/test_testconfig.py
+++ b/ipatests/test_integration/test_testconfig.py
@@ -41,7 +41,8 @@
     "dirman_password": "Secret123",
     "ntp_server": "ntp.clock.test",
     "admin_password": "Secret123",
-    "domain_level": MAX_DOMAIN_LEVEL
+    "domain_level": MAX_DOMAIN_LEVEL,
+    "log_journal_since": "-1h",
 }
 
 DEFAULT_OUTPUT_ENV = {
@@ -60,6 +61,7 @@
     "IPv6SETUP": "",
     "IPADEBUG": "",
     "DOMAINLVL": str(MAX_DOMAIN_LEVEL),
+    "LOG_JOURNAL_SINCE": "-1h",
 }
 
 DEFAULT_INPUT_ENV = {
_______________________________________________
FreeIPA-devel mailing list -- freeipa-devel@lists.fedorahosted.org
To unsubscribe send an email to freeipa-devel-le...@lists.fedorahosted.org

Reply via email to