#! /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 $testname = "cmd_status_shows-master";
my $debuglevel = 2;

print scalar localtime() . "\n";

my $mypid = $$;

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

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

my $outsource = CondorCmdStatusWorker2::SetUp("cmd_status_shows-master");
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 $done = 0;
my $stable = CondorCmdStatusWorker2::IsSystemStable();
my @adarray = (); 
my $nummaster = 0;

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 -claim finds exactly 4
	my $nattempts = 8;
	my $count = 0;
	print "Looking for 2 masters with condor_status -master - ";
	while($count < $nattempts) {
		my $cmd = "condor_status -master";
		CondorTest::debug("Looking for exactly 2 masters\n",$debuglevel);
		my $cmdstatus = CondorTest::runCondorTool($cmd,\@adarray,2);
    	if(!$cmdstatus)
    	{
        	CondorTest::debug("Test failure due to Condor Tool Failure<$cmd>\n",$debuglevel);
			last;
    	}
	
		# 2 nodes 2 masters
	
		$nummaster = 0;
		foreach my $line (@adarray) {
			if($line =~ /^\s*master_.*@.*$/) {
				CondorTest::debug("found master: $line\n",$debuglevel);
				$nummaster = $nummaster + 1;;
				CondorTest::debug("found master slot: $nummaster of 2\n",$debuglevel);
			} else {
				#print "skip: $line\n";
			}
		}

		if($nummaster == 2) {
			CondorTest::debug("Condor_status -master found the expected 2 masters\n",$debuglevel);
			$done = 1;
			last;
		} else {
			CondorTest::debug("Condor_status -master found incorrect number of masters<$nummaster>\n",$debuglevel);
		}

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


if($done == 1) {
	print "ok\n";
} else {
	print "bad\n";
}


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

#fails due to directory that matches symbol

my $res = 1;
my @rm1 = ();
my @rm2 = ();
$ENV{CONDOR_CONFIG} = $locconfig;
$res = runCondorTool("condor_rm -all",\@rm1,2,{emit_output=>0});
$ENV{CONDOR_CONFIG} = $scheddoneconfig;
$res = runCondorTool("condor_rm -all",\@rm2,2,{emit_output=>0});

print scalar localtime() . "\n";

if( $done == 0 )
{
	# actual test failed but we allowed cleanup
	RegisterResult(0,"test_name", "$testname");
	CondorTest::EndTest();
	exit(1);
} else {
	RegisterResult(1,"test_name", "$testname");
	CondorTest::EndTest();
	CondorTest::debug("cmd_status_shows-mmaster SUCCESS\n",$debuglevel);
}
exit(0);
