#!/usr/bin/env expect
############################################################################
# Purpose: Test of SLURM functionality
#          Test of SPANK plugin
#
# 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) 2008-2009 Lawrence Livermore National Security.
# 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 test_id	    "7.11"
set cwd		    "[$bin_pwd]"
set exit_code	    0
set file_in         "${cwd}/test${test_id}.in"
set file_out	    "${cwd}/test${test_id}.out"
set file_prog	    "${cwd}/test${test_id}.prog"
set orig_spank_conf "${cwd}/test${test_id}.orig_conf"
set new_spank_conf  "${cwd}/test${test_id}.new_conf"
set spank_out       "${cwd}/test${test_id}.spank.out"

print_header $test_id

if {[test_super_user] == 0} {
	send_user "\nWARNING: This test must be run as SlurmUser\n"
	exit 0
}
if {[test_aix]} {
	send_user "WARNING: Test is incompatible with AIX\n"
	exit 0
}
if {[test_front_end]} {
        send_user "\nWARNING: This test is incompatible with front-end systems\n"
        exit $exit_code
}

#
# Build the plugin
#
exec $bin_rm -f ${file_prog}.so
exec $bin_cc -fPIC -shared -I${slurm_dir}/include -o ${file_prog}.so ${file_prog}.c

#
# Locate slurm.conf's directory, copy the original plugstack.conf file
# and create an updated one using our new plugin
#
log_user 0
set config_dir ""
set ctld_slurm_ver ""
spawn $scontrol show config
expect {
	-re "SLURM_CONF.*= (.*)/slurm.conf.*SLURM_VERSION *= ($float)" {
		set config_dir $expect_out(1,string)
		set ctld_slurm_ver $expect_out(2,string)
		exp_continue
	}
	eof {
		wait
	}
}
log_user 1
if {[string compare $config_dir ""] == 0} {
	send_user "\nFAILURE: Could not locate slurm.conf directory\n"
	exit 1
}

log_user 0
set loc_slurm_ver ""
spawn $scontrol -V
expect {
	-re "slurm ($float)" {
		set loc_slurm_ver $expect_out(1,string)
		exp_continue
	}
	eof {
		wait
	}
}
log_user 1

if {[string compare $ctld_slurm_ver $loc_slurm_ver]} {
	send_user "\nWARNING: slurmctld ($ctld_slurm_ver) and local Slurm ($loc_slurm_ver) versions are not the same, can not continue.\n"
	exit 0
}


set spank_conf_file ${config_dir}/plugstack.conf
exec $bin_rm -f $orig_spank_conf $new_spank_conf $file_out $spank_out
if {[file exists $spank_conf_file]} {
	spawn $bin_cat $spank_conf_file
	expect {
		-re "test${test_id}" {
			send_user "\nFAILURE: spank plugin includes vestigial test${test_id}\n"
			send_user "   You probably should manually remove it from $spank_conf_file\n"
			send_user "   It was probably left over from some previous test failure.\n"
			exit 1
		}
		eof {
			wait
		}
	}

	exec $bin_cp $spank_conf_file $orig_spank_conf
	exec $bin_cp $spank_conf_file $new_spank_conf
	exec $bin_chmod 700 $spank_conf_file
	exec $bin_chmod 700 $new_spank_conf
} else {
	exec $bin_cp /dev/null $new_spank_conf
}

exec $bin_echo "required ${file_prog}.so ${spank_out}" >>$new_spank_conf
spawn $bin_cp $new_spank_conf $spank_conf_file
expect {
	-re "Permission denied" {
		send_user "\nWARNING: User lacks permission to update plugstack_conf file\n"
		exit 0
	}
	eof {
		wait
	}
}

#
# Test of srun help message
#
# NOTE: Expect parsing failures have been noted running "$srun --help"
#       directly, so we build a script containing a pipe to tail
#

make_bash_script $file_in "$srun --help | $bin_grep test"

set matches 0
set srun_pid [spawn $file_in]
expect {
	-re "Component of slurm test suite" {
		incr matches
		exp_continue
	}
	-re "Registered component of slurm test suite" {
		incr matches
		exp_continue
	}
	timeout {
		send_user "\nFAILURE: srun not responding\n"
		slow_kill $srun_pid
		set exit_code 1
	}
	eof {
		wait
	}
}
if {$matches != 2} {
	send_user "\nFAILURE: spank help message not in srun help message\n"
	set exit_code 1
}

#
# Test of alloc help message
#
# NOTE: Expect parsing failures have been noted running "$salloc --help"
#       directly, so we build a script containing a pipe to tail
#

make_bash_script $file_in "$salloc --help | $bin_grep test"

set matches 0
set srun_pid [spawn $file_in]
expect {
	-re "Registered component of slurm test suite" {
		incr matches
		exp_continue
	}
	timeout {
		send_user "\nFAILURE: salloc not responding\n"
		slow_kill $srun_pid
		set exit_code 1
	}
	eof {
		wait
	}
}
if {$matches != 1} {
	send_user "\nFAILURE: spank help message not in salloc help message\n"
	set exit_code 1
}

