#!/bin/bash
#
# Copyright (C) 2019 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
#
# SPDX-License-Identifier: LGPL-2.1-only

TEST_DESC="Streaming Base Path Override - User space tracing"

CURDIR=$(dirname "$0")/
TESTDIR=$CURDIR/../../..
TESTAPP_PATH="$TESTDIR/utils/testapp"
TESTAPP_NAME="gen-ust-events"
TESTAPP_BIN="$TESTAPP_PATH/$TESTAPP_NAME/$TESTAPP_NAME"
EVENT_NAME="tp:tptest"

TRACE_PATH=$(mktemp --tmpdir -d tmp.test_base_path_ust_trace_path.XXXXXX)

NUM_TESTS=42

source "$TESTDIR/utils/utils.sh"

if [ ! -x "$TESTAPP_BIN" ]; then
	BAIL_OUT "No UST events binary detected."
fi

function ust_app_stream_base_path ()
{
	local session_name="ust_app_stream_base_path"
	local base_path="my/custom/path1"

	diag "Test base path override for trace streaming"
	create_lttng_session_uri $session_name net://localhost/$base_path
	enable_ust_lttng_event_ok $session_name $EVENT_NAME

	start_lttng_tracing_ok $session_name

	$TESTAPP_BIN > /dev/null 2>&1

	stop_lttng_tracing_ok $session_name
	destroy_lttng_session_ok $session_name

	# validate test
	validate_trace_path_ust_uid_network "$TRACE_PATH" "" "$base_path"
	if validate_trace $EVENT_NAME "$TRACE_PATH/$HOSTNAME/$base_path"; then
		# only delete if successful
		rm -rf "$TRACE_PATH"
	fi
}

function ust_app_snapshot_create_base_path ()
{
	local session_name="ust_app_snapshot_create_base_path"
	local base_path="my/custom/path2"

	diag "Test base path override for remote trace snapshot (URI on create)"
	create_lttng_session_uri $session_name net://localhost/$base_path \
			--snapshot
	enable_ust_lttng_event_ok $session_name $EVENT_NAME

	start_lttng_tracing_ok $session_name

	$TESTAPP_BIN > /dev/null 2>&1

	stop_lttng_tracing_ok $session_name

	lttng_snapshot_record $session_name

	destroy_lttng_session_ok $session_name

	# validate test
	validate_trace_path_ust_uid_snapshot_network "$TRACE_PATH" "" "snapshot-1" 0 "$base_path"
	if validate_trace $EVENT_NAME "$TRACE_PATH/$HOSTNAME/$base_path"; then
		# only delete if successful
		rm -rf "$TRACE_PATH"
	fi
}

function ust_app_snapshot_base_path ()
{
	local session_name="ust_app_snapshot_base_path"
	local base_path="my/custom/path3"

	diag "Test base path override for remote trace snapshot (URI on snapshot)"
	create_lttng_session_no_output $session_name --snapshot
	enable_ust_lttng_event_ok $session_name $EVENT_NAME

	start_lttng_tracing_ok $session_name

	$TESTAPP_BIN > /dev/null 2>&1

	stop_lttng_tracing_ok $session_name

	lttng_snapshot_record $session_name net://localhost/$base_path

	destroy_lttng_session_ok $session_name

	# validate test
	validate_trace_path_ust_uid_snapshot_network "$TRACE_PATH" "" "snapshot-0" 0 "$base_path"
	if validate_trace $EVENT_NAME "$TRACE_PATH/$HOSTNAME/$base_path"; then
		# only delete if successful
		rm -rf "$TRACE_PATH"
	fi
}

function ust_app_snapshot_add_output_base_path ()
{
	local session_name="ust_app_snapshot_add_output_base_path"
	local base_path="my/custom/path4"

	diag "Test base path override for remote trace snapshot (URI on add-output)"
	create_lttng_session_no_output $session_name --snapshot
	enable_ust_lttng_event_ok $session_name $EVENT_NAME

	start_lttng_tracing_ok $session_name

	$TESTAPP_BIN > /dev/null 2>&1

	stop_lttng_tracing_ok $session_name

	lttng_snapshot_add_output_ok $session_name net://localhost/$base_path
	lttng_snapshot_record $session_name

	destroy_lttng_session_ok $session_name

	# validate test
	validate_trace_path_ust_uid_snapshot_network "$TRACE_PATH" "" "snapshot-1" 0 "$base_path"
	if validate_trace $EVENT_NAME "$TRACE_PATH/$HOSTNAME/$base_path"; then
		# only delete if successful
		rm -rf "$TRACE_PATH"
	fi
}

function ust_app_stream_base_path_via_load ()
{
	local session_name="load-stream-extra-path"
	local base_path="my/custom/path5"

	diag "Test base path override for trace streaming using lttng load"
	lttng_load_ok "-i $CURDIR/$session_name.lttng"
	start_lttng_tracing_ok $session_name

	$TESTAPP_BIN > /dev/null 2>&1

	stop_lttng_tracing_ok $session_name
	destroy_lttng_session_ok $session_name

	# validate test
	validate_trace_path_ust_uid_network "$TRACE_PATH" "" "$base_path"
	if validate_trace $EVENT_NAME "$TRACE_PATH/$HOSTNAME/$base_path"; then
		# only delete if successful
		rm -rf "$TRACE_PATH"
	fi
}

plan_tests $NUM_TESTS

print_test_banner "$TEST_DESC"

start_lttng_relayd "-o $TRACE_PATH"
start_lttng_sessiond

tests=( ust_app_stream_base_path
	ust_app_snapshot_create_base_path
	ust_app_snapshot_base_path
	ust_app_snapshot_add_output_base_path
	ust_app_stream_base_path_via_load
)
for fct_test in "${tests[@]}";
do
	${fct_test}
done

stop_lttng_sessiond
stop_lttng_relayd
