#! /usr/bin/env perl
##**************************************************************
##
## 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;

#testreq: personal

my $testname = "cmd_q_shows-cputime";
my $ClusterId = 0;
my $debuglevel = 2;

my @adarray;

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 $on_submit = sub {
	my %info = @_;
	my $cmd = "condor_q -cputime -nobatch";

	print "Running $cmd - ";
	my $cmdstatus = CondorTest::runCondorTool($cmd,\@adarray,2);
	if(!$cmdstatus)
	{
		print "bad\n";
		CondorTest::debug("Test failure due to Condor Tool Failure<$cmd>\n",$debuglevel);
		exit(1)
	}
	print "ok\n";

	my $submitline = "";
	my $cluster = "";

	print "My cluster is: $ClusterId\n";

	$cmd = "condor_rm $ClusterId";

	print "Looking for CPU_TIME - ";
	foreach my $line (@adarray) {
		chomp($line);
		if($line =~ /^\s*ID\s+OWNER\s+SUBMITTED\s+CPU_TIME\s+ST\s+PRI\s+SIZE\s+CMD\s*$/) {
				CondorTest::debug("Bingo\n",$debuglevel);
				$cmdstatus = CondorTest::runCondorTool($cmd,\@adarray,8);
				print "ok - Saw CPU_TIME label\n";
				return(0);
		} else {
			CondorTest::debug("<<<$line>>>\n",$debuglevel);
			#$cmdstatus = CondorTest::runCondorTool($cmd,\@adarray,8);
			#exit(1)
		}
	}

	$cmdstatus = CondorTest::runCondorTool($cmd,\@adarray,8);
	print "bad\n";
	exit(1);
};

my $aborted = sub {
	CondorTest::debug("If all is good we abort our own job.......\n",$debuglevel);
};

my $ExitSuccess = sub {
	my %info = @_;
};

my $on_evictedwithoutcheckpoint = sub {
};

my $runresult = SimpleJob::RunCheck(
	duration => 3600,
	on_abort => $aborted,
	on_submit => $on_submit,
	on_success => $ExitSuccess,
	on_evictedwithoutcheckpoint => $on_evictedwithoutcheckpoint,
	GetClusterId => $GetClusterId,
);


