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

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

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

my $alreadydone=0;
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 $execute = sub {
	if($alreadydone == 0) {
		$alreadydone = 1;
	} else {
		return(0);
	}

	print "ok\n";
	print "Cluster is - $ClusterId\n";
	CondorTest::debug("Good, we can't look for a running job without one\n",$debuglevel);
	my @adarray;
	my $running = "no";
	my $status = 1;
	my $cmd = "condor_q -run $ClusterId";
	print "Look for condor_q -run $ClusterId showing this as a running job - ";
	$status = CondorTest::runCondorTool($cmd,\@adarray,2,{emit_output=>0});
	if(!$status)
	{
		print "bad\n";
		CondorTest::debug("Test failure due to Condor Tool Failure<$cmd>\n",$debuglevel);
		$cmd = "condor_rm $ClusterId";
		$status = CondorTest::runCondorTool($cmd,\@adarray,2);
		exit(1)
	}
	foreach my $line (@adarray) {
		chomp($line);
		CondorTest::debug("$line\n",$debuglevel);
		if($line =~ /^.*$ClusterId.*$/) {
			CondorTest::debug("Cluster $ClusterId found running!\n",$debuglevel);
			$running = "yes";
			last;
		}
	}

	if($running eq "no") {
		print "bad\n";
		CondorTest::debug("Failed to find $ClusterId running with condor_q -run \n",$debuglevel);
		exit(1);
	} else {
		print "ok\n";
		$cmd = "condor_rm $ClusterId";
		print "Removing $ClusterId - ";
		$status = CondorTest::runCondorTool($cmd,\@adarray,2,{emit_output=>0});
		if(!$status)
		{
			print "bad\n";
			CondorTest::debug("Test failure due to Condor Tool Failure<$cmd>\n",$debuglevel);
			exit(1)
		}
		print "ok\n";
	}
};

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

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

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

print "Start a long runniing job - ";

my $runresult = SimpleJob::RunCheck(
	duration => 3600,
	on_abort => $abort,
	on_execute => $execute,
	on_success => $success,
	on_evictedwithoutcheckpoint => $on_evictedwithoutcheckpoint,
	GetClusterId => $GetClusterId,
);


