#!/usr/bin/expect
############################################################################
# Purpose: Test of SLURM functionality
#          Bluegene/Q only: Test to make sure that the correct number of
#          nodes and tasks in a job and a step.
#
# Output:  "TEST: #.#" followed by "SUCCESS" if test was successful, OR
#          "FAILURE: ..." otherwise with an explanation of the failure, OR
#          anything else indicates a failure mode that must be investigated.
############################################################################
# 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 <http://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 test_id    "8.10"
set exit_code  0
set file_in    "test$test_id.input"
set job_id     0

print_header $test_id

proc check_job { job_id nodes cpus } {
	global exit_code scontrol number

	#Checks a job allocation
	set matches 0
	spawn $scontrol show job $job_id
	expect {
		-re "NumNodes=($number)(\[K\]*)" {
			set node_cnt $expect_out(1,string)
			if {[string compare $expect_out(2,string) ""]} {
				set node_cnt [expr $node_cnt * 1024]
			}
			if {$node_cnt != $nodes} {
				send_user "\nFAILURE: NumNodes is not $nodes ($node_cnt)\n"
				set exit_code 1
			} else {
				incr matches
			}
			exp_continue
		}
		-re "NumCPUs=($number)(\[K\]*)" {
			set cpu_cnt $expect_out(1,string)
			if {[string compare $expect_out(2,string) ""]} {
				set cpu_cnt [expr $cpu_cnt * 1024]
			}
			if {$cpu_cnt != $cpus} {
				send_user "\nFAILURE: NumCPUs is not $cpus ($cpu_cnt)\n"
				set exit_code 1
			} else {
				incr matches
			}
			exp_continue
		}
		timeout {
			send_user "\nFAILURE: scontrol not responding\n"
			set exit_code 1
		}
		eof {
			wait
		}
	}
	if {$matches == 3} {
		send_user "\nNumber of nodes and tasks are correct\n"
	}
	return $exit_code
}

proc check_step { job_id step_id nodes tasks } {
	global exit_code scontrol number

	#Checks a job step
	set matches 0
	spawn $scontrol show step $job_id.$step_id
	expect {
		-re "Nodes=($number)(\[K\]*)" {
			set node_cnt $expect_out(1,string)
			if {[string compare $expect_out(2,string) ""]} {
				set node_cnt [expr $node_cnt * 1024]
			}
			if {$node_cnt != $nodes} {
				send_user "\nFAILURE: NumNodes is not $nodes ($node_cnt)\n"
				set exit_code 1
			} else {
				incr matches
			}
			exp_continue
		}
		-re "Tasks=($number)(\[K\]*)" {
			set task_cnt $expect_out(1,string)
			if {[string compare $expect_out(2,string) ""]} {
				set task_cnt [expr $task_cnt * 1024]
			}
			if {$task_cnt != $tasks} {
				send_user "\nFAILURE: NumCPUs is not $tasks ($task_cnt)\n"
				set exit_code 1
			} else {
				incr matches
			}
			exp_continue
		}
		-re "not found" {
			send_user "\nFAILURE: step was not found\n"
			set exit_code 1
		}
		timeout {
			send_user "\nFAILURE: scontrol not responding\n"
			set exit_code 1
		}
		eof {
			wait
		}
	}

	if {$matches == 2} {
		send_user "\nNumber of nodes and tasks are correct\n"
	}

	return $exit_code
}

if {([test_bluegene] == 0) || [string compare [get_bluegene_type] "Q"]} {
	send_user "\nWARNING: This test is only compatible with BluegeneQ systems\n"
	exit 0
}

send_user "\n################## Test 1 Starts Here #######################\n"
# Submit a step with 512 tasks
set job_id 0
spawn $srun -n512 -v $bin_sleep 50
expect {
	-re "jobid ($number)" {
		set job_id $expect_out(1,string)
		send_user "\nGot job id $job_id\n"
	}
	timeout {
		send_user "\nFAILURE: srun is not responding\n"
		exit 1
	}
	eof {
		wait
	}
}

# Wait for job to start
sleep 5

# Checks the job
if {[check_job $job_id 32 512]} {
	exit $exit_code
}
# Checks the job step
if {[check_step $job_id 0 32 512]} {
	exit $exit_code
}
cancel_job $job_id

send_user "\n################## Test 2 Starts Here #######################\n"
# Submit a step with 32 task
set job_id 0
spawn $srun -O -n32 --ntasks-per-node=64 -v $bin_sleep 10
expect {
	-re "jobid ($number)" {
		set job_id $expect_out(1,string)
		send_user "\nGot job id $job_id\n"
	}
	timeout {
		send_user "\nFAILURE: srun is not responding\n"
		exit 1
	}
	eof {
		wait
	}
}

# Wait for job to start
sleep 5

# Checks the job step
if {[check_job $job_id 1 16]} {
	exit $exit_code
}
# Checks the job step
if {[check_step $job_id 0 1 32]} {
	exit $exit_code
}

cancel_job $job_id

send_user "\n################## Test 3 Starts Here #######################\n"
# Submit a step with 32 task
set job_id 0
spawn $srun -O -n96 --ntasks-per-node=64 -v $bin_sleep 10
expect {
	-re "jobid ($number)" {
		set job_id $expect_out(1,string)
		send_user "\nGot job id $job_id\n"
	}
	timeout {
		send_user "\nFAILURE: srun is not responding\n"
		exit 1
	}
	eof {
		wait
	}
}

# Wait for job to start
sleep 5