#
# Test of sbatch help message
#
# NOTE: Expect parsing failures have been noted running "$sbatch --help"
#       directly, so we build a script containing a pipe to tail
#

make_bash_script $file_in "$sbatch --help | $bin_grep test"

set matches 0
set srun_pid [spawn $file_in]
expect {
	-re "Registered component of slurm test suite" {
		incr matches
		exp_continue
	}
	timeout {
		send_user "\nFAILURE: srun not responding\n"
		slow_kill $srun_pid
		set exit_code 1
	}
	eof {
		wait
	}
}
if {$matches != 1} {
	send_user "\nFAILURE: spank help message not in sbatch help message\n"
	set exit_code 1
}
exec $bin_rm -f $file_in

#
# Test of locally logged messages().
# We update the plugstatck.conf in this script since NFS delays may
# prevent it from having the same state as the submit host.
#
make_bash_script $file_in "
  $bin_cp $new_spank_conf $spank_conf_file
  $srun --test_suite=5 $bin_uname
  if test -f $orig_spank_conf
  then
    $bin_cp $orig_spank_conf $spank_conf_file
  fi
"
set matches 0
spawn $sbatch --test_suite_reg=4 -N1 -t1 -o $file_out $file_in
expect {
	-re "_test_opt_process: test_suite: opt_arg=4" {
		incr matches
		exp_continue
	}
	-re "slurm_spank_exit: opt_arg=4" {
		incr matches
		exp_continue
	}
	-re timeout {
		send_user "\nFAILURE: sbatch not responding\n"
		set exit_code 1
	}
	eof {
		wait
	}
}
if {$matches != 2} {
	send_user "\nFAILURE: spank options not processed by sbatch\n"
	set exit_code 1
}

# NOTE: spank logs from sbatch and srun would be intermingled here
if {[wait_for_file $file_out] == 0} {
	send_user "\n\n"
	set match1 0
	set match2 0
	set match3 0
	spawn $bin_cat $file_out
	expect {
		-re "error" {
			send_user "\nFAILURE: some error happened\n"
			set exit_code 1
			exp_continue
		}
		-re "_test_opt_process: test_suite: opt_arg=5" {
			set match1 1
			exp_continue
		}
		-re "slurm_spank_local_user_init" {
			set match2 1
			exp_continue
		}
		-re "slurm_spank_exit: opt_arg=5" {
			set match3 1
			exp_continue
		}
		eof {
			wait
		}
	}
	if {[expr $match1 + $match2 + $match3] != 3} {
		send_user "\nFAILURE: local (srun) spank plugin failure\n"
		set exit_code 1
	} else {
		send_user "\nlocal (srun) spank plugin success\n"
	}
} else {
	set exit_code 1
}

#
# Test for slurmd output in file
#
if {[wait_for_file $spank_out] == 0} {
	send_user "\n\n"
	set matches        0
	set matches_sbatch 0
	set matches_srun   0
	spawn $bin_sort $spank_out
	expect {
		-re "slurm_spank_exit: opt_arg=($number)" {
			if {$expect_out(1,string) == 4} {
				incr matches_sbatch
			}
			if {$expect_out(1,string) == 5} {
				incr matches_srun
			}
			exp_continue
		}
		-re "slurm_spank_task_init: opt_arg=($number)" {
			if {$expect_out(1,string) == 4} {
				incr matches_sbatch
			}
			if {$expect_out(1,string) == 5} {
				incr matches_srun
			}
			exp_continue
		}
		-re "spank_get_item: argv" {
			incr matches
			exp_continue
		}
		-re "spank_get_item: my_uid=" {
			incr matches
			exp_continue
		}
		eof {
			wait
		}
	}
	if {$matches_sbatch != 2} {
		send_user "\nFAILURE: remote (slurmd) sbatch spank plugin failure ($matches_sbatch)\n"
		set exit_code 1
	} elseif {$matches_srun != 2} {
		send_user "\nFAILURE: remote (slurmd) srun spank plugin failure ($matches_srun)\n"
		set exit_code 1
	} elseif {$matches != 4} {
		send_user "\nFAILURE: remote (slurmd) spank plugin failure ($matches)\n"
		set exit_code 1
	} else {
		send_user "\n remote (slurmd) spank plugin success\n"
	}
} else {
	send_user "\nWARNING: This can be caused by the plugstack.conf file not\n"
	send_user "      being propagated to the compute node or not being in a\n"
	send_user "      shared file system.\n"
	set exit_code 1
}

#
# Restore the original plugstack
#
if {[file exists $orig_spank_conf]} {
	exec $bin_cp $orig_spank_conf $spank_conf_file
} else {
	exec $bin_rm -f $spank_conf_file
}

if {$exit_code == 0} {
	exec $bin_rm -f $orig_spank_conf $new_spank_conf $file_in $file_out $spank_out ${file_prog}.so
	send_user "\nSUCCESS\n"
}
exit $exit_code
