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

# Tests DAG halt/final node combination.

use CondorTest;
use CondorUtils;

$cmd = 'job_dagman_final-I.dag';
$testdesc =  'Condor submit dag - basic final node test I - scheduler U';
$testname = "job_dagman_final-I";
$dagman_args = "-verbose";

$outfile = "job_dagman_final-I.nodes.out";
$scriptfile = "job_dagman_final-I.scripts.out";
$rescuefile = "job_dagman_final-I.dag.rescue001";
$haltfile = "job_dagman_final-I.dag.halt";
$statusfile = "job_dagman_final-I.status";

# Note: we must find these lines *in the specified order*.
@node_output_lines = ("Job for node I_A",
			"  DAG_STATUS=0",
			"  FAILED_COUNT=0",
			"  OK done with I_A",
			"Job for node I_B",
			"  DAG_STATUS=0",
			"  FAILED_COUNT=0",
			"  Halting DAG",
			"  OK done with I_B",
			"Job for node I_D",
			"  DAG_STATUS=6",
			"  FAILED_COUNT=0",
			"  OK done with I_D");

# Note: we must find these lines *in the specified order*.
@script_output_lines = ("Node I_D Pre",
			"  DAG_STATUS=6",
			"  FAILED_COUNT=0",
			"  RETURN=-1",
			"Node I_D Post",
			"  DAG_STATUS=6",
			"  FAILED_COUNT=0",
			"  RETURN=0");

# 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 = 1;",
    "NodesFailed = 1;",
    "JobProcsHeld = 0;",
    "JobProcsIdle = 0;",
	"Node = \"I_A\";",
	"NodeStatus = 5;.*STATUS_DONE",
	"Node = \"I_B\";",
	"NodeStatus = 5;.*STATUS_DONE",
	"Node = \"I_C1\";",
	"NodeStatus = 0;.*STATUS_NOT_READY",
	"Node = \"I_C2\";",
	"NodeStatus = 6;.*STATUS_ERROR",
	"Node = \"I_D\";",
	"NodeStatus = 5;.*STATUS_DONE");

my $killedchosen = 0;

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

# Get rid of any existing output files.
if (-e $outfile) {
	runcmd("rm -f $outfile");
}
if (-e $scriptfile) {
	runcmd("rm -f $scriptfile");
}
if (-e $statusfile) {
	runcmd("rm -f $statusfile");
}
runcmd("rm -f job_dagman_final-I.dag.rescue*");

# Get rid of halt file if it exists.
if (-e $haltfile) {
	runcmd("rm -f $haltfile");
}

$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;

	# Make sure all expected lines are in the node output file, in the
	# correct order, and there aren't any "extra" lines.
	CondorTest::debug("Checking $outfile\n",1);
	open(CHECK, "<$outfile") ||
			die "Error: Cannot open $outfile: $!\n";
	@lines = <CHECK>;
	close(CHECK);
	$max = $#node_output_lines > $#lines ? $#node_output_lines : $#lines;
	for ($lineindex = 0; $lineindex <= $max; $lineindex++) {
		$line = $lines[$lineindex];
		chomp($line);
		if ($line eq $node_output_lines[$lineindex]) {
			CondorTest::debug("$line\n", 1);
		} else {
			$diditpass = 0;
			CondorTest::debug("Mismatch at line $lineindex: expected <$node_output_lines[$lineindex]>, got <$line>\n", 1);
		}
	}

	# Make sure all expected lines are in the script output file, in the
	# correct order, and there aren't any "extra" lines.
	CondorTest::debug("Checking $scriptfile\n",1);
	open(CHECK, "<$scriptfile") ||
			die "Error: Cannot open $scriptfile: $!\n";
	@lines = <CHECK>;
	close(CHECK);
	$max = $#script_output_lines > $#lines ? $#script_output_lines : $#lines;
	for ($lineindex = 0; $lineindex <= $max; $lineindex++) {
		$line = $lines[$lineindex];
		chomp($line);
		if ($line eq $script_output_lines[$lineindex]) {
			CondorTest::debug("$line\n", 1);
		} else {
			$diditpass = 0;
			CondorTest::debug("Mismatch at line $lineindex: expected <$script_output_lines[$lineindex]>, got <$line>\n", 1);
		}
	}

	# 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 (! -e $rescuefile) {
		CondorTest::debug("Expected rescue DAG $rescuefile does not exist\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";
}

