#! /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 CondorUtils;
use strict;
use warnings;

# Request that a personal HTCondor with the following config appending is
# started before running this test
#testreq: personal
my $config = <<CONDOR_TESTREQ_CONFIG;
	EVENT_LOG = $(LOG)/EventLog
	MAX_EVENT_LOG = 1000000
	EVENT_LOG_JOB_AD_INFORMATION_ATTRS =  BillString, JobStatus, BillInt , BillBool , StreamErr
CONDOR_TESTREQ_CONFIG
#endtestreq

my $testname = "lib_eventlog";
my $mypid = $$;

my $jobexe = "/bin/date";
if (CondorUtils::is_windows()) { $jobexe = "appendmsg.exe"; }
my $readlogexe = "./x_read_joblog.exe";
if (CondorUtils::is_windows()) { $readlogexe = "x_read_joblog.exe"; }

my $uniq_name = "$testname.$mypid";

my $submit_content = "
universe   = vanilla
executable = $jobexe
log = $uniq_name.log
output = $uniq_name.out
error = $uniq_name.err
Notification = NEVER
+BillString = \"Good\"
+BillInt = 69
+BillBool = true
queue
";

# create a submit file for use later
emit_file("$uniq_name.sub", $submit_content);

my $cluster  = 1;

my $executed = sub {
	my %info = @_;
	my $cluster = $info{'cluster'};
	TLOG("$testname job $cluster executed\n");
};
CondorTest::RegisterExecute($testname, $executed);

my $success = sub { TLOG("$testname job success\n"); };
CondorTest::RegisterExitedSuccess($testname, $success);

# run a job, this will have the side effect of creating some events in the global event log
my $job_ran = CondorTest::RunTest($testname, "$uniq_name.sub", 0);

# this is the expected output of x_read_joblog.exe TRACE <EVENT_LOG>
my @want = (
	"Examine cluster -1\n",
	"Job Generic.........\n",
	"Examine cluster $cluster\n",
	"Job Submited.........\n",
	"Job Ad Information.........\n",
	"	JobStatus is 1\n",
	"	BillString is \"Good\"\n",
	"	BillInt is 69\n",
	"	BillBool is true\n",
	"Job Executed.........\n",
	"Job Ad Information.........\n",
	"	JobStatus is 2\n",
	"	BillString is \"Good\"\n",
	"	BillInt is 69\n",
	"	BillBool is true\n",
	"Job Terminated.........\n",
	"Job Ad Information.........\n",
	"	JobStatus is 2\n",
	"	BillString is \"Good\"\n",
	"	BillInt is 69\n",
	"	BillBool is true\n",
	"DONE\n",
	);

my $result = 1; # assume fail

if ($job_ran) {
	$result = 0; # assume success
	my $event_count = 0;

	# 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.

	TLOG("Checking the EVENT_LOG for the expected events\n");
	my $elog = `condor_config_val EVENT_LOG`;
	chomp($elog);

	my @got;
	open(ELOG,"$readlogexe $elog TRACE 2>&1 |") || die "Could not read log <<$elog>>: $!\n";
	print "--x_read_joblog output--\n";
	while(<ELOG>) {
		next if /^Job Image Size Changed/ .. /^BillBool/;
		print $_;
		push  @got, $_;
	}
	close(ELOG);
	print "--end of x_read_joblog output--\n";

	foreach my $line (@got) {
		$event_count += 1;
		my $desired = shift(@want);
		if($desired ne $line) {
			chomp $line;
			chomp $desired;
			print("\tResult Mismatch: '$desired' != '$line'\n");
			$result = 1;
			last;
		} else {
			#print("\tResult Match!:$desired eq $line\n");
		}
	}
	if ($event_count == 0) { 
		TLOG("FAILURE: No events in $elog\n");
		$result = 1;
	}
	if ($result == 0) {
		TLOG("Results all Match! Test is a success.\n");
	}
}

exit($result);

# Helper function to create a file from a string
sub emit_file {
	my $fname = shift;
	my $content = shift;

	open (FH, ">$fname") || die "error writing to $fname: $!\n";
	print FH $content;
	close (FH);
	if ( ! CondorUtils::is_windows()) { chmod (0755, $fname); }
}

