#!/usr/bin/env expect
############################################################################
# Purpose: Test of Slurm functionality
#          Validates that a resource can be added to a cluster after
#          it has been created
#
# 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) 2015 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_accounting

set test_id                          21.33
set exit_code                        0
set test_cluster                     "test$test_id\_cluster"
set test_res1                        "test$test_id\_res1"
set test_res2                        "test$test_id\_res2"
set access_err                       0

array set res1 {}
set res1(count)                      50
set res1(server)                     "test$test_id\_server1"
set res1(servertype)                 "test$test_id\_server_type1"
set res1(type)                       "license"


array set res2 {}
set res2(count)                      25
set res2(server)                     "test$test_id\_server2"
set res2(servertype)                 "test$test_id\_server_type2"
set res2(type)                       "license"

array set res1_cluster {}
set res1_cluster(cluster)            $test_cluster
set res1_cluster(server)             "test$test_id\_server1"
set res1_cluster(percentallowed)     25


array set res2_cluster {}
set res2_cluster(cluster)            $test_cluster
set res2_cluster(server)             "test$test_id\_server2"
set res2_cluster(percentallowed)     25

proc clean_up { } {
	global test_res1 test_res2 test_cluster

	remove_res $test_res1
	remove_res $test_res2
	remove_cluster $test_cluster
}

proc check_val { res_name } {

	global sacctmgr test_cluster exit_code

	set rc 0
	spawn $sacctmgr -n show resource withcluster cluster=$test_cluster \
	    format=name%-30
	expect {
		-re "$res_name" {
			set rc 1
			exp_continue
		}
		timeout {
			send_user "\nFAILURE: sacctmgr is not responding\n"
			set exit_code 1
		}
		eof {
			wait
		}
	}

	return $rc
}

print_header $test_id

if { [test_using_slurmdbd] == 0 } {
        send_user "\nWARNING: This test can't be run without AccountStorageType=slurmdbd\n"
        exit $exit_code
} elseif { [test_super_user] == 0} {
        send_user "\nWARNING: This test can't be run without superuser permissions\n"
        exit $exit_code
}

# Delete any vestigial data
clean_up

set added 0
spawn $sacctmgr add -i cluster $test_cluster
expect {
	-re "Adding Cluster" {
		set added 1
		exp_continue
	}
	timeout {
		send_user "\nFAILURE: sacctmgr is not responding\n"
		set exit_code 1
	}
	eof {
		wait
	}
}

if {$added != 1} {
	send_user "\nFAILURE: test cluster was not added\n"
	exit 1
}

#
# Add resources
#
add_resource $test_res1 [array get res1]
if {[check_resource_limits $test_res1 [array get res1]]} {
	send_user "\nFAILURE: $test_res1 has bad values\n"
	clean_up
}

add_resource $test_res2 [array get res2]
if {[check_resource_limits $test_res2 [array get res2]]} {
	send_user "\nFAILURE: $test_res2 has bad values\n"
	clean_up
}

#
# Add a resources to the cluster
#
add_resource $test_res1 [array get res1_cluster]

if {[check_val $test_res1] != 1} {
	send_user "\nFAILURE: sacctmgr did not add resource $test_res1 to "
	send_user "cluster $test_cluster\n"
	set exit_code 1
}

#
# Add another resource to the cluster
#
add_resource $test_res2 [array get res2_cluster]

if {[check_val $test_res2] != 1} {
	send_user "\nFAILURE: sacctmgr did not add resource $test_res2 to "
	send_user "cluster $test_cluster\n"
	set exit_code 1
}

#
# Remove resource from the cluster
#
set deleted 0
spawn $sacctmgr delete resource -i where name=$test_res1 \
    server=$res1(server) cluster=$test_cluster
expect {
	-re "Deleting" {
		set deleted 1
		exp_continue
	}
	timeout {
		send_user "\nFAILURE: sacctmgr is not responding\n"
		set exit_code 1
	}
	eof {
		wait
	}
}

if {$deleted != 1} {
	send_user "\nFAILURE: resource $test_res1 was not deleted\n"
	set exit_code 1
}

if {[check_val $test_res2] != 1} {
	send_user "\nFAILURE: only $test_res2 should be associated with "
	send_user "cluster $test_cluster\n"
	set exit_code 1
}

remove_res $test_res1
remove_res $test_res2

set match 0
spawn $sacctmgr show resource -n withcluster cluster=$test_cluster
expect {
	-re "$test_res1" {
		set match 1
		exp_continue
	}
	-re "$test_res2" {
		set match 1
		exp_continue
	}
	timeout {
		send_user "\nFAILURE: sacctmgr is not reponding\n"
		set exit_code 1
	}
	eof {
		wait
	}
}

if {$match != 0} {
	send_user "\nFAILURE: $test_res1 and $test_res2 were not deleted\n"
	set exit_code 1
}

remove_cluster $test_cluster

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