# Copyright (C) 2007-2015 Red Hat, Inc. All rights reserved.
#
# This file is part of LVM2.
#
# This copyrighted material is made available to anyone wishing to use,
# modify, copy, or redistribute it subject to the terms and conditions
# of the GNU General Public License v.2.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA

#TEST_OPTS=--verbose --debug
SHELL_PATH ?= $(SHELL)
RM ?= rm -f

subdir = $(shell pwd|sed 's,.*/,,')

srcdir = @srcdir@
top_srcdir = @top_srcdir@
top_builddir = @top_builddir@
abs_srcdir = @abs_srcdir@
abs_builddir = @abs_builddir@
abs_top_builddir = @abs_top_builddir@
abs_top_srcdir = @abs_top_srcdir@
datarootdir = @datarootdir@

LVM_TEST_RESULTS ?= results

SUBDIRS = api unit
SOURCES = lib/not.c lib/harness.c
CXXSOURCES = lib/runner.cpp
CXXFLAGS += $(EXTRA_EXEC_CFLAGS)

include $(top_builddir)/make.tmpl

T ?= .
S ?= @ # never match anything by default
VERBOSE ?= 0
ALL := $(shell find -L $(srcdir) \( -path \*/shell/\*.sh -or -path \*/api/\*.sh \) | $(SORT))
comma = ,
RUN := $(shell find -L $(srcdir) -regextype posix-egrep \( -path \*/shell/\*.sh -or -path \*/api/\*.sh \) -and -regex "$(srcdir)/.*($(subst $(comma),|,$(T))).*" -and -not -regex "$(srcdir)/.*($(subst $(comma),|,$(S))).*" | $(SORT))
RUN_BASE = $(subst $(srcdir)/,,$(RUN))

ifeq ("@BUILD_LVMETAD@", "yes")
LVMETAD_RUN_BASE = $(RUN_BASE)
LVMETAD_NDEV_FLAVOUR = ,ndev-lvmetad
LVMETAD_UDEV_FLAVOUR = ,udev-lvmetad
endif

ifeq ("@BUILD_LVMPOLLD@", "yes")
LVMPOLLD_RUN_BASE = $(RUN_BASE)
LVMPOLLD_NDEV_FLAVOUR = ,ndev-lvmpolld,ndev-cluster-lvmpolld,ndev-lvmetad-lvmpolld
LVMPOLLD_UDEV_FLAVOUR = ,udev-lvmpolld,udev-cluster-lvmpolld,udev-lvmetad-lvmpolld
endif

ifeq ("@BUILD_LVMLOCKD@", "yes")
LVMLOCKD_RUN_BASE = $(RUN_BASE)
LVMLOCKD_UDEV_FLAVOUR = ,udev-lvmlockd-test
endif

# Shell quote;
SHELL_PATH_SQ := $(subst ','\'',$(SHELL_PATH))

ifeq ("@UDEV_SYNC@", "yes")
dm_udev_synchronisation = 1
endif

all: .tests-stamp

