#! /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;

$cmd = 'job_core_hold_java.cmd';
$testdesc =  'Condor allows submit with hold - java U';
$testname = "job_core_hold_java";

my $javainit = CondorTest::JavaInitialize($testname);
if($javainit == 0) {
	die "No java found\n";
}

my $killedchosen = 0;

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

$abnormal = sub {
	my %info = @_;

	die "Want to see $killsig in this file $outfile\n";
};

$aborted = sub {
	my %info = @_;
	my $done;
	CondorTest::debug("Abort event expected as we clear job\n",1);
};

$held = sub {
	my %info = @_;
	my $done;
	CondorTest::debug("Held event expected\n",1);
	my $cluster = $info{"cluster"};
	die "We don't hit Hold state for job started in hold!\n";
};

$executed = sub
{
	my %args = @_;
	my $cluster = $args{"cluster"};

	die "We should never hit execute state but go to hold state\n";
	sleep 5;
};

$submitted = sub
{
	my %args = @_;
	my $cluster = $args{"cluster"};

	CondorTest::debug("submitted: We should never hit execute state but go to hold state\n",1);
	CondorTest::debug("It better be on hold...",1);
	my $qstat = CondorTest::getJobStatus($cluster);
	while($qstat == -1)
	{
		CondorTest::debug("Job status unknown - wait a bit\n",1);
		sleep 2;
		$qstat = CondorTest::getJobStatus($cluster);
	}
	if($qstat != HELD) 
	{
		die "Cluster $cluster failed to go on hold\n";
	} 
	else 
	{
		print "Good job is in held state\n";
		CondorTest::debug("good\n",1);
		my @adarray;
		my $status = 1;
		my $cmd = "condor_rm $cluster";
		$status = CondorTest::runCondorTool($cmd,\@adarray,2,{emit_output=>0});
		if(!$status)
		{
			CondorTest::debug("Test failure due to Condor Tool Failure<$cmd>\n",1);
			exit(1)
		}
	}
	sleep 5;
};

CondorTest::RegisterExecute($testname, $executed);
CondorTest::RegisterExitedAbnormal( $testname, $abnormal );
CondorTest::RegisterAbort( $testname, $aborted );
CondorTest::RegisterHold( $testname, $held );
CondorTest::RegisterSubmit( $testname, $submitted );

if( CondorTest::RunTest($testname, $cmd, 0) ) {
	CondorTest::debug("$testname: SUCCESS\n",1);
	exit(0);
} else {
	die "$testname: CondorTest::RunTest() failed\n";
}

