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

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

#**************************************************************
#
# Job to test always running post.
# We want to make sure that RETRY works correctly
#
#**************************************************************

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

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 success on completion\n",1);
};

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

$success = sub 
{
	CondorTest::debug("Execution was successful.\n",1);
	CondorTest::debug("Verifying output in $outfile\n",1);
	$diditpass = 1;
	$canopen = open OUT,"<$outfile";
	if( ! (defined $canopen)) {
		CondorTest::debug("Could not open outfile<$outfile>:$!\n",1);
		$diditpass = 0;
	}
	my @outfilelines = <OUT>;
	close(OUT);
	$passed1 = 0; # Let's be optimistic
	$passed2 = 0;
	$passed3 = 0;
	$passed4 = 0;
	$passed5 = 0;
	foreach (@outfilelines) {
		chomp;
		if(/Retrying node E_A/) {
			CondorTest::debug("Saw retry statement: $_\n",1);
			$passed1 = 1;
			next;
		}
		if(/Running POST script of Node E_A/) {
			CondorTest::debug("Saw POST script E_A run!\n",1);
			$passed2 = 1;
			next;
		}
		if(/PRE Script of node E_A completed successfully/) {
			CondorTest::debug("PRE script succeeded eventually as expected.\n");
			$passed3 = 1;
			next;
		}
		if(/Submitting HTCondor Node E_A job/){
			CondorTest::debug("Condor Node E_A job submitted eventually, as expected.\n");
			$passed4 = 1;
			next;
		}
		if(/POST Script of node E_A completed successfully/) {
			CondorTest::debug("POST script of E_A completed successfully, as expected.\n");
			$passed5 = 1;
			next;
		}
	}
	if ($passed1 != 1) {
		CondorTest::debug("Missing retry\n",1);
		$diditpass = 0;
	}
	if ($passed2 != 1) {
		CondorTest::debug("Missing E_A POST script run\n",1);
		$diditpass = 0;
	}
	if ($passed3 != 1) {
		CondorTest::debug("Missing E_A PRE script success\n",1);
		$diditpass = 0;
	}
	if ($passed4 != 1) {
		CondorTest::debug("Missing E_A job submission\n",1);
		$diditpass = 0;
	}
	if ($passed5 != 1) {
		CondorTest::debug("Missing E_A POST script success\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) ) {
	CondorTest::debug("$testname: SUCCESS\n",1);
	exit(0);
} else {
	die "$testname: CondorTest::RunTest() failed\n";
}
