#!/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 exit_code      0
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"
}

#
# 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 {[string compare $def_name ""] == 0} {
	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 {[string compare $def_node ""] == 0} {
	fail "Default partition seems to have no nodes"
}

#
# Create a new partition
#
spawn $scontrol create PartitionName=$part_name Nodes=$def_node
expect {
	-re "error" {
		log_error "Something bad happened on partition create"
		set exit_code 1
		exp_continue
	}
	timeout {
		fail "scontrol not responding"
	}
	eof {
		wait
	}
}

#
# 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"
}
if {$allow != 1} {
	log_error "Partition groups value incorrect"
	set exit_code 1
}

#
# 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
	}
}
spawn $scontrol update PartitionName=$part_name AllowGroups=$my_group MaxTime=1
expect {
	timeout {
		fail "scontrol not responding"
	}
	eof {
		wait
	}
}
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
	}
}
if {$found != 1} {
        log_error "Partition group not reset"
	set exit_code 1
}

#
# 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]
set srun_pid [spawn $srun -t1 -p $part_name $bin_sleep $sleep_time]
expect {
	-re "(time limit|TIME LIMIT|Terminated)" {
		set timed_out 1
		exp_continue
	}
	timeout {
		log_error "srun not responding"
		slow_kill $srun_pid
	}
	eof {
		wait
	}
}
if {$timed_out == 1} {
	log_debug "Early termination is expected, no worries"
} else {
	log_error "Partition time limit not enforced"
	set exit_code 1
}

#
# Now reset AllowGroups to ALL
#
spawn $scontrol update PartitionName=$part_name AllowGroups=ALL
expect {
	timeout {
		fail "scontrol not responding"
	}
	eof {
		wait
	}
}
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
	}
}
if {$found != 1} {
	log_error "Partition group not reset"
	set exit_code 1
}


#
# Test modification of TRESBillingWeights
#
set tres_line "cPu=1,mem=-0.5G,node=.3,energy=-10"
spawn $scontrol update partitionname=$part_name TRESBillingWeights=$tres_line
expect {
	-re "error" {
		log_error "Failed to reset TRESBillingWeights"
		set exit_code 1
	}
	timeout {
		fail "scontrol is not responding"
	}
	eof {
		wait
	}
}

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
	}
}
if {$found != 1} {
	log_error "TRESBillingWeights not set"
	set exit_code 1
}


# 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
	}
}
if {$found != 1} {
	log_error "Didn't fail to set TRESBillingWeights"
	set exit_code 1
}


# 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
	}
}
if {$found != 1} {
	log_error "TRESBillingWeights not set"
	set exit_code 1
}

# Clear
set found 0
spawn $scontrol update partitionname=$part_name TRESBillingWeights=
expect {
	-re "error" {
		log_error "Failed to reset TRESBillingWeights"
		set exit_code 1
	}
	timeout {
		fail "scontrol is not responding"
	}
	eof {
		wait
	}
}

set found 0
spawn $scontrol show part $part_name
expect {
	-re "TRESBillingWeights" {
		set found 1
		exp_continue
	}
	timeout {
		fail "scontrol not responding"
	}
	eof {
		wait
	}
}
if {$found} {
	log_error "Found TRESBillingWeights when they should have been removed"
	set exit_code 1
}


#
# Now delete the partition
#
spawn $scontrol delete PartitionName=$part_name
expect {
	timeout {
		fail "scontrol not responding"
	}
	eof {
		wait
	}
}

#
# 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
	}
}
if {$found != 0} {
	fail "Partition not deleted"
}

if {$exit_code != 0} {
	fail "Test failed due to previous errors (\$exit_code = $exit_code)"
}
