#!/usr/bin/env expect
############################################################################
# Purpose: Test of Slurm functionality
#          Test for the qsub -V and -v options
############################################################################
source ./globals

set file_err   "$test_dir/error"
set file_in    "$test_dir/input"
set file_out   "$test_dir/output"
set variable1_value "var1"
set variable2_value "var2"
set job_id     0

if {[file executable $qsub] == 0} {
	skip "qsub was not found"
}

proc cleanup {} {
	global job_id

	cancel_job $job_id
}

proc make_random_var_name {} {
	set randvar "RANDOM_VARIABLE_"
	append randvar [expr int(rand() * 1000 + 1000 )]
	return $randvar
}

proc run_qsub { variable1_name variable1_expect_val variable2_name
                variable2_expect_val qsub_extra_options extra_batch_header } {
	global file_err file_in file_out
	global job_id
	global test_var_value
	global bin_cat bin_echo bin_rm
	global qsub
	global number
	global env
	global variable1_value variable2_value
	global subtest_name

	log_info "=============================================="
	log_info "$subtest_name"
	log_info "=============================================="

	set job_id 0
	file delete $file_err $file_out
	make_bash_script $file_in "
		$extra_batch_header
		$bin_echo $variable1_name=\$$variable1_name
		$bin_echo $variable2_name=\$$variable2_name"

	# Set our 2 test variables
	set env($variable1_name) $variable1_value
	set env($variable2_name) $variable2_value

	set qsub_base_options [concat -l walltime=1:00 -r $file_err -o \
	                       $file_out $file_in]
	set qsub_options [concat $qsub_extra_options $qsub_base_options]

	eval spawn $qsub [lrange $qsub_options 0 end]

        unset env($variable1_name)
	unset env($variable2_name)

	expect {
		-re "($number)" {
			set job_id $expect_out(1,string)
			exp_continue
		}
		timeout {
			fail "qsub not responding"
		}
		eof {
			wait
		}
	}
	if {$job_id == 0} {
		fail "qsub failed to submit job"
	}

	wait_for_file -fail $file_out
	set matches 0
	spawn $bin_cat $file_out
	expect {
		"${variable1_name}=${variable1_expect_val}" {
			incr matches
			exp_continue
		}
		"${variable2_name}=${variable2_expect_val}" {
			incr matches
			exp_continue
		}
		eof {
			wait
		}
	}

	subtest {$matches == 2} "Verify environment variables match their expected values for sub test '$subtest_name'" "$matches != 2"
}

# Test -V
set subtest_name "Test -V"
set var1_name [make_random_var_name]
set var2_name "${var1_name}_2"
run_qsub $var1_name $variable1_value $var2_name $variable2_value "-V" ""
unset var1_name var2_name

# Test -V w/sbatch --export=NONE
set subtest_name "Test -V w/sbatch --export=NONE"
set var1_name [make_random_var_name]
set var2_name "${var1_name}_2"
run_qsub $var1_name $variable1_value $var2_name $variable2_value \
         "-V" "#SBATCH --export=NONE"
unset var1_name var2_name

# Test -V, -v var
set subtest_name "Test -V, -v var"
set var1_name [make_random_var_name]
set var2_name "${var1_name}_2"
run_qsub $var1_name $variable1_value $var2_name $variable2_value \
         "-V -v $var1_name" ""
unset var1_name var2_name

# Test -V, -v var w/sbatch --export=NONE
set subtest_name "Test -V, -v var w/sbatch --export=NONE"
set var1_name [make_random_var_name]
set var2_name "${var1_name}_2"
run_qsub $var1_name $variable1_value $var2_name $variable2_value \
         "-V -v $var1_name" "#SBATCH --export=NONE"
unset var1_name var2_name

# Test -V, -v var=val
set subtest_name "Test -V, -v var=val"
set var1_name [make_random_var_name]
set var2_name "${var1_name}_2"
run_qsub $var1_name "another_value" $var2_name $variable2_value \
         "-V -v $var1_name=another_value" ""
unset var1_name var2_name

# Test -V, -v var=val w/sbatch --export=NONE
set subtest_name "Test -V, -v var=val w/sbatch --export=NONE"
set var1_name [make_random_var_name]
set var2_name "${var1_name}_2"
run_qsub $var1_name "another_value" $var2_name $variable2_value \
         "-V -v $var1_name=another_value" "#SBATCH --export=NONE"
