TESTS="rte0_change_memory rte0_nodeproperty rte0_env rte0_runtime rte rte_with_options"

# NOTEL: RTEs are not able to change the joboption_arg_<n> variables (i.e.
# change executable and arguments) - they are processed before RTE stage 0.
# See bug 3625 - test disabled.
#TESTS="${TESTS} rte0_arg"


# NOTE: Backslashes in environment variables is not handled consistent in bash and dash (RedHat vs Debian).
# TEST DISABLED!
#TESTS="${TESTS} rte0_env_backslashes" # Test passes with bash but not in dash.

simulate_cmds="sbatch rm" # Simulate rm in order not to get job script deleted
read -r -d '' simulator_output <<'EOF'
rargs="/sbatch .*/"
output="Submitted batch job 1"

rargs="/rm .*/"
output=""
EOF

read -r -d '' general_arc_test_configuration <<EOF
[lrms]
slurm_bin_path=@PWD@/bin
EOF

function test_rte0_change_memory() {
rtes="INCREASEMEMORY"

read -r -d '' INCREASEMEMORY <<'EOF'
joboption_memory=2000
EOF

read -r -d '' job_description_input <<'EOF'
&(executable = "/bin/true")
 (memory=500)
 (runtimeenvironment = "INCREASEMEMORY")
EOF

cat "${SRCDIR}/rte-test-rte0_change_memory.patch" | patch -sf -p1 -d ${SRCDIR} -o ${testdir}/expected_lrms_job_script.tmpl
}

function test_rte0_nodeproperty() {
rtes="NODEPROPERTY"

read -r -d '' NODEPROPERTY <<'EOF'
export joboption_nodeproperty_0="TEST"
export joboption_nodeproperty_1="TESTING"
EOF

read -r -d '' job_description_input <<'EOF'
&(executable = "/bin/true")
 (runtimeenvironment = "NODEPROPERTY")
EOF

cat "${SRCDIR}/rte-test-rte0_nodeproperty.patch" | patch -sf -p1 -d ${SRCDIR} -o ${testdir}/expected_lrms_job_script.tmpl
}

function test_rte0_env() {
rtes="ENV"

read -r -d '' ENV <<'EOF'
export joboption_env_1="test=\"TEST\""
export joboption_env_2="rte_foo=\"foobar\""
EOF

read -r -d '' job_description_input <<'EOF'
&(executable = "/bin/true")
 (environment = ("job_foo" "foobar"))
 (runtimeenvironment = "ENV")
EOF

cat "${SRCDIR}/rte-test-rte0_env.patch" | patch -sf -p1 -d ${SRCDIR} -o ${testdir}/expected_lrms_job_script.tmpl
}

# Test passes with bash but not in dash.
function test_rte0_env_backslashes() {
rtes="ENV"

read -r -d '' ENV <<'EOF'
export joboption_env_1="test=\"TEST\""
export joboption_env_2="rte_foo=\"foo\\\\bar\""
EOF

read -r -d '' job_description_input <<'EOF'
&(executable = "/bin/true")
 (environment = ("job_foo" "foo\\bar"))
 (runtimeenvironment = "ENV")
EOF

cat "${SRCDIR}/rte-test-rte0_env_backslashes.patch" | patch -sf -p1 -d ${SRCDIR} -o ${testdir}/expected_lrms_job_script.tmpl
}

function test_rte0_runtime() {
rtes="RTE EXTRA_RTE"

read -r -d '' RTE <<'EOF'
export joboption_runtime_1='EXTRA_RTE'
# TODO: Maybe set options?
EOF

read -r -d '' EXTRA_RTE <<'EOF'
export joboption_count=6
EOF

read -r -d '' job_description_input <<'EOF'
&(executable = "/bin/true")
 (runtimeenvironment = "RTE")
EOF

cat "${SRCDIR}/rte-test-rte0_runtime.patch" | patch -sf -p1 -d ${SRCDIR} -o ${testdir}/expected_lrms_job_script.tmpl
}

# Test case below not supported - see bug 3625.
function test_rte0_arg() {
rtes="ARG"

read -r -d '' ARG <<'EOF'
joboption_arg_0="${joboption_arg_0}/echo"
export joboption_arg_1="Hello"
export joboption_arg_2="World"
EOF

read -r -d '' job_description_input <<'EOF'
&(executable = "/bin")
 (runtimeenvironment = "ARG")
EOF

cat "${SRCDIR}/rte-test-rte0_arg.patch" | patch -sf -p1 -d ${SRCDIR} -o ${testdir}/expected_lrms_job_script.tmpl
}

function test_rte() {
read -r -d '' job_description_input <<'EOF'
&(executable = "/bin/true")
 (runtimeenvironment = "DUMMY")
EOF

rtes="DUMMY"

read -r -d '' DUMMY <<'EOF'
# This is a dummy RTE
EOF

cat "${SRCDIR}/rte-test-rte.patch" | patch -sf -p1 -d ${SRCDIR} -o ${testdir}/expected_lrms_job_script.tmpl
}

function test_rte_with_options() {
read -r -d '' job_description_input <<'EOF'
<?xml version="1.0"?>
<ActivityDescription xmlns="http://www.eu-emi.eu/es/2010/12/adl">
<Application>
<Executable><Path>/bin/true</Path></Executable>
</Application>
<Resources>
<IndividualPhysicalMemory>1572864000</IndividualPhysicalMemory>
<RuntimeEnvironment>
<Name>INCREASEMEMORY_1</Name>
<Option>2000</Option>
<Option>--test "TESTING\\\\ TEST"</Option>
</RuntimeEnvironment>
<RuntimeEnvironment>
<Name>INCREASEMEMORY_2</Name>
<Option>5000</Option>
</RuntimeEnvironment>
</Resources>
</ActivityDescription>
EOF

rtes="INCREASEMEMORY_1 INCREASEMEMORY_2"

read -r -d '' INCREASEMEMORY_1 <<'EOF'
joboption_memory=$(( $(echo ${joboption_memory} + ${2} | tr -d \") ))
EOF

read -r -d '' INCREASEMEMORY_2 <<'EOF'
joboption_memory=$(( $(echo ${joboption_memory} + ${2} + ${2} | tr -d \") ))
EOF

cat "${SRCDIR}/rte-test-rte_with_options.patch" | patch -sf -p1 -d ${SRCDIR} -o ${testdir}/expected_lrms_job_script.tmpl
}
