#! /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 when DAGMan
## is condor_rm'ed.

use CondorTest;
use CondorUtils;

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

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

@outfiles = ("job_dagman_node_status_rm.status");

my $template = "job_dagman_node_status_rm_node.template";
my $nodesubmit = "job_dagman_node_status_rm_node.cmd";

# Lines must be found in order.
@outlines = ("DagStatus = 6;.*STATUS_ERROR \\(removed\\)",
	"NodesTotal = 1;",
	"NodesDone = 0;",
	"NodesPre = 0;",
	"NodesQueued = 0;",
	"NodesPost = 0;",
	"NodesReady = 0;",
	"NodesUnready = 0;",
	"NodesFailed = 1;",
	"JobProcsHeld = 0;",
	"JobProcsIdle = 0;",
	"Node = \"A\";",
	"NodeStatus = 6;.*STATUS_ERROR");

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

# node submit file varies for windows
system("ls -l $template");
open(TF,"<$template") or die "Failed to open:$template:$!\n";
open(NS,">$nodesubmit") or die "Failed to create:$nodesubmit :$!\n";
while (<TF>) {
	my $line = $_;
	fullchomp($line);
	if(CondorUtils::is_windows()) {
		if($line =~ /^\s*executable.*$/) {
			print NS "executable = c:\\perl64\\bin\\perl.exe\n";
		} elsif($line =~ /^\s*arguments.*$/) {
			print NS "arguments =  \"job_dagman_node_status_rm_node.pl A \$(DAGManJobId)\"\n";
		} else {
			print NS "$line\n";
		}
	} else {
		if($line =~ /^\s*executable.*$/) {
			print NS "executable = job_dagman_node_status_rm_node.pl\n";
		} elsif($line =~ /^\s*arguments.*$/) {
			print NS "arguments =  \"A \$(DAGManJobId)\"\n";
		} else {
			print NS "$line\n";
		}
	}
}
close(TF);
close(NS);

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

$aborted = sub 
{
	CondorTest::debug("DAG was aborted as expected\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 2.*$/ ) {
				$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_rm.status", \@outlines);

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

$held = sub 
{
	die "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
{
	die("saw  success: Die: DAG is expected to be aborted.\n");
};

$failure = sub
{
	die("saw exited failure: Die:DAG is expected to be aborted.\n");
};

$evicted = sub
{
	CondorTest::debug("saw eviction: DAG was evicted as expected\n",1);
};

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::RegisterExitedFailure( $testname, $failure);
CondorTest::RegisterExecute($testname, $executed);
CondorTest::RegisterExitedAbnormal( $testname, $abnormal );
CondorTest::RegisterAbort( $testname, $aborted );
CondorTest::RegisterEvictedWithoutCheckpoint( $testname, $evicted );
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";
}

