#!/usr/bin/env expect
############################################################################
# Purpose: Test of Slurm functionality
#          Verify that user user limits are propagated to the job
#
# 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) 2002-2006 The Regents of the University of California.
# Produced at Lawrence Livermore National Laboratory (cf, DISCLAIMER).
# Written by Morris Jette <jette1@llnl.gov>
# CODE-OCEC-09-009. All rights reserved.
#
# 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          "17.15"
set exit_code        0
set file_err         "test$test_id.error"
set file_in          "test$test_id.input"
set file_out         "test$test_id.output"
set file_prog_get    "test$test_id.prog"
set job_id           0
set limit_core       943
set limit_fsize      274515
set limit_nofile     1016
set limit_nproc      1230
set limit_stack      2021
set matches          0

print_header $test_id

#
# Delete left-over programs and rebuild them.
# We use our own program to get ulimit values since the output
# of the ulimit program is inconsistent across systems.
#
exec $bin_rm -f $file_prog_get $file_err $file_in $file_out
exec $bin_cc -O -o $file_prog_get ${file_prog_get}.c

#
# Get our current limits and adjust targets accordingly
#
set cur_core   -1
set cur_fsize  -1
set cur_nofile -1
set cur_nproc  -1
set cur_stack  -1
spawn ./$file_prog_get
expect {
	-re "USER_CORE=($number)" {
		set cur_core $expect_out(1,string)
		exp_continue
	}
	-re "USER_FSIZE=($number)" {
		set cur_fsize $expect_out(1,string)
		exp_continue
	}
	-re "USER_NOFILE=($number)" {
		set cur_nofile $expect_out(1,string)
		exp_continue
	}
	-re "USER_NPROC=($number)" {
		set cur_nproc $expect_out(1,string)
		exp_continue
	}
	-re "USER_STACK=($number)" {
		set cur_stack $expect_out(1,string)
		exp_continue
	}
}

if {$cur_core != -1} {
	set limit_core [expr ($cur_core / 1024) - 2]
	if {$limit_core < 1} {
		set limit_core [expr $cur_core / 1024]
	}
}
if {$cur_fsize != -1} {
	set limit_fsize [expr ($cur_fsize / 1024) - 2]
	if {$limit_fsize < 1} {
		set limit_fsize [expr $cur_fsize / 1024]
	}
}
if {$cur_nofile != -1} {
	set limit_nofile [expr $cur_nofile - 2]
	if {$limit_nofile < 1} {
		set limit_nofile $cur_nofile
	}
}
if {$cur_nproc != -1} {
	set limit_nproc [expr $cur_nproc - 200]
	if {$limit_nproc < 1} {
		set limit_nofile $cur_nproc
	}
}
if {$cur_stack != -1} {
	set limit_stack [expr ($cur_stack / 1024) - 2]
	if {$limit_stack < 1} {
		set limit_stack [expr $cur_stack / 1024]
	}
}

#
# Spawn a job via sbatch to print environment variables and user limits
#
make_bash_script $file_in "
  ulimit -S -c $limit_core
  ulimit -S -f $limit_fsize
  ulimit -S -n $limit_nofile
  ulimit -S -u $limit_nproc
  ulimit -S -s $limit_stack
  ./$file_prog_get
"


spawn $sbatch --output=$file_out --error=$file_err -t1 ./$file_in
expect {
	-re "Submitted batch job ($number)" {
		set job_id $expect_out(1,string)
		exp_continue
	}
	eof {
		wait
	}
}
if {$job_id == 0} {
	send_user "\nFAILURE: batch submit failure\n"
	exit 1
}

#
# Wait for job to complete
#
if {[wait_for_job $job_id "DONE"] != 0} {
	send_user "\nFAILURE: waiting for job to complete\n"
	exit 1
}

#
# Inspect the job's output file
#
if {[wait_for_file $file_out] != 0} {
	exit 1
}
spawn $bin_sort $file_out
expect {
	-re "USER_CORE=($number)" {
		set target [expr $limit_core * 1024]
		if {$expect_out(1,string) == $target} {
			incr matches
		} else {
			send_user "\nCORE value bad ($expect_out(1,string) != $target)\n"
		}
		exp_continue
	}
	-re "USER_FSIZE=($number)" {
		set target [expr $limit_fsize * 1024]
		if {$expect_out(1,string) == $target} {
			incr matches
		} else {
			send_user "\nFSIZE value bad ($expect_out(1,string) != $target)\n"
		}
		exp_continue
	}
	-re "USER_NOFILE=($number)" {
		if {$expect_out(1,string) == $limit_nofile} {
			incr matches
		} else {
			send_user "\nNOFILE value bad ($expect_out(1,string) != $limit_nofile)\n"
		}
		exp_continue
	}
	-re "USER_NPROC=($number)" {
		if {$expect_out(1,string) == $limit_nproc} {
			incr matches
		} else {
			send_user "\nNPROC value bad ($expect_out(1,string) != $limit_nproc)\n"
		}
		exp_continue
	}
	-re "USER_NPROC unsupported" {
		incr matches
		exp_continue
	}
	-re "USER_STACK=($number)" {
		set target [expr $limit_stack * 1024]
		if {$expect_out(1,string) == [expr $limit_stack * 1024]} {
			incr matches
		} else {
			send_user "\nSTACK value bad ($expect_out(1,string) != $target)\n"
		}
		exp_continue
	}
	-re "USER_STACK unsupported" {
		incr matches
		exp_continue
	}
	timeout {
		send_user "\nFAILURE: sort not responding\n"
		set exit_code 1
	}
	eof {
		wait
	}
}

set target 5
if {$matches != $target} {
	send_user "\nFAILURE: User limits not propagated got $matches of $target matches\n"
	send_user "Check PropagateResourceLimits configuration parameter\n"
	send_user "Check $file_err for errors\n"
	send_user "A long running slurmd could cause a file size limit error\n"
	send_user "slurmd could have been started with limits lower than user launching the task\n"
	set exit_code 1
}

if {$exit_code == 0} {
	send_user "\nSUCCESS\n"
        exec $bin_rm -f $file_err $file_in $file_prog_get $file_out

}
exit $exit_code
