#! /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;
use Check::SimpleJob;
use strict;
use warnings;

my $testname = "cmd_q_shows-hold";
my $debuglevel = 2;

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

my $alreadydone=0;
my @adarray;

my $ClusterId = 0;

my $GetClusterId = sub {
	my $cid = shift;
	$ClusterId = $cid;
	print "Resquestd Cluster Got $ClusterId\n";
	##########################
	#
	# Convoluted: This function is paased to RunCheck which uses it in RunTest in the fourth
	# position which is always an undefined variable EXCEPT when it is a call back routine 
	# to pass out the cluster Id.
	#
	##########################
	#runcmd("condor_q",{emit_output => 1});
};

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

my $submit = sub
{
	my $header = 0;
	my $reason = 0;

	print "Job $ClusterId is supposed to be on hold.\n";
	print "See if condor_q -hold $ClusterId says this -  ";
	CondorTest::debug("Good. Cluster $ClusterId is supposed to be held.....\n",$debuglevel);
	my $status = 1;
	my $cmd = "condor_q -hold $ClusterId";
	$status = CondorTest::runCondorTool($cmd,\@adarray,2);
	if(!$status)
	{
		CondorTest::debug("Test failure due to Condor Tool Failure<$cmd>\n",$debuglevel);
		print "bad\n";
		exit(1)
	}
	foreach my $line (@adarray)
	{
		CondorTest::debug("$line\n",$debuglevel);
		if( $line =~ /.*HOLD_REASON.*/ )
		{
			CondorTest::debug("Got HOLD_REASON header line.......\n",$debuglevel);
			$header = 1;
		}
		if( $line =~ /.*submitted on hold.*/ )
		{
			CondorTest::debug("Yup reason is submitted on hold.......\n",$debuglevel);
			$reason = 1;
		}
	}

	print "ok\n";
	if( $reason == 1 && $header == 1 )
	{
		
		print "Releasing cluster $ClusterId - ";
		CondorTest::debug("Good. Cluster $ClusterId needs to be released.....\n",$debuglevel);
		CondorTest::debug("Releasing cluster $ClusterId\n",$debuglevel);
		$status = 1;
		$cmd = "condor_release $ClusterId";
		$status = CondorTest::runCondorTool($cmd,\@adarray,2);
		if(!$status)
		{
			print "bad\n";
			CondorTest::debug("Test failure due to Condor Tool Failure<$cmd>\n",$debuglevel);
			exit(1)
		}
		print "ok\n";
		#system("condor_q");
	}
	else
	{
		CondorTest::debug("Test failed because HOLD header and expected reason were not found\n",$debuglevel);
		exit(1);
	}

};

my $release = sub
{

	print "Cluster released - removing it - ";
	CondorTest::debug("Good hold and release both worked! removing job\n",$debuglevel);
	my @adarray;
	my $status = 1;
	my $cmd = "condor_rm $ClusterId";
	$status = CondorTest::runCondorTool($cmd,\@adarray,2);
	if(!$status)
	{
		print "bad\n";
		CondorTest::debug("Test failure due to Condor Tool Failure<$cmd>\n",$debuglevel);
		exit(1)
	}
	print "ok\n";
	#system("condor_q");
};

my $hold = sub
{
	print "Hold event seen\n";
};

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

my $runresult = SimpleJob::RunCheck(
	duration => 3600,
	on_abort => $abort,
	on_hold => $hold,
	hold => "True",
	on_submit => $submit,
	on_success => $success,
	on_released => $release,
	GetClusterId => $GetClusterId,
);


