Skip to content

Commit fd25d9c

Browse files
committed
tests: monitor /dev for new nvme block device
The attach operation takes a bit of time and is asynchronous, thus we should monitor if the block device appears for a period, before returning an error. Signed-off-by: Daniel Wagner <wagi@kernel.org>
1 parent db2c3e5 commit fd25d9c

File tree

1 file changed

+36
-6
lines changed

1 file changed

+36
-6
lines changed

tests/nvme_test.py

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,21 @@
3131
import subprocess
3232
import sys
3333
import unittest
34+
import pyinotify
3435

3536
from nvme_test_logger import TestNVMeLogger
3637

3738

39+
class EventHandler(pyinotify.ProcessEvent):
40+
def __init__(self, target_file):
41+
self.target_file = target_file
42+
self.file_created = False
43+
44+
def process_IN_CREATE(self, event):
45+
if event.pathname == self.target_file:
46+
self.file_created = True
47+
48+
3849
class TestNVMe(unittest.TestCase):
3950

4051
"""
@@ -407,12 +418,31 @@ def attach_ns(self, ctrl_id, nsid):
407418
err = subprocess.call(attach_ns_cmd,
408419
shell=True,
409420
stdout=subprocess.DEVNULL)
410-
if err == 0:
411-
# enumerate new namespace block device
412-
self.nvme_reset_ctrl()
413-
# check if new namespace block device exists
414-
err = 0 if stat.S_ISBLK(os.stat(self.ctrl + "n" + str(nsid)).st_mode) else 1
415-
return err
421+
if err != 0:
422+
return err
423+
424+
device_path = f"{self.ctrl}n{str(nsid)}"
425+
watch_dir = os.path.dirname(device_path)
426+
427+
wm = pyinotify.WatchManager()
428+
handler = EventHandler(device_path)
429+
notifier = pyinotify.Notifier(wm, handler)
430+
wm.add_watch(watch_dir, pyinotify.IN_CREATE)
431+
432+
if os.path.exists(device_path) and stat.S_ISBLK(os.stat(device_path).st_mode):
433+
return 0
434+
435+
start_time = time.time()
436+
timeout = 5 # seconds
437+
438+
while time.time() - start_time < timeout:
439+
notifier.process_events()
440+
if handler.file_created and stat.S_ISBLK(os.stat(device_path).st_mode):
441+
return 0
442+
if notifier.check_events(timeout=500): # milli seconds
443+
notifier.read_events()
444+
445+
return 1
416446

417447
def detach_ns(self, ctrl_id, nsid):
418448
""" Wrapper for detaching the namespace.

0 commit comments

Comments
 (0)