#! /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.
##
##**************************************************************

# This tests that having a global event log does not mess up DAGman

use Condor;
use CondorTest;
use CondorUtils;

$testname = "job_dagman_global_event_log-A";

#testreq: personal
my $config = <<CONDOR_TESTREQ_CONFIG;
    DAEMON_LIST = MASTER,SCHEDD,COLLECTOR,NEGOTIATOR,STARTD
    ALL_DEBUG =
    NEGOTIATOR_INTERVAL = 5
	EVENT_LOG = $(LOG)/EventLog
CONDOR_TESTREQ_CONFIG
#endtestreq

$testdesc =  'Condor submit dag - Global event log - scheduler U';
$cmd = 'job_dagman_global_event_log-A.dag';
$userlog = "job_dagman_global_event_log-A.log";
$dagman_args = "-verbose";

$outfile = "job_dagman_global_event_log-A.nodes.out";

my $killedchosen = 0;

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

# Get rid of any existing output files.
if (-e $outfile) {
	runcmd("rm -f $outfile");
}

$abnormal = sub 
{
	die "Want to see only submit, execute and successful completion\n";
};

$aborted = sub 
{
	die "Abort event NOT expected\n";
};

$held = sub 
{
	die "Held event NOT expected\n";
};

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

	CondorTest::debug("Good. We need the dag to run<$info{cluster}>\n",1);
};

$submitted = sub
{
	CondorTest::debug("submitted: This test will see submit, executing and successful completion\n",1);
};

$success = sub
{
	
	CondorTest::debug("executed successfully\n",1);
	CondorTest::debug("Verifying output\n",1);
	$diditpass = 1;
	
	my $global_logfile = `condor_config_val EVENT_LOG`;
	@globallog = split "\n", `condor_wait -echo:ISO_DATE $global_logfile`;
	@userlog = split "\n", `condor_wait -echo:ISO_DATE $userlog`;
	for($i=0;$i<=$#globallog;++$i){
		last if($globallog[$i] eq $userlog[0]);
	}
	++$i;
	for($j=1;$j<=$#userlog && $i<=$#globallog;++$j){
		++$i while($i<=$#globallog && ($globallog[$i] ne $userlog[$j]));
		last if($i>$#globallog);
	}
	die "Userlog not contained in global log\n"  if($j<=$#userlog);
};

CondorTest::RegisterExitedSuccess( $testname, $success );
CondorTest::RegisterExecute($testname, $executed );
CondorTest::RegisterExitedAbnormal( $testname, $abnormal );
CondorTest::RegisterAbort( $testname, $aborted );
CondorTest::RegisterHold( $testname, $held );
CondorTest::RegisterSubmit( $testname, $submitted );

# Ensure results aren't fouled by previous runs.
unlink $userlog;
if( CondorTest::RunDagTest($testname, $cmd, 0, $dagman_args) ) {
	CondorTest::RegisterResult(1,"test_name",$testname);
	CondorTest::debug("$testname: SUCCESS\n",1);
	CondorTest::EndTest();
	exit(0);
} else {
	CondorTest::RegisterResult(1,"test_name",$testname);
	CondorTest::EndTest();
	die "$testname: CondorTest::RunTest() failed\n";
}

