#! /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.
##
##**************************************************************

use CondorTest;
use CondorUtils;

$cmd = 'job_dagman_fullremove.dag';
$testdesc =  'Condor submit dag - full cleanup of dag test - scheduler U';
$testname = "job_dagman_fullremove";
$dagman_args = "-v";

my $killedchosen = 0;

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

my $numsubmitted = 0;
my $numaborted = 0;

system("printenv");
my $joblog;

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

$aborted = sub 
{
	$numaborted += 1;
	my $tries = 4; #Used to be a 10 try now try 4 5 second tries
	CondorTest::debug("Abort event expected <$numaborted>\n",1);
	print "Abort event expected <$numaborted>\n";

	my $result = 0;
	if($numsubmitted == $numaborted) {
		while($tries > 0) {
			#my $result = system("/bin/sleep 5 && ./x_read_joblog.exe $joblog FULLREMOVE");
			sleep(5);
			if(is_windows()) {
				$result = system("x_read_joblog.exe $joblog FULLREMOVE");
			} else {
				$result = system("/bin/sleep 5 && ./x_read_joblog.exe $joblog FULLREMOVE");
			}
			if( $result != 0)
			{
			} else {
				$tries -= 1;
				last;
			}

		}
		if( $result != 0)
		{
			die "Not all dag nodes removed!\n";
		}
		CondorTest::debug("x_read_joblog verified all removed!\n",1);
	}
};

$held = sub 
{
	die "Held event NOT expected\n";
};

$executed = sub
{
	CondorTest::debug("Good. We need the remove the dag to test full removal\n",1);
	my %info = @_;
	my $cluster = $info{cluster};
	# Allow time for the jobs to actually start.
	sleep 30;
	my @adarray;
	my $status = 1;
	my $cmd = "condor_rm $cluster";
	$status = CondorTest::runCondorTool($cmd,\@adarray,2);
	if(!$status)
	{
		CondorTest::debug("Test failure due to Condor Tool Failure<$cmd>\n",1);
		exit(1)
	}
};

$submitted = sub
{
	$numsubmitted += 1;
	print "submit event expected <$numsubmitted>\n";
	$joblog = "job_dagman_fullremove.log";
	CondorTest::debug("submitted: This test will see submit, executing and abort\n",1);
};

$success = sub
{
	die "executed successfully not supposed to happen\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::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";
}
