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

my $testname = "job_core_perremove_local";

#testreq: personal
my $config = <<CONDOR_TESTREQ_CONFIG;
    DAEMON_LIST = MASTER,SCHEDD,COLLECTOR,NEGOTIATOR,STARTD
	PERIODIC_EXPR_INTERVAL = 15
    NEGOTIATOR_INTERVAL = 5
CONDOR_TESTREQ_CONFIG
#endtestreq

# which job am I
my $jobcount = 1;

$on_success = sub {
	print "exited well as expected\n";
	if($jobcount != 1) {
		print "only first job was supposed to be not removed\n";
		CondorTest::RegisterResult(0,"test_name", $testname);
	} else {
		CondorTest::RegisterResult(1,"test_name", $testname);
	}
};

$on_abort = sub {
	print "aborted as expected\n";
};

my $on_evictedwithoutcheckpoint = sub {
	print "Exited without checkpoint\n";
	if($jobcount != 2) {
		print "only second job was supposed to exit without a checkpint\n";
		CondorTest::RegisterResult(0,"test_name", $testname);
	} else {
		CondorTest::RegisterResult(1,"test_name", $testname);
	}
};


$testdesc =  'Condor PERIODIC_REMOVE policy test - local U';


my $append_submit_commands_false_rm = '
##
## The PERIODIC_EXPR_INTERVAL is set to 15 for tests so we need
## to make sure we sleep for just a little bit more than that
##
arguments  = 20
periodic_remove = (time() - QDate) < (0)
';


my $append_submit_commands_true_rm = '
##
## The PERIODIC_EXPR_INTERVAL is set to 15 for tests so we need
## to make sure we sleep for just a little bit more than that
##
arguments  = 20
periodic_remove = (time() - QDate) > 2
';

SimpleJob::RunCheck(
  append_submit_commands=>$append_submit_commands_false_rm,
  on_success => $on_success,
  universe => "local",
);

$jobcount += 1;
	
SimpleJob::RunCheck(
  append_submit_commands=>$append_submit_commands_true_rm,
  on_evictedwithoutcheckpoint => $on_evictedwithoutcheckpoint,
  on_abort => $on_abort,
  universe => "local",
);

CondorTest::EndTest();
