#!/usr/bin/env expect
############################################################################
# Purpose: Test of Slurm functionality
#          Test accounting for MPS resources with various allocation options
#
# Output:  "TEST: #.#" followed by "SUCCESS" if test was successful, OR
#          "FAILURE: ..." otherwise with an explanation of the failure, OR
#          anything else indicates a failure mode that must be investigated.
############################################################################
# Copyright (C) 2019 SchedMD LLC
# Written by Morris Jette
#
# 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.,
# 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.
############################################################################
source ./globals

set test_id        "40.3"
set exit_code      0
set file_in1       "test$test_id.input1"
set file_in2       "test$test_id.input2"
set file_out       "test$test_id.output"

#
# Validate the job, batch step and step 0 of a job have the proper MPS counts
# No step to test if step_mps == -1
#
# NOTE: AllocGRES and ReqGRES values for all steps (including batch step)
#	are reported based upon the job specification
#
proc test_acct { job_id job_mps step_mps have_mps_types } {
	global alpha_numeric_under number
	global bin_cat bin_rm bin_grep exit_code file_out sacct

	if {$job_id == 0} {
		return
	}

	sleep 2
	send_user "\nJob $job_id Expected job MPS:$job_mps  Step MPS:$step_mps\n"
	exec $bin_rm -f $file_out
	exec >$file_out $sacct --job=$job_id --parsable2 --start=today -o JobID,AllocGRES,ReqGRES,AllocTRES

	spawn $bin_cat $file_out
	expect {
		eof {
			wait
		}
	}

	log_user 0
	if {$step_mps != -1} {
		set match 0
		spawn $bin_grep $job_id.0 $file_out
		expect {
			-re "mps:($number)" {
				incr match
				if {$expect_out(1,string) != $job_mps} {
					send_user "\nFAILURE: bad step MPS count reported by sacct ($expect_out(1,string) != $step_mps)\n"
					set exit_code 1
				}
				exp_continue
			}
			-re "gres/mps=($number)" {
				incr match
				if {$expect_out(1,string) != $step_mps} {
					send_user "\nFAILURE: bad step MPS count reported by sacct ($expect_out(1,string) != $step_mps)\n"
					set exit_code 1
				}
				exp_continue
			}
			-re "gres/mps:($alpha_numeric_under)=($number)" {
				if {$have_mps_types != 0} {
					incr match
					if {$expect_out(2,string) != $step_mps} {
						send_user "\nFAILURE: bad step MPS count reported by sacct ($expect_out(2,string) != $step_mps)\n"
						set exit_code 1
					}
				}
				exp_continue
			}
			eof {
				wait
			}
		}
		if {$match != 3} {
			send_user "\nFAILURE: Missing step MPS count report from sacct ($match != 3)\n"
		}
	}

	set match 0
	spawn $bin_grep $job_id.batch $file_out
	expect {
		-re "mps:($number)" {
			incr match
			if {$expect_out(1,string) != $job_mps} {
				send_user "\nFAILURE: bad batch MPS count reported by sacct ($expect_out(1,string) != $job_mps)\n"
				set exit_code 1
			}
			exp_continue
		}
		eof {
			wait
		}
	}
	if {$match != 2} {
		send_user "\nFAILURE: missing batch MPS count report from sacct ($match != 2)\n"
	}

	set match 0
	spawn $bin_grep $job_id| $file_out
	expect {
		-re "mps:($number)" {
			incr match
			if {$expect_out(1,string) != $job_mps} {
				send_user "\nFAILURE: Bad job MPS count reported by sacct ($expect_out(1,string) != $job_mps)\n"
				set exit_code 1
			}
			exp_continue
		}
		-re "gres/mps=($number)" {
			incr match
			if {$expect_out(1,string) != $job_mps} {
				send_user "\nFAILURE: Bad job MPS count reported by sacct ($expect_out(1,string) != $job_mps)\n"
				set exit_code 1
			}
			exp_continue
		}
		-re "gres/mps:($alpha_numeric_under)=($number)" {
			if {$have_mps_types != 0} {
				incr match
				if {$expect_out(2,string) != $job_mps} {
					send_user "\nFAILURE: Bad job MPS count reported by sacct ($expect_out(2,string) != $job_mps)\n"
					set exit_code 1
				}
			}
			exp_continue
		}
		eof {
			wait
		}
	}
	if {$match != 3} {
		send_user "\nFAILURE: Missing job MPS count report from sacct ($match != 3)\n"
	}
	exec $bin_rm -f $file_out
	log_user 1
}

