#!/usr/bin/env expect
############################################################################
# Purpose:  Test of Fair Tree multifactor
#
# 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.
#
# Note:    This script generates and then deletes files in the working directory
#          named test24.4.prog
############################################################################
# Modified by Brigham Young University
#      Ryan Cox <ryan_cox@byu.edu> and Levi Morrison <levi_morrison@byu.edu>
#
# 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_id     "24.4"
set exit_code   0
set test_prog   "test$test_id.prog"
set matches     0
set expected_matches 35

print_header $test_id

set damp [get_fs_damping_factor]
if {$damp != 1} {
	send_user "Unable to run with FairShareDampeningFactor=$damp (must be 1)\n"
	exit 0
}

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

send_user "build_dir is $build_dir\n"
if {[test_aix]} {
	send_user "$bin_cc ${test_prog}.c -ldl -lm -lntbl -fno-gcse -fno-strict-aliasing -Wl,-brtl -Wl,-bgcbypass:1000 -Wl,-bexpfull -Wl,-bmaxdata:0x70000000 -Wl,-bhwloc -g -lpthreads -o ${test_prog} -I${build_dir} -I${src_dir} ${build_dir}/src/api/libslurm.o ${build_dir}/src/slurmctld/locks.o  ${build_dir}/src/sshare/process.o\n"
	exec       $bin_cc ${test_prog}.c -ldl -lm -lntbl -fno-gcse -fno-strict-aliasing -Wl,-brtl -Wl,-bgcbypass:1000 -Wl,-bexpfull -Wl,-bmaxdata:0x70000000 -Wl,-bhwloc -g -lpthreads -o ${test_prog} -I${build_dir} -I${src_dir} ${build_dir}/src/api/libslurm.o ${build_dir}/src/slurmctld/locks.o  ${build_dir}/src/sshare/process.o
} else {
	send_user "$bin_cc ${test_prog}.c -g -pthread -o ${test_prog} -I${build_dir} -I${src_dir} ${build_dir}/src/api/libslurm.o ${build_dir}/src/slurmctld/locks.o  ${build_dir}/src/sshare/process.o -ldl -lm -lhwloc -export-dynamic \n"
	exec       $bin_cc ${test_prog}.c -g -pthread -o ${test_prog} -I${build_dir} -I${src_dir} ${build_dir}/src/api/libslurm.o ${build_dir}/src/slurmctld/locks.o ${build_dir}/src/sshare/process.o -ldl -lm -lhwloc -export-dynamic
}
	exec $bin_chmod 700 $test_prog

