#!/usr/bin/env expect
############################################################################
# Purpose: Test of Slurm functionality
#          Validate that Allow/Deny Qos are enforced.
#
#
# 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) 2013 SchedMD LLC
# Written by Nathan Yee <nyee32@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 test_id        "2.19"
set user_name      ""
set node_name      ""
set host_name      ""
set cluster_name   ""
set acct1          "test${test_id}_acct_1"
set acct2          "test${test_id}_acct_2"
set qos_good       "test${test_id}_qos_good"
set qos_bad        "test${test_id}_qos_bad"
set part_name      "test${test_id}_part"
set exit_code      0

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 0
}
if { [string compare [check_accounting_admin_level] "Administrator"] } {
	send_user "\nWARNING: This test can't be run without being an Accounting administrator.\n"
	send_user "Use: sacctmgr mod user \$USER set admin=admin.\n"
	exit 0
}

proc set_part_val {part_type part_val} {
	global scontrol part_name exit_code

	spawn $scontrol update partitionname=$part_name $part_type=$part_val
	expect {
		-re "Error" {
			send_user "\nFAILURE: $part_type was not set\n"
			set exit_code 1
		}
		timeout {
			send_user "\nFAILURE: scontrol is not responding\n"
			set exit_code 1
		}
		eof {
			wait
		}
	}
}

proc cleanup { } {
	global scancel scontrol sacctmgr part_name qos_good qos_bad acct1 acct2 exit_code

	spawn $scancel -p $part_name
	expect {
		timeout {
			send_user "FAILURE: scancel is not responding\n"
			set exit_code 1
		}
		eof {
			wait
		}
	}

	send_user "Any error, except for unresponsiveness, from the previous "
	send_user "scancel is expected and should be ignored.\n"

	spawn $scontrol delete partition=$part_name
	expect {
		-re "error" {
			send_user "\nFAILURE: scontrol did not remove "
			"partition\n"
			set exit_code 1
			exp_continue
		}
		timeout {
			send_user "FAILURE: scontrol is not responding\n"
			set exit_code 1
		}
		eof {
			wait
		}
	}

	set del_part 0
	spawn $sacctmgr -i delete qos $qos_good $qos_bad
	expect {
		-re "Deleting QOS" {
			incr del_part
			exp_continue
		}
		timeout {
			send_user "\nFAILURE: sacctmgr is not responding\n"
			set exit_code 1
		}
		eof {
			wait
		}
	}

	spawn $sacctmgr -i delete account $acct1 $acct2
	expect {
		-re "Deleting accounts" {
			incr del_part
			exp_continue
		}
		timeout {
			send_user "\nFAILURE: sacctmgr is not responding\n"
			set exit_code 1
		}
		eof {
			wait
		}
	}

	return $del_part

}

proc create_qos { acct qos } {
	global sacctmgr user_name exit_code cluster_name

	set create_qos 0
	spawn $sacctmgr -i create qos $qos
	expect {
		-re "Adding QOS" {
			incr create_qos
			exp_continue
		}
		timeout {
			send_user "\nFAILURE: sacctmgr is not responding\n"
			set exit_code 1
		}
		eof {
			wait
		}
	}


	spawn $sacctmgr -i create account $acct qos=$qos cluster=$cluster_name
	expect {
		-re "Adding Account" {
			incr create_qos
			exp_continue
		}
		timeout {
			send_user "\nFAILURE: sacctmgr is not responding\n"
			set exit_code 1
		}
		eof {
			wait
		}
	}

	set create_acct 0
	spawn $sacctmgr -i create user $user_name account=$acct cluster=$cluster_name
	expect {
		timeout {
			send_user "\nFAILURE: sacctmgr is not responding\n"
			set exit_code 1
		}
		eof {
			wait
		}
	}

	if {$create_qos !=2} {
		send_user "\nFAILURE: Qos was not set\n"
		set exit_code 1
	}
}


proc test_part {acct qos part qos_con } {
	global srun part_name exit_code

	set sub_job 0

	spawn $srun -I5 -A $acct --qos $qos -p $part true
	expect {
		-re "error" {
			if { $qos_con == 1 } {
				send_user "\nThis error is expected\n"
			} else {
				send_user "\nFAILURE: This error should not have occured\n"
				set exit_code 1
			}
			exp_continue
		}
		timeout {
			send_user "\nFAILURE: srun is not responding\n"
			set exit_code 1
		}
		eof {
			wait
		}
	}
}

# Delete any vestigial qos or accounts
cleanup

set user_name [get_my_user_name]

# Setup
set partition [default_partition]
set node_name [ get_idle_node_in_part $partition ]
set cluster_name [ get_cluster_name ]

# NOTE: qos_good should always work and
# qos_bad should always cause an error

#
# Create good QOS
#
create_qos $acct1 $qos_good

#
# Create bad QOS
#
create_qos $acct2 $qos_bad


# Create partition
spawn $scontrol create partition=$part_name nodes=$node_name
expect {
	-re "error" {
		send_user "\nFAILURE: partition was not created\n"
		set exit_code 1
	}
	timeout {
		send_user "\nFAILURE: scontrol is not reponding\n"
		set exit_code 1
	}
	eof {
		wait
	}
}

#
# Set Allow Qos to good value
#
set_part_val allowqos $qos_good

######Testing AllowQos######
send_user "\nTesting AllowQos\n"

#
# Test partition with good Qos
# 0 = good test / 1 = bad test
#
test_part $acct1 $qos_good $part_name 0

#
# Test partition with bad Qos
# 0 = good test / 1 = bad test
#
test_part $acct2 $qos_bad $part_name 1

#
# Set Allow Qos back to all and set
# Deny Qos to bad value
#
set_part_val allowqos ALL
set_part_val denyqos $qos_bad

######Testing DenyQos#####
send_user "\nTesting DenyQos\n"

#
# Test partition with good Qos
# 0 = good test / 1 = bad test
#
test_part $acct1 $qos_good $part_name 0

#
# Test partition with bad Qos
# 0 = good test / 1 = bad test
#
test_part $acct2 $qos_bad $part_name 1

sleep 5
if {[cleanup] != 2} {
	send_user "\nFAILURE: Qos/account was not deleted\n"
	set exit_code 1
}

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