#! /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 CondorCmdStatusWorker2;
use strict;
use warnings;

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

my $testdesc =  'cmd_status_shows-claim';
my $testname = "cmd_status_shows-claim";
my $corename = "cmd_status_shows-claim";
my $debuglevel = 2;

print scalar localtime() . "\n";

my $mypid = $$;
my $mysaveme = $corename . ".saveme";

if($mypid == 0) {
	die "Saveme setup for test failed\n";
}

my $mysubmitnm = $corename . $mypid . ".cmd";
my $mysubmitnmlog = $corename . $mypid . ".log";
my $mysubmitnmout = $corename . $mypid . ".out";
my $mycorenm = $corename . ".cmd";
my $mycorenmout = $corename . ".cmd.out";
my $mycorenmlog = $corename . ".log";

CondorTest::debug("Master PID is $mypid\n",$debuglevel);
CondorTest::debug("Master PID submit file is $mysubmitnm\n",$debuglevel);


my $outsource = CondorCmdStatusWorker2::SetUp("cmd_status_shows-claim");
CondorUtils::fullchomp($outsource);

if($outsource eq "") {
	# assume all cleanup of personals which did start occurred in worker
	print "OOOOPPPPSSSSSSSSSSSSyyyyy\n";
	exit(1);
}

( my $locconfig, my $scheddoneconfig) = split('&',$outsource);
CondorTest::debug("Two pools are $locconfig and $scheddoneconfig\n",$debuglevel);

my $stable = CondorCmdStatusWorker2::IsSystemStable();
my $numclaimed = 0;
my $done = 0;
my @adarray;

if($stable == 1) {
	# allow time for all the nodes to update the collector
	# by allowing N attempts
	# with 4 run forever jobs we are not stable until -claimed finds exactly 4
	my $nattempts = 8;
	my $count = 0;

	print "Looking for at least 2 claimed slots - ";
	while($count < $nattempts) {
		my $cmd = "condor_status -claimed -format \"%s\\n\" name";
		CondorTest::debug("Looking for exactly 4 claimed slots\n",$debuglevel);
		my $cmdstatus = CondorTest::runCondorTool($cmd,\@adarray,2);
    	if(!$cmdstatus)
    	{
        	CondorTest::debug("Test failure due to Condor Tool Failure<$cmd>\n",$debuglevel);
			CondorTest::KillPersonal($locconfig);
			CondorTest::KillPersonal($scheddoneconfig);
        	exit(1)
    	}
	
		# 4 are busy so 4 should be claimed....
		# some could be evicted 2 or more is fine
	
		$numclaimed = 0;
		foreach my $line (@adarray) {
			if($line =~ /^\s*slot(\d+)[@]master.*@.*$/) {
				#print "found claimed slot: $line\n";
				$numclaimed = $numclaimed + 1;;
				CondorTest::debug("found claimed slot: $numclaimed of 4\n",$debuglevel);
			} else {
				#print "skip: $line\n";
			}
		}

		if($numclaimed >= 2) {
			CondorTest::debug("Condor_status -claimed found the expected 4 slots\n",$debuglevel);
			print "ok\n";
			$done = 1;
			last;
		} else {
			CondorTest::debug("Condor_status -claimed found incorrect claimed slot count<$numclaimed>\n",$debuglevel);
		}

		$count = $count + 1;
		sleep($count * 5);
	}
}

if($done == 0) {
	print "bad\n";
}

CondorTest::debug("Saving things and cleaning up....\n",$debuglevel);

# fails due to finding the directory of the same name.. 
runcmd("cp *cmd_status_shows-claim* $mysaveme", {expect_result=>\&ANY});

if( -e "$mysubmitnm" )
{
	runcmd("cp $mysubmitnm $mycorenm");
}

if( -e "$mysubmitnmout" )
{
	runcmd("cp $mysubmitnmout $mycorenmout");
}

if( -e "$mysubmitnmlog" )
{
	runcmd("cp $mysubmitnmlog $mycorenmlog");
}

CondorTest::KillPersonal($locconfig);
CondorTest::KillPersonal($scheddoneconfig);
print scalar localtime() . "\n";

if( $done == 0 )
{
	# actual test failed but we allowed cleanup
	exit(1);
}

CondorTest::debug("cmd_status_shows-claim SUCCESS\n",$debuglevel);
exit(0);
