#! /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 a "large" DAG (size is mostly limited by having it
## run within a reasonable amount of time).  (See gittrac #614.)

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

# Set up a directory to contain all of the files in this test.
$dir = "job_dagman_large_dag_files";
if (!-e $dir) {
	mkdir $dir;
}
chdir $dir;
unlink <*>;
#symlink("job_dagman_large_dag-node.pl", ".");
#symlink("dummy_condor_submit.pl", ".");

# Create the DAG file, submit files, etc., to be used in this test.
# It would be nice to make the DAG even bigger, but that could take
# an awfully long time...
runcmd("../create_large_dag.pl job_dagman_large_dag 100 5 0 100");
# Only uncomment this to run manually (outside of the nightly tests).
# This will time out if run as part of the nightly tests.
#runcmd("../create_large_dag.pl job_dagman_large_dag 1000 10 0 1000");

$cmd = 'job_dagman_large_dag.dag';
$testdesc =  'Condor submit dag - large DAG test - scheduler U';
$testname = "job_dagman_large_dag";
$dagman_args = "-verbose";

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

my $dag_was_held = 0;
my $dag_was_released = 0;

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

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

$aborted = sub 
{
	die "Abort event 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 = "";
		$dag_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 =~ /Event: ULOG_SUBMIT for HTCondor Node job_dagman_large_dag-nodeA/ ) {
				CondorTest::debug("$name: $line\n",1);
				if( $dag_state == 0 ) {
					# Pre-recovery -- okay.
					$dag_state = 1;
				} elsif( $dag_state == 1 ) {
					$dag_state = 2;
				} else {
					CondorTest::debug("Got submit event for Node " .
								"job_dagman_large_dag-nodeA in " .
								"wrong sequence\n");
				}

			} elsif( $line =~ /Event: ULOG_JOB_TERMINATED for HTCondor Node job_dagman_large_dag-nodeC / ) {
				CondorTest::debug("$name: $line\n",1);
				if( $dag_state == 2 ) {
					$dag_state = 3;
				} else {
					CondorTest::debug("Got terminated event for Node " .
								"job_dagman_large_dag-nodeC  " .
					   			"in wrong sequence\n");
				}

			} elsif( $line =~ /EXITING WITH STATUS 0/ ) {
				CondorTest::debug("$name: $line\n",1);
				if( $dag_state == 3 ) {
					$passed = 1;
				} else {
					CondorTest::debug("Got EXITING line in wrong sequence\n");
				}
			}
		}
		close(OUT);
		if($passed == 1 ) {
			;
		} else {
			CondorTest::debug("failed $name: $line\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::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";
}

