Skip to content

Commit 3d47aea

Browse files
Copilotigaw
andauthored
tests: replace open-coded Popen+wait patterns with self.exec_cmd()
Agent-Logs-Url: https://github.com/igaw/nvme-cli/sessions/c94edfc8-a4da-40d2-a47c-892dd4a6e765 Co-authored-by: igaw <1050803+igaw@users.noreply.github.com>
1 parent 4323524 commit 3d47aea

6 files changed

Lines changed: 30 additions & 55 deletions

tests/nvme_fw_log_test.py

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@
2626
1. Execute fw-log on a device.
2727
"""
2828

29-
import subprocess
30-
3129
from nvme_test import TestNVMe
3230

3331

@@ -58,11 +56,7 @@ def get_fw_log(self):
5856
- 0 on success, error code on failure.
5957
"""
6058
fw_log_cmd = f"{self.nvme_bin} fw-log {self.ctrl}"
61-
proc = subprocess.Popen(fw_log_cmd,
62-
shell=True,
63-
stdout=subprocess.PIPE,
64-
encoding='utf-8')
65-
return proc.wait()
59+
return self.exec_cmd(fw_log_cmd)
6660

6761
def test_fw_log(self):
6862
""" Testcase main """

tests/nvme_get_features_test.py

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -84,21 +84,13 @@ def get_mandatory_features(self, feature_id):
8484
get_feat_cmd = f"{self.nvme_bin} get-feature {self.ctrl} " + \
8585
f"--feature-id={str(feature_id)} " + \
8686
f"--cdw11={str(vector)} --human-readable"
87-
proc = subprocess.Popen(get_feat_cmd,
88-
shell=True,
89-
stdout=subprocess.PIPE,
90-
encoding='utf-8')
91-
self.assertEqual(proc.wait(), 0)
87+
self.assertEqual(self.exec_cmd(get_feat_cmd), 0)
9288
else:
9389
get_feat_cmd = f"{self.nvme_bin} get-feature {self.ctrl} " + \
9490
f"--feature-id={str(feature_id)} --human-readable"
9591
if str(feature_id) == "0x05":
9692
get_feat_cmd += f" --namespace-id={self.default_nsid}"
97-
proc = subprocess.Popen(get_feat_cmd,
98-
shell=True,
99-
stdout=subprocess.PIPE,
100-
encoding='utf-8')
101-
self.assertEqual(proc.wait(), 0)
93+
self.assertEqual(self.exec_cmd(get_feat_cmd), 0)
10294

10395
def test_get_mandatory_features(self):
10496
""" Testcase main """

tests/nvme_get_lba_status_test.py

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@
1212
1. Execute get-lba-status on a device.
1313
"""
1414

15-
import subprocess
16-
1715
from nvme_test import TestNVMe
1816

1917

@@ -56,11 +54,7 @@ def get_lba_status(self):
5654
f"--max-dw={str(self.max_dw)} " + \
5755
f"--action={str(self.action)} " + \
5856
f"--range-len={str(self.range_len)}"
59-
proc = subprocess.Popen(get_lba_status_cmd,
60-
shell=True,
61-
stdout=subprocess.PIPE,
62-
encoding='utf-8')
63-
return proc.wait()
57+
return self.exec_cmd(get_lba_status_cmd)
6458

6559
def test_get_lba_status(self):
6660
""" Testcase main """

tests/nvme_id_ns_test.py

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@
2727
2. Execute id-ns on all namespaces
2828
"""
2929

30-
import subprocess
31-
3230
from nvme_test import TestNVMe
3331

3432

@@ -62,11 +60,7 @@ def get_id_ns(self, nsid):
6260
"""
6361
id_ns_cmd = f"{self.nvme_bin} id-ns {self.ctrl} " + \
6462
f"--namespace-id={str(nsid)}"
65-
proc = subprocess.Popen(id_ns_cmd,
66-
shell=True,
67-
stdout=subprocess.PIPE,
68-
encoding='utf-8')
69-
return proc.wait()
63+
return self.exec_cmd(id_ns_cmd)
7064

7165
def get_id_ns_all(self):
7266
"""

tests/nvme_lba_status_log_test.py

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@
1212
1. Execute lba-status-log on a device.
1313
"""
1414

15-
import subprocess
16-
1715
from nvme_test import TestNVMe
1816

1917

@@ -46,11 +44,7 @@ def get_lba_stat_log(self):
4644
- 0 on success, error code on failure.
4745
"""
4846
lba_stat_log_cmd = f"{self.nvme_bin} lba-status-log {self.ctrl}"
49-
proc = subprocess.Popen(lba_stat_log_cmd,
50-
shell=True,
51-
stdout=subprocess.PIPE,
52-
encoding='utf-8')
53-
return proc.wait()
47+
return self.exec_cmd(lba_stat_log_cmd)
5448

5549
def test_lba_stat_log(self):
5650
""" Testcase main """

