Skip to content

fix(test): collect coverage without using bash #189

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

Merged
merged 1 commit into from
Apr 3, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 29 additions & 20 deletions test/cases/orchestration.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
from . import formats
from .lazy_singleton import LazySingleton

import tarfile
import tempfile
import contextlib
import faulthandler
import json
Expand Down Expand Up @@ -480,7 +482,6 @@ def down(self):
Run `docker compose down` to bring down the orchestrated services.
Join the log-parsing thread.
"""

if self.has_coverage_data():
self.create_coverage_tarball()

Expand All @@ -506,37 +507,45 @@ def down(self):

@staticmethod
def has_coverage_data():
command = docker_compose_command(
"exec",
"-T",
"--",
"nginx",
"bash",
"-c",
"find /tmp -name '*.profraw' -print -quit | grep -q .",
)
result = subprocess.run(command)
return result.returncode == 0
command = docker_compose_command("exec", "-T", "--", "nginx", "find",
"/tmp", "-name", "*.profraw")
result = subprocess.run(command, capture_output=True)
if result.returncode != 0:
return False

return len(result.stdout)

@staticmethod
def create_coverage_tarball():
tar_command = docker_compose_command(
cmd = docker_compose_command(
"exec",
"-T",
"--",
"nginx",
"bash",
"-c",
"tar --transform='s@tmp/@@' -czf - -T <(find /tmp -maxdepth 1 -name '*.profraw')",
"find",
"/tmp",
"-name",
"*.profraw",
Copy link
Contributor

Choose a reason for hiding this comment

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

I think it would be simpler to just avoid process substitution and pass the files to tar on the command line:

tar_command = docker_compose_command(
    "exec",
    "-T",
    "--",
    "nginx",
    "sh",
    "-c",
    "find /tmp -maxdepth 1 -name '*.profraw' | xargs tar --transform='s@tmp/@@' -czf -",
)

Copy link
Contributor Author

@dmehala dmehala Apr 3, 2025

Choose a reason for hiding this comment

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

That's a possibility. It's clever, however, I am afraid not everyone can understand this command line, whereas python code is much more expressive.

EDIT: I did try using xargs but got piping issues IIRC or I probably did it wrong :/

)

with open("./coverage_data.tar.gz", "wb") as file:
result = subprocess.run(tar_command, stdout=file)

# Check if the tarball was created successfully
result = subprocess.run(cmd, capture_output=True)
if result.returncode != 0:
raise Exception("Failed to create tarball")

with tempfile.TemporaryDirectory() as work_dir:
files = []
for src_profraw in result.stdout.decode().split():
out_profraw = os.path.join(work_dir,
os.path.basename(src_profraw))
cp_cmd = docker_compose_command("cp", f"nginx:{src_profraw}",
out_profraw)
subprocess.run(cp_cmd, stdout=subprocess.DEVNULL)
files.append(out_profraw)

with tarfile.open("./coverage_data.tar.gz", "w:gz") as tar:
for f in files:
tar.add(f, arcname=os.path.basename(f))

@staticmethod
def nginx_version():
result = subprocess.run(
Expand Down