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

$testname = "job_schedd_restart-runningjobs-ok";

#testreq: personal
my $config = <<CONDOR_TESTREQ_CONFIG;
    DAEMON_LIST = MASTER,SCHEDD,COLLECTOR,NEGOTIATOR,STARTD
    WANT_SUSPEND = FALSE
    ALL_DEBUG = D_FULLDEBUG
    NUM_CPUS = 6
    use FEATURE : StaticSlots
CONDOR_TESTREQ_CONFIG
#endtestreq

# where am I running
$currenthost = CondorTest::getFqdnHost();

TLOG "$testname test started\n";


my $alreadydone=0;
$beforequeue = "job_schedd_restart-runningjobs.before";
$afterqueue = "job_schedd_restart-runningjobs.after";

my $count = 0;
my $on_execute = sub {

	my %args = @_;
	my $cluster = $args{"cluster"};
	my $doneyet = "no";

	$count = $count + 1;
	if($count == 6) {

		CondorTest::debug("submitted\n",1);
		CondorTest::debug("Collecting queue details on $cluster\n",1);
		my @adarray;
		my $status = 1;
		my $cmd = "condor_q $cluster";
		$status = CondorTest::runCondorTool($cmd,\@adarray,2);
		if(!$status)
		{
			CondorTest::debug("Test failure due to Condor Tool Failure<$cmd>\n",1);
			exit(1)
		}

		open(BEFORE,">$beforequeue") || die "Could not open file for before stats $!\n";
		foreach my $line (@adarray)
		{
			CondorTest::debug("$line\n",1);
			print BEFORE "$line";
		}
		close(BEFORE);

		$status = CondorTest::changeDaemonState( "schedd", "off", 9 );
		if(!$status)
		{
			CondorTest::debug("Test failure: could not turn scheduler off!\n",1);
			exit(1)
		}

		#$status = CondorTest::changeDaemonState( "startd", "off", 9 );
		#if(!$status)
		#{
			#CondorTest::debug("Test failure: could not turn scheduler off!\n",1);
			#exit(1)
		#}

		$status = CondorTest::changeDaemonState( "schedd", "on", 9 );
		if(!$status)
		{
			CondorTest::debug("Test failure: could not turn scheduler on!\n",1);
			exit(1)
		}

		CondorTest::debug("Collecting queue details on $cluster\n",1);
		my @fdarray;
		my $status = 1;
		my $cmd = "condor_q $cluster";
		$status = CondorTest::runCondorTool($cmd,\@fdarray,2);
		if(!$status)
		{
			CondorTest::debug("Test failure due to Condor Tool Failure<$cmd>\n",1);
			exit(1)
		}

		print "Here follows queue after restart of schedd:\n";
		open(AFTER,">$afterqueue") || die "Could not ope file for before stats $!\n";
		foreach my $line (@fdarray)
		{
			print AFTER "$line\n";
			print "AFTER:$line";
		}
		close(AFTER);

		print "Here follows queue before restart of schedd:\n";
		open(BEFORE,"<$beforequeue") or die "Can not open:$beforequeue:$!\n";
		while(<BEFORE>) {
			print "BEFORE:$_";
		}
		close(BEFORE);

		# Now compare the state of the job queue
		my $afterstate;
		my $beforestate;
		my $allgood = 0;
		my $af1, $af3;
		my $bf1, $bf3;
		foreach $before (@adarray) {
			#print "Before:$before\n";
			$after = shift(@fdarray);
			#print "After:$after\n";
			#handle submitter line
			if($after =~ /^(-- Submitter.*<\d+\.\d+\.\d+\.\d+:)(\d+)(> : .*)$/) {
				$af = $1 . $3;
				if($before =~ /^(-- Submitter.*<\d+\.\d+\.\d+\.\d+:)(\d+)(> : .*)$/) {
					$bf = $1 . $3;
				} else {
					print "Submitter lines from before and after don't line up\n";
					RegisterResult(0,"test_name",$testname);
					$allgood++;
				}
				if($af ne $bf ) {
					print "Submitter lines from before and after don't match\n";
					RegisterResult(0,"test_name",$testname);
					$allgood++;
				}
			} 
			#handle job line
			elsif($after =~ /^\s*(\d+\.\d+\s+[\w+]+\s+\d+\/\d+\s+\d+:\d+\s+\d+\+\d+:\d+:\d+)\s+([HRI]+)\s+(\d+\s+\d+\.\d+\s+.*)$/) {
				CondorTest::debug("AF:$1 $2 $3\n",1);
				$afterstate = $2;
				if($before =~ /^\s*(\d+\.\d+\s+[\w+]+\s+\d+\/\d+\s+\d+:\d+\s+\d+\+\d+:\d+:\d+)\s+([HRI]+)\s+(\d+\s+\d+\.\d+\s+.*)$/) {
					CondorTest::debug("BF:$1 $2 $3\n",1);
					$beforestate = $2;
				} else {
					print "jobs in after restart of schedd do not parse\n";
					RegisterResult(0,"test_name",$testname);
					$allgood++;
				}
				if($afterstate ne $beforestate) {
					print "Jobs before and after expected to show running:$afterstate/$beforestate\n";
					RegisterResult(0,"test_name",$testname);
					$allgood++;
				} elsif(($af1 ne $bf1) || ($af3 ne $bf3)) {
					print "Entire rest of job info should be the same\n";
					print "$af1/$bf1\n";
					print "$af3/$bf3\n";
					RegisterResult(0,"test_name",$testname);
					$allgood++;
				} else {
					print "Job queue was maintained! jobs still shown as running\n";
					RegisterResult(1,"test_name",$testname);
				}
			}
			#handle label line
			else {
				print "$after hit dont care location, ignored\n";
				# don't care
			}
		}
		if($allgood == 0) {
			#$status = CondorTest::changeDaemonState( "startd", "on", 9 );
        	#if(!$status)
        	#{
            	#CondorTest::debug("Test failure: could not turn scheduler off!\n",1);
            	#exit(1)
        	#}
		}
	}
};

my $on_success = sub {
	print "Job done\n";
};

my $on_submit = sub {
};

my $on_disconnect = sub {
};

my $on_evictedwithoutcheckpoint = sub {
	print "We should not need this callback\n";
};

my $on_reconnected = sub {
};

my $appended_submit_info = '
';

$result = SimpleJob::RunCheck(
    append_submit_commands => $appended_submit_info,
    queue_sz => 6,
    duration => 90,
    on_execute => $on_execute,
    on_disconnected => $on_disconnect,
	on_evictedwithoutcheckpoint => $on_evictedwithoutcheckpoint,
    on_reconnected => $on_reconnected,
    on_success => $on_success,
    on_submit => $on_submit,
);

CondorTest::EndTest();
exit(0);
