[mutter/wip/nielsdg/junit-report] ci: Generate a cover report for the test suite



commit eb442eb417508c5e50921af7be4180cfb1e49178
Author: Niels De Graef <niels degraef barco com>
Date:   Mon Apr 8 14:34:13 2019 +0200

    ci: Generate a cover report for the test suite
    
    Based on @ebassi's work from https://gitlab.gnome.org/GNOME/glib/merge_requests/769
    
    This MR allows us to see the succeeded/failed tests each time the CI
    pipeline is run.
    
    https://gitlab.gnome.org/GNOME/mutter/merge_requests/528

 .gitlab-ci.yml                   |  14 ++++--
 .gitlab-ci/meson-junit-report.py | 101 +++++++++++++++++++++++++++++++++++++++
 .gitlab-ci/run-tests.sh          |  20 ++++++++
 3 files changed, 130 insertions(+), 5 deletions(-)
---
diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
index 09b0052c5..c858c8196 100644
--- a/.gitlab-ci.yml
+++ b/.gitlab-ci.yml
@@ -34,11 +34,15 @@ test-mutter:
     XDG_RUNTIME_DIR: "$CI_PROJECT_DIR/runtime-dir"
     GSETTINGS_SCHEMA_DIR: "$CI_PROJECT_DIR/build/data"
   script:
-    - mkdir -m 700 $XDG_RUNTIME_DIR
-    - glib-compile-schemas $GSETTINGS_SCHEMA_DIR
-    - >
-      dbus-run-session -- xvfb-run -s '+iglx -noreset'
-      meson test -C build --no-rebuild -t 10 --verbose --no-stdsplit --wrap catchsegv
+    - bash -x ./.gitlab-ci/run-tests.sh
   only:
     - merge_requests
     - /^.*$/
+  artifacts:
+    reports:
+      junit: "build/${CI_JOB_NAME}-report.xml"
+    name: "mutter-${CI_JOB_NAME}-${CI_COMMIT_REF_NAME}"
+    when: always
+    paths:
+      - "build/meson-logs"
+      - "build/${CI_JOB_NAME}-report.xml"
diff --git a/.gitlab-ci/meson-junit-report.py b/.gitlab-ci/meson-junit-report.py
new file mode 100755
index 000000000..ea4928248
--- /dev/null
+++ b/.gitlab-ci/meson-junit-report.py
@@ -0,0 +1,101 @@
+#!/usr/bin/env python3
+
+import argparse
+import datetime
+import json
+import os
+import sys
+import xml.etree.ElementTree as ET
+
+aparser = argparse.ArgumentParser(description='Turns a Meson test log into a JUnit report')
+aparser.add_argument('--project-name', metavar='NAME',
+                     help='The project name',
+                     default='unknown')
+aparser.add_argument('--job-id', metavar='ID',
+                     help='The job ID for the report',
+                     default='Unknown')
+aparser.add_argument('--branch', metavar='NAME',
+                     help='Branch of the project being tested',
+                     default='master')
+aparser.add_argument('--output', metavar='FILE',
+                     help='The output file, stdout by default',
+                     type=argparse.FileType('w', encoding='UTF-8'),
+                     default=sys.stdout)
+aparser.add_argument('infile', metavar='FILE',
+                     help='The input testlog.json, stdin by default',
+                     type=argparse.FileType('r', encoding='UTF-8'),
+                     default=sys.stdin)
+
+args = aparser.parse_args()
+
+outfile = args.output
+
+testsuites = ET.Element('testsuites')
+testsuites.set('id', '{}/{}'.format(args.job_id, args.branch))
+testsuites.set('package', args.project_name)
+testsuites.set('timestamp', datetime.datetime.utcnow().isoformat(timespec='minutes'))
+
+suites = {}
+for line in args.infile:
+    data = json.loads(line)
+    (full_suite, unit_name) = data['name'].split(' / ')
+    (project_name, suite_name) = full_suite.split(':')
+
+    duration = data['duration']
+    return_code = data['returncode']
+    log = data['stdout']
+
+    unit = {
+        'suite': suite_name,
+        'name': unit_name,
+        'duration': duration,
+        'returncode': return_code,
+        'stdout': log,
+    }
+
+    units = suites.setdefault(suite_name, [])
+    units.append(unit)
+
+for name, units in suites.items():
+    print('Processing suite {} (units: {})'.format(name, len(units)))
+
+    def if_failed(unit):
+        if unit['returncode'] != 0:
+            return True
+        return False
+
+    def if_succeded(unit):
+        if unit['returncode'] == 0:
+            return True
+        return False
+
+    successes = list(filter(if_succeded, units))
+    failures = list(filter(if_failed, units))
+    print(' - {}: {} pass, {} fail'.format(name, len(successes), len(failures)))
+
+    testsuite = ET.SubElement(testsuites, 'testsuite')
+    testsuite.set('name', '{}/{}'.format(args.project_name, name))
+    testsuite.set('tests', str(len(units)))
+    testsuite.set('errors', str(len(failures)))
+    testsuite.set('failures', str(len(failures)))
+
+    for unit in successes:
+        testcase = ET.SubElement(testsuite, 'testcase')
+        testcase.set('classname', '{}/{}'.format(args.project_name, unit['suite']))
+        testcase.set('name', unit['name'])
+        testcase.set('time', str(unit['duration']))
+
+    for unit in failures:
+        testcase = ET.SubElement(testsuite, 'testcase')
+        testcase.set('classname', '{}/{}'.format(args.project_name, unit['suite']))
+        testcase.set('name', unit['name'])
+        testcase.set('time', str(unit['duration']))
+
+        failure = ET.SubElement(testcase, 'failure')
+        failure.set('classname', '{}/{}'.format(args.project_name, unit['suite']))
+        failure.set('name', unit['name'])
+        failure.set('type', 'error')
+        failure.text = unit['stdout']
+
+output = ET.tostring(testsuites, encoding='unicode')
+outfile.write(output)
diff --git a/.gitlab-ci/run-tests.sh b/.gitlab-ci/run-tests.sh
new file mode 100755
index 000000000..a5f90688c
--- /dev/null
+++ b/.gitlab-ci/run-tests.sh
@@ -0,0 +1,20 @@
+#!/usr/bin/bash
+
+set +e
+
+mkdir -m 700 $XDG_RUNTIME_DIR
+glib-compile-schemas $GSETTINGS_SCHEMA_DIR
+
+dbus-run-session -- \
+    xvfb-run -s '+iglx -noreset' \
+        meson test -C build --no-rebuild -t 10 --wrap catchsegv
+
+exit_code=$?
+
+python3 .gitlab-ci/meson-junit-report.py \
+        --project-name=mutter \
+        --job-id "${CI_JOB_NAME}" \
+        --output "build/${CI_JOB_NAME}-report.xml" \
+        build/meson-logs/testlog-catchsegv.json
+
+exit $exit_code


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]