Skip to content

Commit

Permalink
Implement find_command in frameworks
Browse files Browse the repository at this point in the history
  • Loading branch information
acerv committed Nov 1, 2023
1 parent c052ba4 commit d79f5a9
Show file tree
Hide file tree
Showing 6 changed files with 135 additions and 6 deletions.
37 changes: 37 additions & 0 deletions libkirk/kselftests.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,43 @@ async def get_suites(self, sut: SUT) -> list:

return ["cgroup", "bpf"]

async def find_command(self, sut: SUT, command: str) -> Test:
if not sut:
raise ValueError("SUT is None")

if not command:
raise ValueError("command is empty")

cmd_args = command.split(" ")
suite_folder = None

for suite in await self.get_suites(sut):
folder = os.path.join(self._root, suite)
binary = os.path.join(folder, cmd_args[0])

ret = await sut.run_command(f"test -f {binary}")
if ret["returncode"] == 0:
suite_folder = folder
break

cwd = None
env = None

ret = await sut.run_command(f"test -d {suite_folder}")
if ret["returncode"] == 0:
cwd = suite_folder
env={"PATH": suite_folder}

test = Test(
name=cmd_args[0],
cmd=cmd_args[0],
args=cmd_args[1:] if len(cmd_args) > 0 else None,
cwd=cwd,
env=env,
parallelizable=False)

return test

async def find_suite(self, sut: SUT, name: str) -> Suite:
if not sut:
raise ValueError("SUT is None")
Expand Down
26 changes: 26 additions & 0 deletions libkirk/liburing.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,32 @@ async def _is_parallelizable(sut: SUT, cmd: str) -> bool:

return parallel

async def find_command(self, sut: SUT, command: str) -> Test:
if not sut:
raise ValueError("SUT is None")

if not command:
raise ValueError("command is empty")

cmd_args = command.split(" ")
cwd = None
env = None

ret = await sut.run_command(f"test -d {self._root}")
if ret["returncode"] == 0:
cwd = self._root
env={"PATH": self._root}

test = Test(
name=cmd_args[0],
cmd=cmd_args[0],
args=cmd_args[1:] if len(cmd_args) > 0 else None,
cwd=cwd,
env=env,
parallelizable=False)

return test

async def find_suite(self, sut: SUT, name: str) -> Suite:
if not sut:
raise ValueError("SUT is None")
Expand Down
36 changes: 31 additions & 5 deletions libkirk/ltp.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ def __init__(self) -> None:
self._root = None
self._env = None
self._max_runtime = None
self._tc_folder = None

@ property
def config_help(self) -> dict:
Expand Down Expand Up @@ -68,6 +69,8 @@ def setup(self, **kwargs: dict) -> None:
self._root = root
self._env["LTPROOT"] = self._root

self._tc_folder = os.path.join(self._root, "testcases", "bin")

runtime = kwargs.get("max_runtime", None)

if runtime:
Expand All @@ -82,11 +85,9 @@ async def _read_path(self, sut: SUT) -> dict:
"""
Read PATH and initialize it with testcases folder as well.
"""
tc_path = os.path.join(self._root, "testcases", "bin")

