#! /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 makes sure that node status file works correctly.

use CondorTest;
use CondorUtils;

$cmd = 'job_dagman_node_status.dag';
$testdesc =  'Condor submit dag - node-status test - scheduler U';
$testname = "job_dagman_node_status";
$dagman_args = "-verbose";

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

@outfiles = ("job_dagman_node_status.status",
	"job_dagman_node_status.status-A",
	"job_dagman_node_status.status-C");

# Lines must be found in order.
@outlines = ("DagStatus = 5;.*STATUS_DONE \\(success\\)",
	"NodesTotal = 12;",
	"NodesDone = 12;",
	"NodesPre = 0;",
	"NodesQueued = 0;",
	"NodesPost = 0;",
	"NodesReady = 0;",
	"NodesUnready = 0;",
	"NodesFailed = 0;",
	"JobProcsHeld = 0;",
	"JobProcsIdle = 0;",
	"Node = \"A\";",
	"NodeStatus = 5;.*STATUS_DONE",
	"Node = \"B1\";",
	"NodeStatus = 5;.*STATUS_DONE",
	"Node = \"B2\";",
	"NodeStatus = 5;.*STATUS_DONE",
	"Node = \"B3\";",
	"NodeStatus = 5;.*STATUS_DONE",
	"Node = \"B4\";",
	"NodeStatus = 5;.*STATUS_DONE",
	"Node = \"B5\";",
	"NodeStatus = 5;.*STATUS_DONE",
	"Node = \"B6\";",
	"NodeStatus = 5;.*STATUS_DONE",
	"Node = \"B7\";",
	"NodeStatus = 5;.*STATUS_DONE",
	"Node = \"B8\";",
	"NodeStatus = 5;.*STATUS_DONE",
	"Node = \"B9\";",
	"NodeStatus = 5;.*STATUS_DONE",
	"Node = \"B10\";",
	"NodeStatus = 5;.*STATUS_DONE",
	"Node = \"C\";",
	"NodeStatus = 5;.*STATUS_DONE");
@outlinesA = ("DagStatus = 3;.*STATUS_SUBMITTED \\(\\)",
	"NodesTotal = 12;",
	"NodesDone = 0;",
	"NodesPre = 0;",
	"NodesQueued = 1;",
	"NodesPost = 0;",
	"NodesReady = 0;",
	"NodesUnready = 11;",
	"NodesFailed = 0;",
	"JobProcsHeld = 0;",
	"JobProcsIdle = [01];",# Not sure if we can rely on job being idle
	"Node = \"A\";",
	"NodeStatus = 3;.*STATUS_SUBMITTED",
	"Node = \"B1\";",
	"NodeStatus = 0;.*STATUS_NOT_READY",
	"Node = \"B2\";",
	"NodeStatus = 0;.*STATUS_NOT_READY",
	"Node = \"B3\";",
	"NodeStatus = 0;.*STATUS_NOT_READY",
	"Node = \"B4\";",
	"NodeStatus = 0;.*STATUS_NOT_READY",
	"Node = \"B5\";",
	"NodeStatus = 0;.*STATUS_NOT_READY",
	"Node = \"B6\";",
	"NodeStatus = 0;.*STATUS_NOT_READY",
	"Node = \"B7\";",
	"NodeStatus = 0;.*STATUS_NOT_READY",
	"Node = \"B8\";",
	"NodeStatus = 0;.*STATUS_NOT_READY",
	"Node = \"B9\";",
	"NodeStatus = 0;.*STATUS_NOT_READY",
	"Node = \"B10\";",
	"NodeStatus = 0;.*STATUS_NOT_READY",
	"Node = \"C\";",
	"NodeStatus = 0;.*STATUS_NOT_READY");
@outlinesC = ("DagStatus = 3;.*STATUS_SUBMITTED \\(\\)",
	"NodesTotal = 12;",
	"NodesDone = 11;",
	"NodesPre = 0;",
	"NodesQueued = 1;",
	"NodesPost = 0;",
	"NodesReady = 0;",
	"NodesUnready = 0;",
	"NodesFailed = 0;",
	"JobProcsHeld = 0;",
	"JobProcsIdle = [01];",# Not sure if we can rely on job being idle
	"Node = \"A\";",
	"NodeStatus = 5;.*STATUS_DONE",
	"Node = \"B1\";",
	"NodeStatus = 5;.*STATUS_DONE",
	"Node = \"B2\";",
	"NodeStatus = 5;.*STATUS_DONE",
	"Node = \"B3\";",
	"NodeStatus = 5;.*STATUS_DONE",
	"Node = \"B4\";",
	"NodeStatus = 5;.*STATUS_DONE",
	"Node = \"B5\";",
	"NodeStatus = 5;.*STATUS_DONE",
	"Node = \"B6\";",
	"NodeStatus = 5;.*STATUS_DONE",
	"Node = \"B7\";",
	"NodeStatus = 5;.*STATUS_DONE",
	"Node = \"B8\";",
	"NodeStatus = 5;.*STATUS_DONE",
	"Node = \"B9\";",
	"NodeStatus = 5;.*STATUS_DONE",
	"Node = \"B10\";",
	"NodeStatus = 5;.*STATUS_DONE",
	"Node = \"C\";",
	"NodeStatus = 3;.*STATUS_SUBMITTED");

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");
	}
}

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

$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;
		$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.*$/ ) {
				$passed = 1;
				CondorTest::debug("$name: $line\n",1);
			}
		}
		close(OUT);
		if($passed == 1 ) {
			;
		} else {
			CondorTest::debug("failed $name: $line\n",1);
			$diditpass = 0;
		}
	}

	# Check the node status files.
	CheckStatusFile("job_dagman_node_status.status", \@outlines);
	CheckStatusFile("job_dagman_node_status.status-A", \@outlinesA);
	CheckStatusFile("job_dagman_node_status.status-C", \@outlinesC);

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

sub CheckStatusFile {
	$sfile = shift(@_);
	@slines = @{shift(@_)};

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

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

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";
}

