make test: collect symlinks to failed tests 52/8052/5
authorKlement Sekera <ksekera@cisco.com>
Tue, 15 Aug 2017 05:09:02 +0000 (07:09 +0200)
committerDave Wallace <dwallacelf@gmail.com>
Thu, 17 Aug 2017 14:44:05 +0000 (14:44 +0000)
Compress files in temporary directories of failed tests and symlink
the directories under /tmp/vpp-failed-unittests location - preparation
for jenkins archivation. Automatically cleanup the directory at start
of test run.

The compression is performed only when environment variable
COMPRESS_FAILED_TEST_LOGS is set to one of "yes", "y", "1".
This is set in verify target, but left unset by default, so when
invoking make test by hand, files won't be compressed.

Change-Id: I84c8f1c6aa79aa9c0b753357022b1f195f17a283
Signed-off-by: Klement Sekera <ksekera@cisco.com>
Makefile
test/Makefile
test/framework.py
test/scripts/compress_failed.sh [new file with mode: 0755]

index 1cce928..c1a7cbb 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -501,7 +501,7 @@ endif
        $(call banner,"Building $(PKG) packages")
        @make pkg-$(PKG)
 ifeq ($(OS_ID)-$(OS_VERSION_ID),ubuntu-16.04)
-       @make test
+       @make COMPRESS_FAILED_TEST_LOGS=yes test
 endif
 
 
index 33779dc..72b4dac 100644 (file)
@@ -1,5 +1,7 @@
 .PHONY: verify-python-path
 
+VPP_TEST_FAILED_DIR=/tmp/vpp-failed-unittests/
+
 verify-python-path:
 ifndef VPP_PYTHON_PREFIX
        $(error VPP_PYTHON_PREFIX is not set)
@@ -84,7 +86,8 @@ $(PAPI_INSTALL_DONE): $(PIP_PATCH_DONE)
        @touch $@
 
 define retest-func
-       @scripts/setsid_wrapper.sh $(FORCE_FOREGROUND) $(PYTHON_VENV_PATH)/bin/activate python run_tests.py -d $(TEST_DIR) $(UNITTEST_EXTRA_OPTS)
+       @env VPP_TEST_FAILED_DIR=$(VPP_TEST_FAILED_DIR) scripts/setsid_wrapper.sh $(FORCE_FOREGROUND) $(PYTHON_VENV_PATH)/bin/activate python run_tests.py -d $(TEST_DIR) $(UNITTEST_EXTRA_OPTS)
+       @env VPP_TEST_FAILED_DIR=$(VPP_TEST_FAILED_DIR) scripts/compress_failed.sh
 endef
 
 .PHONY: sanity
@@ -129,6 +132,8 @@ shell: verify-python-path $(PAPI_INSTALL_DONE)
 reset:
        @rm -f /dev/shm/vpp-unittest-*
        @rm -rf /tmp/vpp-unittest-*
+       @rm -rf $(VPP_TEST_FAILED_DIR)
+       @mkdir $(VPP_TEST_FAILED_DIR)
 
 wipe: reset
        @rm -rf $(PYTHON_VENV_PATH)
index 89d95cb..008bda3 100644 (file)
@@ -275,7 +275,7 @@ class VppTestCase(unittest.TestCase):
         gc.collect()  # run garbage collection first
         cls.logger = getLogger(cls.__name__)
         cls.tempdir = tempfile.mkdtemp(
-            prefix='vpp-unittest-' + cls.__name__ + '-')
+            prefix='vpp-unittest-%s-' % cls.__name__)
         cls.file_handler = FileHandler("%s/log.txt" % cls.tempdir)
         cls.file_handler.setFormatter(
             Formatter(fmt='%(asctime)s,%(msecs)03d %(message)s',
@@ -781,6 +781,24 @@ class VppTestResult(unittest.TestResult):
         unittest.TestResult.addSkip(self, test, reason)
         self.result_string = colorize("SKIP", YELLOW)
 
+    def symlink_failed(self, test):
+        logger = None
+        if hasattr(test, 'logger'):
+            logger = test.logger
+        if hasattr(test, 'tempdir'):
+            try:
+                failed_dir = os.getenv('VPP_TEST_FAILED_DIR')
+                link_path = '%s/%s-FAILED' % (failed_dir,
+                                              test.tempdir.split("/")[-1])
+                if logger:
+                    logger.debug("creating a link to the failed test")
+                    logger.debug("os.symlink(%s, %s)" %
+                                 (test.tempdir, link_path))
+                os.symlink(test.tempdir, link_path)
+            except Exception as e:
+                if logger:
+                    logger.error(e)
+
     def addFailure(self, test, err):
         """
         Record a test failed result
@@ -800,6 +818,7 @@ class VppTestResult(unittest.TestResult):
         if hasattr(test, 'tempdir'):
             self.result_string = colorize("FAIL", RED) + \
                 ' [ temp dir used by test case: ' + test.tempdir + ' ]'
+            self.symlink_failed(test)
         else:
             self.result_string = colorize("FAIL", RED) + ' [no temp dir]'
 
@@ -822,6 +841,7 @@ class VppTestResult(unittest.TestResult):
         if hasattr(test, 'tempdir'):
             self.result_string = colorize("ERROR", RED) + \
                 ' [ temp dir used by test case: ' + test.tempdir + ' ]'
+            self.symlink_failed(test)
         else:
             self.result_string = colorize("ERROR", RED) + ' [no temp dir]'
 
diff --git a/test/scripts/compress_failed.sh b/test/scripts/compress_failed.sh
new file mode 100755 (executable)
index 0000000..6076b3b
--- /dev/null
@@ -0,0 +1,21 @@
+#!/bin/bash
+
+if [ "$(ls -A ${VPP_TEST_FAILED_DIR})" ]
+then
+       if [ "${COMPRESS_FAILED_TEST_LOGS}" == "yes" ]
+       then
+               echo -n "Compressing files in temporary directories from failed test runs..."
+               cd ${VPP_TEST_FAILED_DIR}
+               for d in *
+               do
+                       cd ${d}
+                       find . ! -path . -print0 | xargs -0 -n1 gzip
+                       cd ${VPP_TEST_FAILED_DIR}
+               done
+               echo "done."
+       else
+               echo "Not compressing files in temporary directories from failed test runs."
+       fi
+else
+       echo "No symlinks to failed tests' temporary directories found in ${VPP_TEST_FAILED_DIR}."
+fi