#! /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.
##
##**************************************************************

## This test checks various combinations of DAG node retries (with
## and without PRE and POST scripts).

use CondorTest;
use CondorUtils;

$cmd = 'job_dagman_retry-B.dag';
$testdesc =  'Condor submit dag - retry B test - scheduler U';
$testname = "job_dagman_retry-B";
$dagman_args = "-verbose";

@logfiles = ("job_dagman_retry-B.dag.dagman.out");

my $killedchosen = 0;

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

# Get rid of any existing log files.
foreach $name (@logfiles) {
	if (-e $name) {
		runcmd("rm -f $name");
	}
}

# Set up files to make node jobs fail the first time.
runcmd("touch job_dagman_retry-B-nodeC.fail");
runcmd("touch job_dagman_retry-B-nodeD.fail");

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

$success = sub
{
	CondorTest::debug("executed successfully\n",1);
	CondorTest::debug("Verifying output\n",1);
	$diditpass = 1;

	foreach $name (@logfiles) {
		$line = "";
		$passed = 0;
		$retryA1 = 0;
		$retryA2 = 0;
		$retryA3 = 0;
		$retryB1 = 0;
		$retryB2 = 0;
		$retryC1 = 0;
		$retryD1 = 0;
		$retryE1 = 0;
		$retryF1 = 0;
		$canopen = open(OUT,"<$name");
		if( ! (defined $canopen)) {
			CondorTest::debug("Can not open logfile<$name>:$!\n",1);
			$diditpass = 0;
		}
		while(<OUT>) {
			chomp();
			$line = $_;
			if( $line =~ /^.*Retrying node NodeA \(retry #1 of 3\).*$/ ) {
				$retryA1 = 1;
				CondorTest::debug("$name: $line\n",1);
			}
			if( $line =~ /^.*Retrying node NodeA \(retry #2 of 3\).*$/ ) {
				$retryA2 = 1;
				CondorTest::debug("$name: $line\n",1);
			}
			if( $line =~ /^.*Retrying node NodeA \(retry #3 of 3\).*$/ ) {
				$retryA3 = 1;
				CondorTest::debug("$name: $line\n",1);
			}
			if( $line =~ /^.*Retrying node NodeB \(retry #1 of 2\).*$/ ) {
				$retryB1 = 1;
				CondorTest::debug("$name: $line\n",1);
			}
			if( $line =~ /^.*Retrying node NodeB \(retry #2 of 2\).*$/ ) {
				$retryB2 = 1;
				CondorTest::debug("$name: $line\n",1);
			}
			if( $line =~ /^.*Retrying node NodeC \(retry #1 of 1\).*$/ ) {
				$retryC1 = 1;
				CondorTest::debug("$name: $line\n",1);
			}
			if( $line =~ /^.*Retrying node NodeD \(retry #1 of 1\).*$/ ) {
				$retryD1 = 1;
				CondorTest::debug("$name: $line\n",1);
			}
			if( $line =~ /^.*Retrying node NodeE \(retry #1 of 1\).*$/ ) {
				$retryE1 = 1;
				CondorTest::debug("$name: $line\n",1);
			}
			if( $line =~ /^.*Retrying node NodeF \(retry #1 of 1\).*$/ ) {
				$retryF1 = 1;
				CondorTest::debug("$name: $line\n",1);
			}
			if( $line =~ /^.*EXITING WITH STATUS 0.*$/ ) {
				$passed = 1;
				CondorTest::debug("$name: $line\n",1);
			}
		}
		close(OUT);
		if($retryA1 != 1 ) {
			CondorTest::debug("Missing NodeA retry 1\n",1);
			$diditpass = 0;
		}
		if($retryA2 != 1 ) {
			CondorTest::debug("Missing NodeA retry 2\n",1);
			$diditpass = 0;
		}
		if($retryA3 != 1 ) {
			CondorTest::debug("Missing NodeA retry 3\n",1);
			$diditpass = 0;
		}
		if($retryB1 != 1 ) {
			CondorTest::debug("Missing NodeB retry 1\n",1);
			$diditpass = 0;
		}
		if($retryB2 != 1 ) {
			CondorTest::debug("Missing NodeB retry 2\n",1);
			$diditpass = 0;
		}
		if($retryC1 != 1 ) {
			CondorTest::debug("Missing NodeC retry 1\n",1);
			$diditpass = 0;
		}
		if($retryD1 != 1 ) {
			CondorTest::debug("Missing NodeD retry 1\n",1);
			$diditpass = 0;
		}
		if($retryE1 != 1 ) {
			CondorTest::debug("Missing NodeE retry 1\n",1);
			$diditpass = 0;
		}
		if($retryF1 != 1 ) {
			CondorTest::debug("Missing NodeF retry 1\n",1);
			$diditpass = 0;
		}
		if($passed != 1 ) {
			CondorTest::debug("DAG failed $name\n",1);
			$diditpass = 0;
		}
	}

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

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::RunDagTest() failed\n";
}

