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

my $testname = "job_core_perrelease_sched";

#testreq: personal
my $config = <<CONDOR_TESTREQ_CONFIG;
    DAEMON_LIST = MASTER,SCHEDD,COLLECTOR,NEGOTIATOR,STARTD
	PERIODIC_EXPR_interval = 30
    NUM_CPUS = 2
    NEGOTIATOR_INTERVAL = 5
CONDOR_TESTREQ_CONFIG
#endtestreq

##
## Status Values
##
sub IDLE{1};
sub HELD{5};
sub RUNNING{2};

my $variation = "perreleasefalse";

my %info;
my $cluster;

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

	if($variation eq "perreleasefalse") {
		CondorTest::debug("Bad. This job tests for policy to periodically release NEVER! Can't run!\n",1);
		RegisterResult(0, "test_name", "$testname");
	} else {
		CondorTest::debug("Periodic Release should see an execute, followed by a release and then we reschedule the job\n",1);
	}
};

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

	if($variation eq "perreleasefalse") {
		CondorTest::debug("Bad. This job tests for policy to periodically release NEVER! Can't complete!\n",1);
		RegisterResult(0, "test_name", "$testname");
	} else {
		CondorTest::debug("Success: ok\n",1);
		RegisterResult(1, "test_name", "$testname");
	}
};

$release = sub
{
	CondorTest::debug("Release expected.........\n",1);
	my @adarray;
	my $status = 1;
	my $cmd = "condor_reschedule";
	$status = CondorTest::runCondorTool($cmd,\@adarray,2,{emit_output=>0});
	if(!$status)
	{
		CondorTest::debug("Test failure due to Condor Tool Failure<$cmd>\n",1);
		RegisterResult(0, "test_name", "$testname");
	}
};

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

	CondorTest::debug("Cluster $cluster alarm wakeup\n",1);
	CondorTest::debug("wakey wakey!!!!\n",1);
	CondorTest::debug("good\n",1);

	print "Cluster:$cluster should still be on hold\n";

	my $qstat = CondorTest::getJobStatus($cluster);
	while($qstat == -1)
	{
		CondorTest::debug("Job status unknown - wait a bit\n",1);
		sleep 2;
		$qstat = CondorTest::getJobStatus($cluster);
	}

	CondorTest::debug("It better be on hold... status is $qstat(5 is correct)",1);
	if($qstat != HELD)
	{
		CondorTest::debug("Cluster $cluster failed to stay on hold\n",1);
		RegisterResult(0, "test_name", "$testname");
		return(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);
		RegisterResult(0, "test_name", "$testname");
	}
	sleep 5;
};

$submit = sub
{
	my %info = @_;
	$cluster = $info{"cluster"};
	my $qstat = CondorTest::getJobStatus($cluster);
	while($qstat == -1)
	{
		CondorTest::debug("Job status unknown - wait a bit\n",1);
		sleep 2;
		$qstat = CondorTest::getJobStatus($cluster);
	}

	CondorTest::debug("It better be on hold... status is $qstat(5 is correct)",1);
	if($qstat != HELD)
	{
		CondorTest::debug("Cluster $cluster failed to go on hold\n",1);
		RegisterResult(0, "test_name", "$testname");
	}


	CondorTest::debug("Cluster $cluster submitted\n",1);
};

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

	if($variation eq "perreleasefalse") {
		CondorTest::debug("Cluster $cluster aborted after hold state verified\n",1);
		RegisterResult(1, "test_name", "$testname");
	} else {
		CondorTest::debug("Abort event not expected!\n",1);
		CondorTest::debug("Want to see only submit, release and successful completion events for periodic release test\n",1);
		RegisterResult(0, "test_name", "$testname");
	}
};

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

	CondorTest::debug("Held event not expected.....\n",1);
	RegisterResult(0, "test_name", "$testname");
};

my $falsepolicy = '
	periodic_release = (time() - QDate) < 0
';

my $truepolicy = '
	periodic_release = (time() - QDate) > 3
';

if(is_windows()) {
	# FALSE
	SimpleJob::RunCheck(
		universe=>"scheduler",
		hold=>"True",
		runthis=>"sleep.exe",
		append_submit_commands=>$falsepolicy,
		duration=>"10s",
	    on_submit=>$submit,
	    on_execute=>$executed,
	    on_abort=>$abort,
	    on_success=>$success,
		alt_timed=>$timed,
		timeout=>60,
	);


	$variation = "perreleasetrue";

	# TRUE
	SimpleJob::RunCheck(
		universe=>"scheduler",
		hold=>"True",
		runthis=>"sleep.exe",
		append_submit_commands=>$truepolicy,
		duration=>"7s",
	    on_release=>$release,
	    on_execute=>$executed,
	    on_hold=>$held,
	    on_success=>$success,
	);
} else {
	# FALSE
	SimpleJob::RunCheck(
		hold=>"True",
		universe=>"scheduler",
		append_submit_commands=>$falsepolicy,
		duration=>10,
	    on_submit=>$submit,
	    on_execute=>$executed,
	    on_success=>$success,
	    on_abort=>$abort,
		alt_timed=>$timed,
		timeout=>60,
	);


	$variation = "perreleasetrue";

	# TRUE
	SimpleJob::RunCheck(
		hold=>"True",
		universe=>"scheduler",
		append_submit_commands=>$truepolicy,
		duration=>7,
	    on_release=>$release,
	    on_execute=>$executed,
	    on_hold=>$held,
	    on_success=>$success,
	);
}

CondorTest::EndTest();
print scalar localtime() . "\n";
my $result = 0;

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

CondorTest::debug("$testname SUCCESS\n",1);
exit(0);
