#!/usr/bin/env expect
############################################################################
# Purpose: Test of Slurm functionality
#          Validate that the mcs plugin (mcs/group) is OK with sbatch
############################################################################
# Copyright (C) 2015 CEA/DAM/DIF
# Written by Aline Roy <aline.roy@cea.fr>
#
# 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 exit_code    0
set cwd          "[$bin_pwd]"
set config_path  ""
set tmp_job      "test$test_id\_tmp_job"
set file_in	 "test${test_id}.in"
set file_out     "test${test_id}.out"
set job_id       0

if {![is_super_user]} {
	skip "This test can't be run except as SlurmUser"
}

set config_path [get_conf_path]
set config_file $config_path/slurm.conf
save_conf $config_file

proc cleanup {} {
	global job_id config_file bin_rm tmp_job

	# Clean up vestigial files, jobs and restore original slurm.conf file
	cancel_job $job_id
	restore_conf $config_file
	reconfigure
	exec $bin_rm $tmp_job
}

log_info "---Checking sbatch uses mcs-label only for some jobs (ondemand mode)---"

#
# Change the slurm.conf MCSparameters and MCSPlugin
#
set groups_name 0
log_debug "groups=$groups_name"
spawn $bin_bash -c "exec groups \| sed \"s/ /\|/g\""
expect {
	-re "($re_word_str)" {
		set groups_name $expect_out(1,string)
	}
	eof {
		wait
	}
}

exec $bin_sed -i /^\[\t\s\]*MCSPlugin\[\t\s\]*=/Id $config_file
exec $bin_sed -i /^\[\t\s\]*MCSParameters\[\t\s\]*=/Id $config_file
exec $bin_sed -i /^\[\t\s\]*PrivateData\[\t\s\]*=/Id $config_file
exec $bin_echo -e "\nMCSPlugin=mcs/group" >> $config_file
exec $bin_echo MCSParameters=ondemand,select,privatedata:$groups_name >> $config_file
exec $bin_echo PrivateData=jobs,nodes >> $config_file
reconfigure -fail


#
# verify slurm conf parameters MCS
#
set found 0
spawn -noecho $bin_bash -c "exec $scontrol show config | $bin_grep MCS"
expect {
	-re "MCSPlugin               = mcs/group" {
		log_debug "MCSPlugin=mcs/group OK"
		set found 1
		exp_continue
	}
	timeout {
		fail "scontrol is not responding"
	}
	eof {
		wait
	}
}

if {$found == 0} {
	log_error "MCSPlugin parameter in scontrol show config is not mcs/group"
	set exit_code 1
}

###### Check that sbatch fails with a bad mcs-label ######
log_info "---Checking sbatch fails with a bad mcs-label ---"

set timeout $max_job_delay
make_bash_script $tmp_job "sleep 10"

spawn $sbatch -N1 --mcs-label=foo -t1 $tmp_job
expect {
	-re "Batch job submission failed: Invalid mcs_label specified" {
		log_debug "This error is expected, no worries"
		exp_continue
	}
	timeout {
		fail "sbatch not responding"
	}
	eof {
		wait
	}
}


###### Check that sbatch uses mcs-label=group ######
log_info "---Checking sbatch uses mcs-label=group---"

make_bash_script $tmp_job "sleep 10"
spawn $sbatch -N1 -o/dev/null --exclusive=mcs -t1 $tmp_job
expect {
	-re "Submitted batch job ($number)" {
		set job_id $expect_out(1,string)
		exp_continue
	}
	timeout {
		fail "sbatch is not responding"
	}
	eof {
		wait
	}
}

if {$job_id == 0} {
	log_error "Job was not submitted"
	set exit_code 1
}

 spawn $bin_bash -c "exec groups \| sed \"s/ /\|/g\""
 expect {
	 -re "($re_word_str)" {
		set pipe_idx [expr [string first "|" $expect_out(1,string)] -1]
		if {$pipe_idx < 0} {
			set group1 $expect_out(1,string)
		} else {
			set group1 [string range $expect_out(1,string) 0 $pipe_idx]
		}
	}
	eof {
		wait
	}
}


set found 0
spawn $squeue --jobs=$job_id --noheader -O "mcslabel"
expect {
	-re "(null)" {
		log_debug "NO MCS-label for this job : this is not expected"
		exp_continue
	}
	-re "$group1" {
		log_debug "MCS-label OK for this job"
		set found 1
		exp_continue
	}
	timeout {
		fail "squeue is not responding"
	}
	eof {
		wait
	}
}

if {$found == 0} {
	log_error "Job was submitted with a bad mcs-label"
	set exit_code 1
}

cancel_job $job_id