help:
	@echo -e "\nAvailable targets:"
	@echo "  all			Default target, run check."
	@echo "  check			Run all tests."
	@echo "  check_system		Run all tests using udev."
	@echo "  check_local		Run tests without clvmd and lvmetad."
	@echo "  check_cluster		Run tests with cluster daemon."
	@echo "  check_lvmetad		Run tests with lvmetad daemon."
	@echo "  check_lvmpolld         Run tests with lvmpolld daemon."
	@echo "  check_cluster_lvmpolld Run tests with clvmd and lvmpolld daemon."
	@echo "  check_lvmetad_lvmpolld Run tests with lvmetad and lvmpolld daemon."
	@echo "  check_all_lvmpolld     Run all tests with lvmpolld daemon."
	@echo "  check_lvmlockd_sanlock Run tests with lvmlockd and sanlock."
	@echo "  check_lvmlockd_dlm     Run tests with lvmlockd and dlm."
	@echo "  check_lvmlockd_test    Run tests with lvmlockd --test."
	@echo "  clean			Clean dir."
	@echo "  help			Display callable targets."
	@echo -e "\nSupported variables:"
	@echo "  LVM_TEST_AUX_TRACE	Set for verbose messages for aux scripts []."
	@echo "  LVM_TEST_BACKING_DEVICE Set device used for testing (see also LVM_TEST_DIR)."
	@echo "  LVM_TEST_CAN_CLOBBER_DMESG Allow to clobber dmesg buffer without /dev/kmsg. (1)"
	@echo "  LVM_TEST_DEVDIR	Set to '/dev' to run on real /dev."
	@echo "  LVM_TEST_DIR		Where to create test files  [$(LVM_TEST_DIR)]."
	@echo "  LVM_TEST_LOCKING	Normal (1), Cluster (3)."
	@echo "  LVM_TEST_LVMETAD	Start lvmetad (1)."
	@echo "  LVM_TEST_LVMETAD_DEBUG_OPTS  Allows to override debug opts [-l all]."
	@echo "  LVM_TEST_LVMPOLLD	Start lvmpolld"
	@echo "  LVM_TEST_NODEBUG	Do not debug lvm commands."
	@echo "  LVM_TEST_PARALLEL	May skip agresive wipe of LVMTEST resources."
	@echo "  LVM_TEST_RESULTS	Where to create result files [results]."
	@echo "  LVM_TEST_THIN_CHECK_CMD   Command for thin_check   [$(LVM_TEST_THIN_CHECK_CMD)]."
	@echo "  LVM_TEST_THIN_DUMP_CMD    Command for thin_dump    [$(LVM_TEST_THIN_DUMP_CMD)]."
	@echo "  LVM_TEST_THIN_REPAIR_CMD  Command for thin_repair  [$(LVM_TEST_THIN_REPAIR_CMD)]."
	@echo "  LVM_TEST_THIN_RESTORE_CMD Command for thin_restore [$(LVM_TEST_THIN_RESTORE_CMD)]."
	@echo "  LVM_TEST_CACHE_CHECK_CMD   Command for cache_check   [$(LVM_TEST_CACHE_CHECK_CMD)]."
	@echo "  LVM_TEST_CACHE_DUMP_CMD    Command for cache_dump    [$(LVM_TEST_CACHE_DUMP_CMD)]."
	@echo "  LVM_TEST_CACHE_REPAIR_CMD  Command for cache_repair  [$(LVM_TEST_CACHE_REPAIR_CMD)]."
	@echo "  LVM_TEST_CACHE_RESTORE_CMD Command for cache_restore [$(LVM_TEST_CACHE_RESTORE_CMD)]."
	@echo "  LVM_TEST_UNLIMITED	Set to get unlimited test log (>32MB)"
	@echo "  LVM_VALGRIND		Enable valgrind testing, execs $$"VALGRIND.
	@echo "  LVM_VALGRIND_CLVMD	Enable valgrind testing of clvmd (1)."
	@echo "  LVM_VALGRIND_DMEVENTD	Enable valgrind testing of dmeventd (1)."
	@echo "  LVM_VALGRIND_LVMETAD	Enable valgrind testing of lvmetad (1)."
	@echo "  LVM_STRACE		Enable strace logging."
	@echo "  LVM_DEBUG_LEVEL	Sets debuging level for valgrind/strace (use > 0)."
	@echo "  LVM_VERIFY_UDEV	Default verify state for lvm.conf."
	@echo "  LVM_LOG_FILE_MAX_LINES	Maximum number of logged lines for lvm2 command [1000000]."
	@echo "  S			Skip given test(s) (regex)."
	@echo "  T			Run given test(s) (regex)."
	@echo "  VERBOSE		Verbose output (1), timing (2)."

check: .tests-stamp
	VERBOSE=$(VERBOSE) ./lib/runner \
		--testdir . --outdir $(LVM_TEST_RESULTS) \
		--flavours ndev-vanilla,ndev-cluster$(LVMETAD_NDEV_FLAVOUR)$(LVMPOLLD_NDEV_FLAVOUR) --only $(T) --skip $(S)

check_system: .tests-stamp
	VERBOSE=$(VERBOSE) ./lib/runner \
		--testdir . --outdir $(LVM_TEST_RESULTS) \
		--flavours udev-vanilla,udev-cluster$(LVMETAD_UDEV_FLAVOUR)$(LVMPOLLD_UDEV_FLAVOUR)$(LVMLOCKD_UDEV_FLAVOUR) --only $(T) --skip $(S)

check_cluster: .tests-stamp
	VERBOSE=$(VERBOSE) ./lib/runner \
		--testdir . --outdir $(LVM_TEST_RESULTS) \
		--flavours ndev-cluster --only $(T) --skip $(S)

check_local: .tests-stamp
	VERBOSE=$(VERBOSE) ./lib/runner \
		--testdir . --outdir $(LVM_TEST_RESULTS) \
		--flavours ndev-vanilla --only $(T) --skip $(S)

