#!/bin/bash
#
# Copyright (C) 2018 Anders Wallin <wallinux@gmail.com>
#
# SPDX-License-Identifier: LGPL-2.1-only

TEST_DESC="Auto load session(s)"

CURDIR=$(dirname $0)/
CONFIG_DIR="${CURDIR}/configuration"
TESTDIR=$CURDIR/../../../
export LTTNG_SESSION_CONFIG_XSD_PATH=$(readlink -m ${TESTDIR}../src/common/config/)

DIR=$(readlink -f $TESTDIR)

NUM_TESTS=9

source $TESTDIR/utils/utils.sh

# MUST set TESTDIR before calling those functions
plan_tests $NUM_TESTS

print_test_banner "$TEST_DESC"

function test_auto_load_file()
{
	diag "Test auto load file"

	start_lttng_sessiond $CURDIR/load-42.lttng
	list_lttng_ok load-42
	stop_lttng_sessiond
}

function test_auto_load_dir()
{
	diag "Test auto load directory"

	cp -f $CURDIR/load-42.lttng $LTTNG_DIR/

	start_lttng_sessiond $LTTNG_DIR
	list_lttng_ok load-42
	stop_lttng_sessiond
}

function test_auto_load_lttng_home()
{
	diag "Test auto load by setting LTTNG_HOME"

	mkdir -p $LTTNG_HOME/.lttng/sessions/auto
	cp -f $CURDIR/load-42.lttng $LTTNG_HOME/.lttng/sessions/auto

	start_lttng_sessiond
	list_lttng_ok load-42
	stop_lttng_sessiond
}

TESTS=(
	test_auto_load_file
	test_auto_load_dir
	test_auto_load_lttng_home
)

for fct_test in ${TESTS[@]};
do
	export LTTNG_HOME=$(mktemp --tmpdir -d tmp.test_lttng_home_dir.XXXXXX)
	export LTTNG_DIR=$(mktemp --tmpdir -d tmp.test_lttng_dir.XXXXXX)

	${fct_test}
	rm -rf $LTTNG_DIR
	rm -rf $LTTNG_HOME
	if [ $? -ne 0 ]; then
		break;
	fi
done
