#!/bin/bash
#
# Copyright (C) 2018 Francis Deslauriers <francis.deslauriers@efficios.com>
#
# SPDX-License-Identifier: GPL-2.0-only
#

TEST_DESC="Kernel tracer - Channel configuration"

CURDIR=$(dirname $0)/
TESTDIR=$CURDIR/../..
NUM_TESTS=8

source $TESTDIR/utils/utils.sh

function test_channel_buffer()
{
	TRACE_PATH=$(mktemp --tmpdir -d "tmp.${FUNCNAME[0]}_trace_path.XXXXXX")
	SESSION_NAME="test_session_name"
	CHANNEL_NAME="test_channel_name"
	create_lttng_session_ok "$SESSION_NAME" "$TRACE_PATH"

	# Try to create a tiny buffer.
	lttng_enable_kernel_channel_ok "$SESSION_NAME" "$CHANNEL_NAME" --subbuf-size=4k --num-subbuf=1

	destroy_lttng_session_ok "$SESSION_NAME"

	rm -rf "$TRACE_PATH"
}

function test_channel_buffer_too_large()
{
	TRACE_PATH=$(mktemp --tmpdir -d "tmp.${FUNCNAME[0]}_trace_path.XXXXXX")
	SESSION_NAME="test_session_name"
	CHANNEL_NAME="test_channel_name"
	create_lttng_session_ok "$SESSION_NAME" "$TRACE_PATH"

	# Try to create a buffer larger than memory. This testcase will need to
	# be adjusted if someone has a computer with 1024*1000 GB of ram.
	lttng_enable_kernel_channel_fail "$SESSION_NAME" "$CHANNEL_NAME" --subbuf-size=1000G --num-subbuf=1024

	destroy_lttng_session_ok "$SESSION_NAME"

	rm -rf "$TRACE_PATH"
}

plan_tests $NUM_TESTS
print_test_banner "$TEST_DESC"

check_skip_kernel_test "$NUM_TESTS" "Skipping all tests." ||
{
	start_lttng_sessiond

	test_channel_buffer
	test_channel_buffer_too_large

	stop_lttng_sessiond
}
