#!/usr/bin/env expect
############################################################################
# Purpose: Test of Slurm functionality
#          Test of job submit to multiple partitions (--partition).
############################################################################
# Copyright (C) 2002-2007 The Regents of the University of California.
# Copyright (C) 2008-2010 Lawrence Livermore National Security.
# 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 file_in     "$test_dir/input"
set job_id      0
set test_node   [lindex [get_nodes_by_state] 0]
set part1       [default_partition]
set part2       "${test_name}_part"

proc cleanup {} {
	global job_id scontrol part2

	cancel_job $job_id
	run_command "$scontrol delete partitionname=$part2"
}

#
# Delete left-over stdin/out/err files
# Build stdin file
#
exec $bin_rm -f $file_in
make_bash_script $file_in "$bin_id"

#
# Submit job to one partition
#
set job_id [submit_job -fail "-N1 --output=none --error=none -t1 --begin=tomorrow --partition=$part1 $file_in"]
subtest {[get_job_param $job_id "Partition"] == $part1} "Verify submission to 1 partition"
cancel_job -fail $job_id

#
# Submit job to two partitions, including one invalid name
#
set output [run_command_output -xfail -fail "$sbatch -N1 --output=none --error=none -t1 --begin=tomorrow --partition=$part1,DUMMY $file_in"]
subtest {![regexp "Submitted batch job ($number)" $output - job_id]} "Verify job cannnot be submitted to an invalid partition"
subtest {[regexp "Invalid partition name specified" $output]} "Verify error message form invalid submission"

#
# Add a second partition
#
run_command -fail "$scontrol create partitionname=$part2 nodes=$test_node"

#
# Submit job to more than one partition
#
set job_id  [submit_job -fail "-N1 --output=none --error=none -t1 --begin=tomorrow --partition=$part1,$part2 $file_in"]
set partitions [get_job_param $job_id "Partition"]
subtest {[param_contains $partitions $part1]} "Verify $part1 is one of the job ($job_id) partitions" "$partitions doesn't contain $part1"
subtest {[param_contains $partitions $part2]} "Verify $part2 is one of the job ($job_id) partitions" "$partitions doesn't contain $part2"
