#! /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;

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

my $killedchosen = 0;

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

##**************************************************************
##
## This test checks that the config file option 
## DAGMAN_ALWAYS_RUN_POST is not ignored by DAGman
##
##**************************************************************

# 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();

$abnormal = sub 
{
	die "Want to see only submit, execute and failure\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\n",1);
};

# Actually expect failure
$failure = sub
{
	CondorTest::debug("DAG failed as expected\n",1);
	CondorTest::debug("Verifying output\n",1);
	my %info = shift;
	open OUT, "<$testname.dag.dagman.out" || die "Could not open $testname.dag.dagman.out\n";
	@outfile = <OUT>;
	close(OUT);
	$diditpass = 0;
	$passed1 = 1; $passed2 = 0; $passed3 = 0;
	foreach (@outfile) {
		if(/PRE Script of Job C_A failed with status/) {
			CondorTest::debug("Saw PRE script for C_A run and fail, as expected.\n");
			$passed1 = 0;
			next;
		}
		if(/Node C_A job completed/){
			CondorTest::debug("Oh no, C_A job should not have run\n");
			$passed2 = 1;
			next;
		}
		if(/Running POST script of Node C_A/) {
			CondorTest::debug("Oh no, POST script for C_A should not have run.\n");
			$passed3 = 1;
			next;
		}
	}
	if( $passed1 == 0 && $passed2 == 0 && $passed3 == 0 ) {
		$diditpass = 1;
	}
	if( $diditpass == 0 ) {
		die "Correct output failed in $testname.dag.dagman.out\n";
	}
};

# Actually expect failure
$success = sub {
	die("Successful completion is NOT expected\n");	
};

CondorTest::RegisterExitedSuccess( $testname, $success );
CondorTest::RegisterExitedFailure( $testname, $failure );
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) ) {
	CondorTest::debug("$testname: SUCCESS\n",1);
	exit(0);
} else {
	die "$testname: CondorTest::RunTest() failed\n";
}

#
# helper functions
#
sub emit_dag_files {
	my $dag_content = "CONFIG ${testname}.cfg
Job C_A ${testname}_A.cmd
SCRIPT PRE C_A ${testname}_A_pre.sh
SCRIPT POST C_A ${testname}_A_post.sh
";

	my $config_content = "DAGMAN_ALWAYS_RUN_POST = False\n";

	my $pre_script_sh_content = "#! /usr/bin/env sh\necho \"PRE C_A running\"\nexit 1\n";
	my $pre_script_bat_content = "\@echo PRE C_A running\n\@exit /b 1\n";
	my $post_script_sh_content = "#! /usr/bin/env sh\necho \"POST C_A running\"\nexit 0\n";
	my $post_script_bat_content = "\@echo POST C_A running\n\@exit /b 0\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(".cfg", $config_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); }
}
