#! /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_retry.dag';
$testdesc =  'Condor submit dag - retry test - scheduler U';
$testname = "job_dagman_retry";
$dagman_args = "-verbose";
# Tell DAGMan to write complete (old-style) rescue DAGs.
$ENV{_CONDOR_DAGMAN_WRITE_PARTIAL_RESCUE} = 0;

#	This test runs a single node which always fails.
#	We asked for three retries and after the last,
#	we will(had better see)have a rescue dag. In
#	that dag we will scrape for three retries having been made.

my $killedchosen = 0;

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

my $rescuedag = $cmd . ".rescue001";
my $newrescuedag = $cmd . ".rescue" . ".new";

my %subinfo;

$ENV{_CONDOR_DAGMAN_AUTO_RESCUE} = false;

$abnormal = sub 
{
	die "Want to see only submit, execute and termination from three failed retries\n";
};

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

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

$executed = sub
{
	CondorTest::debug("Good. We need the dag to run\n",1);
};

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

$success = sub
{
	%subinfo = @_;
	my $joblog = $subinfo{log};
	CondorTest::debug("Joblog is $joblog\n",1);
};

$failure = sub
{
	CondorTest::debug("DAG failed as expected\n",1);
	CondorTest::debug("Verifying output\n",1);

	%subinfo = @_;
	my $joblog = $subinfo{log};
	CondorTest::debug("Joblog is $joblog\n",1);

	if( -e $rescuedag )
	{
		my $line = "";
		CondorTest::debug("Good, a rescue dag was created.......\n",1);
		my $result = `./x_read_joblog.exe $joblog RETURNVALUE`;
		CondorTest::debug("Result of Dag is $result\n",1);
		CondorTest::debug("Checking for retries.....\n",1);
		open( RESCUE, "<$rescuedag");
		open( NEWRESCUE, ">$newrescuedag");
		# write out a rescue dag where node always failing has 0 inserted for return
		# value for post script.... Then run rescue dag.
		while(<RESCUE>)
		{
			CondorUtils::fullchomp($_);
			$line = $_;
			if( $line =~ /^#\s+(\d+)\s+of\s+(\d+)/ )
			{
				print NEWRESCUE "$line\n";
				if( $1 == $2 )
				{
					CondorTest::debug("$1 of $2 retries attempted.... Good\n",1);
				}
				else
				{
					die "Failed do do all requested retries... $1 of $2 done\n";
				}
			}

			if( $line =~ /(^.*)(\$RETURN)$/ )
			{
				print NEWRESCUE "$1 0\n";
			}
			else
			{
				print NEWRESCUE "$_\n";
			}
		}
		close(RESCUE);
		close(NEWRESCUE);
		runcmd("mv $newrescuedag $rescuedag");
		if( CondorTest::RunDagTest($testname, $rescuedag, 0, $dagman_args) ) {
			CondorTest::debug("$testname: SUCCESS\n",1);
			exit(0);
		} else {
			die "$testname: CondorTest::RunTest() failed\n";
		}
	}
	else
	{
		die "No rescue dag created from fail test $cmd\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";
}

