# Testing targets.
#
# Oracle Linux DTrace.
# Copyright (c) 2011, 2019, Oracle and/or its affiliates. All rights reserved.
# Licensed under the Universal Permissive License v 1.0 as shown at
# http://oss.oracle.com/licenses/upl.

# Core testing targets, running the DTrace testsuite runner runtest.sh.
# -verbose in a target name means to report even tests that pass and
#           those that are expected to fail;
# -installed in a target name means to test an installed /usr/sbin/dtrace
#            rather than a copy in the build tree;
# -stress in a target name means to skip tests that may fail inappropriately
#         when the system is under load.
# -internal in a target name means that this is an internal implementation
#           detail and should not be run directly.

# Specialist targets for QA, check-qa-*, all of which run against the installed
# DTrace:
# check-qa-smoke: Runs the smoke testsuite.

ifndef always-installed
git_version=.git-version
RUNTEST=./runtest.sh
RUNTESTFLAGS=
installed_internals=
else
git_version=
RUNTEST=$(TESTDIR)/runtest.sh
RUNTESTFLAGS=--use-installed
DTRACE=/usr/sbin/dtrace
installed_internals=internals,
endif

help::
	@printf "Checking targets (as root):\n\n" >&2
	@printf "check                          Test the built DTrace.\n" >&2
	@printf "check-verbose                  Report successes too.\n" >&2
	@printf "check-installed                Check the installed DTrace.\n" >&2
	@printf "check-installed-verbose        Report successes too.\n" >&2
	@printf "check-stress                   Run testsuites while the system is under stress.\n" >&2
	@printf "check-verbose-stress           Report successes too.\n" >&2
	@printf "check-installed-stress         Check the installed DTrace.\n" >&2
	@printf "check-installed-verbose-stress Report successes too.\n" >&2
	@printf "check-quick                    Run tests that take only a short time.\n" >&2
	@printf "check-verbose-quick            Report successes too.\n" >&2
	@printf "check-installed-quick          Quickly check the installed DTrace.\n" >&2
	@printf "check-installed-verbose-quick  Report successes too.\n" >&2
	@printf "check-module-loading           Check that all modules load.\n" >&2
	@printf "check-installed-module-loading Check that all modules load, using\n">&2
	@printf "                               the installed DTrace for autoinstallation.\n" >&2
	@printf "check-probe-all-syscalls       Probe all syscalls, forever.\n" >&2
	@printf "check-qa-smoke                 Smoke-test the installed DTrace.\n" >&2
	@printf "\n" >&2

check: check-verbose
check: RUNTESTFLAGS+=--quiet

check-verbose: all $(git_version)
	$(RUNTEST) $(RUNTESTFLAGS)

check-installed check-installed-stress check-installed-quick check-installed-verbose check-installed-verbose-stress check-installed-verbose-quick:
	$(MAKE) _DTRACE_CHECK_INSTALLED=t DTRACE=/usr/sbin/dtrace $@-internal

check-installed-internal: check-installed-verbose-internal
check-installed-internal: RUNTESTFLAGS+=--quiet

check-installed-verbose-internal: external-triggers $(git_version)
	$(RUNTEST) --use-installed --testsuites=unittest,$(installed_internals)stress,demo,smoke $(RUNTESTFLAGS)

check-stress: check
check-stress: RUNTESTFLAGS+=--testsuites=unittest,internals,demo,smoke --no-comparison

check-verbose-stress: check-verbose
check-verbose-stress: RUNTESTFLAGS+=--testsuites=unittest,internals,demo,smoke --no-comparison

check-quick: check
check-quick: RUNTESTFLAGS+=--skip-longer

check-verbose-quick: check-verbose
check-verbose-quick: RUNTESTFLAGS+=--skip-longer

check-installed-stress-internal: check-installed-internal
check-installed-stress-internal: RUNTESTFLAGS+=--testsuites=unittest,$(installed_internals)demo,smoke --no-comparison

check-installed-verbose-stress-internal: check-installed-verbose-internal
check-installed-verbose-stress-internal: RUNTESTFLAGS+=--testsuites=unittest,$(installed_internals)demo,smoke --no-comparison

check-installed-quick-internal: check-installed-internal
check-installed-quick-internal: RUNTESTFLAGS+=--skip-longer

check-installed-verbose-quick-internal: check-installed-verbose-internal
check-installed-verbose-quick-internal: RUNTESTFLAGS+=--skip-longer

# QA targets.

check-qa-smoke:
	$(MAKE) _DTRACE_CHECK_INSTALLED=t DTRACE=/usr/sbin/dtrace $@-internal

check-qa-smoke-internal: $(git_version)
	$(RUNTEST) $(RUNTESTFLAGS) --use-installed --testsuites=smoke --quiet

# Load the DTrace modules (installing them if necessary), and do nothing else.
# Useful for testing the impact of inactive DTrace.

check-module-loading:
	@if [[ "x$$(id -u)" = "x0" ]]; then \
		$(RUNTEST) $(RUNTESTFLAGS) --load-modules-only; \
	else \
	        echo "Error: cannot load modules as non-root." >&2; \
		false; \
	fi

check-installed-module-loading:
	@if [[ "x$$(id -u)" = "x0" ]]; then \
		$(RUNTEST) $(RUNTESTFLAGS) --use-installed --load-modules-only; \
	else \
	        echo "Error: cannot load modules as non-root." >&2; \
		false; \
	fi

# Trace all syscalls, forever, and throw the results away.
# The modules are loaded during this process but are not unloaded afterwards.

check-probe-all-syscalls: all check-module-loading check-probe-all-syscalls-internal
ifndef always-installed
check-probe-all-syscalls: DTRACE=$(objdir)/dtrace
check-probe-all-syscalls: export DTRACE_OPT_SYSLIBDIR=$(objdir)/dtrace/dlibs
check-probe-all-syscalls: export LD_LIBRARY_PATH=$(objdir)
endif

# Likewise, using /usr/sbin/dtrace rather than a copy in the build tree.

check-installed-probe-all-syscalls: check-installed-module-loading check-probe-all-syscalls-internal
check-installed-probe-all-syscalls: export DTRACE=/usr/sbin/dtrace

check-probe-all-syscalls-internal:
	$(DTRACE) -qm 'syscall: {}'
	@true

PHONIES += check check-verbose check-installed check-installed-verbose
PHONIES += check-stress check-verbose-stress check-installed-stress
PHONIES += check-installed-verbose-stress check-quick check-verbose-quick
PHONIES += check-verbose-quick check-installed-quick check-installed-verbose-quick
PHONIES += check-module-loading check-installed-module-loading
PHONIES += check-probe-all-syscalls check-installed-probe-all-syscalls
PHONIES += check-probe-all-syscalls-internal
PHONIES += check-installed-internal check-installed-stress-internal
PHONIES += check-installed-verbose-internal
PHONIES += check-installed-verbose-stress-internal
