|
31 | 31 | import subprocess |
32 | 32 | import sys |
33 | 33 | import unittest |
| 34 | +import pyinotify |
34 | 35 |
|
35 | 36 | from nvme_test_logger import TestNVMeLogger |
36 | 37 |
|
37 | 38 |
|
| 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 | + |
38 | 49 | class TestNVMe(unittest.TestCase): |
39 | 50 |
|
40 | 51 | """ |
@@ -407,12 +418,31 @@ def attach_ns(self, ctrl_id, nsid): |
407 | 418 | err = subprocess.call(attach_ns_cmd, |
408 | 419 | shell=True, |
409 | 420 | 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 |
416 | 446 |
|
417 | 447 | def detach_ns(self, ctrl_id, nsid): |
418 | 448 | """ Wrapper for detaching the namespace. |
|
0 commit comments