Skip to content

Commit 78caadd

Browse files
committed
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 sprinkling through 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 78caadd

4 files changed

Lines changed: 77 additions & 183 deletions

File tree

tests/nvme_copy_test.py

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

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

24-
logger = logging.getLogger(__name__)
25-
2621

2722
class TestNVMeCopy(TestNVMe):
2823

@@ -44,14 +39,10 @@ def setUp(self):
4439
# get host behavior support data
4540
get_features_cmd = f"{self.nvme_bin} get-feature {self.ctrl} " + \
4641
"--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()
42+
result = self.run_cmd(get_features_cmd)
43+
err = result.returncode
5344
self.assertEqual(err, 0, "ERROR : nvme get-feature failed")
54-
self.host_behavior_data = proc.stdout.read()
45+
self.host_behavior_data = result.stdout
5546
# enable cross-namespace copy formats
5647
if self.host_behavior_data[4] & cross_namespace_copy:
5748
# skip if already enabled
@@ -61,23 +52,13 @@ def setUp(self):
6152
data = self.host_behavior_data[:4] + cross_namespace_copy.to_bytes(2, 'little') + self.host_behavior_data[6:]
6253
set_features_cmd = f"{self.nvme_bin} set-feature " + \
6354
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")
55+
result = self.run_cmd(set_features_cmd, stdin_data=data)
56+
self.assertEqual(result.returncode, 0, "Failed to enable cross-namespace copy formats")
7157
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()
58+
result = self.run_cmd(get_ns_id_cmd)
59+
err = result.returncode
7860
self.assertEqual(err, 0, "ERROR : nvme get-ns-id failed")
79-
output = proc.stdout.read()
80-
logger.debug(output)
61+
output = result.stdout
8162
self.ns1_nsid = int(output.strip().split(':')[-1])
8263
self.setup_log_dir(self.__class__.__name__)
8364

@@ -87,13 +68,7 @@ def tearDown(self):
8768
# restore saved host behavior support data
8869
set_features_cmd = f"{self.nvme_bin} set-feature {self.ctrl} " + \
8970
"--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)
71+
self.run_cmd(set_features_cmd, stdin_data=self.host_behavior_data)
9772
super().tearDown()
9873

9974
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_get_features_test.py

Lines changed: 2 additions & 13 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

0 commit comments

Comments
 (0)