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

set file_err   "test$test_id.error"
set file_in    "test$test_id.input"
set file_out   "test$test_id.output"
set exit_code  0
set variable1_value "var1"
set variable2_value "var2"

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

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 exit_code
	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
	exec rm -f $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"
	}

	if {[wait_for_file $file_out] != 0} {
		catch {exec $qdel $job_id}
		fail "qsub output file not found"
	}
	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
		}
	}

	if {$matches != 2} {
		log_error "One or more environment variables failed to match their expected values for sub test '$subtest_name'"
		set exit_code 1
	}

	if {$exit_code == 0} {
		exec $bin_rm -f $file_err $file_in $file_out
		set pattern "$file_in*"
		file delete {*}[glob $pattern]
	} else {
		fail "Test failed due to previous errors (\$exit_code = $exit_code)"
	}
}

# 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"
