Skip to content

Commit 5892068

Browse files
Copilotigaw
authored andcommitted
tests: modernize subprocess calls
Python recommends to use the subprocess.run interface these days. There are several different ways how calls are done which also makes the logging sprinling throught the code base. Switch over to the run API and use one central place for it. Signed-off-by: Daniel Wagner <[email protected]>
1 parent 05b3f6b commit 5892068

8 files changed

Lines changed: 83 additions & 187 deletions

tests/nvme_copy_test.py

Lines changed: 9 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,6 @@
1616
1717
"""
1818

19-
import logging
20-
import subprocess
21-
2219
from nvme_test import TestNVMe
2320

2421
logger = logging.getLogger(__name__)
@@ -44,14 +41,10 @@ def setUp(self):
4441
# get host behavior support data
4542
get_features_cmd = f"{self.nvme_bin} get-feature {self.ctrl} " + \
4643
"--feature-id=0x16 --data-len=512 --raw-binary"
47-
logger.debug(get_features_cmd)
48-
proc = subprocess.Popen(get_features_cmd,
49-
shell=True,
50-
stdout=subprocess.PIPE,
51-
encoding='utf-8')
52-
err = proc.wait()
44+
result = self.run_cmd(get_features_cmd)
45+
err = result.returncode
5346
self.assertEqual(err, 0, "ERROR : nvme get-feature failed")
54-
self.host_behavior_data = proc.stdout.read()
47+
self.host_behavior_data = result.stdout
5548
# enable cross-namespace copy formats
5649
if self.host_behavior_data[4] & cross_namespace_copy:
5750
# skip if already enabled
@@ -61,23 +54,13 @@ def setUp(self):
6154
data = self.host_behavior_data[:4] + cross_namespace_copy.to_bytes(2, 'little') + self.host_behavior_data[6:]
6255
set_features_cmd = f"{self.nvme_bin} set-feature " + \
6356
f"{self.ctrl} --feature-id=0x16 --data-len=512"
64-
proc = subprocess.Popen(set_features_cmd,
65-
shell=True,
66-
stdout=subprocess.PIPE,
67-
stdin=subprocess.PIPE,
68-
encoding='utf-8')
69-
proc.communicate(input=data)
70-
self.assertEqual(proc.returncode, 0, "Failed to enable cross-namespace copy formats")
57+
result = self.run_cmd(set_features_cmd, stdin_data=data)
58+
self.assertEqual(result.returncode, 0, "Failed to enable cross-namespace copy formats")
7159
get_ns_id_cmd = f"{self.nvme_bin} get-ns-id {self.ns1}"
72-
logger.debug(get_ns_id_cmd)
73-
proc = subprocess.Popen(get_ns_id_cmd,
74-
shell=True,
75-
stdout=subprocess.PIPE,
76-
encoding='utf-8')
77-
err = proc.wait()
60+
result = self.run_cmd(get_ns_id_cmd)
61+
err = result.returncode
7862
self.assertEqual(err, 0, "ERROR : nvme get-ns-id failed")
79-
output = proc.stdout.read()
80-
logger.debug(output)
63+
output = result.stdout
8164
self.ns1_nsid = int(output.strip().split(':')[-1])
8265
self.setup_log_dir(self.__class__.__name__)
8366

@@ -87,13 +70,7 @@ def tearDown(self):
8770
# restore saved host behavior support data
8871
set_features_cmd = f"{self.nvme_bin} set-feature {self.ctrl} " + \
8972
"--feature-id=0x16 --data-len=512"
90-
logger.debug(set_features_cmd)
91-
proc = subprocess.Popen(set_features_cmd,
92-
shell=True,
93-
stdout=subprocess.PIPE,
94-
stdin=subprocess.PIPE,
95-
encoding='utf-8')
96-
proc.communicate(input=self.host_behavior_data)
73+
self.run_cmd(set_features_cmd, stdin_data=self.host_behavior_data)
9774
super().tearDown()
9875

9976
def copy(self, sdlba, blocks, slbs, **kwargs):

tests/nvme_format_test.py

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@
4040
import json
4141
import logging
4242
import math
43-
import subprocess
4443

4544
from nvme_test import TestNVMe
4645

@@ -110,16 +109,9 @@ def attach_detach_primary_ns(self):
110109
# read lbaf information
111110
id_ns_cmd = f"{self.nvme_bin} id-ns {self.ctrl} " + \
112111
f"--namespace-id={self.default_nsid} --output-format=json"
113-
logger.debug(id_ns_cmd)
114-
proc = subprocess.Popen(id_ns_cmd,
115-
shell=True,
116-
stdout=subprocess.PIPE,
117-
encoding='utf-8')
118-
err = proc.wait()
119-
self.assertEqual(err, 0, "ERROR : nvme id-ns failed")
120-
output = proc.stdout.read()
121-
logger.debug(output)
122-
json_output = json.loads(output)
112+
result = self.run_cmd(id_ns_cmd)
113+
self.assertEqual(result.returncode, 0, "ERROR : nvme id-ns failed")
114+
json_output = json.loads(result.stdout)
123115
self.lba_format_list = json_output['lbafs']
124116
self.assertTrue(len(self.lba_format_list) > 0,
125117
"ERROR : nvme id-ns could not find any lba formats")

tests/nvme_fw_log_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ def get_fw_log(self):
5656
- 0 on success, error code on failure.
5757
"""
5858
fw_log_cmd = f"{self.nvme_bin} fw-log {self.ctrl}"
59-
return self.exec_cmd(fw_log_cmd)
59+
return self.run_cmd(fw_log_cmd).returncode
6060

