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


my $falsepolicy = '
on_exit_remove = (time() - QDate) > (2 )
';
my $truepolicy = '
on_exit_remove = (time() - QDate) > 4200
';


$testname = "job_core_onexitrem_van_false";

#my $nostatus = CondorTest::verbose_system("./job_core_onexitrem-false_van.pl");
my $killedchosen = 0;

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

my %testerrors;
my %info;
my $cluster;

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

	CondorTest::debug("Good. for on_exit_remove cluster $cluster must run first\n",1);
};

$success = sub
{
	my %info = @_;
	my $cluster = $info{"cluster"};
	my $job = $info{"job"};

	CondorTest::debug("Good, good job - $job - should complete trivially\n",1);
};

$abnormal = sub {

	CondorTest::debug("Want to see only submit and abort events for periodic remove test\n",1);
	exit(1);
};

$aborted = sub {
	CondorTest::debug("Abort event expected from expected condor_rm used to remove the requeueing job\n",1);
	CondorTest::debug("Policy test worked.\n",1);
};

$held = sub {
	die "We don't hit Hold state for job restarted for not running long enough!\n";
	exit(1);
};

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

	CondorTest::debug("Good a requeue.....after eviction....\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(0)
	}
};

print "\n\nOn Exit Remove false\n\n";

SimpleJob::RunCheck(
    runthis=>"x_time.pl",
    on_execute=>$executed,
    on_success=>$success,
    duration=>3,
    append_submit_commands=>$falsepolicy,
);

$testname = "job_core_onexitrem_van_true";

print "\n\nOn Exit Remove true\n\n";

SimpleJob::RunCheck(
    runthis=>"x_time.pl",
    on_evictedwithrequeue=>$evicted,
    on_execute=>$executed,
    on_exitedabnormal=>$abnormal,
    on_hold=>$held,
    duration=>3,
    on_abort=>$aborted,
    append_submit_commands=>$truepolicy,
);

CondorTest::EndTest();
print scalar localtime() . "\n";

if( $finalresult != 0 )
{
    exit(1);
}

print "\n\nOn Exit Remove true\n\n";
CondorTest::debug("$testname SUCCESS\n",1);
exit(0);



