#!/usr/bin/env expect
############################################################################
# Purpose: Test of SLURM functionality
#          Validates the rpm build is accepting the prefix and that
#          Slurm is being built in the correct directories.
#
#
# 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) 2013 SchedMD LLC
# Written by Nathan Yee <nyee32@schedmd.com>
#
# This file is part of SLURM, a resource management program.
# For details, see <http://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      "test30.1"
set tar_name     "Slurm-0-0.tar.bz2"
set prefix_loc   "_prefix /opt/slurm"
set rpm_loc      "/opt/slurm"
set topdir       "_topdir /tmp/built_rpms"
set man_dir      "_slurm_mandir /tmp/slurm_man/man"
set data_dir     "_slurm_datadir /tmp/slurm_data/data"
set lib_dir      "_libdir /opt/slurm/slurm_lib"
set bin_dir      "_bindir /etc/slurm"
set rpm_base     "/tmp/built_rpms/RPMS"
set file_name    "slurm"
set exit_code    0

print_header $test_id

proc check_path {path file_name} {
	global bin_bash bin_grep exit_code rpm_path

	set loc_match 0
	spawn $bin_bash -c "exec rpm -qpl $rpm_path | $bin_grep $file_name"
	expect {
		-re "$path" {
			set loc_match 1
			exp_continue
		}
		timeout {
			send_user "\nFAILURE: prefix was not enforced.\n"
			set exit_code 1
		}
		eof {
			wait
		}
	}
	if {$loc_match != 1} {
		send_user "\nFAILURE: $file_name was put in the wrong place\n"
		set exit_code 1
	}
}

if {[test_cray] || [test_alps]} {
	send_user "Warning: This test does not work correctly on a Cray (Native or ALPS) system\n";
	exit $exit_code
}

set timeout 1000
exec $bin_rm -fr /tmp/slurm $tar_name /tmp/built_rpms
spawn $bin_cp -L -R -v $src_dir /tmp/slurm
expect {
	timeout {
		send_user "\nFAILURE: $src_dir was not copied\n"
		set exit_code 1
	}
	eof {
		wait
	}
}
exec $bin_rm -fr /tmp/slurm/.git
exec $bin_sed -i "s/Name:    see META file/Name:    Slurm/g" /tmp/slurm/slurm.spec
exec $bin_sed -i "s/Version: see META file/Version: 0/g" /tmp/slurm/slurm.spec
exec $bin_sed -i "s/Release: see META file/Release: 0/g" /tmp/slurm/slurm.spec
exec $bin_sed -i "s/Source: %{name}-%{version}-%{release}.tgz/Source: Slurm-0-0.tar.bz2/g" /tmp/slurm/slurm.spec
exec $bin_sed -i "s/%setup -n %{name}-%{version}-%{release}/%setup -n $file_name/g" /tmp/slurm/slurm.spec

set build_match 0
spawn tar -v -jcf $tar_name -C /tmp slurm
expect {
	-re "slurm/" {
		set build_match 1
		exp_continue
	}
	timeout {
		send_user "\nFAILURE: Slurm tar-ball create failed\n"
		set exit_code 1
	}
	eof {
		wait
	}
}
if {$build_match != 1} {
	send_user "\nFAILURE: did not tar src\n"
}

