#!/bin/bash
# SPDX-License-Identifier: GPL-3.0+
# Copyright (C) 2019 Logan Gunthorpe
# Copyright (C) 2019 Eideticom Communications Inc.

. tests/nvme/rc

DESCRIPTION="create and connect to an NVMeOF target with a passthru controller"
QUICK=1

requires() {
	_nvme_requires
	_have_kernel_option NVME_TARGET_PASSTHRU
}

nvme_info() {
	local ns=$1

	nvme id-ctrl "${ns}" | grep -E '^(vid|sn|mn|fr) '
	nvme id-ns "${ns}" | grep -E '^(nsze|ncap) '
}

compare_dev_info() {
	local passthru_dev=$1
	local testdev_info
	local passthru_info

	testdev_info=$(nvme_info "${TEST_DEV}")
	passthru_info=$(nvme_info "${passthru_dev}")

	cat >> "${FULL}" <<- EOF

	Test Device ${TEST_DEV} Info:
	$testdev_info

	Passthru Device ${passthru_dev} Info:
	$passthru_info

	EOF

	diff -u <(echo "${testdev_info}") <(echo "${passthru_info}")
	if [[ "${testdev_info}" != "${passthru_info}" ]]; then
		echo "ERROR: Device information does not match! (See ${FULL})"
	fi
}

test_device() {
	local subsys="blktests-subsystem-1"
	local nsdev
	local port

	echo "Running ${TEST_NAME}"

	_setup_nvmet
	port=$(_nvmet_passthru_target_setup "${subsys}")

	nsdev=$(_nvmet_passthru_target_connect "${nvme_trtype}" "${subsys}")

	compare_dev_info "${nsdev}"

	_nvme_disconnect_subsys "${subsys}"
	_nvmet_passthru_target_cleanup "${port}" "${subsys}"

	echo "Test complete"
}
