#!/usr/bin/env expect
############################################################################
# Purpose: Test of Slurm functionality
#          Validate scontrol update of MailType and MailUser
############################################################################
# Copyright (C) SchedMD LLC.
#
# 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 job_id           0
set node_cnt         1
set user             [get_my_user_name]

if {[get_config_param "FrontendName"] ne "MISSING"} {
	skip "This test is incompatible with front-end systems"
}

set available [llength [get_nodes_by_state idle,alloc,comp]]
if {$available < $node_cnt} {
	skip "Not enough nodes currently available ($available avail, $node_cnt needed)"
}

proc cleanup { } {
	global job_id config_path bin_cp

	cancel_job $job_id

	log_info "Changing slurm.conf back"
	restore_conf $config_path/slurm.conf
	reconfigure
}

proc submit { args regex {negate_match 0} } {
	global sbatch scontrol number
	set matches 0
	set jobid 0

	spawn $sbatch --wrap=hostname {*}$args
	expect {
		-re "Submitted batch job ($number)" {
			set jobid $expect_out(1,string)
			exp_continue
		}
		timeout {
			fail "sbatch not responding"
		}
		eof {
			wait
		}
	}

	if {$jobid == 0} {
		fail "Did not get sbatch jobid"
	}

	spawn $scontrol show job $jobid
	expect {
		-re $regex {
			incr matches
			exp_continue
		}
		timeout {
			fail "sbatch not responding"
		}
		eof {
			wait
		}
	}

	if {$negate_match} {
		if {$matches} {
			fail "scontrol show job ($jobid) matched '$regex' and it shouldn't have"
		}
	} elseif {!$matches} {
		fail "scontrol show job $jobid failed to match '$regex'"
	}

	cancel_job $jobid
}

proc update { job_id args regex {negate_match 0}} {
	global scontrol
	set matches 0

	spawn $scontrol update job $job_id {*}$args
	expect {
		timeout {
			fail "scontrol not responding"
		}
		eof {
			wait
		}
	}

	spawn $scontrol show job $job_id
	expect {
		-re $regex {
			incr matches
			exp_continue
		}
		timeout {
			fail "scontrol not responding"
		}
		eof {
			wait
		}
	}

	if {$negate_match} {
		if {$matches} {
			fail "scontrol show job $jobid matched '$regex' and it shouldn't have"
		}
	} elseif {!$matches} {
		fail "scontrol show job $jobid failed to match '$regex'"
	}
}

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

proc test_it {} {
	global test_name sbatch job_id number user domain

	set domain_str ""
	if {$domain != ""} {
		set domain_str "@$domain"
	}

	submit "-H" "MailUser=$user$domain_str MailType=NONE" 1
	submit "-H --mail-type=begin,end" "MailUser=$user$domain_str MailType=BEGIN,END"
	submit "-H --mail-user=blah" "MailUser=blah MailType=NONE" 1

	spawn $sbatch -J $test_name --wrap=hostname -H
	expect {
		-re "Submitted batch job ($number)" {
			set job_id $expect_out(1,string)
			exp_continue
		}
		timeout {
			fail "sbatch not responding"
		}
		eof {
			wait
		}
	}

	update $job_id "mailuser=blah mailtype=begin" "MailUser=blah MailType=BEGIN"
	update $job_id "mailuser= mailtype=begin,end" "MailUser=$user$domain_str MailType=BEGIN,END"
	update $job_id "mailtype=begin" "MailUser=$user$domain_str MailType=BEGIN"
	update $job_id "mailuser=blah mailtype=begin" "MailUser=blah MailType=BEGIN"
	update $job_id "mailuser=blah mailtype=none" "MailUser=blah MailType=NONE" 1

	update $job_id "mailtype=begin" "MailUser=blah MailType=BEGIN"
	update $job_id "mailtype=none" "MailUser=blah MailType=NONE" 1
	update $job_id "mailtype=END" "MailUser=blah MailType=END"

	update $job_id "mailuser=" "MailUser=$user$domain_str MailType=END"
	update $job_id "mailtype=BEGIN" "MailUser=$user$domain_str MailType=BEGIN"
	update $job_id "mailuser=blah" "MailUser=blah MailType=BEGIN"

	cancel_job $job_id
}

# Test w/out MailDomain
set domain ""
exec $bin_sed -i "s/^\\(MailDomain.*\\)/#\\1/Ig" $config_path/slurm.conf
reconfigure -fail
test_it


# Test w/ MailDomain
set domain "blah.com"
set mail_conf "
MailDomain = $domain
"
exec $bin_echo $mail_conf >> $config_path/slurm.conf
reconfigure -fail
test_it