tests/nvme_test.py

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ def validate_pci_device(self):
132132
"""
133133
x1, x2, dev = self.ctrl.split('/')
134134
cmd = "find /sys/devices -name \\*" + dev + " | grep -i pci"
135-
err = self.exec_cmd(cmd)
135+
err = subprocess.call(cmd, shell=True, stdout=subprocess.DEVNULL)
136136
self.assertEqual(err, 0, "ERROR : Only NVMe PCI subsystem is supported")
137137

138138
def load_config(self):
@@ -188,10 +188,17 @@ def nvme_reset_ctrl(self):
188188
- None
189189
"""
190190
nvme_reset_cmd = f"{self.nvme_bin} reset {self.ctrl}"
191-
err = self.exec_cmd(nvme_reset_cmd)
191+
err = subprocess.call(nvme_reset_cmd,
192+
shell=True,
193+
stdout=subprocess.DEVNULL)
192194
self.assertEqual(err, 0, "ERROR : nvme reset failed")
193195
rescan_cmd = "echo 1 > /sys/bus/pci/rescan"
194-
self.assertEqual(self.exec_cmd(rescan_cmd), 0, "ERROR : pci rescan failed")
196+
proc = subprocess.Popen(rescan_cmd,
197+
shell=True,
198+
stdout=subprocess.PIPE,
199+
stderr=subprocess.PIPE,
200+
encoding='utf-8')
201+
self.assertEqual(proc.wait(), 0, "ERROR : pci rescan failed")
195202

196203
def get_ctrl_id(self):
197204
""" Wrapper for extracting the first controller id.
@@ -409,7 +416,9 @@ def create_and_validate_ns(self, nsid, nsze, ncap, flbas, dps):
409416
"ERROR : create namespace failed")
410417
id_ns_cmd = f"{self.nvme_bin} id-ns {self.ctrl} " + \
411418
f"--namespace-id={str(nsid)}"
412-
err = self.exec_cmd(id_ns_cmd)
419+
err = subprocess.call(id_ns_cmd,
420+
shell=True,
421+
stdout=subprocess.DEVNULL)
413422
return err
414423

415424
def attach_ns(self, ctrl_id, nsid):
@@ -422,7 +431,9 @@ def attach_ns(self, ctrl_id, nsid):
422431
"""
423432
attach_ns_cmd = f"{self.nvme_bin} attach-ns {self.ctrl} " + \
424433
f"--namespace-id={str(nsid)} --controllers={ctrl_id} --verbose"
425-
err = self.exec_cmd(attach_ns_cmd)
434+
err = subprocess.call(attach_ns_cmd,
435+
shell=True,
436+
stdout=subprocess.DEVNULL)
426437
if err != 0:
427438
return err
428439

@@ -446,7 +457,9 @@ def detach_ns(self, ctrl_id, nsid):
446457
"""
447458
detach_ns_cmd = f"{self.nvme_bin} detach-ns {self.ctrl} " + \
448459
f"--namespace-id={str(nsid)} --controllers={ctrl_id} --verbose"
449-
return self.exec_cmd(detach_ns_cmd)
460+
return subprocess.call(detach_ns_cmd,
461+
shell=True,
462+
stdout=subprocess.DEVNULL)
450463

451464
def delete_and_validate_ns(self, nsid):
452465
""" Wrapper for deleting and validating that namespace is deleted.
@@ -458,7 +471,9 @@ def delete_and_validate_ns(self, nsid):
458471
# delete the namespace
459472
delete_ns_cmd = f"{self.nvme_bin} delete-ns {self.ctrl} " + \
460473
f"--namespace-id={str(nsid)} --verbose"
461-
err = self.exec_cmd(delete_ns_cmd)
474+
err = subprocess.call(delete_ns_cmd,
475+
shell=True,
476+
stdout=subprocess.DEVNULL)
462477
self.assertEqual(err, 0, "ERROR : delete namespace failed")
463478
return err
464479

@@ -471,11 +486,7 @@ def get_smart_log(self, nsid):
471486
"""
472487
smart_log_cmd = f"{self.nvme_bin} smart-log {self.ctrl} " + \
473488
f"--namespace-id={str(nsid)}"
474-
proc = subprocess.Popen(smart_log_cmd,
475-
shell=True,
476-
stdout=subprocess.PIPE,
477-
encoding='utf-8')
478-
err = proc.wait()
489+
err = self.exec_cmd(smart_log_cmd)
479490
self.assertEqual(err, 0, "ERROR : nvme smart log failed")
480491
return err
481492

@@ -491,11 +502,7 @@ def get_id_ctrl(self, vendor=False):
491502
else:
492503
id_ctrl_cmd = f"{self.nvme_bin} id-ctrl " +\
493504
f"--vendor-specific {self.ctrl}"
494-
proc = subprocess.Popen(id_ctrl_cmd,
495-
shell=True,
496-
stdout=subprocess.PIPE,
497-
encoding='utf-8')
498-
err = proc.wait()
505+
err = self.exec_cmd(id_ctrl_cmd)
499506
self.assertEqual(err, 0, "ERROR : nvme id controller failed")
500507
return err
501508

0 commit comments

Comments
 (0)