make_bash_script $tmp_job "sleep 30"
set job_id 0
spawn $sbatch -N1 --mcs-label=$group1 --exclusive=mcs -o/dev/null -t10 $tmp_job
expect {
	-re "Submitted batch job ($number)" {
		set job_id $expect_out(1,string)
		exp_continue
	}
	timeout {
		fail "sbatch is not responding"
	}
	eof {
		wait
	}
}

if {$job_id == 0} {
	log_error "Job was not submitted"
	set exit_code 1
}
set found 0
sleep 5
spawn $squeue --jobs=$job_id --noheader -O "mcslabel"
expect {
	-re "$group1" {
		log_debug "MCS-label OK for this job"
		set found 1
		exp_continue
	}
	-re "Invalid job format specification" {
		log_error "Invalid job format specification mcslabel"
		set exit_code 1
		set found 1
	}
	timeout {
		fail "squeue is not responding"
	}
	eof {
		wait
	}
}
if {$found == 0} {
	 log_error "Job was submitted with a bad mcs-label"
	 set exit_code 1
}

set found 0
set node 0
spawn $squeue --jobs=$job_id --noheader -O "nodelist"
expect {
	-re "($re_word_str)" {
		set node $expect_out(1,string)
		log_debug "Node for this job : $node"
		set found 1
	}
	timeout {
		fail "squeue is not responding"
	}
	eof {
		wait
	}
}

if {$found == 0} {
	log_error "No node found in squeue command"
	set exit_code 1
}

#
# verify MCS of nodes
#
set found 0
spawn -noecho $bin_bash -c "exec $scontrol show node=$node | $bin_grep MCS"
expect {
	-re "MCS_label=$group1" {
		log_debug "mcs_label OK for node $node"
		set found 1
		exp_continue
	}
	timeout {
		fail "scontrol is not responding"
	}
	eof {
		wait
	}
}

if {$found == 0} {
	log_error "Job was submitted with node with bad mcs-label"
	set exit_code 1
}
cancel_job $job_id

#
# Change the slurm.conf MCSparameters and MCSPlugin
# test with enforced
#
log_info "---Checking sbatch uses mcs-label with all jobs (enforced mode)---"
exec $bin_sed -i /^\[\t\s\]*MCSPlugin\[\t\s\]*=/Id $config_file
exec $bin_sed -i /^\[\t\s\]*MCSParameters\[\t\s\]*=/Id $config_file
exec $bin_sed -i /^\[\t\s\]*PrivateData\[\t\s\]*=/Id $config_file
exec $bin_echo -e "\nMCSPlugin=mcs/group" >> $config_file
exec $bin_echo MCSParameters=enforced,noselect,privatedata:$groups_name >> $config_file
exec $bin_echo PrivateData=jobs,nodes >> $config_file
reconfigure -fail


###### Check that sbatch uses mcs-label=group ######
log_info "---Checking sbatch uses mcs-label=group---"

make_bash_script $tmp_job "sleep 10"

spawn $sbatch -N1 -o/dev/null -t1 $tmp_job
expect {
	-re "Submitted batch job ($number)" {
		set job_id $expect_out(1,string)
		exp_continue
	}
	timeout {
		fail "sbatch is not responding"
	}
	eof {
		wait
	}
}

if {$job_id == 0} {
	log_error "Job was not submitted"
	set exit_code 1
}

set found 0
sleep 3
spawn $squeue --jobs=$job_id --noheader -O "mcslabel"
expect {
	-re "$group1" {
		log_debug "MCS-label OK for this job"
		set found 1
		exp_continue
	}
	-re "(null)" {
		log_error "NO MCS-label for this job"
		set exit_code 1
		exp_continue
	}
	timeout {
		fail "squeue is not responding"
	}
	eof {
		wait
	}
}

if {$found == 0} {
	log_error "Job was submitted with a bad mcs-label"
	set exit_code 1
}

set found 0
set node 0
spawn $squeue --jobs=$job_id --noheader -O "nodelist"
expect {
	-re "($re_word_str)" {
		set node $expect_out(1,string)
		log_debug "Node for this job : $node"
		set found 1
	}
	timeout {
		fail "squeue is not responding"
	}
	eof {
		wait
	}
}

if {$found == 0} {
	log_error "No node found in squeue command"
	set exit_code 1
}

#
# verify MCS of nodes
#
spawn -noecho $bin_bash -c "exec $scontrol show node=$node | $bin_grep MCS"
expect {
	-re "MCS_label=$group1" {
		log_error "A mcs_label is found for this job. It was not expected"
		set exit_code 1
	}
	-re "MCS_label=N/A" {
		log_debug "No mcs_label for this node. It was expected"
	}
	timeout {
		fail "scontrol is not responding"
	}
	eof {
		wait
	}
}

if {$exit_code != 0} {
	fail "Test failed due to previous errors (\$exit_code = $exit_code)"
}