#
# Validate the job, batch step and step 0 of a job have the proper MPS counts
# No step to test if step_mps == -1
#
# NOTE: AllocGRES and ReqGRES values for all steps (including batch step)
#	are reported based upon the job specification
#
proc test_out_file { file_out target } {
	global alpha_numeric_under number bin_cat exit_code

	if {[wait_for_file $file_out] != 0} {
		send_user "\nFAILURE: no output file\n"
		exit 1
	}

	set match 0
	spawn $bin_cat $file_out
	expect {
		-re "TRES=.*,gres/mps=($number)" {
			set match $expect_out(1,string)
			exp_continue
		}
		-re "TRES=.*,gres/mps:($alpha_numeric_under)=($number)" {
			set match $expect_out(2,string)
			exp_continue
		}
		eof {
			wait
		}
	}
	if {$match != $target} {
		send_user "\nFAILURE: Failed to account for proper MPS count ($match != $target)\n"
		set exit_code 1
	}
}

print_header $test_id

set store_tres [string tolower [get_acct_store_tres]]
set store_mps [string first "gres/mps" $store_tres]
if {$store_mps == -1} {
	send_user "\nWARNING: This test requires accounting for MPS\n"
	exit $exit_code
} elseif {[test_front_end]} {
	send_user "\nWARNING: This test is incompatible with front-end systems\n"
	exit $exit_code
}

if {[test_cons_tres]} {
	send_user "\nValid configuration, using select/cons_tres\n"
} else {
	send_user "\nWARNING: This test is only compatible with select/cons_tres\n"
	exit 0
}

set def_part_name [default_partition]
set nb_nodes [get_node_cnt_in_part $def_part_name]
send_user "\nDefault partition node count is $nb_nodes\n"
if {$nb_nodes > 2} {
	set nb_nodes 2
}
set mps_cnt [get_mps_count $nb_nodes]
if {$mps_cnt < 0} {
	send_user "\nFAILURE: Error getting MPS count\n"
	exit 1
}
if {$mps_cnt < 100} {
	send_user "\nWARNING: This test requires 100 or more MPS per node in the default partition\n"
	exit 0
}
send_user "\nMPS count is $mps_cnt\n"

#
# Test --gres=mps option by job
#

send_user "\n\nTEST 1: --gres=mps option by job\n"

make_bash_script $file_in1 "
	$scontrol -dd show job \${SLURM_JOBID} | grep mps
	exit 0"

set target [expr $nb_nodes * 49]
exec $bin_rm -f $file_out
set job_id 0
set timeout $max_job_delay
spawn $sbatch --gres=craynetwork:0 --gres=mps:49 -N$nb_nodes -t1 -o $file_out -J "test$test_id" ./$file_in1
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 not submitted\n"
	exit 1
}

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

if {[wait_for_file $file_out] != 0} {
	send_user "\nFAILURE: no output file\n"
	exit 1
}
set have_mps_types 0
set match 0
spawn $bin_cat $file_out
expect {
	-re "TRES=.*,gres/mps=($number)" {
		set match $expect_out(1,string)
		exp_continue
	}
	-re "TRES=.*,gres/mps:($alpha_numeric_under)=($number)" {
		if {$match == 0} {
			set have_mps_types 1
			set match $expect_out(2,string)
		}
		exp_continue
	}
	eof {
		wait
	}
}
if {$match != $target} {
	send_user "\nFAILURE: failed to account for proper MPS count ($match != $target)\n"
	set exit_code 1
}
test_acct $job_id $target -1 $have_mps_types

#
# Test --gres=mps option by step
#
make_bash_script $file_in1 "
	$srun ./$file_in2
	exit 0"

make_bash_script $file_in2 "
	if \[ \$SLURM_PROCID -eq 0 \]; then
		$scontrol show step \${SLURM_JOBID}.\${SLURM_STEPID}
	fi
	exit 0"

send_user "\n\nTEST 2: --gres=mps option by step\n"

set target [expr $nb_nodes * 51]
exec $bin_rm -f $file_out
set job_id 0
set timeout $max_job_delay
spawn $sbatch --gres=craynetwork:0 --gres=mps:51 -N$nb_nodes -t1 -o $file_out -J "test$test_id" ./$file_in1
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 not submitted\n"
	exit 1
}

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

test_out_file $file_out $target
test_acct $job_id $target $target $have_mps_types

if {$exit_code == 0} {
	exec $bin_rm -f $file_in1 $file_in2 $file_out
	send_user "\nSUCCESS\n"
}
exit $exit_code