# Usage: test24.4.prog
spawn ./$test_prog
expect {
	"No last decay" {
		send_user "This error is expected.  No worries.\n"
		exp_continue
	}
	"error: Can't save decay state" {
		send_user "This error is expected.  No worries.\n"
		exp_continue
	}

	"root|||0.000000|240||1.000000||||cpu=0|" {
		incr matches
		exp_continue
	}
	"root|u1|10|0.048077|0|0.000000|0.000000|1.000000|inf||cpu=0|" {
		incr matches
		exp_continue
	}
	"root|u2|10|0.048077|10|0.041667|0.041667|0.666667|1.153846||cpu=0|" {
		incr matches
		exp_continue
	}
	"aA||40|0.192308|45|0.187500|0.187500||1.025641||cpu=0|" {
		incr matches
		exp_continue
	}
	"aAA||30|0.750000|20|0.083333|0.444444||1.687500||cpu=0|" {
		incr matches
		exp_continue
	}
	"aAA|uAA1|1|1.000000|20|0.083333|1.000000|0.619048|1.000000||cpu=0|" {
		incr matches
		exp_continue
	}
	"aAB||10|0.250000|25|0.104167|0.555556||0.450000||cpu=0|" {
		incr matches
		exp_continue
	}
	"aAB|uAB1|1|0.500000|25|0.104167|1.000000|0.523810|0.500000||cpu=0|" {
		incr matches
		exp_continue
	}
	"aAB|uAB2|1|0.500000|0|0.000000|0.000000|0.571429|inf||cpu=0|" {
		incr matches
		exp_continue
	}
	"aB||60|0.288462|25|0.104167|0.104167||2.769231||cpu=0|" {
		incr matches
		exp_continue
	}
	"aBA||25|0.416667|25|0.104167|1.000000||0.416667||cpu=0|" {
		incr matches
		exp_continue
	}
	"aBA|uBA1|1|1.000000|25|0.104167|1.000000|0.714286|1.000000||cpu=0|" {
		incr matches
		exp_continue
	}
	"aBB||35|0.583333|0|0.000000|0.000000||inf||cpu=0|" {
		incr matches
		exp_continue
	}
	"aBB|uBB1|1|1.000000|0|0.000000|0.000000|0.761905|inf||cpu=0|" {
		incr matches
		exp_continue
	}
	"aC||0|0.000000|30|0.125000|0.125000||0.000000||cpu=0|" {
		incr matches
		exp_continue
	}
	"aC|uC1|0|0.000000|30|0.125000|1.000000|0.047619|0.000000||cpu=0|" {
		incr matches
		exp_continue
	}
	"aD||50|0.240385|110|0.458333|0.458333||0.524476||cpu=0|" {
		incr matches
		exp_continue
	}
	"aD|uD1|parent|0.240385|20|0.083333|0.181818|0.476190|||cpu=0|" {
		incr matches
		exp_continue
	}
	"aD|uD2|40|0.235294|20|0.083333|0.181818|0.238095|1.294118||cpu=0|" {
		incr matches
		exp_continue
	}
	"aD|uD3|50|0.294118|25|0.104167|0.227273|0.333333|1.294118||cpu=0|" {
		incr matches
		exp_continue
	}
	"aDA||parent|0.240385|45|0.187500|0.409091||||cpu=0|" {
		incr matches
		exp_continue
	}
	"aDA|uDA1|parent|0.240385|10|0.041667|0.090909|0.476190|||cpu=0|" {
		incr matches
		exp_continue
	}
	"aDA|uDA2|30|0.176471|10|0.041667|0.090909|0.380952|1.941176||cpu=0|" {
		incr matches
		exp_continue
	}
	"aDA|uDA3|50|0.294118|25|0.104167|0.227273|0.333333|1.294118||cpu=0|" {
		incr matches
		exp_continue
	}
	"aE||10|0.048077|0|0.000000|0.000000||inf||cpu=0|" {
		incr matches
		exp_continue
	}
	"aE|aE1|10|0.500000|0|0.000000|0.000000|1.000000|inf||cpu=0|" {
		incr matches
		exp_continue
	}
	"aE|aE2|10|0.500000|0|0.000000|0.000000|1.000000|inf||cpu=0|" {
		incr matches
		exp_continue
	}
	"aF||20|0.096154|0|0.000000|0.000000||inf||cpu=0|" {
		incr matches
		exp_continue
	}
	"aF|uF1|10|0.333333|0|0.000000|0.000000|1.000000|inf||cpu=0|" {
		incr matches
		exp_continue
	}
	"aF|uF2|20|0.666667|0|0.000000|0.000000|1.000000|inf||cpu=0|" {
		incr matches
		exp_continue
	}
	"aG||8|0.038462|20|0.083333|0.083333||0.461538||cpu=0|" {
		incr matches
		exp_continue
	}
	"aG|uG1|10|0.500000|10|0.041667|0.500000|0.190476|1.000000||cpu=0|" {
		incr matches
		exp_continue
	}
	"aGA||10|0.500000|10|0.041667|0.500000||1.000000||cpu=0|" {
		incr matches
		exp_continue
	}
	"aGA|uGA1|20|0.500000|4|0.016667|0.400000|0.190476|1.250000||cpu=0|" {
		incr matches
		exp_continue
	}
	"aGA|uGA2|20|0.500000|6|0.025000|0.600000|0.095238|0.833333||cpu=0|" {
		incr matches
		exp_continue
	}

	timeout {
		send_user "\nFAILURE: spawn IO not responding\n"
		cancel_job $job_id
		set exit_code 1
	}
	eof {
		wait
	}
}

if {$matches != $expected_matches} {
	send_user "\nFAILURE: we didn't get the correct priorities from the plugin ($matches != $expected_matches)\n"
	send_user "Some potentially useful information is below:
* Account aA, aB, and aC are general purpose algorithm tests with various shares, usage values, children counts, and depths
* Account aD is used to test USE_PARENT. Any account that is USE_PARENT should have its children effectively reparented to the parent's parent. Any user with USE_PARENT should be ranked highest in its account, subject to ties with any other association with level_fs==1.0. uD2 and uD3 do NOT have the same level_fs; sshare doesn't show enough digits to visually distinguish.
* Account aE and its children should have fairshare==1.0 as a result of tie handling code.
* Account aF is similar to aE except its shares are different; they should still be 1.0.
* Accounts aE and aF and their descendants and u1 (root) should have fairshare==1.0 or ranking is broken.
* Account aG is set up so that uGA1 and uG1 will have equivalent fs factors due to the tie handling code but uGA2 will not.\n"
	set exit_code 1
}

if {$exit_code == 0} {
	file delete $test_prog
	send_user "\nSUCCESS\n"
}
exit $exit_code
