#! /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 = "lib_eventlog-xml";

#testreq: personal
my $config = <<CONDOR_TESTREQ_CONFIG;
	DAEMON_LIST = MASTER,SCHEDD,COLLECTOR,NEGOTIATOR,STARTD
	EVENT_LOG = $(LOG)/EventLog
	MAX_EVENT_LOG = 1000000
	EVENT_LOG_USE_XML = true
	EVENT_LOG_JOB_AD_INFORMATION_ATTRS =  BillString, JobStatus, BillInt , BillBool , StreamErr
	ALL_DEBUG = D_FULLDEBUG
CONDOR_TESTREQ_CONFIG
#endtestreq

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

TLOG("$testname begins\n");

my $on_execute = sub {
};

my $on_success = sub {
};

my $on_submit = sub {
};


my $appended_submit_info = '
    +BillString = "Good"
	+BillInt = 69
	+BillBool = true
	';

$result = SimpleJob::RunCheck(
append_submit_commands => $appended_submit_info,
on_execute => $on_execute,
on_success => $on_success,
on_submit => $on_submit,
); # jobid 1

my $result = 0;


TLOG("Result of basic eventlog  test is  $result\n",1);

# well did we get out the results into the log we wanted?
# determine where the log went
# run log reader and compare with expected values.


print "\tChecking results......\n";

my $elog = `condor_config_val EVENT_LOG`;
fullchomp($elog);

my $isitwindows = CondorUtils::is_windows();
if($isitwindows == 1) {
	$_ = $elog;
	s/\\/\//;
	$elog = $_;
}

print "\tEVENT_LOG is:$elog\n";

my $expected = "x_lib_eventlog.expected";
my @want;
my @got;

#print "Open expected results and real results\n";

if(is_windows()) {
	open(ELOG,"x_read_joblog.exe $elog TRACE 2>&1 |") || die "Could not read log <<$elog>>: $!\n";
} else {
	open(ELOG,"./x_read_joblog.exe $elog TRACE 2>&1 |") || die "Could not read log <<$elog>>: $!\n";
}
open(EXP,"<$expected") || die "Could not read expected values <<$expected>>: $!\n";
while(<ELOG>) {
	# ignore Job Image Size Changed output if any (several lines)
	next if /^Job Image Size Changed/ .. /^BillBool/;
	fullchomp($_);
	push  @got, $_;
}
close(ELOG);
while(<EXP>) {
	fullchomp($_);
	push  @want, $_;
}
close(EXP);
foreach $line (@got) {
	my $desired = shift(@want);
	if($desired ne $line) {
		print "\tResult Mismatch: wanted:$desired ne got:$line\n";
		$result += 1;
		last;
	} else {
		#print "\tResult Match: $desired eq $line\n";
	}
}
if($result == 0) {
	print "\tResults all Match. Test is a success.\n";
	CondorTest::RegisterResult(1, test_name, $testname);
} else {
	print "\t$result Mismatches. Test is a failure.\n";
	CondorTest::RegisterResult(0, test_name, $testname);
}


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