-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Post process e2e tests logs to make them more readable #31615
Merged
Merged
Changes from 11 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
5f4b757
Post process e2e tests logs to make them more readable
KevinFairise2 88dc105
Update logs folder param name
KevinFairise2 219f23b
Handle better test depth
KevinFairise2 b37c8e6
Merge branch 'main' of github.com:DataDog/datadog-agent into kfairise…
KevinFairise2 807705c
Fix test depth and add it for installer tests
KevinFairise2 f00bc0f
Improve processing by adding parent test logs for nested tests
KevinFairise2 0736aaf
Fix args of post process function
KevinFairise2 f156d50
Remove print
KevinFairise2 1865c3d
Pretty print max logs
KevinFairise2 0c46490
Merge branch 'main' into kfairise/post-process-e2e-logs
KevinFairise2 27b99a2
Remove print
KevinFairise2 596cffb
Adress comments
KevinFairise2 596c242
Merge branch 'main' of github.com:DataDog/datadog-agent into kfairise…
KevinFairise2 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,7 +23,7 @@ | |
from tasks.libs.common.git import get_commit_sha | ||
from tasks.libs.common.go import download_go_dependencies | ||
from tasks.libs.common.gomodules import get_default_modules | ||
from tasks.libs.common.utils import REPO_PATH, color_message, running_in_ci | ||
from tasks.libs.common.utils import REPO_PATH, color_message, gitlab_section, running_in_ci | ||
from tasks.tools.e2e_stacks import destroy_remote_stack | ||
|
||
|
||
|
@@ -66,6 +66,9 @@ def run( | |
test_washer=False, | ||
agent_image="", | ||
cluster_agent_image="", | ||
logs_post_processing=False, | ||
logs_post_processing_test_depth=1, | ||
logs_folder="e2e_logs", | ||
): | ||
""" | ||
Run E2E Tests based on test-infra-definitions infrastructure provisioning. | ||
|
@@ -164,6 +167,23 @@ def run( | |
'You can also add `E2E_DEV_MODE="true"` to run in dev mode which will leave the environment up after the tests.' | ||
) | ||
|
||
if logs_post_processing: | ||
post_processed_output = post_process_output( | ||
test_res[0].result_json_path, test_depth=logs_post_processing_test_depth | ||
KevinFairise2 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
) | ||
|
||
os.makedirs(logs_folder, exist_ok=True) | ||
write_result_to_log_files(post_processed_output, logs_folder) | ||
try: | ||
pretty_print_logs(post_processed_output) | ||
except TooManyLogsError: | ||
print( | ||
color_message( | ||
"Too many logs to print, skipping pretty print. You can still find them properly organized in the job artifacts", | ||
KevinFairise2 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
"yellow", | ||
) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🥜 nitpick: I prefer to print There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Or maybe we can color all the message in bold? |
||
) | ||
|
||
if not success: | ||
raise Exit(code=1) | ||
|
||
|
@@ -253,6 +273,92 @@ def cleanup_remote_stacks(ctx, stack_regex, pulumi_backend): | |
print(f"Failed to destroy stack {stack}") | ||
|
||
|
||
def post_process_output(path: str, test_depth: int = 1): | ||
""" | ||
Post process the test results to add the test run name | ||
path: path to the test result json file | ||
test_depth: depth of the test name to consider | ||
|
||
By default the test_depth is set to 1, which means that the logs will be splitted depending on the test suite name. | ||
If we use a single test suite to run multiple tests we can increase the test_depth to split the logs per test. | ||
For example with: | ||
TestPackages/run_ubuntu | ||
TestPackages/run_centos | ||
TestPackages/run_debian | ||
We should set test_depth to 2 to avoid mixing all the logs of the different tested platform | ||
""" | ||
|
||
def is_parent(parent: list[str], child: list[str]) -> bool: | ||
for i in range(len(parent)): | ||
if parent[i] != child[i]: | ||
return False | ||
return True | ||
|
||
logs_per_test = {} | ||
with open(path) as f: | ||
all_lines = f.readlines() | ||
|
||
# Initalize logs_per_test with all test names | ||
for line in all_lines: | ||
json_line = json.loads(line) | ||
if "Package" not in json_line or "Test" not in json_line or "Output" not in json_line: | ||
continue | ||
splitted_test = json_line["Test"].split("/") | ||
if len(splitted_test) < test_depth: | ||
continue | ||
if json_line["Package"] not in logs_per_test: | ||
logs_per_test[json_line["Package"]] = {} | ||
|
||
test_name = splitted_test[: min(test_depth, len(splitted_test))] | ||
logs_per_test[json_line["Package"]]["/".join(test_name)] = [] | ||
|
||
for line in all_lines: | ||
json_line = json.loads(line) | ||
if "Package" not in json_line or "Test" not in json_line or "Output" not in json_line: | ||
continue | ||
|
||
if "===" in json_line["Output"]: # Ignore these lines that are produced when running test concurrently | ||
continue | ||
|
||
splitted_test = json_line["Test"].split("/") | ||
|
||
if len(splitted_test) < test_depth: # Append logs to all children tests | ||
for test_name in logs_per_test[json_line["Package"]]: | ||
if is_parent(splitted_test, test_name.split("/")): | ||
logs_per_test[json_line["Package"]][test_name].append(json_line["Output"]) | ||
continue | ||
|
||
logs_per_test[json_line["Package"]]["/".join(splitted_test[:test_depth])].append(json_line["Output"]) | ||
return logs_per_test | ||
|
||
|
||
def write_result_to_log_files(logs_per_test, log_folder): | ||
for package, tests in logs_per_test.items(): | ||
for test, logs in tests.items(): | ||
with open(f"{log_folder}/{package.replace('/', '-')}.{test.replace('/', '-')}.log", "w") as f: | ||
KevinFairise2 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
f.write("".join(logs)) | ||
|
||
|
||
class TooManyLogsError(Exception): | ||
pass | ||
|
||
|
||
def pretty_print_logs(logs_per_test, max_size=250000): | ||
# Compute size in bytes of what we are about to print. If it exceeds max_size, we skip printing because it will make the Gitlab logs almost completely collapsed. | ||
# By default Gitlab has a limit of 500KB per job log, so we want to avoid printing too much. | ||
size = 0 | ||
for _, tests in logs_per_test.items(): | ||
for _, logs in tests.items(): | ||
size += len("".join(logs).encode()) | ||
if size > max_size: | ||
raise TooManyLogsError | ||
for package, tests in logs_per_test.items(): | ||
for test, logs in tests.items(): | ||
with gitlab_section("Complete logs for " + package + "." + test, collapsed=True): | ||
print("Complete logs for " + package + "." + test) | ||
print("".join(logs)) | ||
|
||
|
||
@task | ||
def deps(ctx, verbose=False): | ||
""" | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
💬 suggestion
What about parsing
E2E_LOGS_PROCESSING_TEST_DEPTH
at python time?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could, but I prefered to keep it as a parameter so it is more discoverable. Otherwise you can only use it if you know it exists