diff --git a/scripts/pytest-tests.sh b/scripts/pytest-tests.sh index d0b5164ba..efd5081f9 100755 --- a/scripts/pytest-tests.sh +++ b/scripts/pytest-tests.sh @@ -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 @@ -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 diff --git a/test/python/common/nvme.py b/test/python/common/nvme.py index d26455fa9..e0e85e752 100644 --- a/test/python/common/nvme.py +++ b/test/python/common/nvme.py @@ -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" @@ -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) @@ -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) diff --git a/test/python/tests/nexus_fault/test_nexus_fault.py b/test/python/tests/nexus_fault/test_nexus_fault.py index 078d01298..c78e6fd25 100644 --- a/test/python/tests/nexus_fault/test_nexus_fault.py +++ b/test/python/tests/nexus_fault/test_nexus_fault.py @@ -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 @@ -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")