#! /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;
#Condor::DebugOn();

$cmd      = 'job_core_holdrelease_sched.cmd';
$testdesc =  'Jobs properly hold and release - scheduler U';
$testname = "job_core_holdrelease_sched";

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

my $alreadydone=0;
my $stat=0;

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

	# avoid reentrant death
	if($alreadydone != 0) {
		return true;
	} else {
		$alreadydone = 1;
		#CondorTest::RegisterTimed($testname, $timed, 600);
	}
	
	CondorTest::debug("Putting cluster $cluster on hold\n",1);
	my @adarray;
	my $status = 1;
	my $cmd = "condor_hold $cluster";
	$status = CondorTest::runCondorTool($cmd,\@adarray,2);
	if($status == 0)
	{
		CondorTest::debug("Test failure due to Condor Tool Failure<$cmd>\n",1);
		exit(1)
	}

};

$abort = sub {
	CondorTest::debug("Job is gone now, cool.\n",1);
};

my $on_evictedwithoutcheckpoint = sub {
	CondorTest::debug("Evicted Without Checkpoint from removing sleep 0 jobs.\n",1);
};


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

	CondorTest::debug("Good. Cluster $cluster is supposed to be held.....\n",1);
	CondorTest::debug("Releasing cluster $cluster\n",1);
	my @adarray;
	my $status = 1;
	my $cmd = "condor_release $cluster";
	$status = CondorTest::runCondorTool($cmd,\@adarray,2);
	if($status == 0)
	{
		CondorTest::debug("Test failure due to Condor Tool Failure<$cmd>\n",1);
		exit(1)
	}
	# must reschedule to get released job out of Inactive and into Running
	$cmd = "condor_reschedule";
	$stat = CondorTest::runCondorTool($cmd,\@adarray,2);
	if($stat == 0)
	{
		CondorTest::debug("Test failed - could not reschedule\n",1);
		exit(1);
	}
};

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

	CondorTest::debug("Good hold and release both worked! removing job\n",1);
	$cmd = "condor_rm $cluster";
	$stat = CondorTest::runCondorTool($cmd,\@adarray,2);
	if($stat == 0)
	{
		CondorTest::debug("Test worked but runCondorTool $cmd failed\n",1);
		exit(0);
	}
};

$success = sub
{
	die "Hold/Release/Remove/Abort is expected event trail!\n";
};

$timed = sub
{
	die "Job should have ended by now. Hold/Realease broken!\n";
};

CondorTest::RegisterEvictedWithoutCheckpoint($testname, $on_evictedwithoutcheckpoint);
CondorTest::RegisterExecute($testname, $execute);
CondorTest::RegisterAbort($testname, $abort);
CondorTest::RegisterHold($testname, $hold);
CondorTest::RegisterRelease($testname, $release);
CondorTest::RegisterExitedSuccess($testname, $success);

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

