#!/bin/bash
#
# Copyright (C) 2012 Christian Babeux <christian.babeux@efficios.com>
# Copyright (C) 2014 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
#
# SPDX-License-Identifier: GPL-2.0-only

TEST_DESC="Health check - Thread OK"

CURDIR=$(dirname $0)/
TESTDIR=$CURDIR/../../..
SESSION_NAME="health_thread_ok"
UST_EVENT_NAME="tp:tptest"
KERNEL_EVENT_NAME="sched_switch"
CHANNEL_NAME="testchan"
HEALTH_CHECK_BIN="health_check"
NUM_TESTS=17
SLEEP_TIME=30

source $TESTDIR/utils/utils.sh

function report_errors
{
	# Report health errors
	out=$(cat ${STDOUT_PATH} | wc -l)
	if [ $out -ne 0 ]; then
		fail "Validation failure"
		diag "Health returned:"
		diag "stdout:"
		file=${STDOUT_PATH}
		while read line ; do
		    diag "$line"
		done < ${file}

		diag "stderr:"
		file=${STDERR_PATH}
		while read line ; do
		    diag "$line"
		done < ${file}
	else
		pass "Validation OK"
	fi
}

function test_thread_ok
{
	diag "Test health OK"

	# Set the socket timeout to 5 so the health check delta is set to 25.
	export LTTNG_NETWORK_SOCKET_TIMEOUT=5
	export LTTNG_RELAYD_HEALTH="${HEALTH_PATH}/test-health"

	diag "Only session daemon"
	start_lttng_sessiond

	# Check health status
	$CURDIR/$HEALTH_CHECK_BIN > ${STDOUT_PATH} 2> ${STDERR_PATH}
	report_errors

	diag "With UST consumer daemons"
	create_lttng_session_no_output $SESSION_NAME
	enable_ust_lttng_event_ok $SESSION_NAME $UST_EVENT_NAME $CHANNEL_NAME
	start_lttng_tracing_ok $SESSION_NAME
	destroy_lttng_session_ok $SESSION_NAME

	# Check health status
	$CURDIR/$HEALTH_CHECK_BIN > ${STDOUT_PATH} 2> ${STDERR_PATH}
	report_errors

	check_skip_kernel_test "5" "Skipping kernel consumer health check test." ||
	{
		diag "With kernel consumer daemon"
		create_lttng_session_no_output $SESSION_NAME
		lttng_enable_kernel_event $SESSION_NAME $KERNEL_EVENT_NAME $CHANNEL_NAME
		start_lttng_tracing_ok $SESSION_NAME
		destroy_lttng_session_ok $SESSION_NAME

		# Check health status
		$CURDIR/$HEALTH_CHECK_BIN > ${STDOUT_PATH} 2> ${STDERR_PATH}
		report_errors
	}

	diag "With relay daemon"

	start_lttng_relayd "-o $TRACE_PATH"
	# Check health status
	$CURDIR/$HEALTH_CHECK_BIN \
		--relayd-path="${LTTNG_RELAYD_HEALTH}" \
		> ${STDOUT_PATH} 2> ${STDERR_PATH}
	report_errors

	# Wait
	diag "Check after running for ${SLEEP_TIME} seconds"
	sleep ${SLEEP_TIME}

	# Check health status
	$CURDIR/$HEALTH_CHECK_BIN \
		--relayd-path="${LTTNG_RELAYD_HEALTH}" \
		> ${STDOUT_PATH} 2> ${STDERR_PATH}
	report_errors

	stop_lttng_relayd
	stop_lttng_sessiond

	unset LTTNG_NETWORK_SOCKET_TIMEOUT
	unset LTTNG_RELAYD_HEALTH
}

plan_tests $NUM_TESTS

print_test_banner "$TEST_DESC"

STDOUT_PATH=$(mktemp --tmpdir tmp.test_thread_ok_stdout_path.XXXXXX)
STDERR_PATH=$(mktemp --tmpdir tmp.test_thread_ok_stderr_path.XXXXXX)
TRACE_PATH=$(mktemp --tmpdir -d tmp.test_thread_ok_trace_path.XXXXXX)
HEALTH_PATH=$(mktemp --tmpdir -d tmp.test_thread_ok_trace_path.XXXXXX)

test_thread_ok

rm -rf ${HEALTH_PATH}
rm -rf ${TRACE_PATH}
rm -f ${STDOUT_PATH}
rm -f ${STDERR_PATH}
