#!/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_dir/input"
set job_id      0
set step_id	0
set pid_count	1
set test_prog   "$test_name.prog"

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

proc cleanup {} {
	global job_id test_prog

	cancel_job $job_id
	file delete $test_prog
}

#
# Rebuild program
#
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
#
wait_for_job -fail $job_id "RUNNING"

# wait for job step to start
wait_for_command_match -fail -pollinterval .1 "$sstat --allsteps --format JobID --noheader $job_id" "$job_id\.0"

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)"
}
