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

$cmd1 = 'job_core_coredump-yes_van.cmd';
$handle1 = 'job_core_coredump-yes_van';
$wants_core{$handle1} = 1;

$cmd2 = 'job_core_coredump-xfer_van.cmd';
$handle2 = 'job_core_coredump-xfer_van';
$wants_core{$handle2} = 1;

$cmd3 = 'job_core_coredump-none_van.cmd';
$handle3 = 'job_core_coredump-none_van';
$wants_core{$handle3} = 0;


$core_file = 'core';

$CheckCoreFiles = sub {
    my %info = @_;
    my $core = $info{"core"};
    my $handle = $info{"handle"};
    my $core_file_found = 0;
    my @expected_output;

	# Actual output files
	#$cmd1 = 'job_core_coredump-yes_van.cmd';
	#$cmd2 = 'job_core_coredump-xfer_van.cmd';
	#$cmd3 = 'job_core_coredump-none_van.cmd';
	
	my %listoftests; 
	$listoftests{"corename"} = "job_core_coredump_van";
	$listoftests{"tests"} = [ "job_core_coredump-yes_van", "job_core_coredump-none_van", "job_core_coredump-xfer_van"];
	$listoftests{"extensions"} = [ ".cmd", ".cmd.out", ".err", ".out", ".log"];
	CondorTest::MergeOutputFiles(\%listoftests);

    CondorTest::debug("Checking for $core...",1);
    if( $core ne "" && (-e $core) ) {
	CondorTest::debug("yes.\n",1);
	$core_file_found = 1;
    } else {
	CondorTest::debug("no.\n",1);
    }

    if( $wants_core{$handle} ) {
	$core_file_found || 
	    die "$handle: FAILURE (No core file created)\n";
    } else {
	$core_file_found && 
	    die "$handle: FAILURE (core file created)\n";
    }

    -z $info{"error"} || 
	die "$handle: FAILURE (STDERR contains data)\n";

    @expected_output = ( 'About to divide by zero' );

   CondorTest::CompareText( $info{"output"}, \@expected_output ) ||
	die "$handle: FAILURE (STDOUT doesn't match expected output)\n";
};

CondorTest::RegisterExitedAbnormal( $handle1, $CheckCoreFiles );
CondorTest::RegisterExitedAbnormal( $handle2, $CheckCoreFiles );
CondorTest::RegisterExitedAbnormal( $handle3, $CheckCoreFiles );

if( CondorTest::RunTest($handle1, $cmd1, 0) ) {
    CondorTest::debug("$handle1: SUCCESS\n",1);
} else {
    die "$handle1: CondorTest::RunTest() failed\n";
}

if( CondorTest::RunTest($handle2, $cmd2, 0) ) {
    CondorTest::debug("$handle2: SUCCESS\n",1);
} else {
    die "$handle2: CondorTest::RunTest() failed\n";
}

if( CondorTest::RunTest($handle3, $cmd3, 0) ) {
    CondorTest::debug("$handle3: SUCCESS\n",1);
} else {
    die "$handle3: CondorTest::RunTest() failed\n";
}

exit(0);

