#!/bin/bash
#
# Copyright (C) - 2014 David Goulet <dgoulet@efficios.com>
#
# This library is free software; you can redistribute it and/or modify it under
# the terms of the GNU Lesser General Public License as published by the Free
# Software Foundation; version 2.1 of the License.
#
# This library is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
# details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this library; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA

TEST_DESC="Load session(s)"

CURDIR=$(dirname $0)/
TESTDIR=$CURDIR/../../../
SESSIOND_BIN="lttng-sessiond"
RELAYD_BIN="lttng-relayd"
LTTNG_BIN="lttng"
export LTTNG_SESSION_CONFIG_XSD_PATH=$(readlink -m ${TESTDIR}../src/common/config/)

SESSION_NAME="load-42"
EVENT_NAME="tp:tptest"

DIR=$(readlink -f $TESTDIR)

NUM_TESTS=28

source $TESTDIR/utils/utils.sh

# MUST set TESTDIR before calling those functions
plan_tests $NUM_TESTS

print_test_banner "$TEST_DESC"

function test_basic_load()
{
	diag "Test basic load"

	lttng_load "-i $CURDIR/$SESSION_NAME.lttng"

	destroy_lttng_session_ok $SESSION_NAME
}

function test_complex_load()
{
	local sess="$SESSION_NAME-complex"
	diag "Test complex load"

	# Start relayd with localhost binding. The complex session uses those
	# custom values.
	start_lttng_relayd "-C tcp://localhost:8172 -D tcp://localhost:9817"

	lttng_load "-i $CURDIR/$sess.lttng"

	# Once loaded, we are suppose to be able to disable certain events/channels
	# thus having a confirmation that it's valid
	disable_ust_lttng_event $sess uevent1 chan1
	disable_ust_lttng_event $sess uevent2 chan2
	disable_ust_lttng_event $sess uevent3* chan3

	disable_ust_lttng_channel $sess chan1
	disable_ust_lttng_channel $sess chan2
	disable_ust_lttng_channel $sess chan3

	# Confirm that an event stored as disabled is restored in its disabled state
	local mi_output_file=$(mktemp)
	if [ $? -ne 0 ]; then
		break;
	fi
	$TESTDIR/../src/bin/lttng/$LTTNG_BIN --mi XML list $sess -c chan2 > $mi_output_file
	mi_result=$($CURDIR/../mi/extract_xml $mi_output_file "//command/output/sessions/session/domains/domain/channels/channel[name='chan2']/events/event[name='uevent_disabled']/enabled/text()")
	if [[ $mi_result = "false" ]]; then
	    ok 0 "Disabled event is loaded in disabled state"
	else
	    fail "Disabled event is loaded in disabled state"
	fi
	destroy_lttng_session_ok $sess

	stop_lttng_relayd
}

function test_all_load()
{
	diag "Test load all sessions"

	# Start relayd with localhost binding. The complex session uses those
	# custom values.
	start_lttng_relayd "-C tcp://localhost:8172 -D tcp://localhost:9817"

	lttng_load "-a -i $CURDIR"

	destroy_lttng_session_ok $SESSION_NAME
	destroy_lttng_session_ok "$SESSION_NAME-complex"
	destroy_lttng_session_ok "$SESSION_NAME-trackers"

	stop_lttng_relayd
}

function test_overwrite()
{
	diag "Test load overwrite"

	lttng_load "-i $CURDIR/$SESSION_NAME.lttng"

	# This one should succeed
	lttng_load "-f -i $CURDIR $SESSION_NAME"

	destroy_lttng_session_ok $SESSION_NAME
}

function test_trackers()
{
	diag "Test trackers loading"

	lttng_load "-i $CURDIR/$SESSION_NAME-trackers.lttng"

	diag "Test pid tracker"
	local mi_output_file=$(mktemp)
	if [ $? -ne 0 ]; then
		break;
	fi
	$TESTDIR/../src/bin/lttng/$LTTNG_BIN --mi XML list "$SESSION_NAME-trackers" > $mi_output_file
	mi_result=$($CURDIR/../mi/extract_xml -e $mi_output_file "//command/output/sessions/session/domains/domain/trackers/pid_tracker/targets/pid_target")
	if [[ $mi_result = "true" ]]; then
	    ok 0 "Pid target is present"
	else
	    fail "Pid target missing"
	fi

	# Test to remove the target just to make sure
	lttng_untrack_ok "-p 666 -u -s $SESSION_NAME-trackers"

	destroy_lttng_session_ok "$SESSION_NAME-trackers"
}

start_lttng_sessiond

TESTS=(
	test_basic_load
	test_complex_load
	test_all_load
	test_overwrite
	test_trackers
)

for fct_test in ${TESTS[@]};
do
	TRACE_PATH=$(mktemp -d)

	${fct_test}
	if [ $? -ne 0 ]; then
		break;
	fi
	# Only delete if successful
	rm -rf $TRACE_PATH
done

stop_lttng_sessiond
