#!/bin/bash
# this script contains scenarios called from run_tests script

# WARNING: !!! this test needs to have databaze on destination initialized
# with users from 1m.tgz and domains from domains.sql (both from common data
# directory) !!!

run_ser_script=run_pt.sh
ser_template="run_ser_template.sh"

function clear_location()
{
	if [ ${test_is_running} -eq 1 ] ; then
		if [[ "x$bdb_dir" != "x" ]] ; then
			# BDB
			echo "Removing BDB database content from ${bdb_dir}"
			ssh ${user}@${db_machine} "rm ${bdb_dir}/*"
		else
			echo "Cleaning SQL database"
			run_sql clear_location.sql
		fi
	fi
}

function prepare_ser_run()
{	
	ser_program="ser"

	echo ${desc}

	# create script
	cat ${ser_template} | \
		sed s/___SHM_SIZE___/$shm_size/ | \
		sed s@___SER_BASE_DIR___@${ser_base_dir}@ | \
		sed s/___SER_PRG___/${ser_program}/ | \
		sed s/___SER_DB_MODE___/${db_mode}/  > ${run_ser_script}

	clear_location
}

# # long run test:
# echo "Running 'long run' test"
# max_rate="1500"

testgrp_name="Registrar performance tests"
testgrp_desc="<p>Testing SER as registrar with database
and testing important parts of it like uri to uid translation, loading attributes, ...</p>
<p>SER used ${shm_size} MB of shared memory.</p>
"

db_mode="1" # always this DB mode

add_sipp_args="-inf ${datafile}"

## test 1 - only save
desc="Registrar only - there is only save called and no other functions."
prepare_ser_run
run_sipp_scenario "save_only.cfg" "register_users_one_contact.xml" "save_only"

## test 1.0 - only save using UID
desc="Registrar only - there is only only UID get from mesage and save called."
prepare_ser_run
run_sipp_scenario "save_uid.cfg" "register_users_one_contact.xml" "save_uid"

## test 1a - domain -> did, user -> uid
desc="Lookup_domain and lookup_user."
prepare_ser_run
run_sipp_scenario "uri2uid.cfg" "register_users_one_contact.xml" "uri2uid_only"

## test 1b - load_attrs
desc="Loading user attributes only."
prepare_ser_run
run_sipp_scenario "load_attrs.cfg" "register_users_one_contact.xml" "load_attrs_only"

## test 1c - domain -> did, user -> uid, load_attrs
desc="Lookup_domain and lookup_user and reading user attributes."
prepare_ser_run
run_sipp_scenario "uri2uid_with_attrs.cfg" "register_users_one_contact.xml" "uri2uid_with_attrs"

## test 2 - domain -> did, user -> uid and save
desc="Registrar with lookup_domain and lookup_user."
prepare_ser_run
run_sipp_scenario "no_attrs.cfg" "register_users_one_contact.xml" "registrar_no_attrs"

## test 3 - domain -> did, user -> uid, load_attrs and save
desc="Registrar with lookup_domain and lookup_user and loading user attrs."
prepare_ser_run
run_sipp_scenario "full.cfg" "register_users_one_contact.xml" "full"

rm -f $run_ser_script

echo "done"

