#!/bin/bash
# SPDX-License-Identifier: GPL-2.0+
# Copyright (c) 2017-2018 Western Digital Corporation or its affiliates.
#
# Test NVMeOF target creation with a file backed ns.

. tests/nvme/rc

DESCRIPTION="create an NVMeOF target with a file-backed ns"
QUICK=1

requires() {
	_have_program nvme && _have_modules nvme-loop nvmet && _have_configfs
}

test() {
	echo "Running ${TEST_NAME}"

	local port
	local file_path
	local subsys_name="blktests-subsystem-1"

	modprobe nvmet
	modprobe nvme-loop

	file_path="${TMPDIR}/img"

	truncate -s 1G "${file_path}"

	_create_nvmet_subsystem "${subsys_name}" "${file_path}" \
		"91fdba0d-f87b-4c25-b80f-db7be1418b9e"
	port="$(_create_nvmet_port "loop")"
	_add_nvmet_subsys_to_port "${port}" "${subsys_name}"

	_remove_nvmet_subsystem_from_port "${port}" "${subsys_name}"
	_remove_nvmet_subsystem "${subsys_name}"
	_remove_nvmet_port "${port}"

	rm "${file_path}"

	modprobe -r nvme-loop
	modprobe -r nvmet

	echo "Test complete"
}
