#!/usr/bin/env expect
############################################################################
# Purpose: Test of Slurm functionality
#		  test that when a job array is submitted to multiple
#		  partitions that the jobs run on all the assigned partitions
############################################################################
# Copyright (C) 2011-2013 SchedMD LLC
# Written by Nathan Yee <nyee32@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 job_id	 0
set def_part	 ""
set test_part	 "$test_name\_part"
set script	 "$test_name\_script"
set array_size	 2
set exit_code	 0

proc cleanup {} {
	global bin_rm script

	exec $bin_rm -f $script
}

if {[get_config_param "MaxArraySize"] < [expr $array_size + 1]} {
	skip "MaxArraySize is to small for this test"
} elseif {![is_super_user]} {
	skip "This test must be done from a super-user"
}

make_bash_script $script "
sleep 100
"

proc check_job { job_id } {
	global scontrol array_size number exit_code

	for {set index 0} {$index<$array_size} {incr index} {

		set matches 0
		spawn $scontrol show job $job_id\_$index
		expect {
			-re "JobState=RUNNING" {
				incr matches
				exp_continue
			}
			timeout {
				fail "scontrol is not responding"
			}
			eof {
				wait
			}
		}
		if {$matches != 1} {
			log_error "Job $job_id\_$index was not found"
			set exit_code 1
		}
	}
}

#
# Get the default partition 
#
set def_part [default_partition]
if {[string compare $def_part ""] == 0} {
	log_error "Default partition was not found"
	set exit_code 1
}

#
# Get the available nodes from the partition
#
set node_list ""
spawn $sinfo -h -o "%N" -p $def_part
expect {
	-re "($re_word_str)" {
		set node_list $expect_out(1,string)
		exp_continue
	}
	timeout {
		fail "sinfo is not responding"
	}
	eof {
		wait
	}
}
if {[string compare $node_list ""] == 0} {
	log_error "Node list was not found"
	set exit_code 1
}

#
# Create partition
#
spawn $scontrol create PartitionName=$test_part Nodes=$node_list
expect {
	-re "error" {
		log_error "scontrol did not create partition"
		set exit_code 1
		exp_continue
	}
	timeout {
		fail "scontrol is not responding"
	}
	eof {
		wait
	}
}

#
# Submit array job on default partition
#
spawn $sbatch -N1 -t1 -o /dev/null -e /dev/null --array=0-[expr $array_size -1] --partition=$def_part,$test_part $script
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 "sbatch did not submit job"
	set exit_code 1
}

if {[wait_for_job $job_id "RUNNING"] != 0} {
	log_error "Job never started"
	set exit_code 1
}
cancel_job $job_id

#
# Delete the new partition
#
spawn $scontrol delete partition=$test_part
expect {
	-re "error" {
		log_error "Partition was not deleted"
		set exit_code 1
		exp_continue
	}
	timeout {
		fail "scontrol is not responding"
	}
	eof {
		wait
	}
}

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