#! /usr/bin/env perl
#testreq: personal
##**************************************************************
##
## Copyright (C) 1990-2007, Condor Team, Computer Sciences Department,
## University of Wisconsin-Madison, WI.
## 
## Licensed under the Apache License, Version 2.0 (the "License"); you
## may not use this file except in compliance with the License.  You may
## obtain a copy of the License at
## 
##    http://www.apache.org/licenses/LICENSE-2.0
## 
## Unless required by applicable law or agreed to in writing, software
## distributed under the License is distributed on an "AS IS" BASIS,
## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
## See the License for the specific language governing permissions and
## limitations under the License.
##
##**************************************************************

use CondorTest;
use CondorUtils;

$testname = "job_dagman_always_run_post-A";
$cmd = 'job_dagman_always_run_post-A.dag';
$testdesc =  'Condor DAGman test for always running the POST script - scheduler U';
$testname = "job_dagman_always_run_post-A";
$dagman_args = "-verbose -AlwaysRunPost";

#**************************************************************
#
# Job to test always running post.
# PRE script is expected to fail
# Node Job is not to run
# POST script runs, and the node fails.
#
#**************************************************************

# TEMP -- to-do: add check of jobstate.log file.

$outfile = "$testname.dag.dagman.out";
runcmd("rm -f $outfile");

# remove any dag files created by the last run.
unlink glob "${testname}.dag*";

# create dag, submit & script files needed for this test run.
emit_dag_files();

my $killedchosen = 0;

# truly const variables in perl
sub IDLE{1};
sub HELD{5};
sub RUNNING{2};

$abnormal = sub 
{
	die "Want to see only submit, execute and successful completion\n";
};

$aborted = sub 
{
	die "Abort event NOT expected\n";
};

$held = sub 
{
	die "Held event NOT expected\n";
};

$executed = sub
{
	my %info = @_;

	CondorTest::debug("Good. We need the dag to run<$info{cluster}>\n",1);
};

$submitted = sub
{
	CondorTest::debug("submitted: This test will see submit, executing and failure on completion\n",1);
};

$success = sub
{
	die("DAG is expected to fail.\n");
};

$failure = sub 
{
	CondorTest::debug("DAG failed as expected\n",1);
	CondorTest::debug("Verifying output\n",1);
	$diditpass = 1;
	if (-e "A_A.out") {
		CondorTest::debug("A_A.out exists, but should not!\n",1);
		$diditpass = 0;
	}
	$canopen = open(OUT,"<$outfile");
	if( ! (defined $canopen)) {
		CondorTest::debug("Could not open outfile<$outfile>:$!\n",1);
		$diditpass = 0;
	}
	my @outfilelist = <OUT>;
	close(OUT);
	$passed_first = 0; # Let's be optimistic
	$passed_second = 0;
	foreach (@outfilelist) {
		chomp;
		if(/PRE Script of Job A_A failed/) {
			CondorTest::debug("Saw PRE script A_A fail correctly: $_\n",1);
			$passed_first = 1;
		}
		if(/Running POST script of Node A_A/) {
			CondorTest::debug("Saw POST script A_A run!\n",1);
			$passed_second = 1;
		}
		if(/PRE Script of Node A_A completed successfully/) {
			CondorTest::debug("PRE script is expected to fail.\n");
			$diditpass = 0;		
		}
		if(/Submitting Condor Node A_A job/){
			CondorTest::debug("Condor Node A_A job should not be submitted.\n");
			$diditpass = 0;		
		}
	}
	if($passed_first != 1) {
		CondorTest::debug("Missing A_A PRE script failure!\n",1);
		$diditpass = 0;
	}
	if($passed_second != 1) {
		CondorTest::debug("Missing A_A POST script run!\n",1);
		$diditpass = 0;
	}

	if($diditpass == 0) {
		die "All expected files and correct output failed\n";
	}
};

CondorTest::RegisterExitedFailure( $testname, $failure);
CondorTest::RegisterExitedSuccess( $testname, $success);
CondorTest::RegisterExecute($testname, $executed);
CondorTest::RegisterExitedAbnormal( $testname, $abnormal );
CondorTest::RegisterAbort( $testname, $aborted );
CondorTest::RegisterHold( $testname, $held );
CondorTest::RegisterSubmit( $testname, $submitted );

if( CondorTest::RunDagTest($testname, $cmd, 0, $dagman_args) ) {
	if (! -e $outfile) {
		die "$testname: failed -- no $outfile file!  DAG not run?\n";
	}
	CondorTest::debug("$testname: SUCCESS\n",1);
	exit(0);
} else {
	die "$testname: CondorTest::RunTest() failed\n";
}


#
# helper functions
#
sub emit_dag_files {
	my $dag_content = "JOBSTATE_LOG $testname.jobstate.log

Job A_A ${testname}_A.cmd
SCRIPT PRE A_A ${testname}_A_pre.sh
SCRIPT POST A_A ${testname}_A_post.sh
";

	my $pre_script_sh_content = "#! /usr/bin/env sh\necho \"PRE A_A running\"\nexit 1\n";
	my $pre_script_bat_content = "\@echo PRE A_A running\n\@exit /b 1\n";
	my $post_script_sh_content = "#! /usr/bin/env sh\necho \"POST A_A running\"\nexit 1\n";
	my $post_script_bat_content = "\@echo POST A_A running\n\@exit /b 1\n";

	my $submit_content =
"if \$(IsWindows)
  executable = echo.exe
else
  executable = /bin/echo
endif
output = ${testname}_A.out
error = ${testname}_A.err
arguments = Not OK that we are running
queue
";

	if (CondorUtils::is_windows()) {
		$dag_content =~ s/\.sh/.bat/g;
		emit_file("_A_pre.bat", $pre_script_bat_content);
		emit_file("_A_post.bat", $post_script_bat_content);
	} else {
		emit_file("_A_pre.sh", $pre_script_sh_content);
		emit_file("_A_post.sh", $post_script_sh_content);
	}
	emit_file(".dag", $dag_content);
	emit_file("_A.cmd", $submit_content);
}

sub emit_file {
	my $namex = shift;
	my $content = shift;

	my $fname = $testname . $namex;
	open (FH, ">$fname") || die "error writing to $fname: $!\n";
	print FH $content;
	close (FH);
	if ( ! CondorUtils::is_windows()) { chmod (0755, $fname); }
}
