#!/usr/bin/env expect
############################################################################
# Purpose:  Test of slurm_job_step_stat() and slurm_load_job() API calls.
#
# Note:    This script generates and then deletes a file in the working
#          directory named test7.12.prog
############################################################################
# Portions Copyright (C) 2014 SchedMD LLC
# Copyright (C) 2010 Lawrence Livermore National Security.
# Produced at Lawrence Livermore National Laboratory (cf, DISCLAIMER).
# Written by Morris Jette <jette@schedmd.com>
# 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$test_id.input"
set job_id      0
set step_id	0
set pid_count	1
set test_prog   "test$test_id.prog"

proc cleanup {} {
	global job_id file_in test_prog

	cancel_job $job_id
	file delete $file_in $test_prog
}

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

#
# Delete left-over program and rebuild it
#
file delete $file_in $test_prog
make_bash_script $file_in "
  $srun $bin_sleep 120
"
if [compile_against_libslurm ${test_prog}] {
	fail "Cannot compile test program"
}

#
# Spawn program via sbatch
#
spawn $sbatch -N1 -t1 --output=/dev/null $file_in
expect {
	-re "Submitted batch job ($number)" {
		set job_id $expect_out(1,string)
		exp_continue
	}
	timeout {
		fail "sbatch not responding"
	}
	eof {
		wait
	}
}
if { $job_id == 0 } {
	fail "Failed to submit job"
}

#
# Wait for job to start
#
if {[wait_for_job $job_id "RUNNING"] != 0} {
	cancel_job $job_id
	fail "Waiting for job to start"
}
# wait for job step to start
sleep 5
set debug 0
if {$debug} {
	spawn $sstat -j $job_id
	expect {
		eof {
			wait
		}
	}
}

set job_matches  0
set pid_matches  0
set step_matches 0
spawn ./$test_prog $job_id $step_id
expect {
	-re "job_id:$job_id step_id" {
		incr step_matches
		exp_continue
	}
	-re "pid:$number" {
		incr pid_matches
		exp_continue
	}
	-re "job_id:$job_id name" {
		incr job_matches
		exp_continue
	}
	timeout {
		cancel_job $job_id
		fail "Spawn IO not responding"
	}
	eof {
		wait
	}
}

if {$step_matches != 1} {
	fail "Error running slurm_job_step_stat() program ($step_matches != 1)"
}
if {$pid_matches != $pid_count} {
	log_warn "Failed to load PIDs associated with job step ($pid_matches != $pid_count). This is dependent upon the ProctrackType configured. proctrack/pgid does NOT support this functionality"
}
if {$job_matches != 1} {
	fail "Failed to load job info for this job ($job_matches != 1)"
}
