#!/usr/bin/env expect
############################################################################
# Purpose: Test of sacct functionality for array jobs.
#          sacct options j
############################################################################
# Copyright (C) 2019 SchedMD LLC.
# Written by Dominik Bartkiewicz <bart@schedmd.com>
#
# 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 timeout	60

#
# Check accounting config and bail if not found.
#
if {[get_config_param "AccountingStorageType"] ne "accounting_storage/slurmdbd"} {
	skip "This test can't be run without a usable AccountStorageType"
}

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

proc run_sacct { jobs exp_match_1 exp_match_2 } {
	global sacct expected_steps array1_job_id array2_job_id

	set matches_1 0
	set matches_2 0
	set expected_matches [expr $exp_match_1 + $exp_match_2]

	# Now test sacct
	spawn $sacct --noheader --starttime=00:00 -j $jobs
	expect {
		-re "$array1_job_id" {
			incr matches_1
			exp_continue
		}
		-re "$array2_job_id" {
			incr matches_2
			exp_continue
		}
		timeout {
			fail "sacct not responding"
		}
		eof {
			wait
		}
	}

	if {$matches_1 != $exp_match_1} {
		fail "sacct failure for array 1 ($matches_1 != $exp_match_1)"
	}

	if {$matches_2 != $exp_match_2} {
		fail "sacct failure for array 2 ($matches_2 != $exp_match_2)"
	}

	set matches [expr $matches_1 + $matches_2]

	if {$matches != $expected_matches} {
		fail "sacct failure ($matches != $expected_matches)"
	}
}

set array1_job_id 0
set array2_job_id 0
set timeout $max_job_delay

spawn $sbatch -N1 --array=0-5 --output=/dev/null -t1 --wrap=exit
expect {
	-re "Submitted batch job ($number)" {
		set array1_job_id $expect_out(1,string)
		log_debug "Job $array1_job_id was submitted"
	}
	-re "error" {
		fail "sbatch did not submit jobs"
	}
	timeout {
		fail "sbatch not responding"
	}
	eof {
		wait
	}
}

eval spawn $sbatch -N1 --array=0-5 --output=/dev/null -t1 --wrap=exit
expect {
	-re "Submitted batch job ($number)" {
		set array2_job_id $expect_out(1,string)
		log_debug "Job $array2_job_id was submitted"
	}
	-re "error" {
		fail "sbatch did not submit jobs"
	}
	timeout {
		fail "sbatch not responding"
	}
	eof {
		wait
	}
}

# If PrologFlags=contain is in slurm.conf, then an "extern" step will be
# launched on each node, so we need to check for 3 steps per job instead of 2.
if {[param_contains [get_config_param "PrologFlags"] "Contain"]} {
	set expected_steps 3
} else {
	set expected_steps 2
}

#
# Wait for both jobs to complete
#
if {[wait_for_job $array1_job_id "DONE"] || [wait_for_job $array2_job_id "DONE"]} {
	cancel_job $array1_job_id
	cancel_job $array2_job_id
	fail "Waiting for jobs to complete"
}

# Wait for accounting records to reach slurmdbd
sleep 5
set timeout 10

# Now test sacct one task from each array
run_sacct "$array1_job_id\_1,$array2_job_id\_3" $expected_steps $expected_steps

# Now test sacct first full array and one task from the second array
run_sacct "$array1_job_id,$array2_job_id\_3" [expr $expected_steps * 6] $expected_steps

# Now test sacct one task from the first array and the second full
run_sacct "$array1_job_id\_2,$array2_job_id" $expected_steps [expr $expected_steps * 6]
