#!/usr/bin/env expect
############################################################################
# Purpose: Test of Slurm functionality
#          Validate scontrol displays and updates Allow/Deny Qos.
############################################################################
# Copyright (C) SchedMD LLC.
#
# 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 qos_name       "${test_name}_qos"
set part_name      "${test_name}_part"

#
# 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_admin_level] ne "Administrator"} {
	skip "This test can't be run without being an Accounting administrator.\nUse: sacctmgr mod user \$USER set admin=admin"
}

proc set_part_val {part_type part_val} {
	global scontrol part_name

	spawn $scontrol update partitionname=$part_name $part_type=$part_val
	expect {
		-re "Error" {
			fail "$part_type was not set"
		}
		timeout {
			fail "scontrol is not responding"
		}
		eof {
			wait
		}
	}
}

proc check_part {part_type part_val} {
	global scontrol part_name

	set val_found 0
	spawn $scontrol show partition=$part_name
	expect {
		-re "$part_type=$part_val" {
			set val_found 1
			exp_continue
		}
		timeout {
			fail "scontrol is not responding"
		}
		eof {
			wait
		}
	}

	if {$val_found != 1} {
		fail "$part_type was not set to $part_val"
	}
}

proc delete_part { } {
	global scontrol sacctmgr part_name qos_name

	spawn $scontrol delete partition=$part_name
	expect {
		-re "error" {
			fail "scontrol did not remove partition"
		}
		timeout {
			fail "scontrol is not responding"
		}
		eof {
			wait
		}
	}

	set del_acct 0
	spawn $sacctmgr -i delete qos $qos_name
	expect {
		-re "Deleting QOS" {
			set del_acct 1
			exp_continue
		}
		timeout {
			fail "sacctmgr is not responding"
		}
		eof {
			wait
		}
	}

	if {$del_acct != 1} {
		fail "Qos was not deleted"
	}

}

proc cleanup {} {

	#
	# Delete partition and Qos
	#
	delete_part
}

spawn $scontrol create partition=$part_name
expect {
	-re "error" {
		fail "Partition was not created"
	}
	timeout {
		fail "scontrol is not reponding"
	}
	eof {
		wait
	}
}

set create_qos 0
spawn $sacctmgr -i create qos $qos_name
expect {
	-re "Adding QOS" {
		set create_qos 1
		exp_continue
	}
	timeout {
		fail "sacctmgr is not responding"
	}
	eof {
		wait
	}
}

if {$create_qos != 1} {
	fail "QOS was not created"
}

#
# Set AllowQos
#
set_part_val AllowQos $qos_name

#
# Check that AllowQos is set
#
check_part AllowQos $qos_name

#
# Set AllowQos to ALL
#
set_part_val AllowQos ALL

#
# Check that Qos is set
#
check_part AllowQos ALL

#
# Set DenyQos
#
set_part_val DenyQos $qos_name

#
# Check that DenyQos is set
#
check_part DenyQos $qos_name

#
# set DenyQos to none
#
set_part_val DenyQos none

#
# Check that DenyQos is set to none
#
check_part DenyQos none
