#!/usr/bin/env expect
############################################################################
# Purpose:  Test of priority multifactor algo to get correct decay and
#           such.
#
# Note:    This script generates and then deletes files in the working directory
#          named test24.1.prog
############################################################################
# Copyright (C) 2009 Lawrence Livermore National Security.
# Produced at Lawrence Livermore National Laboratory (cf, DISCLAIMER).
# Written by Danny Auble <da@llnl.gov>
# 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_prog   "$test_name.prog"
set matches     0

proc cleanup {} {
	global test_prog

	file delete $test_prog
}

set damp [get_config_param "FairShareDampeningFactor"]
if {$damp != 1} {
	skip "Unable to run with FairShareDampeningFactor=$damp (must be 1)"
}

#
# Delete left-over programs and rebuild them
#
file delete $test_prog

if [compile_against_libslurm -full $test_prog "-ldl -lm -export-dynamic \
				   ${build_dir}/src/slurmctld/locks.o \
				   ${build_dir}/src/sshare/process.o -lgcov"] {
	fail "Cannot compile test program"
}

# Usage: test24.1.prog
spawn ./$test_prog
expect {
	"No last decay" {
		log_debug "This error is expected.  No worries"
		exp_continue
	}
	"error: Can't save decay state" {
		log_debug "This error is expected.  No worries"
		exp_continue
	}
	"AccountA||40|0.400000|45|0.450000|0.450000|0.458502|" {
		incr matches
		exp_continue
	}
	"AccountB||30|0.300000|20|0.200000|0.387500|0.408479|" {
		incr matches
		exp_continue
	}
	"AccountB|User1|1|0.300000|20|0.200000|0.387500|0.408479|" {
		incr matches
		exp_continue
	}
	"AccountC||10|0.100000|25|0.250000|0.300000|0.125000|" {
		incr matches
		exp_continue
	}
	"AccountC|User2|1|0.050000|25|0.250000|0.275000|0.022097|" {
		incr matches
		exp_continue
	}
	"AccountC|User3|1|0.050000|0|0.000000|0.150000|0.125000|" {
		incr matches
		exp_continue
	}
	"AccountD||60|0.600000|25|0.250000|0.250000|0.749154|" {
		incr matches
		exp_continue
	}
	"AccountE||25|0.250000|25|0.250000|0.250000|0.500000|" {
		incr matches
		exp_continue
	}
	"AccountE|User4|1|0.250000|25|0.250000|0.250000|0.500000|" {
		incr matches
		exp_continue
	}
	"AccountF||35|0.350000|0|0.000000|0.145833|0.749154|" {
		incr matches
		exp_continue
	}
	"AccountF|User5|1|0.350000|0|0.000000|0.145833|0.749154|" {
		incr matches
		exp_continue
	}
	"AccountG||0|0.000000|30|0.300000|0.300000|0.000000|" {
		incr matches
		exp_continue
	}
	"AccountG|User6|0|" {
		incr matches
		exp_continue
	}
	timeout {
		fail "spawn IO not responding"
	}
	eof {
		wait
	}
}

if {$matches != 13} {
	fail "We didn't get the correct priorities from the plugin ($matches != 13)"
}
