pkgname=xfstests
pkgver=git
pkgrel=1
url='https://git.kernel.org/pub/scm/fs/xfs/xfstests-dev.git'
arch=('i386' 'x86_64')
license=('GPL')
source=('https://git.kernel.org/pub/scm/linux/kernel/git/jaegeuk/f2fs-tools.git' 'https://git.kernel.org/pub/scm/fs/xfs/xfstests-dev.git' 'https://git.kernel.org/pub/scm/linux/kernel/git/djwong/e2fsprogs.git')
md5sums=('SKIP' 'SKIP' 'SKIP')

. $LKP_SRC/lib/tests/common.sh

build_install_xfstests()
{
	cd ${srcdir}/xfstests-dev || return
	## Currently, ./configure file can't work with "--prefix" option correctly,
	## so we just use the "make" command to generate the execuable file.
	make
}

build_install_f2fs_tools()
{
	cd ${srcdir}/f2fs-tools || return
	./autogen.sh || return
	./configure || return
	make || return
	make install || return
	make install DESTDIR="${pkgdir}"
}

build_install_e2fuzz()
{
	cd ${srcdir}/e2fsprogs || return
	./configure || return
	make || return
	cp misc/e2fuzz ${pkgdir}/usr/local/sbin/
}

# Automatically generate ignore file to skip test cases which can not be enabled at present.
generate_exclude_files()
{
	benchmark_path="${pkgdir}/lkp/benchmarks/${pkgname}"

	mkdir $benchmark_path/tests/exclude

	# refer to xfstests-bld/kvm-xfstests/test-appliance/files/root/fs
	# $ ls -d */exclude
	# ext2/exclude  ext3/exclude  ext4/exclude  local/exclude  nfs/exclude  reiserfs/exclude  tmpfs/exclude
	for fs in ext2 ext4
	do
		wget -O $benchmark_path/tests/exclude/$fs https://git.kernel.org/pub/scm/fs/ext2/xfstests-bld.git/plain/kvm-xfstests/test-appliance/files/root/fs/$fs/exclude || return
	done
}

hide()
{
	mv $1 .$1
}

generate_special_require_tests()
{
	benchmark_path="${pkgdir}/lkp/benchmarks/${pkgname}"
	cd ${srcdir}/xfstests-dev || return

	git grep -l "_require_realtime" | grep tests/xfs | awk -F/ '{ print $3 }' | grep -v -f <(cat $benchmark_path/tests/xfs-*) > $benchmark_path/tests/xfs-external
	git grep -L "_require_realtime" $(git grep -l "_require_scratch_reflink" $(git grep -l "_require_xfs_scratch_rmapbt" | grep tests/xfs))  | awk -F/ '{print $3}' | grep -v -f <(cat $benchmark_path/tests/xfs-*) > $benchmark_path/tests/xfs-reflink-rmapbt
	git grep -L "_require_realtime" $(git grep -L "_require_scratch_reflink" $(git grep -l "_require_xfs_scratch_rmapbt" | grep tests/xfs))  | awk -F/ '{print $3}' | grep -v -f <(cat $benchmark_path/tests/xfs-*) > $benchmark_path/tests/xfs-rmapbt
	git grep -L "_require_realtime" $(git grep -L "_require_xfs_scratch_rmapbt" $(git grep -l "_require_scratch_reflink" | grep tests/xfs))  | awk -F/ '{print $3}' | grep -v -f <(cat $benchmark_path/tests/xfs-*) > $benchmark_path/tests/xfs-reflink

	# these tests requires a valid $LOGWRITES_DEV
	# rli9 FIXME consider other fs that has this requirement
	grep -l "_require_log_writes$" -r tests/btrfs | cut -d/ -f3 | grep -v -f <(cat $benchmark_path/tests/btrfs-*) > $benchmark_path/tests/btrfs-logwrites
	grep -l "_require_log_writes$" -r tests/generic | cut -d/ -f3 | grep -v -f <(cat $benchmark_path/tests/generic-*) > $benchmark_path/tests/generic-logwrites

	# these tests requires a valid $SCRATCH_LOGDEV
	for fs in generic ext4 xfs
	do
		grep -l "_require_logdev$" -r tests/$fs | cut -d/ -f3 > $benchmark_path/tests/$fs-logdev
	done
}

build()
{
	benchmark_path="${pkgdir}/lkp/benchmarks/${pkgname}"
	mkdir -p ${benchmark_path}/tests

	cd $srcdir
	build_install_xfstests || return
	build_install_f2fs_tools || return
	build_install_e2fuzz
}

package()
{
	benchmark_path="${pkgdir}/lkp/benchmarks/${pkgname}"
	cp -af $srcdir/xfstests-dev/* $benchmark_path

	generate_exclude_files || return

	generate_tests || return

	split_tests
}

generate_tests()
{
	benchmark_path="${pkgdir}/lkp/benchmarks/${pkgname}"

	for tests in $LKP_SRC/pkg/${pkgname}/addon/tests/*
	do
		cat $tests | grep -v '^#' > $benchmark_path/tests/$(basename $tests)
	done

	generate_special_require_tests || return

	# generate the new add subcase automatically after every upgrade xfstests
	cd $benchmark_path/tests

	$LKP_SRC/tools/generate-tests ext4
	$LKP_SRC/tools/generate-tests btrfs
	$LKP_SRC/tools/generate-tests xfs
	$LKP_SRC/tools/generate-tests generic
	$LKP_SRC/tools/generate-tests udf
	$LKP_SRC/tools/generate-tests ocfs2
}

# Some files contain too many tests, which are split for easy testing
split_tests()
{
	benchmark_path="${pkgdir}/lkp/benchmarks/${pkgname}"

	cd $benchmark_path/tests
	split_standalone_tests || return

	$LKP_SRC/tools/split-tests xfs-all 10 xfs-group-
	$LKP_SRC/tools/split-tests btrfs-all 10 btrfs-group-
	$LKP_SRC/tools/split-tests generic-all 20 generic-group-
	$LKP_SRC/tools/split-tests ext4-all 20 ext4-group-
	$LKP_SRC/tools/split-tests udf-all 10 udf-group-
	$LKP_SRC/tools/split-tests ocfs2-all 10 ocfs2-group-

	$LKP_SRC/tools/split-tests xfs-reflink 20 xfs-reflink-
	hide xfs-reflink
}

# Some special tests need to be separated and tested separately
split_standalone_tests()
{
	# These special tests are written in the relevant files under LKP-SRC/pack/xfstests-addon/tests
	for file in *-standalone
	do
		fields=(`echo $file | tr '-' ' '`)
		fs=${fields[0]}

		tests=$(cat $file)
		for test in "${tests[@]}"
		do
			echo "$test" >> ./$fs-$test
		done
	done
}