env = self._env.copy()
if 'PATH' in env:
env["PATH"] = env["PATH"] + f":{tc_path}"
env["PATH"] = env["PATH"] + f":{self._tc_folder}"
else:
ret = await sut.run_command("echo -n $PATH")
if ret["returncode"] != 0:
Expand Down Expand Up @@ -145,7 +146,6 @@ async def _read_runtest(

tests = []
lines = content.split('\n')
tc_path = os.path.join(self._root, "testcases", "bin")

for line in lines:
if not line.strip() or line.strip().startswith("#"):
Expand Down Expand Up @@ -199,7 +199,7 @@ async def _read_runtest(
name=test_name,
cmd=test_cmd,
args=test_args,
cwd=tc_path,
cwd=self._tc_folder,
env=env,
parallelizable=parallelizable)

Expand Down Expand Up @@ -241,6 +241,32 @@ async def get_suites(self, sut: SUT) -> list:
suites = [line for line in stdout.split('\n') if line]
return suites

async def find_command(self, sut: SUT, command: str) -> Test:
if not sut:
raise ValueError("SUT is None")

if not command:
raise ValueError("command is empty")

cmd_args = command.split(" ")
cwd = None
env = None

ret = await sut.run_command(f"test -d {self._tc_folder}")
if ret["returncode"] == 0:
cwd = self._tc_folder
env = await self._read_path(sut)

test = Test(
name=cmd_args[0],
cmd=cmd_args[0],
args=cmd_args[1:] if len(cmd_args) > 0 else None,
cwd=cwd,
env=env,
parallelizable=False)

return test

async def find_suite(self, sut: SUT, name: str) -> Suite:
if not sut:
raise ValueError("SUT is None")
Expand Down
12 changes: 12 additions & 0 deletions libkirk/tests/test_kselftests.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,18 @@ async def test_get_suites(self, framework, sut):
suites = await framework.get_suites(sut)
assert suites == self.GROUPS

async def test_find_command(self, framework, sut, tmpdir):
"""
Test find_command method.
"""
test = await framework.find_command(sut, "test_progs")
assert test.name == "test_progs"
assert test.command == "test_progs"
assert not test.arguments
assert not test.parallelizable
assert test.env == {"PATH": str(tmpdir / "bpf")}
assert test.cwd == str(tmpdir / "bpf")

async def test_find_suite(self, framework, sut, tmpdir):
"""
Test find_suite method.
Expand Down
12 changes: 12 additions & 0 deletions libkirk/tests/test_liburing.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,18 @@ async def test_get_suites(self, framework, sut):
suites = await framework.get_suites(sut)
assert suites == ["default"]

async def test_find_command(self, framework, sut, tmpdir):
"""
Test find_command method.
"""
test = await framework.find_command(sut, "test0 ciao bepi")
assert test.name == "test0"
assert test.command == "test0"
assert test.arguments == ["ciao", "bepi"]
assert not test.parallelizable
assert test.env == {"PATH": str(tmpdir)}
assert test.cwd == str(tmpdir)

async def test_find_suite(self, framework, sut, tmpdir):
"""
Test find_suite method.
Expand Down
18 changes: 17 additions & 1 deletion libkirk/tests/test_ltp.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def prepare_tmpdir(self, tmpdir):
for i in range(self.TESTS_NUM):
content += f"test0{i} echo ciao\n"

tmpdir.mkdir("testcases").mkdir("bin")
testcases = tmpdir.mkdir("testcases").mkdir("bin")
runtest = tmpdir.mkdir("runtest")

for i in range(self.SUITES_NUM):
Expand All @@ -75,6 +75,10 @@ def prepare_tmpdir(self, tmpdir):
metadata = tmpdir.mkdir("metadata") / "ltp.json"
metadata.write(json.dumps(metadata_d))

# create shell test
test_sh = testcases / "test.sh"
test_sh.write("#!/bin/bash\necho $1 $2\n")

def test_name(self, framework):
"""
Test that name property is not empty.
Expand All @@ -91,6 +95,18 @@ async def test_get_suites(self, framework, sut, tmpdir):
assert "suite2" in suites
assert "slow_suite" in suites

async def test_find_command(self, framework, sut, tmpdir):
"""
Test find_command method.
"""
test = await framework.find_command(sut, "test.sh ciao bepi")
assert test.name == "test.sh"
assert test.command == "test.sh"
assert test.arguments == ["ciao", "bepi"]
assert not test.parallelizable
assert test.cwd == tmpdir / "testcases" / "bin"
assert test.env

async def test_find_suite(self, framework, sut, tmpdir):
"""
Test find_suite method.
Expand Down

0 comments on commit d79f5a9

Please sign in to comment.