#!/bin/bash
# SPDX-License-Identifier: GPL-3.0+
# Copyright (C) 2023 Ming Lei
#
# Test if gendisk is leaked, and regression in the following commit
# c43332fe028c ("blk-cgroup: delay calling blkcg_exit_disk until disk_release")
# can be covered

. tests/block/rc
. common/ublk

DESCRIPTION="add & delete ublk device and test if gendisk is leaked"
QUICK=1

requires() {
	_have_ublk
}

test() {
	local ublk_prog="src/miniublk"

	echo "Running ${TEST_NAME}"

	if ! _init_ublk; then
		return 1
	fi

	${ublk_prog} add -t null -n 0 > "$FULL"
	udevadm settle
	if ! ${ublk_prog} list -n 0 >> "$FULL"; then
		echo "fail to list dev"
	fi
	if ! dd if=/dev/ublkb0 iflag=direct of=/dev/null bs=1M count=512 >> "$FULL" 2>&1; then
		echo "fail io"
	fi
	${ublk_prog} del -n 0 >> "$FULL"

	_exit_ublk

	echo "Test complete"
}
