#! /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_abort-final-B.dag';
$testdesc =  'Condor submit dag - basic abort-final-dag-on test - scheduler U';
$testname = "job_dagman_abort-final-B";
$dagman_args = "-verbose";

@outfiles = ("job_dagman_abort-final-B-node.A", "job_dagman_abort-final-B-node.B");
$statusfile = "job_dagman_abort-final-B.status";
# Lines must be found in order.
@statuslines = ("DagStatus = 5;.*STATUS_DONE \\(success\\)",
    "NodesTotal = 5;",
    "NodesDone = 3;",
    "NodesPre = 0;",
    "NodesQueued = 0;",
    "NodesPost = 0;",
    "NodesReady = 0;",
    "NodesUnready = 0;",
    "NodesFutile = 1",
    "NodesFailed = 1;",
    "JobProcsHeld = 0;",
    "JobProcsIdle = 0;",
	"Node = \"A\";",
	"NodeStatus = 5;.*STATUS_DONE",
	"Node = \"B\";",
	"NodeStatus = 5;.*STATUS_DONE",
	"Node = \"C\";",
	"NodeStatus = 6;.*STATUS_ERROR",
	"Node = \"D\";",
	"NodeStatus = 7;.*STATUS_FUTILE",
	"Node = \"E\";",
	"NodeStatus = 5;.*STATUS_DONE");
$finalfile = "job_dagman_abort-final-B-nodeE.out";
@finallines = ("DAG_STATUS=3",
	"FAILED_COUNT=1",
	"E");

my $killedchosen = 0;

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

# clean out old output and status files
foreach $name (@outfiles) {
	runcmd("rm -f $name.out $name.err");
}
runcmd("rm -f $statusfile");
runcmd("rm -f $finalfile");

$abnormal = sub 
{
	die "Error: Want to see only submit, execute and successful completion\n";
};

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

$held = sub 
{
	die "Error: 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 (@outfiles) {
		$line = "";
		$passed = 0;
		$canopen = open(OUT,"<$name.out");
		if( ! (defined $canopen)) {
			CondorTest::debug("Can not open outfile<$name.out>:$!\n",1);
			$diditpass = 0;
		}
		while(<OUT>) {
			chomp();
			$line = $_;
			if( $line =~ /^.*OK/ ) {
				$passed = 1;
				CondorTest::debug("$name.out: $line\n",1);
			}
		}
		close(OUT);
		if($passed == 1 ) {
			;
		} else {
			CondorTest::debug("failed $name.out: $line\n",1);
			$diditpass = 0;
		}
	}

	# Check the final node output file.
	$line = "";
	$passed = 0;
	$canopen = open(FINAL,"<$finalfile");
	if( ! (defined $canopen)) {
		CondorTest::debug("Can not open finalfile<$finalfile>:$!\n",1);
		$diditpass = 0;
	}
	while(<FINAL>) {
		chomp();
		$line = $_;
		for ( $ind = 0; $ind <= $#finallines; $ind++ ) {
			$target = $finallines[$ind];
			if ($target eq "") {
				last;
			}
			if( $line =~ /$target/ ) {
				CondorTest::debug("$name: $line\n",1);
				# Remove the target we just found from the array.
				splice(@finallines, $ind, 1);
				last;
			}
		}
	}
	close(FINAL);

	if ($#finallines >= 0) {
		CondorTest::debug("failed missing final node output lines:\n");
		foreach $missing (@finallines) {
			print "  $missing\n";
		}
		$diditpass = 0;
	}

	# Check the node status file.
	$line = "";
	$passed = 0;
	$canopen = open(STATUS,"<$statusfile");
	if( ! (defined $canopen)) {
		CondorTest::debug("Can not open statusfile<$statusfile>:$!\n",1);
		$diditpass = 0;
	}
	while(<STATUS>) {
		chomp();
		$line = $_;
		$target = $statuslines[0];
		if( $line =~ /$target/ ) {
			CondorTest::debug("$name: $line\n",1);
			# Remove the target we just found from the array.
			shift @statuslines;
			if ($#statuslines < 0) {
				last;
			}
		}
	}
	close(STATUS);

	if ($#statuslines >= 0) {
		CondorTest::debug("failed missing node status lines:\n");
		foreach $missing (@statuslines) {
			print "  $missing\n";
		}
		$diditpass = 0;
	}

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

$failure = sub
{
	die "Error: DAG is not expected to fail!\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 "Error: $testname: CondorTest::RunTest() failed\n";
}

