Skip to content

Commit 1b09bb8

Browse files
joannekoongosandov
authored andcommitted
tests: add kernfs_children() test
Add a basic test for the kernfs_children() helper. This checks: * for a kernfs_node that represents a directory, calling kernfs_children() on it successfully returns children whose names match the entries in the directory with the correct kernfs_node values * for a kernfs_node that does not represent a directory, calling kernfs_children() on it raises an exception Signed-off-by: Joanne Koong <[email protected]>
1 parent d7d75c0 commit 1b09bb8

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

tests/linux_kernel/helpers/test_kernfs.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from drgn import NULL, cast
88
from drgn.helpers.linux.fs import fget
99
from drgn.helpers.linux.kernfs import (
10+
kernfs_children,
1011
kernfs_name,
1112
kernfs_parent,
1213
kernfs_path,
@@ -77,3 +78,35 @@ def test_kernfs_walk(self):
7778
finally:
7879
for fd in fds:
7980
os.close(fd)
81+
82+
def test_kernfs_children(self):
83+
path = b"/sys/kernel"
84+
fd = os.open(path, os.O_RDONLY)
85+
try:
86+
kn = self.kernfs_node_from_fd(fd)
87+
self.assertCountEqual(
88+
[kernfs_name(child) for child in kernfs_children(kn)], os.listdir(path)
89+
)
90+
91+
for child in kernfs_children(kn):
92+
child_fd = os.open(b"/sys/" + kernfs_path(child), os.O_RDONLY)
93+
try:
94+
child_kn = self.kernfs_node_from_fd(child_fd)
95+
if child_kn:
96+
self.assertEqual(child, child_kn)
97+
finally:
98+
os.close(child_fd)
99+
finally:
100+
os.close(fd)
101+
102+
# Check that calling kernfs_children() on a non-directory raises an
103+
# exception
104+
path = b"/sys/kernel/vmcoreinfo"
105+
fd = os.open(path, os.O_RDONLY)
106+
try:
107+
kn = self.kernfs_node_from_fd(fd)
108+
with self.assertRaises(ValueError) as context:
109+
kernfs_children(kn)
110+
self.assertEqual(str(context.exception), "not a directory")
111+
finally:
112+
os.close(fd)

0 commit comments

Comments
 (0)