#! /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 the new "default node log file" feature (including
## making sure it works in recovery mode).

## 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.

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

$cmd = 'job_dagman_default_log.dag';
$testdesc =  'Condor submit dag - default log test - scheduler U';
$testname = "job_dagman_default_log";
$dagman_args = "-verbose";

@logfiles = ("job_dagman_default_log.dag.dagman.out");
$logdir = `condor_config_val LOG`;
chomp $logdir;
$nodelog = "$logdir/job_dagman_default_log.dag.test_node_log";

my $dag_was_held = 0;
my $dag_was_released = 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) {
	unlink $name if (-e $name);
}
unlink $nodelog if (-e $nodelog);

#
#sub print_core_file_info {
#	my $core = shift;
#	if (-s $core) {
#		print "\tfound core file: $core\n";
#		if (CondorUtils::is_windows()) {
#			system("type $core");
#		} else {
#			my $fileinfo = `file $core`;
#			print "\t$fileinfo";
#			my ($dagman) = $fileinfo =~ m/execfn:\s*'(.*)'/;
#			if (defined $dagman) {
#				system("echo bt | gdb --quiet $dagman $core");
#			}
#		}
#	}
#}

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

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

$evicted = sub
{
	my %info = @_;
	CondorTest::debug("Got Evicted with requeue:\n");
	print Dumper(\%info);
	foreach $core (glob "core.*") { CondorUtils::print_core_file_info($core); }
	die CondorUtils::TimeStr() . "Evicted with requeue was 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 = "";
		$cache_state = 0;
		$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;
		}
	}

	# Check for proper events in default node log file
	$passed = 1;
	$state = 0;
	$canopen = open(NODE_LOG,"<$nodelog");
	if( ! (defined $canopen)) {
		CondorTest::debug("Can not open node logfile<$nodelog>:$!\n",1);
		$diditpass = 0;
	}
	while(<NODE_LOG>) {
		chomp();
		$line = $_;

		if( $line =~ /DAG Node: NodeA/ ) {
			CondorTest::debug("$name: $line\n",1);
			if( $state == 0 or $state == 2 ) {
				$state++;
			} else {
				CondorTest::debug("Line in wrong sequence\n");
				$passed = 0;
			}
		} elsif( $line =~ /POST Script terminated/ ) {
			CondorTest::debug("$name: $line\n",1);
			if( $state == 1 ) {
				$state++;
			}
		} elsif( $line =~ /DAG Node: NodeB1/ ) {
			CondorTest::debug("$name: $line\n",1);
			if( $state == 3 ) {
				$state++;
			}
		} elsif( $line =~ /DAG Node: NodeB2/ ) {
			CondorTest::debug("$name: $line\n",1);
			if( $state == 4 ) {
				$state++;
			}
		}
	}
	close(NODE_LOG);
	if($passed == 1 and $state == 5) {
		;
	} else {
		CondorTest::debug("failed $name: $line (state $state)\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::RegisterEvictedWithRequeue( $testname, $evicted );
CondorTest::RegisterHold( $testname, $held );
CondorTest::RegisterRelease( $testname, $released );
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";
}

