Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

KAFKA-17767 Extract test catalog from JUnit output [1/n] #17397

Merged
merged 11 commits into from
Oct 17, 2024
75 changes: 75 additions & 0 deletions .github/scripts/junit.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,22 @@
# limitations under the License.

import argparse
from collections import OrderedDict
import dataclasses
from functools import partial
from glob import glob
import logging
import os
import os.path
import pathlib
import re
import sys
from typing import Tuple, Optional, List, Iterable
import xml.etree.ElementTree
import html

import yaml


logger = logging.getLogger()
logger.setLevel(logging.DEBUG)
Expand Down Expand Up @@ -78,6 +83,56 @@ class TestSuite:
passed_tests: List[TestCase]


# Java method names can start with alpha, "_", or "$". Following characters can also include digits
method_matcher = re.compile(r"([a-zA-Z_$][a-zA-Z0-9_$]+).*")


def clean_test_name(test_name: str) -> str:
cleaned = test_name.strip("\"").rstrip("()")
m = method_matcher.match(cleaned)
return m.group(1)


class TestCatalogExporter:
def __init__(self):
self.all_tests = {} # module -> class -> set of methods

def handle_suite(self, module: str, suite: TestSuite):
if module not in self.all_tests:
self.all_tests[module] = OrderedDict()

for test in suite.failed_tests:
if test.class_name not in self.all_tests[module]:
self.all_tests[module][test.class_name] = set()
self.all_tests[module][test.class_name].add(clean_test_name(test.test_name))
for test in suite.passed_tests:
if test.class_name not in self.all_tests[module]:
self.all_tests[module][test.class_name] = set()
self.all_tests[module][test.class_name].add(clean_test_name(test.test_name))

def export(self, out_dir: str):
if not os.path.exists(out_dir):
logger.debug(f"Creating output directory {out_dir} for test catalog export.")
os.makedirs(out_dir)

total_count = 0
for module, module_tests in self.all_tests.items():
sorted_tests = {}
count = 0
for test_class, methods in module_tests.items():
sorted_methods = sorted(methods)
count += len(sorted_methods)
sorted_tests[test_class] = sorted_methods

out_path = os.path.join(out_dir, f"{module}.yaml")
logger.debug(f"Writing {count} tests for {module} into {out_path}.")
total_count += count
with open(out_path, "w") as fp:
yaml.dump(sorted_tests, fp)

logger.debug(f"Wrote {total_count} tests into test catalog.")


def parse_report(workspace_path, report_path, fp) -> Iterable[TestSuite]:
cur_suite: Optional[TestSuite] = None
partial_test_case = None
Expand Down Expand Up @@ -152,6 +207,10 @@ def pretty_time_duration(seconds: float) -> str:
required=False,
default="build/junit-xml/**/*.xml",
help="Path to XML files. Glob patterns are supported.")
parser.add_argument("--export-test-catalog",
required=False,
default="",
help="Optional path to dump all tests")

if not os.getenv("GITHUB_WORKSPACE"):
print("This script is intended to by run by GitHub Actions.")
Expand All @@ -177,8 +236,14 @@ def pretty_time_duration(seconds: float) -> str:
flaky_table = []
skipped_table = []

exporter = TestCatalogExporter()

logger.debug(f"::group::Parsing {len(reports)} JUnit Report Files")
for report in reports:
# Test report paths look like junit-xml/module/[test,quarantinedTest]/TEST-class.method.xml
report_path = pathlib.Path(report)
module_name = report_path.parent.parent.name
logger.debug(f"Module: {module_name}")
with open(report, "r") as fp:
logger.debug(f"Parsing {report}")
for suite in parse_report(workspace_path, report, fp):
Expand Down Expand Up @@ -216,7 +281,17 @@ def pretty_time_duration(seconds: float) -> str:
simple_class_name = skipped_test.class_name.split(".")[-1]
logger.debug(f"Found skipped test: {skipped_test}")
skipped_table.append((simple_class_name, skipped_test.test_name))

if args.export_test_catalog:
exporter.handle_suite(module_name, suite)

logger.debug("::endgroup::")

if args.export_test_catalog:
logger.debug(f"::group::Generating Test Catalog Files")
exporter.export(args.export_test_catalog)
logger.debug("::endgroup::")

