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

$cmd = 'job_core_matchlist_van.cmd';
$testdesc =  'Test match_list_length - vanilla U';
$testname = "job_core_matchlist_van";
my $counter = 5;
my $lmn1 = "";
my $nextlmn1 = "";



$ExitSuccess = sub {
	my %args = @_;
	my $cluster = $args{"cluster"};
	CondorTest::debug("Saw job exit!<<$counter>>\n",1);
	$counter -= 1;
	if( $counter == 0 )
	{
		my @adarray;
		my $status = 1;
		my $cmd = "condor_rm $cluster";
		$status = CondorTest::runCondorTool($cmd,\@adarray,2);
		if(!$status)
		{
			CondorTest::debug("Test failure due to Condor Tool Failure<$cmd>\n",1);
			exit(1)
		}
	}
};

$evicted = sub {
	my %args = @_;
	my $cluster = $args{"cluster"};
	CondorTest::debug("Saw job evicted!<<$counter>>\n",1);
	$counter -= 1;
	CondorTest::debug("counter now $counter\n",1);
	if( $counter == 0 )
	{
		CondorTest::debug("Counter 0 so removing cluster\n",1);
		my @adarray;
		my $status = 1;
		my $cmd = "condor_rm $cluster";
		$status = CondorTest::runCondorTool($cmd,\@adarray,2);
		if(!$status)
		{
			CondorTest::debug("Test failure due to Condor Tool Failure<$cmd>\n",1);
			exit(1)
		}
	}
};

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

	CondorTest::debug("Saw job executing! Crunching ads\n",1);
	my $lmn0 = "";
	my $remote = "";
	# get host for test via RemoteHost from job ad
	my @adarray;
	my $status = 1;
	my $cmd = "condor_q -l $cluster";
	$status = CondorTest::runCondorTool($cmd,\@adarray,2);
	if(!$status)
	{
		CondorTest::debug("Test failure due to Condor Tool Failure<$cmd>\n",1);
		exit(1)
	}
	foreach $line (@adarray)
	{
		my $line = "";
		CondorUtils::fullchomp($_);
		$line = $_;
		if( $line =~ /^RemoteHost.*/ )
		{
			CondorTest::debug("$line\n",1);
		}
		elsif( $line =~ /^\s*LastMatchName0\s*=\s*\"([@\.a-zA-Z_0-9]+)\"\s*$/ )
		{
			CondorTest::debug("Setting lmn0 to be $1\n",1);
			$lmn0 = $1;
			CondorTest::debug("$line\n",1);
			CondorTest::debug("LastMatchName0 is <<<< $lmn0 >>>>\n",1);
		}
		elsif( $line =~ /^\s*LastMatchName1\s*=\s*\"([@\.a-zA-Z_0-9]+)\"\s*$/ )
		{
			CondorTest::debug("Setting lmn1 to be $1\n",1);
			$lmn1 = $1;
			CondorTest::debug("$line\n",1);
			CondorTest::debug("Last Match 1 <<<< $lmn1 >>>>\n",1);
		}
		elsif( $line =~ /LastMatchName.*/ )
		{
			CondorTest::debug("LM: $line\n",1);
		}
		elsif( $line =~ /^\s*MATCH_name\s*=\s*\"([@\.a-zA-Z_0-9]+)\"\s*$/ )
		{
			$remote = $1;
			CondorTest::debug("$line\n",1);
			CondorTest::debug("Setting Remote as <<<< $remote >>>>\n",1);
		}
		#else
		#{
			#print "ELSE: $line\n";
		#}
	}
	if( ($nextlmn1 ne "") && ($lmn1 ne "") && ($lmn1 ne $nextlmn1) )
	{
		CondorTest::debug("rotating failed.... $lmn1 ne $nextlmn1\n",1);
	}
	if( $lmn0 ne $remote )
	{
		CondorTest::debug("$lmn0 = lmn0\n",1);
		CondorTest::debug("$remote = remote\n",1);
		CondorTest::debug("LastMatchName out of sync with current Match\n",1);
	}
	CondorTest::debug("Setting nextlmn1 to be lmn0($lmn0)\n",1);
	$nextlmn1 = $lmn0;
};

$aborted = sub
{
	if($counter == 0)
	{
		CondorTest::debug("Abort expected after job removal!\n",1);
	}
	else
	{
		die "Unexpected Abort!\n";
	}
};

runcmd("rm job_core_matchlist.data");
runcmd("touch job_core_matchlist.data");

CondorTest::RegisterAbort( $testname, $aborted );
CondorTest::RegisterEvicted($testname, $evicted);
CondorTest::RegisterExecute($testname, $execute);
CondorTest::RegisterExitedSuccess( $testname, $ExitSuccess );

if( CondorTest::RunTest($testname, $cmd, 0) ) {
	CondorTest::debug("$testname: SUCCESS\n",1);
	exit(0);
} else {
	die "$testname: CondorTest::RunTest() failed\n";
}

