#! /usr/bin/env perl
#testreq: personal
##**************************************************************
##
## 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 strict;
use warnings;

my $cmd = 'cmd_q_shows-better-analyze.cmd';
my $cmdout = $cmd . ".out";
my $testdesc =  'cmd_q_shows-better-analyze  ';
my $testname = "cmd_q_shows-better-analyze";
my $debuglevel = 2;
my $cmdstatus;

my $test_failed = 0;

my $whoami = `whoami`;
chomp($whoami);


my @adarray;

my $submit = sub {
	my %info = @_;

	my $cluster = $info{'cluster'};;
	CondorTest::debug("Job submitted, cluster is $cluster\n",$debuglevel);

	my $foundnegotiator = 0;
	my $foundstartd = 0;
	my $wait_time = 0;
	my $wait_max = 60;
	my $wait_interval = 5;
	my @statarray = ();
	
	print "Have cluster - $cluster\n";

	CondorTest::debug("Checking for negotiator and startd ads\n",$debuglevel);

	print "Do we have a negotiator and a startd advertised - ";
	# Wait for the negotiator and startd to advertise themselves
	while ( ( !$foundnegotiator || !$foundstartd ) && $wait_time < $wait_max ) {
		if ( !$foundnegotiator ) {
			@statarray = ();
			CondorTest::runCondorTool("condor_status -negotiator -autoformat MyAddress Name",\@statarray,0,{emit_output=>0});
			foreach my $line (@statarray) {
				if( $line =~ /^<\[[:0-9A-Za-z]+]:\d+.*$/ || $line =~ /^<\d+\.\d+\.\d+\.\d+:\d+.*$/ ) {
					$foundnegotiator = 1;
				}
			}
		}

		if ( !$foundstartd ) {
			@statarray = ();
			CondorTest::runCondorTool("condor_status -startd -autoformat MyAddress Name",\@statarray,0,{emit_output=>0});
			foreach my $line (@statarray) {
				if( $line =~ /^<\[[:0-9A-Za-z]+]:\d+.*$/ || $line =~ /^<\d+\.\d+\.\d+\.\d+:\d+.*$/ ) {
					#print "Got a sinful string for startd:$line\n";
					$foundstartd = 1;
				}
			}
		}

		if ( $foundnegotiator && $foundstartd ) {
			last;
		}

		sleep $wait_interval;
		$wait_time += $wait_interval;
	}

	if ( !$foundnegotiator || !$foundstartd ) {
		print "bad\n";
		CondorTest::debug("Negotiator or startd still missing after $wait_max seconds of waiting!\n",$debuglevel);
		die "No sense going on\n";
	}
	print "ok\n";

	run_analyze("-better-analyze", $cluster);
	run_analyze("-better-analyse", $cluster);
	run_analyze("-analyze", $cluster);
	run_analyze("-analyse", $cluster);

	$cmd = "condor_rm $cluster";
	$cmdstatus = CondorTest::runCondorTool($cmd,\@adarray,2);
	print "ok\n";
};

# pass in the argument: -better-analyze, -analyze, or possibly the analyse varients
sub run_analyze {
	my($arg, $cluster) = @_;
	my $foundcluster = 0;
	my $foundanalyze = 0;
	my $cmd = "condor_q $arg $cluster";
	print "Execute: $cmd - ";

	$cmdstatus = CondorTest::runCondorTool($cmd,\@adarray,2);
	if(!$cmdstatus)
	{
		print "bad\n";
		CondorTest::debug("Test failure due to Condor Tool Failure<$cmd>\n",$debuglevel);
		$test_failed = 1;
		exit(1)
	}
	print "ok\n";

	CondorTest::debug("Examining $cmd output...\n",$debuglevel);

	print "Looking at command output - ";
	foreach my $line (@adarray) {
		chomp($line);
		if($line =~ /^\s*(\d+)\.(\d+):\s*Run\s+analysis\s+summary\.\s+.*$/) {
				CondorTest::debug("Good: found cluster $cluster:\n",$debuglevel);
				CondorTest::debug("  $line\n",$debuglevel);
				$foundcluster = 1;
		}
		if($line =~ /^\s*The\s+Requirements\s+expression\s+for\s+your\s+job\s+is:\s*.*$/) {
				CondorTest::debug("Good: found -better-analyze output:\n",$debuglevel);
				CondorTest::debug("  $line\n",$debuglevel);
				$foundanalyze = 1;
		}
	}


	if( !$foundcluster | !$foundanalyze ) {
		print "bad\n";
		CondorTest::debug("Failed to find expected output from $cmd\n",$debuglevel);
		return 0;
		$test_failed = 1;
	}
	return 1;
}

my $aborted = sub {
	CondorTest::debug("Job aborted\n",$debuglevel);
	if ( $test_failed ) {
		CondorTest::debug("Test Failed!\n",$debuglevel);
	} else {
		CondorTest::debug("Test Succeeded!\n",$debuglevel);
	}
	exit($test_failed);
};

my $exited = sub {
	CondorTest::debug("Our job shouldn't have run, failing...\n",$debuglevel);
	exit(1);
};

my $timed = sub
{
    die "Job should have ended by now. condor_q -better-analyze broken!\n";
};

CondorTest::RegisterSubmit( $testname, $submit );
CondorTest::RegisterAbort($testname, $aborted);
CondorTest::RegisterExited( $testname, $exited );

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