set build_match 0
spawn rpmbuild -ta -v $tar_name --define=$prefix_loc --define=$topdir --nodeps
expect {
	-re "Wrote" {
		set build_match 1
		exp_continue
	}
	-re "\nerror: db5 error" {
		send_user "\nWARNING: No installed RPM database on this system.\n"
		exp_continue
	}
	-re "\nerror: cannot open Packages" {
		send_user "\nWARNING: No installed RPM database on this system.\n"
		exp_continue
	}
	-re "\nerror: " {
		send_user "\nFAILURE: There was an error during the RPM build.\n"
		set exit_code 1
		exp_continue
	}
	-re "\nwarning: File listed twice: /etc/slurm/slurm.conf.example" {
		send_user "\nWARNING: This seems to be an rpmbuild problem.\n"
		exp_continue
	}
	-re "\nwarning: File listed twice: /etc/slurm/slurm.epilog.clean" {
		send_user "\nWARNING: This seems to be an rpmbuild problem.\n"
		exp_continue
	}
	-re "\nwarning: " {
		send_user "\nFAILURE: There was a warning during the RPM build.\n"
		set exit_code 1
		exp_continue
	}
	timeout {
		send_user "\nFAILURE: rpmbuild is not responding\n"
		set exit_code 1
	}
	eof {
		wait
	}
}
if {$build_match != 1} {
	send_user "\nFAILURE: there was an error during the rpmbuild\n"
	set exit_code 1
}

# Determine RPM build path. It could include "x86_64", "i386", etc.
set sub_dir ""
set timeout 10
spawn ls $rpm_base
expect {
	-re "($alpha_numeric_under)" {
		set sub_dir $expect_out(1,string)
		exp_continue
	}
	eof {
		wait
	}
}
set rpm_path "${rpm_base}/${sub_dir}/Slurm-0-0.${sub_dir}.rpm"
set timeout 1000

# Check to see that srun man pages are in the correct directory.
check_path /opt/slurm srun.1

# Check to see that srun is in the correct directory.
check_path /opt/slurm srun

# Check to see that slurmctld is in the correct directory.
check_path /opt/slurm/sbin slurmctld

if {$exit_code == 0} {
	exec $bin_rm -fr /tmp/built_rpm
} else {
	send_user "\nFAILURE: In phase one of the rpmbuild\n"
	exit $exit_code
}

set build_match 0
spawn rpmbuild -ta -v $tar_name --define=$prefix_loc --define=$lib_dir \
    --define=$topdir --define=$man_dir --define=$data_dir --define=$bin_dir --nodeps
expect {
	-re "Wrote" {
		set build_match 1
		exp_continue
	}
	-re "\nerror: db5 error" {
		send_user "\nWARNING: No installed RPM database on this system.\n"
		exp_continue
	}
	-re "\nerror: cannot open Packages" {
		send_user "\nWARNING: No installed RPM database on this system.\n"
		exp_continue
	}
	-re "\nerror: " {
		send_user "\nFAILURE: There was an error during the RPM build.\n"
		set exit_code 1
		exp_continue
	}
	-re "\nwarning: File listed twice: /etc/slurm/slurm.conf.example" {
		send_user "\nWARNING: This seems to be an rpmbuild problem.\n"
		exp_continue
	}
	-re "\nwarning: File listed twice: /etc/slurm/slurm.epilog.clean" {
		send_user "\nWARNING: This seems to be an rpmbuild problem.\n"
		exp_continue
	}
	-re "\nwarning: " {
		send_user "\nFAILURE: There was a warning during the RPM build.\n"
		set exit_code 1
		exp_continue
	}
	timeout {
		send_user "\nFAILURE: rpmbuild is not responding\n"
		set exit_code 1
	}
	eof {
		wait
	}
}
if {$build_match != 1} {
	send_user "\nFAILURE: there was an error during the rpmbuild\n"
	set exit_code 1
}

# Check that scontrol is in the correct place
check_path /etc/slurm scontrol

# Check that srun man page is in the correct place
check_path /tmp/slurm_man/man srun.1

# Check that slurmctld is in the correct place
check_path /opt/slurm/sbin slurmctld

# Check that libraries are in the correct place
check_path /opt/slurm/slurm_lib lib

# Check that web pages are in the correct place
check_path /tmp/slurm_data/data accounting.html

if {$exit_code == 0} {
	send_user "\nSUCCESS\n"
	exec $bin_rm -fr /tmp/slurm $tar_name /tmp/built_rpms
} else {
	send_user "\nFAILURE: In phase two of the rpmbuild\n"
}

exit $exit_code
