#! /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 test only tests to see if we can parse a dagfile containing many
# splices. It does not test the running of it--only the parsing of it.

use CondorTest;
use Data::Dumper;

$cmd = 'job_dagman_splice-scaling.dag';
$testdesc =  'Splice Inclusion Scaling Test';
$testname = "job_dagman_splice-scaling";
$dagman_args = "-DumpRescue -verbose";
$max_splices = 10000;

# 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 
{
	die "Held event NOT expected - aborting test\n";
};

$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, and successful completion\n",1);
};

$success = sub
{
	# This test only checks to see if condor_dagman exited with 0 after
	# it dumps the rescue dag of the parsed splices. It does not check to
	# see if the rescue dag is actually correct.
	CondorTest::debug("executed successfully\n",1);
};

# create the submitting dag file
CondorUtils::runcmd("rm -f $cmd");
CondorUtils::runcmd("rm -f $cmd.rescue*");

print "Generating dag file $cmd with $max_splices splices...";
open(DOUT, ">$cmd") or die "Can't open dagfile '$cmd': $!";
print DOUT<<END;
# This dag file is autogenerated by the program $testname.run.
# It's purpose is to test how many splices can be included into a dag. It
# ensure that we don't hit limits in the code such as described by gittrac
# ticket #870.
#
# In this test we include $max_splices splices--this value is configurable in
# the test.
#
# There are simple dependencies between the splices. We simply see if we can
# write a rescue dag properly after dagman loads the dag file. If so, then
# dagman was able to parse it (and the splices) without hitting any limits.
#

END

# generate the splice directives
for($i = 0; $i < $max_splices; $i++) {
	print DOUT "SPLICE S$i job_dagman_splice-scaling-splice.dag\n";
}

# generate parent/child relationship to see if it causes anything serious
# to explode in memory usage or anything.
for($i = 0; $i < $max_splices-1; $i++) {
	$n = $i + 1;
	print DOUT "PARENT S$i CHILD S$n\n";
}
close(DOUT);
print "DONE\n";

# set up the callbacks to the event handlers..
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::RunTest() failed\n";
}