duration = pretty_time_duration(total_time)
logger.info(f"Finished processing {len(reports)} reports")

Expand Down
15 changes: 15 additions & 0 deletions .github/scripts/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
PyYAML~=6.0
22 changes: 21 additions & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,10 @@ jobs:
uses: actions/checkout@v4
with:
persist-credentials: false
- uses: actions/setup-python@v5
with:
python-version: '3.12'
- run: pip install -r .github/scripts/requirements.txt
- name: Setup Gradle
uses: ./.github/actions/setup-gradle
with:
Expand Down Expand Up @@ -145,6 +149,14 @@ jobs:
**/build/reports/tests/*
compression-level: 9
if-no-files-found: ignore
- name: Archive JUnit XML
uses: actions/upload-artifact@v4
with:
name: junit-xml-${{ matrix.java }}
path: |
build/junit-xml/**/*.xml
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we export the artifact url for this task?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure we'll keep this step in long term since it is ~50Mb compressed (~1GB decompressed!).

compression-level: 9
if-no-files-found: ignore
- name: Archive Thread Dumps
id: thread-dump-upload-artifact
if: always() && steps.junit-test.outputs.exitcode == '124'
Expand All @@ -156,12 +168,20 @@ jobs:
compression-level: 9
if-no-files-found: ignore
- name: Parse JUnit tests
run: python .github/scripts/junit.py >> $GITHUB_STEP_SUMMARY
run: python .github/scripts/junit.py --export-test-catalog ./test-catalog >> $GITHUB_STEP_SUMMARY
env:
GITHUB_WORKSPACE: ${{ github.workspace }}
JUNIT_REPORT_URL: ${{ steps.junit-upload-artifact.outputs.artifact-url }}
THREAD_DUMP_URL: ${{ steps.thread-dump-upload-artifact.outputs.artifact-url }}
GRADLE_EXIT_CODE: ${{ steps.junit-test.outputs.exitcode }}
- name: Archive Test Catalog
if: ${{ always() && matrix.java == '21' }}
uses: actions/upload-artifact@v4
with:
name: test-catalog
path: test-catalog
compression-level: 9
if-no-files-found: ignore
- name: Archive Build Scan
if: always()
uses: actions/upload-artifact@v4
Expand Down
20 changes: 18 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,22 @@ def determineCommitId() {
}
}


/**
* For a given Project, compute a nice dash separated directory name
* to store the JUnit XML files in. E.g., Project ":connect:api" -> "connect-api"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't have consistent sub-module naming. The connect-related modules don't have a prefix, while other sub-modules must include a prefix to avoid conflicts to connect's sub modules. This means the helper should only revise the module names for the connect modules.

Copy link
Member Author

@mumrah mumrah Oct 11, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I considered this, but didn't want to add special cases to the "module name finding" method.

The module name computed here (e.g., connect-api, clients, group-coordinator-group-coordinator-api) will determine the name of the directory in build/junit-xml. As long as its unique, it doesn't matter too much what it is.

Another option here would be to reflect the actual module paths in the output directory paths. So, :connect:api test results would end up in "build/junit-xml/connect/api/[test, quarantinedTest]/...". This would also cause the test catalog directories to mirror the actual layout of the project.

*/
static def projectToJUnitXmlPath(project) {
var p = project
var projectNames = []
while (p != null) {
projectNames.push(p.name)
p = p.parent
if (p.name == "kafka") {
break;
}
}
return projectNames.join("-")
}


apply from: file('wrapper.gradle')
Expand Down Expand Up @@ -526,7 +541,8 @@ subprojects {
// were not run, but instead were restored via FROM-CACHE. See KAFKA-17479 for more details.
doLast {
if (ext.isGithubActions) {
def dest = rootProject.layout.buildDirectory.dir("junit-xml/${project.name}").get().asFile
def moduleDirPath = projectToJUnitXmlPath(project)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we apply this change to quarantinedTest as well?

def dest = rootProject.layout.buildDirectory.dir("junit-xml/${moduleDirPath}").get().asFile
println "Copy JUnit XML for ${project.name} to $dest"
ant.copy(todir: "$dest/test") {
ant.fileset(dir: "${test.reports.junitXml.entryPoint}")
Expand Down