#!/usr/bin/env expect
############################################################################
# Purpose: Test of sacct functionality for heterogeneous jobs.
#          sacct options j
#
# 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) 2017 SchedMD LLC.
# Written by Isaac Hartung <ihartung@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 test_id	"38.8"
set exit_code	0
set prompt	"PROMPT: "
set timeout	60

print_header $test_id

#
# Check accounting config and bail if not found.
#
if { [test_account_storage] == 0 } {
	send_user "\nWARNING: This test can't be run without a usable AccountStorageType\n"
	exit $exit_code
}

if {[test_front_end]} {
        send_user "\nWARNING: This test is incompatible with front-end systems\n"
        exit $exit_code
} elseif {[test_serial]} {
	set task_num 1
} else {
	set task_num 2
}

set def_part_name [default_partition]
set nb_nodes [get_node_cnt_in_part $def_part_name]
if {$nb_nodes < 3} {
	send_user "\nWARNING: Need 3 or more nodes in default partition\n"
	exit $exit_code
}

proc end_it { exit_code } {
	global pack_job_id scancel
	if {$pack_job_id > 0} {
		exec $scancel $pack_job_id
	}
	exit $exit_code
}

set pack_job_id 0
set matches 0
set index 0
set sid 0
set timeout $max_job_delay
spawn $salloc -t1 -N1 : -N1 : -N1 $bin_bash
expect {
	-re "job ($number) has been allocated resources" {
		set pack_job_id $expect_out(1,string)
		send "export PS1=\"$prompt\"\r"
		exp_continue
	}
	-re "\"$prompt" {
		# skip this, just echo of setting prompt"
		exp_continue
	}
	-re "$prompt" {
		#send_user "Job initiated\n"
	}
	timeout {
		log_error "salloc : allocation not granted in $timeout seconds\n"
		end_it 1
	}
	eof {
		wait
	}
}
if {$pack_job_id == 0} {
	log_error "salloc failure\n"
	end_it 1
}

expect *

send_user "\n\nTest Sending Sacct To Various Components\n\n"

# Wait for accounting records to reach slurmdbd
sleep 5
set timeout 3
set matches 0
set expected_matches 3
set has_prolog_flags 0

# If PrologFlags=contain is in slurm.conf, then an "extern" step will be
# launched on each node, so we need to check for 6 matches instead of 3.
spawn $scontrol show config
expect {
	-re "PrologFlags\\s*=\\s*\[A-z/,\]*Contain" {
		set expected_matches 6
		set has_prolog_flags 1
	}
	timeout {
		log_error "scontrol show config not responding\n"
		end_it 1
	}
	eof {
		wait
	}
}

# Now test sacct
spawn $sacct -j $pack_job_id
expect {
	-re "$pack_job_id\\+\[0-2\]" {
		incr matches
		exp_continue
	}
	timeout {
		log_error "sacct not responding\n"
		end_it 1
	}
	eof {
		wait
	}
}
if {$matches != $expected_matches} {
	log_error "sacct failure ($matches != $expected_matches)\n"
	end_it 1
}

set expected_matches 1
if {$has_prolog_flags} {
	set expected_matches 2
}

for {set inx 0} {$inx < 3} {incr inx} {
	send_user "\n\n"
	set bad_matches 0
	set matches 0
	spawn $sacct -j $pack_job_id+$inx
	expect {
		-re "$pack_job_id\\+($number)" {
			if {$expect_out(1,string) == $inx} {
				incr matches
			} else {
				incr bad_matches
			}
			exp_continue
		}
		timeout {
			log_error "sacct not responding\n"
			end_it 1
		}
		eof {
			wait
		}
	}
	if {$matches != $expected_matches || $bad_matches != 0} {
		log_error "sacct failure ($matches != 1 || $bad_matches != 0)\n"
		end_it 1
	}
}

send_user "\nSUCCESS\n"
end_it 0
