#! /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 tests recovery of Pegasus-generated sub-DAGs (see gittrac
## #3882, #3890).

## Note: this is a bit of a tricky test -- the node C job actually
## puts DAGMan on hold and then releases it to force it into recovery
## mode.  (The DAG for this test isn't really generated by Pegasus; but
## the important thing is that the .condor.sub file doesn't have the
## -CsdVersion argument; that goofed up the pre-8.0.3 check for
## whether we should fall back to non-workflow log mode.)

use CondorTest;
use CondorUtils;
use Data::Dumper;

$cmd = 'job_dagman_pegasus_recovery.dag.condor.sub';
$testdesc =  'Condor submit dag - retry-recovery test - scheduler U';
$testname = "job_dagman_pegasus_recovery";
$dagman_args = "-verbose";

@logfiles = ("job_dagman_pegasus_recovery.dag.dagman.out");

my $dag_was_held = 0;
my $dag_was_released = 0;

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

# Create the real .condor.sub file.
GenerateSubFile();

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

# Get rid of any existing rescue DAGs.
runcmd("rm -f job_dagman_pegasus_recovery.dag.rescue*");

# So nodeA and nodeB will work, but only once.
runcmd("touch job_dagman_pegasus_recovery-nodeA.works");
runcmd("touch job_dagman_pegasus_recovery-nodeB.works");

$abnormal = sub 
{
	my %info = @_;
	CondorTest::debug("Got Abnormal job exit:\n");
	print Dumper($info);
	die "Abnormal exit was NOT expected - aborting test\n";
};

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

$held = sub 
{
	CondorTest::debug("DAGMan was held -- good!\n");
	$dag_was_held = 1;
};

$released = sub 
{
	CondorTest::debug("DAGMan was released -- good!\n");
	$dag_was_released = 1;
};

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

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

$submitted = sub
{
	my %info = @_;
	CondorTest::debug("DAG $info{cluster} submitted:\n\tWe should see submit, executing, held, released, and successful completion\n",1);
};

$success = sub
{
	if (!$dag_was_held) {
		die "Error: DAGMan was not held\n";
	}
	if (!$dag_was_released) {
		die "Error: DAGMan was not released\n";
	}

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

	foreach $name (@logfiles) {
		$line = "";
		$passed = 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 =~ /EXITING WITH STATUS 0/ ) {
				CondorTest::debug("$name: $line\n",1);
				$passed = 1;
			}
		}
		close(OUT);
		if($passed == 1 ) {
			;
		} else {
			CondorTest::debug("failed $name: $line\n",1);
			$diditpass = 0;
		}
	}

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

my $on_evictedwithoutcheckpoint = sub {
	CondorTest::debug("Evicted Without Checkpoint from removing jobs.\n",1);
};

CondorTest::RegisterEvictedWithoutCheckpoint($testname, $on_evictedwithoutcheckpoint);
CondorTest::RegisterExitedSuccess( $testname, $success);
CondorTest::RegisterExecute($testname, $executed);
CondorTest::RegisterExitedAbnormal( $testname, $abnormal );
CondorTest::RegisterAbort( $testname, $aborted );
CondorTest::RegisterHold( $testname, $held );
CondorTest::RegisterRelease( $testname, $released );
CondorTest::RegisterSubmit( $testname, $submitted );

# Note that we're doing RunTest here instead of RunDagTest, because
# we need to use the "special" .condor.sub file we generated in
# this script, instead of having condor_submit_dag generate the
# .condor.sub file in the normal way.
if( CondorTest::RunTest($testname, $cmd, 0) ) {
	CondorTest::debug("$testname: SUCCESS\n",1);
	exit(0);
} else {
	die "$testname: CondorTest::RunDagTest() failed\n";
}

sub GenerateSubFile {
	$template_file = "job_dagman_pegasus_recovery.dag.condor.sub_template";
	$submit_file = "job_dagman_pegasus_recovery.dag.condor.sub";
	$dagman_path;
	if (CondorUtils::is_windows()) {
		$dagman_path = `where condor_dagman`;
		CondorUtils::fullchomp($dagman_path);
	} else {
		$dagman_path = `which condor_dagman`;
		chomp($dagman_path);
	}

	open(TEMPL, "<$template_file") or die "Can't open template file $template_file\n";
	@template = <TEMPL>;
	close(TEMPL);

	open(SUB, ">$submit_file") or die "Can't open submit file $submit_file\n";
	foreach (@template) {
		s/condor_dagman/$dagman_path/;
		print SUB "$_";
	}
	close(SUB);
}
