Skip to content

Commit

Permalink
test(bdd): make nvme controller usage more robust
Browse files Browse the repository at this point in the history
Caters for when the device is /dev/nvmeX but X not the same as the controller!

Signed-off-by: Tiago Castro <[email protected]>
  • Loading branch information
tiagolobocastro committed Nov 4, 2024
1 parent 98cc83e commit f74a0cd
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 7 deletions.
14 changes: 13 additions & 1 deletion scripts/pytest-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,15 @@ function clean_all()
echo "Done"
}

function is_test()
{
file="${1:-}"
if [ -d "$file" ] || [ -f "$file" ] || [ -f "${file%::*}" ]; then
return 0
fi
return 1
}

function run_tests()
{
while read name extra
Expand Down Expand Up @@ -95,10 +104,13 @@ while [ "$#" -gt 0 ]; do
*)
set +e
real_1="$(realpath $1 2>/dev/null)"
real_2="$(realpath $SRCDIR/test/python/$1 2>/dev/null)"
set -e
param="$1"
if [ -d "$real_1" ] || [ -f "$real_1" ] || [ -f "${real_1%::*}" ]; then
if is_test "$real_1"; then
param="$real_1"
elif is_test "$real_2"; then
param="$real_2"
else
TEST_ARGS="${TEST_ARGS:-}$1"
fi
Expand Down
41 changes: 41 additions & 0 deletions test/python/common/nvme.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ def nvme_connect(uri, delay=10, tmo=600):
command = (
f"nix-sudo nvme connect -t tcp -s {port} -a {host} -n {nqn} -c {delay} -l {tmo}"
)
print(command)
subprocess.run(command, check=True, shell=True, capture_output=False)
time.sleep(1)
command = "nix-sudo nvme list -v -o json"
Expand Down Expand Up @@ -97,6 +98,43 @@ def nvme_id_ctrl(device):
return id_ctrl


def match_host_port(addr, host, port):
traddr = f"traddr={host}"
trsvcid = f"trsvcid={port}"

return traddr in addr and trsvcid in addr


def nvme_find_ctrl(uri):
"""Find controller from the device uri."""
u = urlparse(uri)
port = u.port
host = u.hostname
nqn = u.path[1:]

command = "nix-sudo nvme list -v -o json"
discover = json.loads(
subprocess.run(
command, shell=True, check=True, text=True, capture_output=True
).stdout
)

# Finds correct Device
devs = list(filter(lambda d: nqn in d.get("SubsystemNQN"), discover.get("Devices")))
assert len(devs) is 1, "Multiple devices with the same subnqn"

# Find correct Controller
ctrls = list(
filter(
lambda d: match_host_port(d.get("Address"), host, port),
devs[0].get("Controllers"),
)
)
assert len(ctrls) is 1, "Multiple controllers with the same address"

return ctrls[0].get("Controller")


def nvme_resv_report(device):
"""Reservation report."""
command = "nix-sudo nvme resv-report {0} -c 1 -o json".format(device)
Expand Down Expand Up @@ -129,18 +167,21 @@ def nvme_disconnect(uri):
nqn = u.path[1:]

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


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


def nvme_disconnect_all():
"""Disconnect from all connected nvme subsystems"""
command = "nix-sudo nvme disconnect-all"
print(command)
subprocess.run(command, check=True, shell=True, capture_output=True)


Expand Down
16 changes: 10 additions & 6 deletions test/python/tests/nexus_fault/test_nexus_fault.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,12 @@
from common.command import run_cmd
from common.fio import Fio
from common.mayastor import container_mod, mayastor_mod
from common.nvme import nvme_connect, nvme_disconnect, nvme_disconnect_controller
from common.nvme import (
nvme_connect,
nvme_disconnect,
nvme_disconnect_controller,
nvme_find_ctrl,
)

import nexus_pb2 as pb

Expand Down Expand Up @@ -119,13 +124,12 @@ def _(recreate_pool, republish_nexus_ana):


@when("the initiator swaps the nexuses")
def _(recreate_pool, republish_nexus_ana):
def _(publish_nexus, recreate_pool, republish_nexus_ana):
"""the initiator swaps the nexuses."""
print(republish_nexus_ana)
device = nvme_connect(republish_nexus_ana)
# disconnect previous nexus: /dev/nvme*n*
split_dev = device.split("n")
nvme_disconnect_controller(f"{split_dev[0]}n{split_dev[1]}")
prev_ctrl = nvme_find_ctrl(publish_nexus)
nvme_connect(republish_nexus_ana)
nvme_disconnect_controller(f"/dev/{prev_ctrl}")


@then("the fio workload should complete gracefully")
Expand Down

0 comments on commit f74a0cd

Please sign in to comment.