#!/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
}

device_requires() {
	_require_test_dev_is_not_nvme_multipath
}

set_conditions() {
	_set_nvme_trtype "$@"
}

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() {
	echo "Running ${TEST_NAME}"

	_setup_nvmet

	local nsdev

	_nvmet_passthru_target_setup

	nsdev=$(_nvmet_passthru_target_connect)
	if [[ -z "$nsdev" ]]; then
		echo "FAIL: Failed to find passthru target namespace"
	else
		compare_dev_info "${nsdev}"
	fi

	_nvme_disconnect_subsys
	_nvmet_passthru_target_cleanup

	echo "Test complete"
}
