#!/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=21

source $TESTDIR/utils/utils.sh

# MUST set TESTDIR before calling those functions
plan_tests $NUM_TESTS

print_test_banner "$TEST_DESC"

function disable_event()
{
	local sess_name="$1"
	local chan_name="$2"
	local event_name="$3"

	$TESTDIR/../src/bin/lttng/$LTTNG_BIN disable-event "$event_name" -s $sess_name -c $chan_name -u >/dev/null 2>&1
	ok $? "Disable event $event_name for session $sess_name"
}

function test_basic_load()
{
	diag "Test basic load"

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

	destroy_lttng_session $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_event $sess chan1 uevent1
	disable_event $sess chan2 uevent2
	disable_event $sess chan3 uevent3*

	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 $sess

	stop_lttng_relayd_nocheck
}

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 $SESSION_NAME
	destroy_lttng_session "$SESSION_NAME-complex"

	stop_lttng_relayd_nocheck
}

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 $SESSION_NAME
}

start_lttng_sessiond

TESTS=(
	test_basic_load
	test_complex_load
	test_all_load
	test_overwrite
)

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
