#!/usr/bin/env expect
############################################################################
# Purpose: Test of Slurm functionality
#          Validate scontrol create, delete, and update for partitions.
############################################################################
# Copyright (C) 2002 The Regents of the University of California.
# Produced at Lawrence Livermore National Laboratory (cf, DISCLAIMER).
# Written by Morris Jette <jette1@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 authorized     1
set part_name      "QA_TEST"

if {![is_super_user]} {
	skip "Can not test more unless SlurmUser or root"
}

#
# Cannot run the test if OverTimeLimit is set, since we test time limits.
#
regexp "($number)" [get_config_param "OverTimeLimit"] {} overtimelim
if {$overtimelim != 0} {
	skip "Cannot run this test when OverTimeLimit is set. Exiting now"
}

proc cleanup {} {
	global scontrol part_name

	run_command "$scontrol delete PartitionName=$part_name"
}
cleanup

#
# Confirm the partition name does not already exist
#
set found -1
spawn $scontrol -a show part $part_name
expect {
	-re "not found" {
		log_debug "This error was expected, no worries"
		set found 0
		exp_continue
	}
	-re "PartitionName" {
		set found 1
		exp_continue
	}
	timeout {
		fail "scontrol not responding"
	}
	eof {
		wait
	}
}
if {$found == -1} {
	fail "scontrol output format error"
}
if {$found == 1} {
	fail "Partition ($part_name) already exists"
}

#
# Identify usable nodes in default partition
#
set def_name ""
set def_node ""
spawn $sinfo -h -o %32P
expect {
	-re "($re_word_str)(\\*)" {
		set def_name $expect_out(1,string)
		exp_continue
	}
	eof {
		wait
	}
}
if {$def_name eq ""} {
	fail "Failed to find default partition"
}
spawn $sinfo -h -o "=%N=" -p $def_name
expect {
	-re "=(.+)=" {
		set def_node $expect_out(1,string)
		exp_continue
	}
	eof {
		wait
	}
}
if {$def_node eq ""} {
	fail "Default partition seems to have no nodes"
}

#
# Create a new partition
#
run_command -fail "$scontrol create PartitionName=$part_name Nodes=$def_node"

#
# Confirm the partition now exists
#
set allow  0
set found -1
spawn $scontrol show part $part_name
expect {
	-re "not found" {
		set found 0
		exp_continue
	}
	-re "PartitionName" {
		set found 1
		exp_continue
	}
	-re "AllowGroups=ALL" {
		set allow 1
		exp_continue
	}
	timeout {
		fail "scontrol not responding"
	}
	eof {
		wait
	}
}
if {$found != 1} {
	fail "Partition not created"
}
subtest {$allow == 1} "Verify correct partition groups value"

#
# Now set AllowGroups to mine and TimeLimit=1
#
spawn $bin_id -gn
expect {
	-re "($re_word_str)" {
		set my_group  $expect_out(1,string)
		exp_continue
	}
	timeout {
		fail "id not responding"
	}
	eof {
		wait
	}
}
run_command -fail "$scontrol update PartitionName=$part_name AllowGroups=$my_group MaxTime=1"

set found 0
spawn $scontrol show part $part_name
expect {
	-re "AllowGroups=$my_group" {
		set found 1
		exp_continue
	}
	timeout {
		fail "scontrol not responding"
	}
	eof {
		wait
	}
}
subtest {$found == 1} "Partition group should be reset"

#
# Run a job in this new partition and validate the time limit
#
set timed_out 0
set sleep_time 300
set timeout [expr $max_job_delay + $sleep_time]
spawn $srun -t1 -p $part_name $bin_sleep $sleep_time
expect {
	-re "(time limit|TIME LIMIT|Terminated)" {
		set timed_out 1
		exp_continue
	}
	timeout {
		fail "srun not responding"
	}
	eof {
		wait
	}
}
subtest {$timed_out == 1} "Partition time limit should be enforced"

#
# Now reset AllowGroups to ALL
#
run_command -fail "$scontrol update PartitionName=$part_name AllowGroups=ALL"

set found 0
spawn $scontrol show part $part_name
expect {
	-re "AllowGroups=ALL" {
		set found 1
		exp_continue
	}
	timeout {
		fail "scontrol not responding"
	}
	eof {
		wait
	}
}
subtest {$found == 1} "Partition group should be reset"


#
# Test modification of TRESBillingWeights
#
set tres_line "cPu=1,mem=-0.5G,node=.3,energy=-10"
run_command -fail "$scontrol update partitionname=$part_name TRESBillingWeights=$tres_line"

set found 0
spawn $scontrol show part $part_name
expect {
	-re "TRESBillingWeights=$tres_line" {
		set found 1
		exp_continue
	}
	timeout {
		fail "scontrol not responding"
	}
	eof {
		wait
	}
}
subtest {$found == 1} "TRESBillingWeights should be set"


# Attempt invalid weights
set found 0
spawn $scontrol update partitionname=$part_name TRESBillingWeights=cpu=abc
expect {
	-re "error" {
		set found 1
	}
	timeout {
		fail "scontrol is not responding"
	}
	eof {
		wait
	}
}
subtest {$found == 1} "TRESBillingWeights should not be set"


# The original weights should be intact after the previous invalid attempt.
set found 0
spawn $scontrol show part $part_name
expect {
	-re "TRESBillingWeights=$tres_line" {
		set found 1
		exp_continue
	}
	timeout {
		fail "scontrol not responding"
	}
	eof {
		wait
	}
}
subtest {$found == 1} "TRESBillingWeights should be set"

# Clear
set found 0
run_command -fail "$scontrol update partitionname=$part_name TRESBillingWeights="

set found 0
spawn $scontrol show part $part_name
expect {
	-re "TRESBillingWeights" {
		set found 1
		exp_continue
	}
	timeout {
		fail "scontrol not responding"
	}
	eof {
		wait
	}
}
subtest {! $found} "TRESBillingWeights should be removed"


#
# Now delete the partition
#
run_command -fail "$scontrol delete PartitionName=$part_name"

#
# Confirm the partition is now gone
#
set found -1
spawn $scontrol show part $part_name
expect {
	-re "not found" {
		log_debug "This error was expected, no worries"
		set found 0
		exp_continue
	}
	-re "PartitionName" {
		set found 1
		exp_continue
	}
	timeout {
		fail "scontrol not responding"
	}
	eof {
		wait
	}
}
subtest {$found == 0} "Partition should be deleted"
