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

print "Starting $testname.run at " .  scalar localtime() . " PID=$$\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-any");
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 = 1;
my $stable = CondorCmdStatusWorker2::IsSystemStable();
my @adarray = (); 
my $numstartd = 0;

my $numany = 0;
my %discoveries = ();
my @expectations = ("Submitter", "Negotiator", "DaemonMaster", "Scheduler", "Slot");

if($stable == 1) {
	print "Looking for 5 Ad types - ";
	# process one of each ad type and then compare with expected list
	my $cmd = "condor_status -any -format \"%s:\" MyType -format \"%s\\n\" Name";
	CondorTest::debug("Looking at -any($cmd)\n",$debuglevel);
	my $cmdstatus = CondorTest::runCondorTool($cmd,\@adarray,2);
    if(!$cmdstatus)
    {
        CondorTest::debug("Test failure due to Condor Tool Failure<$cmd>\n",$debuglevel);
		$done = 1;
    } else {
	
		foreach my $line (@adarray) {
			CondorTest::debug("-any: $line\n",$debuglevel);
			if( $line =~ /^(\w+):(.*)$/ ) {
				my $mytype = $1; if ($mytype eq "Machine") {$mytype = "Slot";}
				if( !(exists $discoveries{$mytype})) {
					print "Adding ($mytype/$2) to hash\n";
					$discoveries{$mytype} = $2;
				} else {
					#print "($mytype) already found....\n";
				}
			}
		}

		foreach my $search (@expectations) {
			# it had best be in the hash!
			if(! (exists $discoveries{$search})) {
				print "bad\n";
				$done = 0;
				print "Expected ad type \"$search\" not discovered(Failure of Test)\n";
				last;
			} else {
				print "Expected Ad Type ($search) found!\n";
			}
		}
	}

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

if($done == 0) {
	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 "Exiting $testname.run at " .  scalar localtime() . " PID=$$ success=$done\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-any SUCCESS\n",$debuglevel);
}
exit(0);
