#!/usr/bin/env expect
############################################################################
# Purpose: Test of Slurm functionality
#          Confirm that memory per cpu properly set on an srun job that
#          does not have a preexisting allocation.
############################################################################
# Copyright (C) 2019 SchedMD LLC
# Written by Chad Vizino <chad@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 timeout		$max_job_delay
set job_id		0
set mem_spec		"100M"
set job_min_memory_cpu 	0

#
# submit srun job with a specific mem-per-cpu request
#
set srun_pid [spawn $srun --mem-per-cpu=$mem_spec $bin_bash -c "echo SLURM_JOB_ID=\$SLURM_JOB_ID"]
expect {
	-re {SLURM_JOB_ID=([0-9]+)} {
		set job_id $expect_out(1,string)
	}
	timeout {
		slow_kill $srun_pid
		fail "srun not responding"
	}
	eof {
		wait
	}
}

#
# check to make sure a job id was set
#
if {$job_id == 0} {
	fail "Did not get job id"
}

#
# get job memory info
#
spawn $scontrol show job $job_id
expect {
	-re {MinMemoryCPU=([0-9]+.?)} {
		set job_min_memory_cpu $expect_out(1,string)
	}
	timeout {
		fail "Scontrol not responding"
	}
	eof {
		wait
	}
}

#
# check that the memory reported matches the requested value
#
if {[string compare $job_min_memory_cpu $mem_spec] != 0} {
	fail "Job MinMemoryCPU $job_min_memory_cpu != $mem_spec"
}