6161
def test_fw_log(self):
6262
""" Testcase main """

tests/nvme_get_features_test.py

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,8 @@
3434
9. 0Bh M Asynchronous Event Configuration.
3535
"""
3636

37-
import logging
38-
import subprocess
39-
4037
from nvme_test import TestNVMe
4138

42-
logger = logging.getLogger(__name__)
43-
4439

4540
class TestNVMeGetMandatoryFeatures(TestNVMe):
4641

@@ -62,14 +57,8 @@ def setUp(self):
6257
device = self.ctrl.split('/')[-1]
6358
get_vector_list_cmd = "grep " + device + "q /proc/interrupts |" \
6459
" cut -d : -f 1 | tr -d ' ' | tr '\n' ' '"
65-
logger.debug(get_vector_list_cmd)
66-
proc = subprocess.Popen(get_vector_list_cmd,
67-
shell=True,
68-
stdout=subprocess.PIPE,
69-
encoding='utf-8')
70-
output = proc.stdout.read()
71-
logger.debug(output)
72-
self.vector_list_len = len(output.strip().split(" "))
60+
result = self.run_cmd(get_vector_list_cmd)
61+
self.vector_list_len = len(result.stdout.strip().split(" "))
7362

7463
def tearDown(self):
7564
""" Post Section for TestNVMeGetMandatoryFeatures
@@ -90,13 +79,13 @@ def get_mandatory_features(self, feature_id):
9079
get_feat_cmd = f"{self.nvme_bin} get-feature {self.ctrl} " + \
9180
f"--feature-id={str(feature_id)} " + \
9281
f"--cdw11={str(vector)} --human-readable"
93-
self.assertEqual(self.exec_cmd(get_feat_cmd), 0)
82+
self.assertEqual(self.run_cmd(get_feat_cmd).returncode, 0)
9483
else:
9584
get_feat_cmd = f"{self.nvme_bin} get-feature {self.ctrl} " + \
9685
f"--feature-id={str(feature_id)} --human-readable"
9786
if str(feature_id) == "0x05":
9887
get_feat_cmd += f" --namespace-id={self.default_nsid}"
99-
self.assertEqual(self.exec_cmd(get_feat_cmd), 0)
88+
self.assertEqual(self.run_cmd(get_feat_cmd).returncode, 0)
10089

10190
def test_get_mandatory_features(self):
10291
""" Testcase main """

tests/nvme_get_lba_status_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ def get_lba_status(self):
5454
f"--max-dw={str(self.max_dw)} " + \
5555
f"--action={str(self.action)} " + \
5656
f"--range-len={str(self.range_len)}"
57-
return self.exec_cmd(get_lba_status_cmd)
57+
return self.run_cmd(get_lba_status_cmd).returncode
5858

5959
def test_get_lba_status(self):
6060
""" Testcase main """

tests/nvme_id_ns_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ def get_id_ns(self, nsid):
6060
"""
6161
id_ns_cmd = f"{self.nvme_bin} id-ns {self.ctrl} " + \
6262
f"--namespace-id={str(nsid)}"
63-
return self.exec_cmd(id_ns_cmd)
63+
return self.run_cmd(id_ns_cmd).returncode
6464

6565
def get_id_ns_all(self):
6666
"""

tests/nvme_lba_status_log_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ def get_lba_stat_log(self):
4444
- 0 on success, error code on failure.
4545
"""
4646
lba_stat_log_cmd = f"{self.nvme_bin} lba-status-log {self.ctrl}"
47-
return self.exec_cmd(lba_stat_log_cmd)
47+
return self.run_cmd(lba_stat_log_cmd).returncode
4848

4949
def test_lba_stat_log(self):
5050
""" Testcase main """

0 commit comments

Comments
 (0)