Skip to content

Commit a5dd255

Browse files
test(bdd): make nvme controller usage more robust
Caters for when the device is /dev/nvmeX but X not the same as the controller! Signed-off-by: Tiago Castro <tiagolobocastro@gmail.com>
1 parent 58b3726 commit a5dd255

File tree

3 files changed

+64
-7
lines changed

3 files changed

+64
-7
lines changed

scripts/pytest-tests.sh

+13-1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,15 @@ function clean_all()
3636
echo "Done"
3737
}
3838

39+
function is_test()
40+
{
41+
file="${1:-}"
42+
if [ -d "$file" ] || [ -f "$file" ] || [ -f "${file%::*}" ]; then
43+
return 0
44+
fi
45+
return 1
46+
}
47+
3948
function run_tests()
4049
{
4150
while read name extra
@@ -95,10 +104,13 @@ while [ "$#" -gt 0 ]; do
95104
*)
96105
set +e
97106
real_1="$(realpath $1 2>/dev/null)"
107+
real_2="$(realpath $SRCDIR/test/python/$1 2>/dev/null)"
98108
set -e
99109
param="$1"
100-
if [ -d "$real_1" ] || [ -f "$real_1" ] || [ -f "${real_1%::*}" ]; then
110+
if is_test "$real_1"; then
101111
param="$real_1"
112+
elif is_test "$real_2"; then
113+
param="$real_2"
102114
else
103115
TEST_ARGS="${TEST_ARGS:-}$1"
104116
fi

test/python/common/nvme.py

+41
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ def nvme_connect(uri, delay=10, tmo=600):
6868
command = (
6969
f"nix-sudo nvme connect -t tcp -s {port} -a {host} -n {nqn} -c {delay} -l {tmo}"
7070
)
71+
print(command)
7172
subprocess.run(command, check=True, shell=True, capture_output=False)
7273
time.sleep(1)
7374
command = "nix-sudo nvme list -v -o json"
@@ -97,6 +98,43 @@ def nvme_id_ctrl(device):
9798
return id_ctrl
9899

99100

101+
def match_host_port(addr, host, port):
102+
traddr = f"traddr={host}"
103+
trsvcid = f"trsvcid={port}"
104+
105+
return traddr in addr and trsvcid in addr
106+
107+
108+
def nvme_find_ctrl(uri):
109+
"""Find controller from the device uri."""
110+
u = urlparse(uri)
111+
port = u.port
112+
host = u.hostname
113+
nqn = u.path[1:]
114+
115+
command = "nix-sudo nvme list -v -o json"
116+
discover = json.loads(
117+
subprocess.run(
118+
command, shell=True, check=True, text=True, capture_output=True
119+
).stdout
120+
)
121+
122+
# Finds correct Device
123+
devs = list(filter(lambda d: nqn in d.get("SubsystemNQN"), discover.get("Devices")))
124+
assert len(devs) is 1, "Multiple devices with the same subnqn"
125+
126+
# Find correct Controller
127+
ctrls = list(
128+
filter(
129+
lambda d: match_host_port(d.get("Address"), host, port),
130+
devs[0].get("Controllers"),
131+
)
132+
)
133+
assert len(ctrls) is 1, "Multiple controllers with the same address"
134+
135+
return ctrls[0].get("Controller")
136+
137+
100138
def nvme_resv_report(device):
101139
"""Reservation report."""
102140
command = "nix-sudo nvme resv-report {0} -c 1 -o json".format(device)
@@ -129,18 +167,21 @@ def nvme_disconnect(uri):
129167
nqn = u.path[1:]
130168

131169
command = "nix-sudo nvme disconnect -n {0}".format(nqn)
170+
print(command)
132171
subprocess.run(command, check=True, shell=True, capture_output=True)
133172

134173

135174
def nvme_disconnect_controller(name):
136175
"""Disconnect the given NVMe controller on this host."""
137176
command = "nix-sudo nvme disconnect -d {0}".format(name)
177+
print(command)
138178
subprocess.run(command, check=True, shell=True, capture_output=True)
139179

140180

141181
def nvme_disconnect_all():
142182
"""Disconnect from all connected nvme subsystems"""
143183
command = "nix-sudo nvme disconnect-all"
184+
print(command)
144185
subprocess.run(command, check=True, shell=True, capture_output=True)
145186

146187

test/python/tests/nexus_fault/test_nexus_fault.py

+10-6
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,12 @@
1616
from common.command import run_cmd
1717
from common.fio import Fio
1818
from common.mayastor import container_mod, mayastor_mod
19-
from common.nvme import nvme_connect, nvme_disconnect, nvme_disconnect_controller
19+
from common.nvme import (
20+
nvme_connect,
21+
nvme_disconnect,
22+
nvme_disconnect_controller,
23+
nvme_find_ctrl,
24+
)
2025

2126
import nexus_pb2 as pb
2227

@@ -119,13 +124,12 @@ def _(recreate_pool, republish_nexus_ana):
119124

120125

121126
@when("the initiator swaps the nexuses")
122-
def _(recreate_pool, republish_nexus_ana):
127+
def _(publish_nexus, recreate_pool, republish_nexus_ana):
123128
"""the initiator swaps the nexuses."""
124129
print(republish_nexus_ana)
125-
device = nvme_connect(republish_nexus_ana)
126-
# disconnect previous nexus: /dev/nvme*n*
127-
split_dev = device.split("n")
128-
nvme_disconnect_controller(f"{split_dev[0]}n{split_dev[1]}")
130+
prev_ctrl = nvme_find_ctrl(publish_nexus)
131+
nvme_connect(republish_nexus_ana)
132+
nvme_disconnect_controller(f"/dev/{prev_ctrl}")
129133

130134

131135
@then("the fio workload should complete gracefully")

0 commit comments

Comments
 (0)