# Checks the job
if {[check_job $job_id 2 32]} {
	exit $exit_code
}

if {[check_step $job_id 0 2 96]} {
	exit $exit_code
}

cancel_job $job_id

send_user "\n################## Test 4 Starts Here #######################\n"
# Submit a step with 4 nodes
set job_id 0
spawn $srun -O -N4 --ntasks-per-node=32 -v $bin_sleep 10
expect {
	-re "jobid ($number)" {
		set job_id $expect_out(1,string)
		send_user "\nGot job id $job_id\n"
	}
	timeout {
		send_user "\nFAILURE: srun is not responding\n"
		exit 1
	}
	eof {
		wait
	}
}

# Wait for job to start
sleep 5

# Checks the job step
if {[check_job $job_id 4 64]} {
	exit $exit_code
}

if {[check_step $job_id 0 4 128]} {
	exit $exit_code
}

cancel_job $job_id

send_user "\n################## Test 5 Starts Here #######################\n"
set job_id 0
spawn $srun -N1 -v $bin_sleep 10
expect {
	-re "jobid ($number)" {
		set job_id $expect_out(1,string)
		send_user "\nGot job id $job_id\n"
	}
	timeout {
		send_user "\nFAILURE: srun is not responding\n"
		exit 1
	}
	eof {
		wait
	}
}

# Wait for job to start
sleep 5

# Checks the job step
if {[check_job $job_id 1 16]} {
	exit $exit_code
}

if {[check_step $job_id 0 1 1]} {
	exit $exit_code
}

cancel_job $job_id

send_user "\n################## Test 6 Starts Here #######################\n"
# Submit allocation and runs job
set job_id 0
spawn $salloc -n512 $srun $bin_sleep 10
expect {
	-re "Granted job allocation ($number)" {
		set job_id $expect_out(1,string)
	}
	timeout {
		send_user "\nFAILURE: salloc is not responding\n"
		exit 1
	}
	eof {
		wait
	}
}
if {$job_id == 0} {
	send_user "\nFAILURE: jobs were not submitted\n"
	exit 1
}

# Wait for jobs to start
sleep 5

# Checks job
if {[check_job $job_id 32 512]} {
	exit $exit_code
}
# Checks the job step
if {[check_step $job_id 0 32 512]} {
	exit $exit_code
}
cancel_job $job_id

send_user "\n################## Test 7 Starts Here #######################\n"
# Submit job with 32 tasks
set job_id 0
spawn $salloc -O -n32 --ntasks-per-node=64 $srun $bin_sleep 10
expect {
	-re "Granted job allocation ($number)" {
		set job_id $expect_out(1,string)
	}
	timeout {
		send_user "\nFAILURE: salloc is not responding\n"
		exit 1
	}
	eof {
		wait
	}
}
if {$job_id == 0} {
	send_user "\nFAILURE: jobs were not submitted\n"
	exit 1
}

# Wait for jobs to start
sleep 5

# Checks job
if {[check_job $job_id 1 16]} {
	exit $exit_code
}
# Checks the job step
if {[check_step $job_id 0 1 32]} {
	exit $exit_code
}
cancel_job $job_id

send_user "\n################## Test 8 Starts Here #######################\n"
# Submit allocation with 96 task and runs job
set job_id 0
spawn $salloc -O -n96 --ntasks-per-node=64 $srun  $bin_sleep 10
expect {
	-re "Granted job allocation ($number)" {
		set job_id $expect_out(1,string)
	}
	timeout {
		send_user "\nFAILURE: salloc is not responding\n"
		exit 1
	}
	eof {
		wait
	}
}
if {$job_id == 0} {
	send_user "\nFAILURE: jobs were not submitted\n"
	exit 1
}

# Wait for jobs to start
sleep 5

# Checks the job
if {[check_job $job_id 2 32]} {
	exit $exit_code
}
# Checks the job step
if {[check_step $job_id 0 2 96]} {
	exit $exit_code
}

cancel_job $job_id

send_user "\n################## Test 9 Starts Here #######################\n"
# Submit allocation with 4 nodes task and runs job
set job_id 0
spawn $salloc -O -N4 --ntasks-per-node=32 $srun $bin_sleep 10
expect {
	-re "Granted job allocation ($number)" {
		set job_id $expect_out(1,string)
	}
	timeout {
		send_user "\nFAILURE: salloc is not responding\n"
		exit 1
	}
	eof {
		wait
	}
}
if {$job_id == 0} {
	send_user "\nFAILURE: jobs were not submitted\n"
	exit 1
}

# Wait for jobs to start
sleep 5

# Checks the job
if {[check_job $job_id 4 64]} {
	exit $exit_code
}
# Checks the job step
if {[check_step $job_id 0 4 128]} {
	exit $exit_code
}
cancel_job $job_id

send_user "\n################## Test 10 Starts Here #######################\n"
set job_id 0
spawn $salloc -N1 $srun $bin_sleep 10
expect {
	-re "Granted job allocation ($number)" {
		set job_id $expect_out(1,string)
	}
	timeout {
		send_user "\nFAILURE: salloc is not responding\n"
		exit 1
	}
	eof {
		wait
	}
}

# Wait for job to start
sleep 5

# Checks the job step
if {[check_job $job_id 1 16]} {
	exit $exit_code
}

if {[check_step $job_id 0 1 1]} {
	exit $exit_code
}


if {$exit_code == 0} {
	send_user "\nSUCCESS\n"
} else {
	send_user "\nFAILURE\n"
}
exit $exit_code