ifeq ("@BUILD_LVMETAD@", "yes")
check_lvmetad: .tests-stamp
	VERBOSE=$(VERBOSE) ./lib/runner \
		--testdir . --outdir $(LVM_TEST_RESULTS) \
		--flavours ndev-lvmetad --only $(T) --skip $(S)
endif

ifeq ("@BUILD_LVMPOLLD@", "yes")
check_lvmpolld: .tests-stamp
	VERBOSE=$(VERBOSE) ./lib/runner \
		--testdir . --outdir results \
		--flavours ndev-lvmpolld --only $(T) --skip $(S)

check_cluster_lvmpolld: .tests-stamp
	VERBOSE=$(VERBOSE) ./lib/runner \
		--testdir . --outdir results \
		--flavours ndev-cluster-lvmpolld --only $(T) --skip $(S)

check_lvmetad_lvmpolld: .tests-stamp
	VERBOSE=$(VERBOSE) ./lib/runner \
		--testdir . --outdir results \
		--flavours ndev-lvmetad-lvmpolld --only $(T) --skip $(S)

check_all_lvmpolld: .tests-stamp
	VERBOSE=$(VERBOSE) ./lib/runner \
		--testdir . --outdir results \
		--flavours ndev-lvmpolld,ndev-cluster-lvmpolld,ndev-lvmetad-lvmpolld --only $(T) --skip $(S)
endif

ifeq ("@BUILD_LVMLOCKD@", "yes")
check_lvmlockd_sanlock: .tests-stamp
	VERBOSE=$(VERBOSE) ./lib/runner \
		--testdir . --outdir results \
		--flavours udev-lvmlockd-sanlock --only shell/aa-lvmlockd-sanlock-prepare.sh,$(T),shell/zz-lvmlockd-sanlock-remove.sh --skip $(S)
endif

ifeq ("@BUILD_LVMLOCKD@", "yes")
check_lvmlockd_dlm: .tests-stamp
	VERBOSE=$(VERBOSE) ./lib/runner \
		--testdir . --outdir results \
		--flavours udev-lvmlockd-dlm --only shell/aa-lvmlockd-dlm-prepare.sh,$(T),shell/zz-lvmlockd-dlm-remove.sh --skip $(S)
endif

ifeq ("@BUILD_LVMLOCKD@", "yes")
check_lvmlockd_test: .tests-stamp
	VERBOSE=$(VERBOSE) ./lib/runner \
		--testdir . --outdir results \
		--flavours udev-lvmlockd-test --only $(T) --skip $(S)
endif

DATADIR = $(datadir)/lvm2-testsuite
EXECDIR = $(libexecdir)/lvm2-testsuite

LIB_FLAVOURS = \
 flavour-ndev-cluster-lvmpolld\
 flavour-ndev-cluster\
 flavour-ndev-lvmetad-lvmpolld\
 flavour-ndev-lvmetad\
 flavour-ndev-lvmpolld\
 flavour-ndev-vanilla\
 flavour-udev-cluster-lvmpolld\
 flavour-udev-cluster\
 flavour-udev-lvmetad-lvmpolld\
 flavour-udev-lvmetad\
 flavour-udev-lvmpolld\
 flavour-udev-lvmlockd-sanlock\
 flavour-udev-lvmlockd-dlm\
 flavour-udev-lvmlockd-test\
 flavour-udev-vanilla

LIB_LVMLOCKD_CONF = \
 test-corosync-conf \
 test-dlm-conf \
 test-sanlock-conf

LIB_LOCAL = paths runner
LIB_NOT = not
LIB_LINK_NOT = invalid fail should
LIB_SHARED = check aux inittest utils get lvm-wrapper

