#!/usr/bin/env expect
############################################################################
# Purpose: Test of Slurm functionality
#          Validate that Allow/Deny accounts are enforced.
############################################################################
# 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 user_name      ""
set node_name      ""
set cluster_name   ""
set acct_good      "test${test_id}_acct_good"
set acct_bad       "test${test_id}_acct_bad"
set part_name      "test${test_id}_part"

# TODO: Remove this precondition check once Slurm 22.05 is no longer supported
regexp {(\d+).(\d+).(\S+)} [get_config_param SLURM_VERSION] - major minor release
if {$major < 23} {
	skip "This test is disabled in Slurm versions older than 23.02 (see bugs 14459 and 14864)"
}

#
# 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 cleanup { } {
	global scancel scontrol sacctmgr part_name acct_good acct_bad

	run_command "$scancel -p $part_name"

	log_debug "Any error, except for unresponsiveness, from the previous scancel is expected and should be ignored"

	run_command "$scontrol delete partition=$part_name"
	run_command "$sacctmgr -i delete account $acct_good $acct_bad"
}

proc create_acct { acct } {
	global sacctmgr user_name cluster_name

	set create_acct 0
	spawn $sacctmgr -i create account $acct cluster=$cluster_name
	expect {
		-re "Adding Account" {
			set create_acct 1
			exp_continue
		}
		timeout {
			fail "sacctmgr is not responding"
		}
		eof {
			wait
		}
	}

	spawn $sacctmgr -i create user $user_name account=$acct cluster=$cluster_name
	expect {
		timeout {
			fail "sacctmgr is not responding"
		}
		eof {
			wait
		}
	}

	if {$create_acct !=1 } {
		fail "Account was not added"
	}
}

proc test_part { acct part acct_con } {
	global srun

	set sub_job 0

	spawn $srun -I5 -A $acct -p $part true
	expect {
		-re "error:" {
			if { $acct_con == 1 } {
				log_debug "This error is expected"
			} else {
				fail "This error should not have occurred"
			}
			exp_continue
		}
		timeout {
			fail "srun is not responding"
		}
		eof {
			wait
		}
	}


}

# Remove any vestigial accounts or partitions
cleanup

set user_name [get_my_user_name]
set node_name [lindex [get_nodes_by_state] 0]
set cluster_name [get_config_param "ClusterName"]

# NOTE: acct_good should always work and
# acct_bad should always cause an error

#
# Create good account
#
create_acct $acct_good

#
# Create bad account
#
create_acct $acct_bad

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

#
# Set Allow Account to good values
#
set_part_val allowaccount $acct_good

######Testing AllowAccount#####
log_info "Testing AllowAccount"

#
# Test partition with good values
# 0 = good test / 1 = bad test
#
test_part $acct_good $part_name 0

#
# Test partition with bad values
# 0 = good test / 1 = bad test
#
test_part $acct_bad $part_name 1

#
# Set Allow Accounts to all and
# set Deny Account to bad value
#
set_part_val allowaccount ALL
set_part_val denyaccount $acct_bad


######Testing DenyAccount#####
log_info "Testing DenyAccount"

#
# Test partition with good values
# 0 = good test / 1 = bad test
#
test_part $acct_good $part_name 0

#
# Test partition with bad values
# 0 = good test / 1 = bad test
#
test_part $acct_bad $part_name 1

# Sleep before automatically calling cleanup
sleep 5
