Skip to content

Commit

Permalink
tests: add infrastructure for test resources
Browse files Browse the repository at this point in the history
We currently only have one test resource file, sample.coredump.zst, but
the tests for #332 will add more. Create a package, tests.resources, to
contain test resources and a function, get_resource(), to decompress
them. It can also be used on the command line:

  python3 -m tests.resources $resource_name

Signed-off-by: Omar Sandoval <osandov@osandov.com>
  • Loading branch information
osandov committed Mar 22, 2024
1 parent a6b2506 commit 24b53f2
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 21 deletions.
3 changes: 2 additions & 1 deletion MANIFEST.in
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
recursive-include docs *.css *.ico *.png *.py *.rst
recursive-include tests *.py *.zst
recursive-include tests *.py
recursive-include tests/resources *.zst
include tests/linux_kernel/kmod/Makefile tests/linux_kernel/kmod/drgn_test.c
recursive-include contrib *.py *.rst
recursive-include tools *.py *.rst
Expand Down
5 changes: 5 additions & 0 deletions tests/resources/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
*
!/.gitignore
!/__init__.py
!/__main__.py
!*.zst
39 changes: 39 additions & 0 deletions tests/resources/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Copyright (c) Meta Platforms, Inc. and affiliates.
# SPDX-License-Identifier: LGPL-2.1-or-later

import os
from pathlib import Path
import subprocess
import tempfile
import unittest

from util import out_of_date


def get_resource(name: str) -> Path:
dir = Path(__file__).parent
decompressed_path = dir / name
compressed_path = dir / (name + ".zst")
if out_of_date(decompressed_path, compressed_path):
tmp_file = tempfile.NamedTemporaryFile(dir=dir, prefix=name, delete=False)
try:
try:
subprocess.check_call(
[
"zstd",
"--quiet",
"--force",
"--decompress",
"--stdout",
str(compressed_path),
],
stdout=tmp_file,
)
except FileNotFoundError:
raise unittest.SkipTest("zstd not found")
except BaseException:
os.unlink(tmp_file.name)
raise
else:
os.rename(tmp_file.name, decompressed_path)
return decompressed_path
15 changes: 15 additions & 0 deletions tests/resources/__main__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Copyright (c) Meta Platforms, Inc. and affiliates.
# SPDX-License-Identifier: LGPL-2.1-or-later

import argparse

from tests.resources import get_resource

parser = argparse.ArgumentParser(
description="decompress test resources and print their paths"
)
parser.add_argument("name", nargs="+", help="resource name")
args = parser.parse_args()

for name in args.name:
print(get_resource(name))
File renamed without changes.
23 changes: 3 additions & 20 deletions tests/test_thread.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@

import os
import os.path
import subprocess
import tempfile
import unittest

from drgn import Program
from tests import TestCase
from tests.resources import get_resource


class TestLive(TestCase):
Expand Down Expand Up @@ -60,23 +58,8 @@ class TestCoreDump(TestCase):
@classmethod
def setUpClass(cls):
super().setUpClass()
with tempfile.NamedTemporaryFile() as core_dump_file:
try:
subprocess.check_call(
[
"zstd",
"--quiet",
"--force",
"--decompress",
"--stdout",
os.path.join(os.path.dirname(__file__), "sample.coredump.zst"),
],
stdout=core_dump_file,
)
except FileNotFoundError:
raise unittest.SkipTest("zstd not found")
cls.prog = Program()
cls.prog.set_core_dump(core_dump_file.name)
cls.prog = Program()
cls.prog.set_core_dump(get_resource("multithreaded.core"))

def test_threads(self):
self.assertSequenceEqual(
Expand Down

0 comments on commit 24b53f2

Please sign in to comment.