#!/usr/bin/env expect
############################################################################
# Purpose: Test of Slurm functionality
#          Cray burst buffer use by job arrays
#
# Output:  "TEST: #.#" followed by "SUCCESS" if test was successful, OR
#          "WARNING: ..." with an explanation of why the test can't be made, OR
#          "FAILURE: ..." otherwise with an explanation of the failure, OR
#          anything else indicates a failure mode that must be investigated.
############################################################################
# Copyright (C) 2015-2018 SchedMD LLC.
# Written by Morris Jette <jette(at)schedmd.com>
#
# This file is part of Slurm, a resource management program.
# For details, see <https://slurm.schedmd.com/>.
# Please also read the included file: DISCLAIMER.
#
# Slurm is free software; you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free
# Software Foundation; either version 2 of the License, or (at your option)
# any later version.
#
# Slurm is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along
# with Slurm; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA.
############################################################################
source ./globals

set test_id     "35.3"
set array_end   3
set data_in     "DATA_IN_0123456789"
set data_out    "DATA_OUT_9876543210"
set exit_code   0
set input_data  "test$test_id.data.in"
set output_data "test$test_id.data.out"
set output_use  "test$test_id-%A_%a.output"
set script_use  "test$test_id.use.bash"
set tmp_file    "test$test_id"

stop_root_user

proc find_bb_jobid { fname bb_jobid } {
	global bin_cat

	set found 0
	log_user 0
	spawn $bin_cat $fname
	expect {
		-re "JobID=$bb_jobid" {
			set found 1
			exp_continue
		}
		eof {
			wait
		}
	}
	log_user 1
	return $found
}

proc find_bb_data { file_name string_seek } {
	global bin_cat

	set found 0
	spawn $bin_cat $file_name
	expect {
		-re $string_seek {
			set found 1
			exp_continue
		}
		eof {
			wait
		}
	}
	return $found
}

print_header $test_id

set bb_types [get_bb_types]
if {[string first "cray" $bb_types] == -1} {
	send_user "\nWARNING: This test can only be run with the burst_buffer/cray plugin\n"
	exit 0
}

set cwd [$bin_pwd]
exec $bin_rm -f $input_data $output_data
exec $bin_echo $data_in >$cwd/$input_data

if {[get_bb_emulate] != 0} {
make_bash_script $script_use "#DW jobdw type=scratch capacity=1GiB access_mode=striped,private type=scratch
#DW stage_in type=file source=$cwd/$input_data destination=\$DW_JOB_STRIPED/$tmp_file
#DW stage_out type=file destination=$cwd/$output_data source=\$DW_JOB_STRIPED/$tmp_file
$bin_cat  $cwd/$input_data
$bin_echo $data_out >$cwd/$output_data
$scontrol show burst
exit 0"
} else {
make_bash_script $script_use "#DW jobdw type=scratch capacity=1GiB access_mode=striped,private type=scratch
#DW stage_in type=file source=$cwd/$input_data destination=\$DW_JOB_STRIPED/$tmp_file
#DW stage_out type=file destination=$cwd/$output_data source=\$DW_JOB_STRIPED/$tmp_file
$bin_cat  \$DW_JOB_STRIPED/$tmp_file
$bin_echo $data_out >\$DW_JOB_STRIPED/$tmp_file
$scontrol show burst
exit 0"
}

incr max_job_state_delay 300
set job_id 0
spawn $sbatch -t1 -N1 --output=$output_use --array=1-${array_end} $script_use
expect {
	-re "Submitted batch job ($number)" {
		set job_id $expect_out(1,string)
		exp_continue
	}
	timeout {
		send_user "\nFAILURE: sbatch not responding\n"
		set exit_code 1
	}
	eof {
		wait
	}
}

if {$job_id == 0} {
	send_user "\nFAILURE: job did not launch\n"
	set exit_code 1
} elseif {[wait_for_job $job_id "DONE"] != 0} {
	send_user "\nFAILURE: job did not complete\n"
	set exit_code 1
	cancel_job $job_id
}

for {set cnt 1} {$cnt<=$array_end} {incr cnt} {
	set job_array_id "${job_id}_${cnt}"
	set output_file "test${test_id}-${job_array_id}.output"
	if {[wait_for_file $output_file] != 0} {
		send_user "\nFAILURE: output file was not created or not the correct format. $output_file not found\n"
		set exit_code 1
	} elseif {[find_bb_jobid $output_file $job_array_id] == 0} {
		send_user "\nFAILURE: job array $job_array_id burst buffer not found\n"
		set exit_code 1
	} elseif {[find_bb_data $output_file $data_in] == 0} {
		send_user "\nFAILURE: job array $job_array_id burst buffer input data not found\n"
		set exit_code 1
	} else {
		send_user "Job array task $cnt output file is good\n\n"
		exec $bin_rm -f $output_file
	}
}

if {[find_bb_data $output_data $data_out] == 0} {
	send_user "\nFAILURE: job burst buffer output data not found\n"
	set exit_code 1
}

# Wait for stage-out completion
sleep 5
set found 0
spawn $scontrol show burst
expect {
	-re "JobID=$job_id" {
		set found 1
		exp_continue
	}
	timeout {
		send_user "\nFAILURE: scontrol not responding\n"
		set exit_code 1
	}
	eof {
		wait
	}
}
if {$found != 0} {
	send_user "\nFAILURE: job burst buffer not removed\n"
	set exit_code 1
}

if {$exit_code == 0} {
	exec $bin_rm -f $input_data $output_data $script_use
	send_user "\nSUCCESS\n"
}
exit $exit_code