install: .tests-stamp lib/paths-installed
	@echo $(srcdir)
	$(INSTALL_DIR) $(DATADIR)/{shell,api,lib,dbus} $(EXECDIR)
	$(INSTALL_DATA) shell/*.sh $(DATADIR)/shell
	$(INSTALL_DATA) api/*.sh $(DATADIR)/api
	$(INSTALL_DATA) lib/mke2fs.conf $(DATADIR)/lib
	$(INSTALL_PROGRAM) api/*.{t,py} $(DATADIR)/api
	$(INSTALL_PROGRAM) dbus/*.py $(DATADIR)/dbus/
	$(INSTALL_DATA) lib/paths-installed $(DATADIR)/lib/paths
	cd lib && $(INSTALL_DATA) \
		$(LIB_FLAVOURS) \
		$(LIB_LVMLOCKD_CONF) \
		dm-version-expected \
		version-expected \
		$(DATADIR)/lib
	@for i in cache-mq cache-smq thin-performance lvmdbusd ; do \
		echo "$(INSTALL_DATA) $(abs_top_srcdir)/conf/$$i.profile $(DATADIR)/lib"; \
		$(INSTALL_DATA) $(abs_top_srcdir)/conf/$$i.profile $(DATADIR)/lib; done
	cd lib && $(INSTALL_SCRIPT) $(LIB_SHARED) $(DATADIR)/lib
	@cd $(DATADIR)/lib && for i in $(CMDS); do \
		echo "$(LN_S) -f lvm-wrapper $$i"; \
		$(LN_S) -f lvm-wrapper $$i; done
	$(INSTALL_PROGRAM) lib/$(LIB_NOT) $(EXECDIR)
	@cd $(EXECDIR) && for i in $(LIB_LINK_NOT); do \
		echo "$(LN_S) -f not $$i"; \
		$(LN_S) -f not $$i; done
	$(INSTALL_PROGRAM) -D lib/runner $(bindir)/lvm2-testsuite

lib/should: lib/not
	$(LN_S) -f not lib/should

lib/invalid: lib/not
	$(LN_S) -f not lib/invalid

lib/fail: lib/not
	$(LN_S) -f not lib/fail

lib/runner: lib/runner.o .lib-dir-stamp
	$(CXX) $(LDFLAGS) $(EXTRA_EXEC_LDFLAGS) $(ELDFLAGS) -o $@ $<

lib/runner.o: $(wildcard $(srcdir)/lib/*.h)

CFLAGS_runner.o += $(EXTRA_EXEC_CFLAGS)

lib/%: lib/%.o .lib-dir-stamp
	$(CC) $(CFLAGS) $(LDFLAGS) $(ELDFLAGS) -o $@ $<

lib/%: $(srcdir)/lib/%.sh .lib-dir-stamp
	cp $< $@
	$(CHMOD) +x $@

lib/flavour-%: $(srcdir)/lib/flavour-%.sh .lib-dir-stamp
	cp $< $@

lib/paths-common: $(srcdir)/Makefile.in .lib-dir-stamp Makefile
	echo 'DM_UDEV_SYNCHRONISATION=$(dm_udev_synchronisation)' >> $@-t
	echo 'THIN=@THIN@' >> $@-t
	echo 'RAID=@RAID@' >> $@-t
	echo 'CACHE=@CACHE@' >> $@-t
	echo 'LVMETAD_PIDFILE="@LVMETAD_PIDFILE@"' >> $@-t
	echo 'LVMPOLLD_PIDFILE="@LVMPOLLD_PIDFILE@"' >> $@-t
	echo 'DMEVENTD_PIDFILE="@DMEVENTD_PIDFILE@"' >> $@-t
	echo 'CLVMD_PIDFILE="@CLVMD_PIDFILE@"' >> $@-t
	echo 'LVM_TEST_THIN_CHECK_CMD=$${LVM_TEST_THIN_CHECK_CMD-@THIN_CHECK_CMD@}' >> $@-t
	echo 'LVM_TEST_THIN_DUMP_CMD=$${LVM_TEST_THIN_DUMP_CMD-@THIN_DUMP_CMD@}' >> $@-t
	echo 'LVM_TEST_THIN_REPAIR_CMD=$${LVM_TEST_THIN_REPAIR_CMD-@THIN_REPAIR_CMD@}' >> $@-t
	echo 'LVM_TEST_THIN_RESTORE_CMD=$${LVM_TEST_THIN_RESTORE_CMD-@THIN_RESTORE_CMD@}' >> $@-t
	echo 'LVM_TEST_CACHE_CHECK_CMD=$${LVM_TEST_CACHE_CHECK_CMD-@CACHE_CHECK_CMD@}' >> $@-t
	echo 'LVM_TEST_CACHE_DUMP_CMD=$${LVM_TEST_CACHE_DUMP_CMD-@CACHE_DUMP_CMD@}' >> $@-t
	echo 'LVM_TEST_CACHE_REPAIR_CMD=$${LVM_TEST_CACHE_REPAIR_CMD-@CACHE_REPAIR_CMD@}' >> $@-t
	echo 'LVM_TEST_CACHE_RESTORE_CMD=$${LVM_TEST_CACHE_RESTORE_CMD-@CACHE_RESTORE_CMD@}' >> $@-t
	echo 'export DM_UDEV_SYNCHRONISATION THIN RAID CACHE\' >> $@-t
	echo ' LVMETAD_PIDFILE LVMPOLLD_PIDFILE DMEVENTD_PIDFILE CLVMD_PIDFILE\' >> $@-t
	echo ' LVM_TEST_THIN_CHECK_CMD LVM_TEST_THIN_DUMP_CMD LVM_TEST_THIN_REPAIR_CMD LVM_TEST_THIN_RESTORE_CMD\' >> $@-t
	echo ' LVM_TEST_CACHE_CHECK_CMD LVM_TEST_CACHE_DUMP_CMD LVM_TEST_CACHE_REPAIR_CMD LVM_TEST_CACHE_RESTORE_CMD' >> $@-t
	mv $@-t $@

lib/paths-installed: lib/paths-common
	$(RM) $@-t
	cat lib/paths-common > $@-t
	echo 'installed_testsuite=1' >> $@-t
	echo 'export PATH=@libexecdir@/lvm2-testsuite:@datadir@/lvm2-testsuite/lib:@datadir@/lvm2-testsuite/api:$$PATH' >> $@-t
	mv $@-t $@

lib/paths: lib/paths-common
	$(RM) $@-t
	cat lib/paths-common > $@-t
	echo 'top_srcdir="$(top_srcdir)"' >> $@-t
	echo 'abs_top_builddir="$(abs_top_builddir)"' >> $@-t
	echo 'abs_top_srcdir="$(abs_top_srcdir)"' >> $@-t
	echo 'abs_srcdir="$(abs_srcdir)"' >> $@-t
	echo 'abs_builddir="$(abs_builddir)"' >> $@-t
	mv $@-t $@

lib/version-expected: $(top_srcdir)/VERSION .lib-dir-stamp
	cut -f 1 -d ' ' <$< >$@

lib/dm-version-expected: $(top_srcdir)/VERSION_DM .lib-dir-stamp
	cut -f 1 -d ' ' <$< >$@

CMDS = lvm $(shell cat $(top_builddir)/tools/.commands 2>/dev/null)
LIB = $(addprefix lib/, $(LIB_SHARED) $(LIB_LOCAL) $(LIB_NOT) $(LIB_LINK_NOT) $(LIB_FLAVOURS))

.tests-stamp: $(ALL) $(LIB) $(SUBDIRS) lib/version-expected lib/dm-version-expected
	@if test "$(srcdir)" != . ; then \
		echo "Linking tests to builddir."; \
		$(MKDIR_P) shell; \
		for f in $(subst $(srcdir)/,,$(ALL)); do \
			$(LN_S) -f $(abs_top_srcdir)/test/$$f $$f; \
		done; \
	fi
	@$(MKDIR_P) -m a=rwx $(LVM_TEST_RESULTS)
	touch $@

.lib-dir-stamp:
	$(MKDIR_P) lib
	for i in $(CMDS); do $(LN_S) -f lvm-wrapper lib/$$i; done
	for i in daemons/clvmd/clvmd daemons/dmeventd/dmeventd \
		tools/dmsetup daemons/lvmetad/lvmetad \
		daemons/lvmpolld/lvmpolld ; do \
		$(LN_S) -f $(abs_top_builddir)/$$i lib/; done
	$(LN_S) -f $(abs_top_builddir)/tools/dmsetup lib/dmstats
	$(LN_S) -f $(abs_top_srcdir)/conf/lvmdbusd.profile lib/
	$(LN_S) -f $(abs_top_srcdir)/conf/thin-performance.profile lib/
	$(LN_S) -f $(abs_top_srcdir)/scripts/fsadm.sh lib/fsadm
	test "$(srcdir)" = . || for i in $(LIB_LVMLOCKD_CONF); do \
		$(LN_S) -f $(abs_top_srcdir)/test/lib/$$i lib/; done
	touch $@

CLEAN_DIRS += $(LVM_TEST_RESULTS)
ifneq (.,$(firstword $(srcdir)))
CLEAN_TARGETS += $(RUN_BASE) $(addprefix lib/,$(LIB_LVMLOCKD_CONF))
endif

CLEAN_TARGETS += .lib-dir-stamp .tests-stamp $(LIB) $(addprefix lib/,\
	$(CMDS) clvmd dmeventd dmsetup dmstats lvmetad lvmpolld \
	harness lvmdbusd.profile thin-performance.profile fsadm \
	dm-version-expected version-expected \
	paths-installed paths-installed-t paths-common paths-common-t)

Makefile: $(srcdir)/Makefile.in $(top_builddir)/config.status
	cd $(top_builddir) && $(SHELL) ./config.status $(subdir)/$@

.NOTPARALLEL:
