#!/bin/bash
# SPDX-License-Identifier: GPL-3.0+
# Copyright (C) 2018 Josef Bacik
#
# Test nbd device resizing. Regression test for the following commits:
#
# 8364da4751cf ("nbd: fix nbd device deletion")
# c3f7c9397609 ("nbd: update size when connected")
# 9e2b19675d13 ("nbd: use bd_set_size when updating disk size")
# 96d97e17828f ("nbd: clear_sock on netlink disconnect")
# fe1f9e6659ca ("nbd: fix how we set bd_invalidated")
# 76aa1d341257 ("nbd: call nbd_bdev_reset instead of bd_set_size on disconnect")

. tests/nbd/rc

DESCRIPTION="tests on partition handling for an nbd device"
QUICK=1

requires() {
	_have_nbd_netlink && _have_program parted
}

test() {
	echo "Running ${TEST_NAME}"
	_start_nbd_server
	{
	nbd-client -N export localhost /dev/nbd0
	parted -s /dev/nbd0 mklabel msdos
	parted -s /dev/nbd0 mkpart primary 0 100
	} >> "$FULL" 2>&1

	# We need to wait for udev to do its thing before we disconnect or else
	# we'll get timed out requests.
	udevadm settle

	nbd-client -d /dev/nbd0 >> "$FULL" 2>&1

	if ! _wait_for_nbd_disconnect; then
		echo "Disconnect didn't happen?"
		_stop_nbd_server
		return 1
	fi

	udevadm settle

	if stat /dev/nbd0p1 >> "$FULL" 2>&1; then
		echo "Had partition after disconnect?"
		_stop_nbd_server
		return 1
	fi

	# Do it with ioctls

	echo "Testing IOCTL path"

	nbd-client -N export localhost /dev/nbd0 >> "$FULL" 2>&1

	if ! _wait_for_nbd_connect; then
		echo "Connect didn't happen?"
		nbd-client -d /dev/nbd0 >> "$FULL" 2>&1
		_stop_nbd_server
		return 1
	fi

	udevadm settle

	if ! stat /dev/nbd0p1 >> "$FULL" 2>&1; then
		echo "Didn't have partition on ioctl path"
		nbd-client -d /dev/nbd0 >> "$FULL" 2>&1
		_stop_nbd_server
		return 1
	fi

	nbd-client -d /dev/nbd0 >> "$FULL" 2>&1

	udevadm settle

	if stat /dev/nbd0p1 >> "$FULL" 2>&1; then
		echo "Partition still exists after disconnect"
		_stop_nbd_server
		return 1
	fi

	# Do it with netlink
	echo "Testing the netlink path"
	nbd-client -L -N export localhost /dev/nbd0 >> "$FULL" 2>&1

	if ! _wait_for_nbd_connect; then
		echo "Connect didn't happen?"
		nbd-client -d /dev/nbd0 >> "$FULL" 2>&1
		_stop_nbd_server
		return 1
	fi

	udevadm settle

	if  ! stat /dev/nbd0p1 >/dev/null 2>&1; then
		echo "Didn't have parition on the netlink path"
		nbd-client -L -d /dev/nbd0 >> "$FULL" 2>&1
		_stop_nbd_server
		return 1
	fi

	nbd-client -L -d /dev/nbd0 >> "$FULL" 2>&1

	if ! _wait_for_nbd_disconnect; then
		echo "Disconnect didn't happen?"
		_stop_nbd_server
		return 1
	fi

	udevadm settle

	if stat /dev/nbd0p1 >> "$FULL" 2>&1; then
		echo "Partition still exists after netlink disconnect"
		_stop_nbd_server
		return 1
	fi

	echo "Test complete"
	_stop_nbd_server
	return 0
}
